Deep Learning
Keras
PyTorch
TensorFlow
Theano
CNTK
Deeplearning4j
Caffe
Churn_Modelling.csv
Python neural_network.py
1 import pandas as pd
2
3 # Load the data
4 data = pd.read_csv('Churn_Modelling.csv')
5 X = [Link][:, 3:13].values
6 y = [Link][:, 13].values
7
8 # Encoding categorical data
9 from sklearn import preprocessing
10 le = [Link]()
11 X[:, 1] = le.fit_transform(X[:, 1])
12 le2 = [Link]()
13 X[:, 2] = le2.fit_transform(X[:, 2])
14
15 # One hot encoding
16 from [Link] import OneHotEncoder
17 from [Link] import ColumnTransformer
18 ct = ColumnTransformer([("ohe", OneHotEncoder(dtype=float),
[1])], remainder='passthrough')
19 X = ct.fit_transform(X)
20 X = X[:, 1:]
21
22 # Splitting the dataset into the Training set and Test set
23 from sklearn.model_selection import train_test_split
24 X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.33, random_state=0)
25
26 # Feature Scaling
27 from [Link] import StandardScaler
28 sc = StandardScaler()
29 X_train = sc.fit_transform(X_train)
30 X_test = [Link](X_test)
31
32 # Importing the Keras libraries and packages
33 from [Link] import Sequential # initialize the neural
network
34 from [Link] import Dense # build the layers
35
36 # Initialising the ANN
37 classifier = Sequential()
38
39 # Adding the input layer and the first hidden layer
40 [Link](Dense(units=6, kernel_initializer='uniform',
activation='relu', input_dim=11)) # units = 6 is the number
of nodes in the first hidden layer, input_dim = 11 is the
number inputs
41
42 # Adding the second hidden layer
43 [Link](Dense(units=6, kernel_initializer='uniform',
activation='relu')) # units = 6 is the number of nodes in
the second hidden layer
44
45 # Adding the output layer
46 [Link](Dense(units=1, kernel_initializer='uniform',
activation='sigmoid')) # units = 1 is the number of nodes
in the output layer, activation = 'sigmoid' is the
activation function for the output layer (sigmoid for
binary outcome).
47
48 # Compiling the ANN
49 [Link](optimizer='adam',
loss='binary_crossentropy', metrics=['accuracy']) #
optimizer = 'adam' is the algorithm to find the optimal
weights, loss = 'binary_crossentropy' is the loss function
within the stochastic gradient descent algorithm, metrics =
['accuracy'] is the metric used to evaluate the model.
50
51 # Fitting the ANN to the Training set
52 [Link](X_train, y_train, batch_size=10, epochs=100)
# batch_size = 10 is the number of observations after which
the weights are updated, epochs = 100 is the number of
times the whole dataset is passed through the ANN.
53
54 # Predicting the Test set results
55 y_pred = [Link](X_test)
56 y_pred = (y_pred > 0.5)
57
58 # Making the Confusion Matrix
59 from [Link] import confusion_matrix
60 cm = confusion_matrix(y_test, y_pred)
61 print(cm)
62 print((cm[0][0] + cm[1][1]) / len(y_test))