<jupyter_start><jupyter_text>Train Test Split<jupyter_code># Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
# Try my way: remove this if things go wrong later:
X_train, X_test, y_train, y_test = train_test_split(d_X, y, test_size = 0.2, random_state = 0)<jupyter_output><empty_output><jupyter_text>Feature scaling<jupyter_code># Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)<jupyter_output><empty_output><jupyter_text>Part 2 - Now let's make the ANN!<jupyter_code>import keras
from keras.models import Sequential
from keras.layers import Dense

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session

# To make results reproduceable and setting gpu memory limit on tf.
# https://github.com/fchollet/keras/issues/2280

np.random.seed(123)
tf.set_random_seed(123)
config = tf.ConfigProto(inter_op_parallelism_threads=1)
config.gpu_options.per_process_gpu_memory_fraction = 0.1 # in my case this setting will use around 1G memory on GPU
set_session(tf.Session(config=config))


# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 11))

# Adding the second hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))

# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))

# Compiling the ANN
opt_adam = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) # 
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 100, epochs = 100, verbose=True)<jupyter_output>Epoch 1/100
8000/8000 [==============================] - 0s - loss: 0.6727 - acc: 0.7935     
Epoch 2/100
8000/8000 [==============================] - 0s - loss: 0.5633 - acc: 0.7960     
Epoch 3/100
8000/8000 [==============================] - 0s - loss: 0.4571 - acc: 0.7960     
Epoch 4/100
8000/8000 [==============================] - 0s - loss: 0.4407 - acc: 0.7960     
Epoch 5/100
8000/8000 [==============================] - 0s - loss: 0.4361 - acc: 0.7960     
Epoch 6/100
8000/8000 [==============================] - 0s - loss: 0.4331 - acc: 0.7960     
Epoch 7/100
8000/8000 [==============================] - 0s - loss: 0.4311 - acc: 0.7960     
Epoch 8/100
8000/8000 [==============================] - 0s - loss: 0.4296 - acc: 0.7960     
Epoch 9/100
8000/8000 [==============================] - 0s - loss: 0.4287 - acc: 0.7960     
Epoch 10/100
8000/8000 [==============================] - 0s - loss: 0.4275 - acc: 0.7960     
Epoch 11/100
8000/8000 [==============================] - 0[...]<jupyter_text>Part 3 - Making predictions and evaluating the model<jupyter_code># Predicting the Test set results
y_pred_pro = classifier.predict(X_test)
y_pred = (y_pred_pro > 0.5)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
from sklearn.metrics import accuracy_score, roc_auc_score

accuracy_score(y_test, y_pred)
roc_auc_score(y_test, y_pred_pro, average='macro')
roc_auc_score(y_test, y_pred_pro, average='micro')<jupyter_output><empty_output><jupyter_text>Use the model as if we have a new customer<jupyter_code>new_customer = np.array([[600, 40, 3, 6000, 2, 1, 1, 50000,0, 0, 1]], dtype='float')
new_customer = sc.transform(new_customer)
new_customer
new_customer_pre = classifier.predict(new_customer)
new_customer_pre[0] > 0.5<jupyter_output><empty_output><jupyter_text>Using CV to evaluate<jupyter_code># Evaluating the ANN
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
from keras.layers import Dropout

def build_classifier():
    classifier = Sequential()
    classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 11))
    classifier.add(Dropout(0.3))
    classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))
    # classifier.add(Dropout(0.3))
    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 = 100, epochs = 100, verbose=0)
accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10, n_jobs = 1)
mean = accuracies.mean()
variance = accuracies.std()
mean
variance<jupyter_output><empty_output><jupyter_text>Parameter Turning<jupyter_code># Tuning the ANN
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
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, verbose=0)


parameters = {'batch_size': [25, 32, 50],
              'epochs': [50, 100],
              'optimizer': ['adam', 'rmsprop']}
grid_search = GridSearchCV(estimator = classifier,
                           param_grid = parameters,
                           scoring = 'accuracy',
                           cv = 10, n_jobs=1, verbose=10 )
grid_search = grid_search.fit(X_train, y_train)

print(grid_search.best_params_)
print(grid_search.best_score_)<jupyter_output>{'batch_size': 5, 'epochs': 500, 'optimizer': 'adam'}
0.85575
{'fraction_non_alphanumeric': 0.16190022243406418, 'fraction_numerical': 0.05910390848427073, 'mean_word_length': 5.07039537126326, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '426222', 'license': 'MIT', 'n_tokens_mistral': 2398, 'n_tokens_neox': 2074, 'n_words': 511, 'path': 'Deep-Learning-A-Z/Volume 1 - Supervised Deep Learning/Part 1 - Artificial Neural Networks (ANN)/Section 4 - Building an ANN/ann.ipynb', 'repo_name': 'liufuyang/deep_learning_tutorial', 'size': '6294'}