Ticker

6/recent/ticker-posts

Artificial Neural Network - Keras


In this post, we are gonna make a Artificial Neural Network(ANN)  with Keras and tensorflow, Firstly we are gonna import the data file - Churn_Modelling.csv  , just click on this link and download this file from my drive.

We also have to clean the data and take only important things for our model. And then do the regular things of splitting the data and then we would make the neural network using Sequential function.

I have used a 100 epochs for the program below for more accuracy score but you can change the number of epochs and see its impact on the accuracy score and mean.


Preview:

  # in this we are gonna do a k-fold cross validation method to improve and tune our model
  # for the normal ANN just go to the jupyter shell in the same folder--ann.ipynb
  # Importing the libraries
   import numpy as np
   import matplotlib.pyplot as plt
   import pandas as pd

  # Importing the dataset
   dataset = pd.read_csv('Churn_Modelling.csv')
   x = dataset.iloc[:, 3:13].values
   y = dataset.iloc[:, 13].values

  # Encoding categorical data
  # encode both 1st and 2nd column of the x dataset(country and gender)
  # Encoding categorical data, 
  # encoding the text values in country and gender coulumn into float values  
   from sklearn.preprocessing import LabelEncoder, OneHotEncoder
   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])


   from sklearn.compose import ColumnTransformer
   ct = ColumnTransformer([("Geography", OneHotEncoder(), [1])], remainder = 'passthrough')
   x = ct.fit_transform(x)
   x = x[:, 1:]


  # split the data into training and testing data
   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)


  # feature scaling should be done for all the values, so the values in the dataset dont dominate each other
  # Feature Scaling
   from sklearn.preprocessing import StandardScaler
   sc = StandardScaler()
   x_train = sc.fit_transform(x_train)
   x_test = sc.transform(x_test)

  # now we are gonna make a more improved and tuned ANN
  # we are gonna do a different type of training of data with cross validation method(k-fold cross validation-cv)
  # for this ANN to work, you dont have to execute the above neural network, just import the libraries and data and   # scale the data and split it into train and test data

   from keras.wrappers.scikit_learn import KerasClassifier
   from sklearn.model_selection import cross_val_score
   from keras.models import Sequential
   import keras
   from keras.layers import Dense

   def build_classifier():
     classifier = Sequential()
     classifier.add(Dense(6,activation="relu",input_dim=11,kernel_initializer="uniform"))
     classifier.add(Dense(6,activation="relu", kernel_initializer="uniform"))
     classifier.add(Dense(1,activation="sigmoid",kernel_initializer="uniform"))
     classifier.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
     return classifier
   classifier = KerasClassifier(build_fn=build_classifier, batch_size=10, epochs=100)
   accuracies = cross_val_score(estimator=classifier, X=x_train,y=y_train, cv=10, n_jobs=-1)

  # n_jobs are the no. of CPUs working on the k-folds(cross validation method), and -1 is the highest
   print(accuracies)
   mean=accuracies.mean()
   print(mean)



Post a Comment

0 Comments