Skip to content

Instantly share code, notes, and snippets.

@Kanhasonu21
Created January 23, 2020 13:40
Show Gist options
  • Save Kanhasonu21/255bfa6cbabc2d11f365ffcaad4b0d57 to your computer and use it in GitHub Desktop.
Save Kanhasonu21/255bfa6cbabc2d11f365ffcaad4b0d57 to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import confusion_matrix
dataSet = pd.read_csv('Churn_Modelling.csv')
X = dataSet.iloc[:, 3:13].values
y = dataSet.iloc[:, 13].values
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])
transformer = ColumnTransformer([('one_hot_encoder', OneHotEncoder(), [1])], remainder='passthrough')
X = np.array(transformer.fit_transform(X),dtype=float)
X=X[:,1:]
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state = 0 )
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Part 2 - Now let's make the ANN!
# 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
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)
# Part 3 - Making the predictions and evaluating the model
# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred>0.5)
cm=confusion_matrix(y_test,y_pred)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment