<jupyter_start><jupyter_text>Machine Learning in Python    Artificial Neural Network    <NAME>  Importing the libraries<jupyter_code>import numpy as np
import pandas as pd
import tensorflow as tf
tf.__version__<jupyter_output><empty_output><jupyter_text>Part 1 - Data Preprocessing  Importing the dataset<jupyter_code>dataset = pd.read_csv('Churn_Modelling.csv')
dataset.head()
X = dataset.iloc[:, 3:-1].values
y = dataset.iloc[:, -1].values
print(X)
print(y)<jupyter_output>[1 0 1 ... 1 1 0]<jupyter_text>Encoding categorical data Label Encoding the "Gender" column<jupyter_code>from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
X[:, 2] = le.fit_transform(X[:, 2])
print(X)<jupyter_output>[[619 'France' 0 ... 1 1 101348.88]
 [608 'Spain' 0 ... 0 1 112542.58]
 [502 'France' 0 ... 1 0 113931.57]
 ...
 [709 'France' 0 ... 0 1 42085.58]
 [772 'Germany' 1 ... 1 0 92888.52]
 [792 'France' 0 ... 1 0 38190.78]]<jupyter_text>One Hot Encoding the "Geography" column<jupyter_code>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))
print(X)<jupyter_output>[[1.0 0.0 0.0 ... 1 1 101348.88]
 [0.0 0.0 1.0 ... 0 1 112542.58]
 [1.0 0.0 0.0 ... 1 0 113931.57]
 ...
 [1.0 0.0 0.0 ... 0 1 42085.58]
 [0.0 1.0 0.0 ... 1 0 92888.52]
 [1.0 0.0 0.0 ... 1 0 38190.78]]<jupyter_text>Splitting the dataset into the Training set and Test set<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.2, random_state = 0)<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 - Building the ANN  Initializing the ANN<jupyter_code>ann = tf.keras.models.Sequential()<jupyter_output><empty_output><jupyter_text>Adding the input layer and the first hidden layer<jupyter_code>ann.add(tf.keras.layers.Dense(units=6, activation='relu'))<jupyter_output><empty_output><jupyter_text>Adding the second hidden layer<jupyter_code>ann.add(tf.keras.layers.Dense(units=6, activation='relu'))<jupyter_output><empty_output><jupyter_text>Adding the output layer<jupyter_code>ann.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))<jupyter_output><empty_output><jupyter_text>Part 3 - Training the ANN  Compiling the ANN<jupyter_code>ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>Training the ANN on the Training set<jupyter_code>ann.fit(X_train, y_train, batch_size = 32, epochs = 100)<jupyter_output>Epoch 1/100
250/250 [==============================] - 0s 363us/step - loss: 0.5316 - accuracy: 0.7960
Epoch 2/100
250/250 [==============================] - 0s 360us/step - loss: 0.4670 - accuracy: 0.7960
Epoch 3/100
250/250 [==============================] - 0s 360us/step - loss: 0.4484 - accuracy: 0.7960
Epoch 4/100
250/250 [==============================] - 0s 360us/step - loss: 0.4398 - accuracy: 0.7960
Epoch 5/100
250/250 [==============================] - 0s 360us/step - loss: 0.4353 - accuracy: 0.8008
Epoch 6/100
250/250 [==============================] - 0s 360us/step - loss: 0.4323 - accuracy: 0.8040
Epoch 7/100
250/250 [==============================] - 0s 352us/step - loss: 0.4299 - accuracy: 0.8073
Epoch 8/100
250/250 [==============================] - 0s 352us/step - loss: 0.4278 - accuracy: 0.8098
Epoch 9/100
250/250 [==============================] - 0s 372us/step - loss: 0.4257 - accuracy: 0.8146
Epoch 10/100
250/250 [==============================] - 0s 363us/step - l[...]<jupyter_text>Part 4 - Making the predictions and evaluating the model  Predicting the result of a single observation **Homework**Use our ANN model to predict if the customer with the following informations will leave the bank: Geography: FranceCredit Score: 600Gender: MaleAge: 40 years oldTenure: 3 yearsBalance: \$ 60000Number of Products: 2Does this customer have a credit card ? YesIs this customer an Active Member: YesEstimated Salary: \$ 50000So, should we say goodbye to that customer ? **Solution**<jupyter_code>print(ann.predict(sc.transform([[1, 0, 0, 600, 1, 40, 3, 60000, 2, 1, 1, 50000]])) > 0.5)<jupyter_output>[[False]]<jupyter_text>Therefore, our ANN model predicts that this customer stays in the bank!**Important note 1:** Notice that the values of the features were all input in a double pair of square brackets. That's because the "predict" method always expects a 2D array as the format of its inputs. And putting our values into a double pair of square brackets makes the input exactly a 2D array.**Important note 2:** Notice also that the "France" country was not input as a string in the last column but as "1, 0, 0" in the first three columns. That's because of course the predict method expects the one-hot-encoded values of the state, and as we see in the first row of the matrix of features X, "France" was encoded as "1, 0, 0". And be careful to include these values in the first three columns, because the dummy variables are always created in the first columns.  Predicting the Test set results<jupyter_code>y_pred = ann.predict(X_test)
y_pred = (y_pred > 0.5)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))<jupyter_output>[[0 0]
 [0 1]
 [0 0]
 ...
 [0 0]
 [0 0]
 [0 0]]<jupyter_text>Making the Confusion Matrix<jupyter_code>from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)<jupyter_output>[[1505   90]
 [ 192  213]]
{'fraction_non_alphanumeric': 0.16987951807228915, 'fraction_numerical': 0.0882960413080895, 'mean_word_length': 5.261853448275862, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 57, 'https://': 0, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '98733', 'license': 'MIT', 'n_tokens_mistral': 2400, 'n_tokens_neox': 1974, 'n_words': 587, 'path': 'ANN_Xtcxiil/ANN .ipynb', 'repo_name': 'SMSajadi99/Machine-Learning', 'size': '5825'}