Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 8 - Deep Learning/Artificial Neural Networks/ann.py
1009 views
1
# Artificial Neural Network
2
3
# Installing Theano
4
# pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
5
6
# Installing Tensorflow
7
# Install Tensorflow from the website: https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html
8
9
# Installing Keras
10
# pip install --upgrade keras
11
12
# Part 1 - Data Preprocessing
13
14
# Importing the libraries
15
import numpy as np
16
import matplotlib.pyplot as plt
17
import pandas as pd
18
19
# Importing the dataset
20
dataset = pd.read_csv('Churn_Modelling.csv')
21
X = dataset.iloc[:, 3:13].values
22
y = dataset.iloc[:, 13].values
23
24
# Encoding categorical data
25
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
26
labelencoder_X_1 = LabelEncoder()
27
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
28
labelencoder_X_2 = LabelEncoder()
29
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
30
onehotencoder = OneHotEncoder(categorical_features = [1])
31
X = onehotencoder.fit_transform(X).toarray()
32
X = X[:, 1:]
33
34
# Splitting the dataset into the Training set and Test set
35
from sklearn.model_selection import train_test_split
36
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
37
38
# Feature Scaling
39
from sklearn.preprocessing import StandardScaler
40
sc = StandardScaler()
41
X_train = sc.fit_transform(X_train)
42
X_test = sc.transform(X_test)
43
44
# Part 2 - Now let's make the ANN!
45
46
# Importing the Keras libraries and packages
47
import keras
48
from keras.models import Sequential
49
from keras.layers import Dense
50
51
# Initialising the ANN
52
classifier = Sequential()
53
54
# Adding the input layer and the first hidden layer
55
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))
56
57
# Adding the second hidden layer
58
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))
59
60
# Adding the output layer
61
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))
62
63
# Compiling the ANN
64
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
65
66
# Fitting the ANN to the Training set
67
classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)
68
69
# Part 3 - Making the predictions and evaluating the model
70
71
# Predicting the Test set results
72
y_pred = classifier.predict(X_test)
73
y_pred = (y_pred > 0.5)
74
75
# Making the Confusion Matrix
76
from sklearn.metrics import confusion_matrix
77
cm = confusion_matrix(y_test, y_pred)
78