<jupyter_start><jupyter_text>Importing liabraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from sklearn.metrics import confusion_matrix
from keras.models import load_model
path=r'C:\Users\sahoo\Desktop\Models\Digit Recognizer'
tr=pd.read_csv(path+"/train.csv")
tr
feature=tr.drop('label',axis=1)
feature
a=np.array(feature.iloc[41999,:])
features=[]
for i in range(len(tr)):
    features.append(np.array(feature.iloc[i,:]))
print(tr['label'].unique()) #unique classes
label=tr['label']
label=np.array(label)
label
features[1]<jupyter_output><empty_output><jupyter_text>Convert raw data to image data<jupyter_code>def resizeimg(img):
    img=np.reshape(img,(28,28,1))
    img=img/255.0
    return img
features=np.array(list(map(resizeimg,features)))<jupyter_output><empty_output><jupyter_text>Print Sample image<jupyter_code>plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(features[i], cmap=plt.cm.binary)
    plt.xlabel(label[i])
plt.show()<jupyter_output><empty_output><jupyter_text>Splitting the dataset<jupyter_code>x_train,x_test,y_train,y_test=train_test_split(features, label, random_state=0, test_size=0.25)

print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)<jupyter_output>(31500, 28, 28, 1)
(10500, 28, 28, 1)
(31500,)
(10500,)<jupyter_text>Model Building<jupyter_code>model= Sequential()

model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(2,2))

model.add(Conv2D(128, kernel_size=(3,3), activation='relu'))
model.add(Conv2D(128, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(2,2))

model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(2,2))

model.add(Flatten(input_shape=x_train.shape[1:]))

model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(len(tr['label'].unique()), activation='softmax'))

model.compile(loss='SparseCategoricalCrossentropy',metrics=['accuracy'],optimizer='Adam')

model.fit(x_train,
          y_train,
          epochs=20,
          batch_size=32,
          verbose=1,
          validation_data=(x_test,y_test))
print(model.summary())

pred=model.predict(x_test)
y_pred=[]
for i in range(len(pred)):
    y_pred.append(np.argmax(pred[i]))

print(confusion_matrix(y_test, y_pred))
#model.save(path+"/digit_classifier.h5")
mymodel=load_model(path+"/digit_classifier.h5")
mymodel<jupyter_output><empty_output><jupyter_text>Let's predict with the new data<jupyter_code>test_data=pd.read_csv(path+"/test.csv")
test_data
new_features=[]
for i in range(len(test_data)):
    new_features.append(np.array(test_data.iloc[i,:]))

new_features=np.array(list(map(resizeimg,new_features)))

new_prediction=model.predict(new_features)
plt.figure(figsize=(15,15))
for i in range(100):
    plt.subplot(10,10,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(new_features[i], cmap=plt.cm.binary)
    plt.xlabel(np.argmax(new_prediction[i]))
plt.show()
final_pred=[]
for i in range(len(new_prediction)):
    final_pred.append(np.argmax(new_prediction[i]))
frame=pd.DataFrame()
frame['ImageId']=list(range(1,28001))
frame['Label']=final_pred
frame
frame.to_csv(path+'/final_prediction.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1644811858608894, 'fraction_numerical': 0.03506271379703535, 'mean_word_length': 7.579462102689487, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '287680', 'license': 'Unlicense', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1378, 'n_words': 181, 'path': 'Digit Recognizer.ipynb', 'repo_name': 'SibPrasad/Digit-Recognizer', 'size': '3508'}