Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

5/21/22, 11:11 PM 19012021014_Jay Patel_DSML_Practical-6.

ipynb - Colaboratory

#**Practical-6**
Practical-6
**Aim:**  Aim:

Study of Artificial Neural Network (ANN) for a


Study of Artificial Neural Network (ANN) for 
different activation functions. given dataset. Study of different activation
functions.
**19012021014_Jay Patel**
19012021014_Jay Patel

Aritificial Neural Networks

import keras
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#dataset = pd.read_csv('Churn_Modelling.csv')
dataset = pd.read_csv('/content/drive/MyDrive/Churn_Modelling.csv')

dataset.head()

RowNumber CustomerId Surname CreditScore Geography Gender Age Tenure Bal

0 1 15634602 Hargrave 619 France Female 42 2

1 2 15647311 Hill 608 Spain Female 41 1 838

2 3 15619304 Onio 502 France Female 42 8 1596

3 4 15701354 Boni 699 France Female 39 1

4 5 15737888 Mitchell 850 Spain Female 43 2 1255

X = dataset.iloc[:, 3:-1].values

Y = dataset.iloc[:, -1].values

array([[619, 'France', 'Female', ..., 1, 1, 101348.88],

[608, 'Spain', 'Female', ..., 0, 1, 112542.58],

[502, 'France', 'Female', ..., 1, 0, 113931.57],

...,

https://colab.research.google.com/drive/1UqniYmaPRC08QOjO1S3F-CC-IuqlWY87#scrollTo=Uggdo35ruZsJ&printMode=true 1/8
5/21/22, 11:11 PM 19012021014_Jay Patel_DSML_Practical-6.ipynb - Colaboratory

[709, 'France', 'Female', ..., 0, 1, 42085.58],

[772, 'Germany', 'Male', ..., 1, 0, 92888.52],

[792, 'France', 'Female', ..., 1, 0, 38190.78]], dtype=object)

array([1, 0, 1, ..., 1, 1, 0])

# Encoding categorical data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

from sklearn.compose import ColumnTransformer

transform =  ColumnTransformer([("Cs",OneHotEncoder(),[1])], remainder = 'passthrough')

X = transform.fit_transform(X)

X = X[:,1:]

array([[0.0, 0.0, 619, ..., 1, 1, 101348.88],

[0.0, 1.0, 608, ..., 0, 1, 112542.58],

[0.0, 0.0, 502, ..., 1, 0, 113931.57],

...,

[0.0, 0.0, 709, ..., 0, 1, 42085.58],

[1.0, 0.0, 772, ..., 1, 0, 92888.52],

[0.0, 0.0, 792, ..., 1, 0, 38190.78]], dtype=object)

