Link to the previous post : https://statinfer.com/204-5-11-hidden-layers-and-their-roles/
As promised, in the first post of the series we will build a Neural Network that will read the image of a digit and correctly identify the number.
#Importing test and training data
import numpy as np
digits_train = np.loadtxt("datasets\\Digit Recognizer\\USPS\\zip.train.txt")
#digits_train is numpy array. we convert it into dataframe for better handling
train_data=pd.DataFrame(digits_train)
train_data.shape
digits_test = np.loadtxt("datasets\\Digit Recognizer\\USPS\\zip.test.txt")
#digits_test is numpy array. we convert it into dataframe for better handling
test_data=pd.DataFrame(digits_test)
test_data.shape
train_data[0].value_counts() #To get labels of the images
import matplotlib.pyplot as plt
#Lets have a look at some images.
for i in range(0,5):
data_row=digits_train[i][1:]
#pixels = matrix(as.numeric(data_row),16,16,byrow=TRUE)
pixels = np.matrix(data_row)
pixels=pixels.reshape(16,16)
plt.figure(figsize=(10,10))
plt.subplot(3,3,i+1)
plt.imshow(pixels)
#Creating multiple columns for multiple outputs
#####We need these variables while building the model
digit_labels=pd.DataFrame()
digit_labels['label']=train_data[0:][0]
label_names=['I0','I1','I2','I3','I4','I5','I6','I7','I8','I9']
for i in range(0,10):
digit_labels[label_names[i]]=digit_labels.label==i
#see our newly created labels data
digit_labels.head(10)
#Update the training dataset
train_data1=pd.concat([train_data,digit_labels],axis=1)
print(train_data1.shape)
train_data1.head(5)
#########Neural network building
import neurolab as nl
import numpy as np
import pylab as pl
x_train=train_data.drop(train_data.columns[[0]], axis=1)
y_train=digit_labels.drop(digit_labels.columns[[0]], axis=1)
#getting minimum and maximum of each column of x_train into a list
def minMax(x):
return pd.Series(index=['min','max'],data=[x.min(),x.max()])
listvalues = x_train.apply(minMax).T.values.tolist()
error = []
# Create network with 1 layer and random initialized
net = nl.net.newff(listvalues,[20,10],transf=[nl.trans.LogSig()] * 2)
net.trainf = nl.train.train_rprop
# Train network
import time
start_time = time.time()
error.append(net.train(x_train, y_train, show=0, epochs = 250,goal=0.02))
print("--- %s seconds ---" % (time.time() - start_time))
# Prediction testing data
x_test=test_data.drop(test_data.columns[[0]], axis=1)
y_test=test_data[0:][0]
predicted_values = net.sim(x_test.as_matrix())
predict=pd.DataFrame(predicted_values)
index=predict.idxmax(axis=1)
#confusion matrix
from sklearn.metrics import confusion_matrix as cm
ConfusionMatrix = cm(y_test,index)
print('Confusion Matrix : ', ConfusionMatrix)
#accuracy
accuracy=np.trace(ConfusionMatrix)/sum(sum(ConfusionMatrix))
print('Accuracy : ', accuracy)
error=1-accuracy
print('Error : ', error)