<jupyter_start><jupyter_text>Building Our Artificial Neural Network<jupyter_code># Initialising the ANN
classifier = Sequential()

#Input and 1st Hidden Layer
classifier.add(Dense(units = 13,
                     activation = 'relu',
                     kernel_initializer = 'uniform',
                     input_dim = 11))
classifier.add(Dropout(p = 0.2))


#2nd Hidden Layer
classifier.add(Dense(units = 13,
                     activation = 'relu',
                     kernel_initializer = 'uniform'))
classifier.add(Dropout(p = 0.2))   


#3rd Hidden Layer
classifier.add(Dense(units = 13,
                     activation = 'relu',
                     kernel_initializer = 'uniform'))
classifier.add(Dropout(p = 0.2))               

#Output Layer
classifier.add(Dense(units = 1,
                     activation = 'sigmoid',
                     kernel_initializer = 'uniform'))
               
classifier.compile(optimizer = 'adam',
                   loss = 'binary_crossentropy',
                   metrics = ['accuracy'])               
classifier.summary()
#training our ANN Model
history = classifier.fit(X_train, y_train, batch_size = 32, epochs = 50, validation_split=0.25)
# Plot training & validation accuracy values
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

# Plot training & validation loss values
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
# Part 3 - Making predictions and evaluating the model

# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
#Model Evaluation

print('Accuracy Score: ' + str(accuracy_score(y_test, y_pred)))

print('Precision Score: ' + str(precision_score(y_test, y_pred)))

print('Recall Score: ' + str(recall_score(y_test, y_pred)))

print('F1 Score: ' + str(f1_score(y_test, y_pred)))

print('Classification Report: \n' + str(classification_report(y_test, y_pred)))
#Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
cm
# Evaluating the ANN with KFold Cross Validation

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from keras.models import Sequential
from keras.layers import Dense

def build_classifier():
    classifier = Sequential()
    
    classifier.add(Dense(units = 6, 
                         kernel_initializer = 'uniform', 
                         activation = 'relu', input_dim = 11))
    
    classifier.add(Dense(units = 6, 
                         kernel_initializer = 'uniform', 
                         activation = 'relu'))
    
    classifier.add(Dense(units = 1, 
                         kernel_initializer = 'uniform', 
                         activation = 'sigmoid'))
    
    classifier.compile(optimizer = 'adam', 
                       loss = 'binary_crossentropy', 
                       metrics = ['accuracy'])
    
    return classifier
  
classifier = KerasClassifier(build_fn = build_classifier, 
                             batch_size = 10, 
                             epochs = 100)

accuracies = cross_val_score(estimator = classifier, 
                             X = X_train, 
                             y = y_train, 
                             cv = 10, 
                             n_jobs = -1)

mean = accuracies.mean()
print("Mean: ", mean)

variance = accuracies.std()
print('Variance: ', variance)
# Tuning the ANN
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import RandomizedSearchCV
from keras.models import Sequential
from keras.layers import Dense

def build_classifier(optimizer):
    classifier = Sequential()
    
    classifier.add(Dense(units = 6, 
                         kernel_initializer = 'uniform', 
                         activation = 'relu', input_dim = 11))
    
    classifier.add(Dense(units = 6, 
                         kernel_initializer = 'uniform', 
                         activation = 'relu'))
    
    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_classifier)

parameters = {'batch_size': [25, 32],
              'epochs': [100, 200],
              'optimizer': ['adam', 'rmsprop']}

random_search = RandomizedSearchCV(estimator = classifier,
                                   param_distributions  = parameters,
                                   n_iter = 15,
                                   cv = 10,
                                   n_jobs = -1)

random_search = random_search.fit(X_train, y_train)
best_parameters = random_search.best_params_
print(best_parameters)

best_accuracy = random_search.best_score_
print(best_accuracy)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1046444401618809, 'fraction_numerical': 0.012141067643091155, 'mean_word_length': 2.1684981684981683, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '180499', 'license': 'Apache-2.0', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1449, 'n_words': 318, 'path': 'Artificial_Neural_Networks.ipynb', 'repo_name': 'subhadeep-123/Bank-Loss-Prediction-with-ANN', 'size': '5189'}