import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import pandas as pd
import tensorflow as tf
# import theano
import keras
import numpy as np
import matplotlib.pyplot as plt
from time import time
# import tensorflow as tf
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
# import tensorflow as tf
# with tf.device('/gpu:0'):
# a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
# b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
# c = tf.matmul(a, b)
# with tf.Session() as sess:
# print (sess.run(c))
!nvidia-smi
def show(id, w=6):
from IPython.display import Image
return Image('./pics/class1/{}.jpg'.format(id), width=w*100)
!ls Artificial_Neural_Networks/
df = pd.read_csv('Churn_Modelling.csv')
df.shape
df.head(3)
df.iloc[:,3:-1].head(2)
df.iloc[:,3:13].head(2)
df['Geography'].value_counts()
df['Gender'].value_counts()
X = df.iloc[:,3:-1].values
print (X.shape)
print (type(X))
X[:3]
y = df.iloc[:,-1].values
print (y.shape)
print (type(y))
y
X[:,1:3]
%%time
# Encoding the Independent Variable
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
print (X[:3])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
(X[:3])
X
%%time
onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
print (X.shape)
onehotencoder.fit_transform(X)
# %precision % .2f
onehotencoder.fit_transform(X).toarray()[0]
np.set_printoptions(precision=1, suppress=True)
onehotencoder.fit_transform(X).toarray()[0]
X[0]
print (X.shape)
X = X[:, 1:]
print (X.shape)
X[0]
S = lambda *x: [i.shape for i in x]
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)
print (S(X_train,X_test, y_train, y_test))
X_train[0]
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
# fit on training set
X_train = sc.fit_transform(X_train)
# only transform on test set
X_test = sc.transform(X_test)
print (S(X_train,X_test, y_train, y_test))
X_train[0]
# show(13, w=8)
from keras.models import Sequential
from keras.layers import Dense
classifier = Sequential()
# classifier?
# first hidden layer
# classifier.add(Dense(units = 6, input_shape=(11,), kernel_initializer='uniform', activation='relu'))
classifier.add(Dense(units = 6, input_dim=11, kernel_initializer='uniform', activation='relu'))
# second hidden layer
classifier.add(Dense(units = 6, kernel_initializer='uniform', activation='relu'))
# ouput layer
classifier.add(Dense(units = 1, kernel_initializer='uniform', activation='sigmoid'))
# show(19, w=8)
from keras.optimizers import adam, Adam
# compiling the ANN
classifier.compile(optimizer=adam(0.01), loss='binary_crossentropy', metrics= ['accuracy'])
classifier.summary()
!ps aux --sort=-%mem | awk 'NR<=6{print $0}'
from keras.callbacks import EarlyStopping
# tf.all_variables()
# if 'session' in locals() and session is not None:
# print('Close interactive session')
# session.close()
# fitting ANN with training set
classifier.fit(X_train, y_train, batch_size=10, epochs=100, callbacks=[EarlyStopping(patience=10)])
y_pred = classifier.predict(X_test)
print (type(y_pred))
y_pred.shape
y_pred
y_pred[0][0]
y_pred = (y_pred> 0.5)
y_pred
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
type(cm)
cm
accuracy_score(y_test, y_pred)
(1512+211)/2000.0
# Predicting a single new observation
"""Predict if the customer with the following informations will leave the bank:
Geography: France
Credit Score: 600
Gender: Male
Age: 40
Tenure: 3
Balance: 60000
Number of Products: 2
Has Credit Card: Yes
Is Active Member: Yes
Estimated Salary: 50000"""
new_prediction = classifier.predict(sc.transform(np.array([[0.0, 0, 600, 1, 40, 3, 60000, 2, 1, 1, 50000]])))
print (new_prediction)
new_prediction = (new_prediction > 0.5)
new_prediction