transform =  ColumnTransformer([("Gender",OneHotEncoder(),[3])], remainder = 'passthrough'
X = transform.fit_transform(X)

X = X[:,1:]

array([[0.0, 0.0, 0.0, ..., 1, 1, 101348.88],

[0.0, 0.0, 1.0, ..., 0, 1, 112542.58],

[0.0, 0.0, 0.0, ..., 1, 0, 113931.57],

...,

[0.0, 0.0, 0.0, ..., 0, 1, 42085.58],

[1.0, 1.0, 0.0, ..., 1, 0, 92888.52],

[0.0, 0.0, 0.0, ..., 1, 0, 38190.78]], dtype=object)

# 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)

X_train, X_test, Y_train, Y_test

(array([[0.0, 0.0, 1.0, ..., 1, 0, 163830.64],

[1.0, 1.0, 0.0, ..., 1, 1, 57098.0],

[0.0, 0.0, 0.0, ..., 1, 0, 185630.76],

...,

[1.0, 0.0, 0.0, ..., 1, 0, 181429.87],

[0.0, 0.0, 1.0, ..., 1, 1, 148750.16],

[0.0, 1.0, 0.0, ..., 1, 0, 118855.26]], dtype=object),

array([[0.0, 1.0, 0.0, ..., 1, 1, 192852.67],

[0.0, 0.0, 0.0, ..., 1, 0, 128702.1],

[0.0, 0.0, 1.0, ..., 1, 1, 75732.25],

...,

https://colab.research.google.com/drive/1UqniYmaPRC08QOjO1S3F-CC-IuqlWY87#scrollTo=Uggdo35ruZsJ&printMode=true 2/8
5/21/22, 11:11 PM 19012021014_Jay Patel_DSML_Practical-6.ipynb - Colaboratory

[1.0, 0.0, 1.0, ..., 1, 0, 141533.19],

[1.0, 1.0, 0.0, ..., 1, 1, 11276.48],

[1.0, 1.0, 0.0, ..., 1, 0, 192950.6]], dtype=object),

array([0, 0, 0, ..., 0, 0, 1]),

array([0, 1, 0, ..., 0, 0, 0]))

# Feature Scaling

from sklearn.preprocessing import StandardScaler

sc = StandardScaler()

X_train = sc.fit_transform(X_train)

X_test = sc.transform(X_test)

X_train

array([[-1.09168714, -0.5698444 , 1.74309049, ..., 0.64259497,

-1.03227043, 1.10643166],

[ 0.91601335, 1.75486502, -0.57369368, ..., 0.64259497,

0.9687384 , -0.74866447],

[-1.09168714, -0.5698444 , -0.57369368, ..., 0.64259497,

-1.03227043, 1.48533467],

...,

[ 0.91601335, -0.5698444 , -0.57369368, ..., 0.64259497,

-1.03227043, 1.41231994],

[-1.09168714, -0.5698444 , 1.74309049, ..., 0.64259497,

0.9687384 , 0.84432121],

[-1.09168714, 1.75486502, -0.57369368, ..., 0.64259497,

-1.03227043, 0.32472465]])

X_test

array([[-1.09168714, 1.75486502, -0.57369368, ..., 0.64259497,

0.9687384 , 1.61085707],

[-1.09168714, -0.5698444 , -0.57369368, ..., 0.64259497,

-1.03227043, 0.49587037],

[-1.09168714, -0.5698444 , 1.74309049, ..., 0.64259497,

0.9687384 , -0.42478674],

...,

[ 0.91601335, -0.5698444 , 1.74309049, ..., 0.64259497,

-1.03227043, 0.71888467],

[ 0.91601335, 1.75486502, -0.57369368, ..., 0.64259497,

0.9687384 , -1.54507805],

[ 0.91601335, 1.75486502, -0.57369368, ..., 0.64259497,

-1.03227043, 1.61255917]])

# Intitialising the ANN

classifier = Sequential()

# Adding the input layer and First hidden layer

classifier.add(Dense(units=6, kernel_initializer = 'uniform', activation = 'relu', 

                     input_shape = (11,)))

#classifier.add(Dense(output_dim=6, init = 'uniform', activation = 'relu', input_dim = 11)

# Adding 2nd hidden layer

#classifier.add(Dense(output_dim=6, init = 'uniform', activation = 'relu'))

classifier.add(Dense(units=6, kernel_initializer = 'uniform', activation = 'relu'))

https://colab.research.google.com/drive/1UqniYmaPRC08QOjO1S3F-CC-IuqlWY87#scrollTo=Uggdo35ruZsJ&printMode=true 3/8
5/21/22, 11:11 PM 19012021014_Jay Patel_DSML_Practical-6.ipynb - Colaboratory

# Adding the output layer

#classifier.add(Dense(output_dim=1, init = 'uniform', activation = 'sigmoid'))

classifier.add(Dense(units=1, kernel_initializer = 'uniform', activation = 'sigmoid'))

# Compiling the ANN - Adadelta - categorical_crossentropy

classifier.compile(optimizer = 'adam', loss='binary_crossentropy', metrics = ['accuracy'])

# Fitting the ANN to the training dataset

classifier.fit(X_train, Y_train, batch_size = 10, epochs = 100)

# Predicting the Test set results
Y_pred = classifier.predict(X_test)

Y_pred = (Y_pred > 0.5)

Y_pred

Epoch 1/100

800/800 [==============================] - 2s 2ms/step - loss: 0.4895 - accuracy:


Epoch 2/100

800/800 [==============================] - 1s 2ms/step - loss: 0.4281 - accuracy:


Epoch 3/100

800/800 [==============================] - 1s 2ms/step - loss: 0.4233 - accuracy:


Epoch 4/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4194 - accuracy:


Epoch 5/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4177 - accuracy:


Epoch 6/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4156 - accuracy:


Epoch 7/100

800/800 [==============================] - 1s 2ms/step - loss: 0.4142 - accuracy:


Epoch 8/100

800/800 [==============================] - 1s 2ms/step - loss: 0.4128 - accuracy:


Epoch 9/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4118 - accuracy:


Epoch 10/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4107 - accuracy:


Epoch 11/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4096 - accuracy:


Epoch 12/100

800/800 [==============================] - 1s 2ms/step - loss: 0.4084 - accuracy:


Epoch 13/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4082 - accuracy:


Epoch 14/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4073 - accuracy:


Epoch 15/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4071 - accuracy:


Epoch 16/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4068 - accuracy:


Epoch 17/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4063 - accuracy:


Epoch 18/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4054 - accuracy:


Epoch 19/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4052 - accuracy:


Epoch 20/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4048 - accuracy:


Epoch 21/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4040 - accuracy:

https://colab.research.google.com/drive/1UqniYmaPRC08QOjO1S3F-CC-IuqlWY87#scrollTo=Uggdo35ruZsJ&printMode=true 4/8
5/21/22, 11:11 PM 19012021014_Jay Patel_DSML_Practical-6.ipynb - Colaboratory

Epoch 22/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4041 - accuracy:


Epoch 23/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4044 - accuracy:


Epoch 24/100

800/800 [==============================] - 1s 2ms/step - loss: 0.4040 - accuracy:


Epoch 25/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4037 - accuracy:


Epoch 26/100

800/800 [==============================] - 1s 2ms/step - loss: 0.4036 - accuracy:


Epoch 27/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4033 - accuracy:


Epoch 28/100

800/800 [==============================] - 1s 1ms/step - loss: 0.4032 - accuracy:

# Making the confusion matrix

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(Y_test, Y_pred)

cm

array([[1547, 48],

[ 265, 140]])

print((cm[0][0]+cm[1][1])/(cm[0][0]+cm[0][1]+cm[1][0]+cm[1][1]) * 100)

84.35000000000001

from keras.wrappers.scikit_learn import KerasClassifier

from sklearn.model_selection import cross_val_score

from keras.layers import Dense

from keras.models import Sequential

from keras.layers import Dropout

def build_classifier():
# creating the model

  model = Sequential()

# first hidden layer

  model.add(Dense(output_dim = 8, init = 'uniform', activation = 'relu', input_dim = 14))

  model.add(Dropout(0.5))

# second hidden layer

  model.add(Dense(output_dim = 8, init = 'uniform', activation = 'relu'))

  model.add(Dropout(0.5))

# output layer

  model.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))

