<jupyter_start><jupyter_text>Model<jupyter_code>def build_classifier():  # for kerasclassifier as it needs a function
    classifier = Sequential()
    classifier.add(Dense(units=6, kernel_initializer='uniform', activation='relu', input_dim=11))
    classifier.add(Dropout(rate=0.1))
    classifier.add(Dense(units=6, kernel_initializer='uniform', activation='relu'))
    classifier.add(Dropout(rate=0.1))
    classifier.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))

    classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    
    return classifier
classifier = build_classifier()

classifier.fit(X_train, y_train, batch_size=10, epochs=10)
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)  
# convert probability to 0(False) or 1(True)

y_pred<jupyter_output><empty_output><jupyter_text>Confusion Matrix- (0, 0) - True Negative- (0, 1) - Falso Positive- (1, 0) - False Negative- (1, 1) - True Negative<jupyter_code># We will use a confusion matrix to predict the accuracy
cm = confusion_matrix(y_test, y_pred)
# (i=j) diagonal of confusion matrix represents correct predictions and the other diagonal shows incorrect predictions
print(cm)
accuracy = ((cm[0][0]+cm[1][1])/2000)
# 2000 is the number of rows in test dataset and sum of diagonal elements gives total correct predictions
print("accuracy: ", accuracy)
# making new predictions
new_prediction = classifier.predict(sc.transform(np.array([[0.0, 0, 600, 1, 40, 3, 60000, 2, 1, 1, 50000]])))
# we need a horizontal vector here so we use [[]] which creates a 2d array
# to avoid warning we make first element float, also do not forget to scale the data
new_prediction = (new_prediction > 0.5)
new_prediction<jupyter_output><empty_output><jupyter_text>Evaluating the modelUsing K-fold cross validation - In this method dataset is divided into k parts and then k-1 parts are used for training and 1 part is used for testing. Also our model is trained k times with different parts of dataset as test set each time.<jupyter_code>classifier = KerasClassifier(build_fn=build_classifier, batch_size=10, epochs=10)
accuracies = cross_val_score(estimator=classifier, X=X_train, y=y_train, cv=10, n_jobs=1)
# we basically break X_train and y_train into 10 different parts, and then train and test on those 10 different models
# n_jobs is number of CPUs to use(-1 for all), X is data to fit, y are actual answers to compare with predictions
# cross_val_score will return k(10 here) accuracies as obtained from k fold cross validation
print(accuracies.mean())  # mean
print(accuracies.std())  # variance<jupyter_output>0.8196250021457672
0.01822643186291515<jupyter_text>Tuning the modelUsing GridSearchCV - trying multiple models with different hyperparameters and also performs k-fold cross validation<jupyter_code>def build_best_classifier(optimizer):  # important argument if optimizer is to be changed
    classifier = Sequential()
    classifier.add(Dense(units=6, kernel_initializer='uniform', activation='relu', input_dim=11))
    classifier.add(Dropout(rate=0.1))
    classifier.add(Dense(units=6, kernel_initializer='uniform', activation='relu'))
    classifier.add(Dropout(rate=0.1))
    classifier.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))
    classifier.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
    return classifier
classifier = KerasClassifier(build_fn=build_best_classifier)  
# NOTE that we do not need to provide arguments here

# hyperparameters
parameters = {'batch_size': [16, 32],  # checks two values 16, 32
              'epochs': [10, 20],  # checks two values 10 and 20
              'optimizer': ['adam', 'rmsprop']}  # tries with 2 optimizers
grid_search = GridSearchCV(estimator=classifier, param_grid=parameters, scoring='accuracy', cv=10)
# estimator is our model, param_grid contains parameters dictionary we try and cv aur number of k folds

grid_search = grid_search.fit(X_train, y_train)
best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_
print("best_parameters: ", best_parameters)
print("best_accuracy: ", best_accuracy)<jupyter_output>best_parameters:  {'batch_size': 16, 'epochs': 20, 'optimizer': 'rmsprop'}
best_accuracy:  0.8318749999999999
{'fraction_non_alphanumeric': 0.10418604651162791, 'fraction_numerical': 0.03790697674418605, 'mean_word_length': 6.027777777777778, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '361082', 'license': 'MIT', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1331, 'n_words': 400, 'path': 'ANN-ChurnRate/model.ipynb', 'repo_name': 'Frostday/Small-Tensorflow-Projects', 'size': '4300'}