In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
In [2]:
# Importing the dataset
dataset = pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values
In [3]:
dataset.head()
Out[3]:
In [4]:
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
In [5]:
X[0:3, :]
Out[5]:
In [6]:
onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
In [7]:
# Removing dummy variable, as [1, 0, 0] can be presented as [0,0]
X = X[:, 1:]
In [8]:
X[0:2, :]
Out[8]:
In [9]:
# My way to get similar data above:
d_X = dataset.iloc[:, 3:13]
d_y = dataset.iloc[:, 13]
In [10]:
d_X = pd.get_dummies(d_X)
In [11]:
d_X.head()
Out[11]:
In [12]:
d_X.drop(['Geography_France', 'Gender_Female'], axis=1, inplace=True)
In [13]:
d_X.head()
Out[13]:
In [14]:
d_X.shape
Out[14]:
In [15]:
X.shape
Out[15]:
In [16]:
# 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)
In [17]:
# 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)
In [18]:
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
In [64]:
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))
In [65]:
# 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'])
In [66]:
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 100, epochs = 100, verbose=True)
Out[66]:
In [67]:
# Predicting the Test set results
y_pred_pro = classifier.predict(X_test)
y_pred = (y_pred_pro > 0.5)
In [68]:
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
In [69]:
from sklearn.metrics import accuracy_score, roc_auc_score
accuracy_score(y_test, y_pred)
Out[69]:
In [70]:
roc_auc_score(y_test, y_pred_pro, average='macro')
Out[70]:
In [71]:
roc_auc_score(y_test, y_pred_pro, average='micro')
Out[71]:
In [33]:
new_customer = np.array([[600, 40, 3, 6000, 2, 1, 1, 50000,0, 0, 1]], dtype='float')
new_customer = sc.transform(new_customer)
In [34]:
new_customer
Out[34]:
In [35]:
new_customer_pre = classifier.predict(new_customer)
In [69]:
new_customer_pre[0] > 0.5
Out[69]:
In [38]:
# 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()
In [39]:
mean
Out[39]:
In [40]:
variance
Out[40]:
In [72]:
# 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_)
{'batch_size': 5, 'epochs': 500, 'optimizer': 'adam'} 0.85575
In [ ]: