from sklearn.neural_network import MLPClassifier
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Dataset has 60,000 rows and 28X28 columns
data_train = pd.read_csv("mnist-train.csv")
#Converting data into floating type
data_train = data_train.astype(float)
#Dataset has 10,000 rows and 28X28 columns
data_test = pd.read_csv("mnist-test.csv")
#Converting data into floating type
data_test = data_test.astype(float)
y_train = data_train["label"]
x_train = data_train.drop("label",axis=1)
y_test = data_test["label"]
x_test = data_test.drop("label",axis=1)
def show_img(img_data):
img_data = np.array(img_data)
for single_img in img_data:
plt.figure()
single_img_reshaped = np.reshape(single_img, (28, 28))
plt.imshow(single_img_reshaped,cmap='gray')
plt.show()
show_img(x_train.iloc[[0,1],:])
clf = MLPClassifier(hidden_layer_sizes=(10, 10))
clf.fit(X=x_train,y=y_train)
#For regression using neural network import the following
#from sklearn.neural_network import MLPRegressor
y_true = y_train
y_pred = clf.predict(x_train)
print("Training accuracy",clf.score(X=x_train,y=y_train))
Training accuracy 0.8898833333333334
indices = [0,1,2,3,4,5,6,7,8,9,10,11,12]
c_predicted = clf.predict(x_test.iloc[indices,:])
c_true = y_test.iloc[indices]
print("True:",list(c_true))
print("Pred:",list(c_predicted))
# Use the following command to see the images
show_img(x_test.iloc[indices,:])
True: [7.0, 2.0, 1.0, 0.0, 4.0, 1.0, 4.0, 9.0, 5.0, 9.0, 0.0, 6.0, 9.0] Pred: [7.0, 2.0, 1.0, 0.0, 4.0, 1.0, 4.0, 9.0, 4.0, 9.0, 0.0, 6.0, 9.0]
print("Testing accuracy",clf.score(X=x_test,y=y_test))
Testing accuracy 0.8748