Path: blob/master/Part 8 - Deep Learning/Artificial Neural Networks/ann.py
1009 views
# Artificial Neural Network12# Installing Theano3# pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git45# Installing Tensorflow6# Install Tensorflow from the website: https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html78# Installing Keras9# pip install --upgrade keras1011# Part 1 - Data Preprocessing1213# Importing the libraries14import numpy as np15import matplotlib.pyplot as plt16import pandas as pd1718# Importing the dataset19dataset = pd.read_csv('Churn_Modelling.csv')20X = dataset.iloc[:, 3:13].values21y = dataset.iloc[:, 13].values2223# Encoding categorical data24from sklearn.preprocessing import LabelEncoder, OneHotEncoder25labelencoder_X_1 = LabelEncoder()26X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])27labelencoder_X_2 = LabelEncoder()28X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])29onehotencoder = OneHotEncoder(categorical_features = [1])30X = onehotencoder.fit_transform(X).toarray()31X = X[:, 1:]3233# Splitting the dataset into the Training set and Test set34from sklearn.model_selection import train_test_split35X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)3637# Feature Scaling38from sklearn.preprocessing import StandardScaler39sc = StandardScaler()40X_train = sc.fit_transform(X_train)41X_test = sc.transform(X_test)4243# Part 2 - Now let's make the ANN!4445# Importing the Keras libraries and packages46import keras47from keras.models import Sequential48from keras.layers import Dense4950# Initialising the ANN51classifier = Sequential()5253# Adding the input layer and the first hidden layer54classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))5556# Adding the second hidden layer57classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))5859# Adding the output layer60classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))6162# Compiling the ANN63classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])6465# Fitting the ANN to the Training set66classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)6768# Part 3 - Making the predictions and evaluating the model6970# Predicting the Test set results71y_pred = classifier.predict(X_test)72y_pred = (y_pred > 0.5)7374# Making the Confusion Matrix75from sklearn.metrics import confusion_matrix76cm = confusion_matrix(y_test, y_pred)7778