# Compiling the NN

# binary_crossentropy loss function used when a binary output is expected

  model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) 

https://colab.research.google.com/drive/1UqniYmaPRC08QOjO1S3F-CC-IuqlWY87#scrollTo=Uggdo35ruZsJ&printMode=true 5/8
5/21/22, 11:11 PM 19012021014_Jay Patel_DSML_Practical-6.ipynb - Colaboratory

  model.fit(x_train, y_train, batch_size = 10, nb_epoch = 50)

from keras.wrappers.scikit_learn import KerasClassifier

from sklearn.model_selection import cross_val_score

from keras.layers import Dense

from keras.models import Sequential

def build_classifier():
# creating the model

  model = Sequential()

# first hidden layer

  model.add(Dense(output_dim = 8, init = 'uniform', activation = 'relu', input_dim = 13))

# second hidden layer

  model.add(Dense(output_dim = 8, init = 'uniform', activation = 'relu'))

# output layer

  model.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))

# Compiling the NN

# binary_crossentropy loss function used when a binary output is expected

  model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) 

  model.fit(x_train, y_train, batch_size = 10, nb_epoch = 49)

dataset.columns

Index(['RowNumber', 'CustomerId', 'Surname', 'CreditScore', 'Geography',

'Gender', 'Age', 'Tenure', 'Balance', 'NumOfProducts', 'HasCrCard',

'IsActiveMember', 'EstimatedSalary', 'Exited'],

dtype='object')

'''

predicting if the costumer having following information will leave the bank or not ?

Geography : france

Age = 50

Credit score = 850

Tenure = 4

Balance = 150000

Number of Products = 5

Gender = Female

Has Credit Card = yes

Is Active Member = yes

Estimated Salary = 85000

'''

new_prediction = model.predict(sc.transform(np.array([[850, 50, 4, 15000, 0, 5, 1, 1, 8500
new_prediction = (new_prediction > 0.5 )

print(new_prediction)

https://colab.research.google.com/drive/1UqniYmaPRC08QOjO1S3F-CC-IuqlWY87#scrollTo=Uggdo35ruZsJ&printMode=true 6/8
5/21/22, 11:11 PM 19012021014_Jay Patel_DSML_Practical-6.ipynb - Colaboratory

File "<ipython-input-48-4a6656c099d3>", line 18

new_prediction = (new_prediction > 0.5 )

SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

from keras.wrappers.scikit_learn import KerasClassifier

from sklearn.model_selection import cross_val_score

from keras.layers import Dense

from keras.models import Sequential

def build_classifier():
  # creating the model

  model = Sequential()

  # first hidden layer

  model.add(Dense(output_dim = 8, init = 'uniform', activation = 'relu', input_dim = 13))

  # second hidden layer

  model.add(Dense(output_dim = 8, init = 'uniform', activation = 'relu'))

  # output layer

  model.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))

  # Compiling the NN

  # binary_crossentropy loss function used when a binary output is expected

  model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

  

  return model

model = KerasClassifier(build_fn = build_classifier, batch_size = 10, nb_epoch = 50)

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:25: DeprecationWarning:

Double-click (or enter) to edit

https://colab.research.google.com/drive/1UqniYmaPRC08QOjO1S3F-CC-IuqlWY87#scrollTo=Uggdo35ruZsJ&printMode=true 7/8
5/21/22, 11:11 PM 19012021014_Jay Patel_DSML_Practical-6.ipynb - Colaboratory

https://colab.research.google.com/drive/1UqniYmaPRC08QOjO1S3F-CC-IuqlWY87#scrollTo=Uggdo35ruZsJ&printMode=true 8/8

You might also like