<jupyter_start><jupyter_text>Artificial Neural Networks Geo-Demographic Segmentation<jupyter_code>import tensorflow as tf
import numpy as np
import pandas as pd
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)<jupyter_output><empty_output><jupyter_text>Part 1 - Data Preprocessing  Data Loading<jupyter_code>PATH = "../../../../Deep_Learning/ANN/Python/Churn_Modelling.csv"
dataset = pd.read_csv(PATH)
dataset.head()
X = dataset.iloc[:, 3:-1].values
y = dataset.iloc[:, -1].values<jupyter_output><empty_output><jupyter_text>Encoding the Categorical Variables<jupyter_code>from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
X[:, 2] = le.fit_transform(X[:, 2])
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder

ct = ColumnTransformer(transformers=[('encoder', 
                                      OneHotEncoder(), [1])], 
                       remainder='passthrough')
X = np.array(ct.fit_transform(X))<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                    test_size=0.20, 
                                                    random_state=42)<jupyter_output><empty_output><jupyter_text>Feature Scaling<jupyter_code>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 - Model Building<jupyter_code>print(X_train.shape)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout

tf.random.set_seed(42)

ann = Sequential()
ann.add(Dense(6, activation='relu', input_shape=(12, )))
ann.add(Dropout(0.1))
ann.add(Dense(3, activation='relu'))
ann.add(Dropout(0.1))
ann.add(Dense(1, activation='sigmoid'))

ann.compile(optimizer='adam', loss='binary_crossentropy', 
            metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Part 3 - Model Training<jupyter_code>epochs = 500
batch_size = 32
ann.fit(X_train, y_train, batch_size=batch_size , epochs=epochs)<jupyter_output>Train on 8000 samples
Epoch 1/500
8000/8000 [==============================] - 1s 98us/sample - loss: 0.6018 - accuracy: 0.7169
Epoch 2/500
8000/8000 [==============================] - 0s 52us/sample - loss: 0.5024 - accuracy: 0.7859
Epoch 3/500
8000/8000 [==============================] - 0s 52us/sample - loss: 0.4780 - accuracy: 0.7931
Epoch 4/500
8000/8000 [==============================] - 0s 52us/sample - loss: 0.4698 - accuracy: 0.7974
Epoch 5/500
8000/8000 [==============================] - 0s 52us/sample - loss: 0.4568 - accuracy: 0.8049
Epoch 6/500
8000/8000 [==============================] - 0s 50us/sample - loss: 0.4553 - accuracy: 0.8051
Epoch 7/500
8000/8000 [==============================] - 0s 53us/sample - loss: 0.4538 - accuracy: 0.8052
Epoch 8/500
8000/8000 [==============================] - 0s 54us/sample - loss: 0.4434 - accuracy: 0.8055
Epoch 9/500
8000/8000 [==============================] - 0s 54us/sample - loss: 0.4431 - accuracy: 0.8041
Epoch 10/500
8000/8000 [[...]<jupyter_text>Part 4 - Model Evaluation  History<jupyter_code>history = ann.history.history
loss = history['loss']
accuracy = history['accuracy']
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 8))
plt.plot(loss, label='Loss')
plt.plot(accuracy, label='Accuracy')
plt.legend()
plt.xlabel('Epochs')
plt.show()<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>sample = [[600, 'France', 'Male', 40, 3, 60000, 2, 1, 1, 50000]]

sample = np.array(sample)
sample[:, 2] = le.transform(sample[:, 2])
sample = np.array(ct.transform(sample))
sample = sc.transform(sample)
print(ann.predict(sample) > 0.5)
y_pred = ann.predict_classes(X_test)
print(np.concatenate((y_test.reshape(len(y_pred),1), y_pred.reshape(len(y_pred),1)), axis=1))<jupyter_output>[[0 0]
 [0 0]
 [0 0]
 ...
 [1 1]
 [1 0]
 [1 0]]<jupyter_text>Confusion Matrix<jupyter_code>import seaborn as sn
from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test, y_pred)
df_cm = pd.DataFrame(cm, range(len(cm[0])), range(len(cm[0])))
sn.set(font_scale=1.4)
sn.heatmap(df_cm, annot=True, annot_kws={"size": 16})
plt.show()<jupyter_output><empty_output><jupyter_text>Accuracy Score<jupyter_code>from sklearn.metrics import accuracy_score

accuracy_score(y_test, y_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18844005156854318, 'fraction_numerical': 0.06918779544477868, 'mean_word_length': 5.198402130492677, 'pattern_counts': {'":': 1, '<': 43, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '646675', 'license': 'MIT', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1608, 'n_words': 307, 'path': 'Machine Learning Projects/Useful_Code_Examples/Deep_Learning/ANN-GeoDemographicSegmentation/ANN-Main.ipynb', 'repo_name': 'samlaubscher/HacktoberFest2020-Contributions', 'size': '4654'}