{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing datasets and libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nfrom keras import layers, callbacks","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"papermill":{"duration":3.902806,"end_time":"2021-09-05T16:59:16.911176","exception":false,"start_time":"2021-09-05T16:59:13.00837","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T12:11:31.247327Z","iopub.execute_input":"2021-09-06T12:11:31.24776Z","iopub.status.idle":"2021-09-06T12:11:36.687167Z","shell.execute_reply.started":"2021-09-06T12:11:31.247721Z","shell.execute_reply":"2021-09-06T12:11:36.686318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring and converting dataset to feed into CNN","metadata":{}},{"cell_type":"code","source":"print(\"Train shape:\",train.shape,\"\\nTest shape:\",test.shape)","metadata":{"papermill":{"duration":0.036111,"end_time":"2021-09-05T16:59:23.331523","exception":false,"start_time":"2021-09-05T16:59:23.295412","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T12:11:41.198349Z","iopub.execute_input":"2021-09-06T12:11:41.198674Z","iopub.status.idle":"2021-09-06T12:11:41.207547Z","shell.execute_reply.started":"2021-09-06T12:11:41.198627Z","shell.execute_reply":"2021-09-06T12:11:41.206703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"papermill":{"duration":0.059646,"end_time":"2021-09-05T16:59:23.417635","exception":false,"start_time":"2021-09-05T16:59:23.357989","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T12:11:41.21012Z","iopub.execute_input":"2021-09-06T12:11:41.210403Z","iopub.status.idle":"2021-09-06T12:11:41.241414Z","shell.execute_reply.started":"2021-09-06T12:11:41.210346Z","shell.execute_reply":"2021-09-06T12:11:41.240498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting dataset\n## into validation sets","metadata":{}},{"cell_type":"code","source":"ytr = train['label']\nxtr = train.drop('label',axis = 1)","metadata":{"papermill":{"duration":0.142344,"end_time":"2021-09-05T16:59:23.588073","exception":false,"start_time":"2021-09-05T16:59:23.445729","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T12:18:57.235729Z","iopub.execute_input":"2021-09-06T12:18:57.236095Z","iopub.status.idle":"2021-09-06T12:18:57.317472Z","shell.execute_reply.started":"2021-09-06T12:18:57.236066Z","shell.execute_reply":"2021-09-06T12:18:57.316263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(xtr.shape,ytr.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T12:19:09.22752Z","iopub.execute_input":"2021-09-06T12:19:09.227911Z","iopub.status.idle":"2021-09-06T12:19:09.23361Z","shell.execute_reply.started":"2021-09-06T12:19:09.227879Z","shell.execute_reply":"2021-09-06T12:19:09.232428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtr, xval, ytr, yval = train_test_split(xtr, ytr, test_size = 0.27, random_state = 108)","metadata":{"papermill":{"duration":0.276681,"end_time":"2021-09-05T16:59:23.892895","exception":false,"start_time":"2021-09-05T16:59:23.616214","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T12:21:21.889374Z","iopub.execute_input":"2021-09-06T12:21:21.889729Z","iopub.status.idle":"2021-09-06T12:21:22.139673Z","shell.execute_reply.started":"2021-09-06T12:21:21.889695Z","shell.execute_reply":"2021-09-06T12:21:22.138828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('X_tr,y_tr shape',xtr.shape,ytr.shape,'\\nX_val,Y_val shape:',xval.shape,yval.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T12:22:22.637367Z","iopub.execute_input":"2021-09-06T12:22:22.637705Z","iopub.status.idle":"2021-09-06T12:22:22.644578Z","shell.execute_reply.started":"2021-09-06T12:22:22.637673Z","shell.execute_reply":"2021-09-06T12:22:22.643693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Converting the datasets to the required shape and also normalizing the data","metadata":{}},{"cell_type":"code","source":"xtr = np.array(xtr).reshape(xtr.shape[0],28,28,1)/255\nxval = np.array(xval).reshape(xval.shape[0],28,28,1)/255","metadata":{"execution":{"iopub.status.busy":"2021-09-06T12:44:03.005278Z","iopub.execute_input":"2021-09-06T12:44:03.005612Z","iopub.status.idle":"2021-09-06T12:44:03.208424Z","shell.execute_reply.started":"2021-09-06T12:44:03.005581Z","shell.execute_reply":"2021-09-06T12:44:03.207567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('X_tr,X_val shape',xtr.shape,xval.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T12:45:00.079633Z","iopub.execute_input":"2021-09-06T12:45:00.080001Z","iopub.status.idle":"2021-09-06T12:45:00.084785Z","shell.execute_reply.started":"2021-09-06T12:45:00.07997Z","shell.execute_reply":"2021-09-06T12:45:00.083711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Converting Y dataset shape","metadata":{}},{"cell_type":"code","source":"ytr=np.array(pd.get_dummies(ytr))\nyval=np.array(pd.get_dummies(yval))","metadata":{"execution":{"iopub.status.busy":"2021-09-06T13:08:55.12453Z","iopub.execute_input":"2021-09-06T13:08:55.12492Z","iopub.status.idle":"2021-09-06T13:08:55.141311Z","shell.execute_reply.started":"2021-09-06T13:08:55.12489Z","shell.execute_reply":"2021-09-06T13:08:55.140221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Y_tr,Y_val shape:',ytr.shape,yval.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-06T13:08:57.797675Z","iopub.execute_input":"2021-09-06T13:08:57.79831Z","iopub.status.idle":"2021-09-06T13:08:57.805336Z","shell.execute_reply.started":"2021-09-06T13:08:57.79819Z","shell.execute_reply":"2021-09-06T13:08:57.804465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ploting sample image from dataset","metadata":{}},{"cell_type":"code","source":"plt.imshow(xtr[0])\nprint('  0 1 2 3 4 5 6 7 8 9\\n',ytr[0])","metadata":{"papermill":{"duration":0.209893,"end_time":"2021-09-05T16:59:24.129929","exception":false,"start_time":"2021-09-05T16:59:23.920036","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T13:10:30.441868Z","iopub.execute_input":"2021-09-06T13:10:30.442197Z","iopub.status.idle":"2021-09-06T13:10:30.57762Z","shell.execute_reply.started":"2021-09-06T13:10:30.442169Z","shell.execute_reply":"2021-09-06T13:10:30.576709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building the CNN model","metadata":{"papermill":{"duration":0.029689,"end_time":"2021-09-05T16:59:25.051732","exception":false,"start_time":"2021-09-05T16:59:25.022043","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model = keras.Sequential([layers.Conv2D(6,(5,5), activation='relu', input_shape=(28,28,1)),\n                          layers.BatchNormalization(),\n                          layers.MaxPooling2D(pool_size=(2,2)),\n                          layers.Conv2D(16, (5,5), activation='relu'),\n                          layers.BatchNormalization(),\n                          layers.MaxPooling2D(pool_size=(2,2)),\n                          layers.Flatten(),\n                          layers.Dense(128, activation='relu'),\n                          layers.Dropout(0.3),\n                          layers.BatchNormalization(),\n                          layers.Dense(10, activation='softmax')])","metadata":{"execution":{"iopub.status.busy":"2021-09-06T13:36:45.018162Z","iopub.execute_input":"2021-09-06T13:36:45.018488Z","iopub.status.idle":"2021-09-06T13:36:45.094529Z","shell.execute_reply.started":"2021-09-06T13:36:45.018458Z","shell.execute_reply":"2021-09-06T13:36:45.09367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compiling the model","metadata":{"papermill":{"duration":0.030072,"end_time":"2021-09-05T16:59:25.300742","exception":false,"start_time":"2021-09-05T16:59:25.27067","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model.compile(optimizer = 'adamax', loss='categorical_hinge', metrics=['accuracy','categorical_accuracy','categorical_crossentropy','categorical_hinge','Recall','Precision'])","metadata":{"papermill":{"duration":0.050328,"end_time":"2021-09-05T16:59:25.381179","exception":false,"start_time":"2021-09-05T16:59:25.330851","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T13:50:57.135104Z","iopub.execute_input":"2021-09-06T13:50:57.135438Z","iopub.status.idle":"2021-09-06T13:50:57.150936Z","shell.execute_reply.started":"2021-09-06T13:50:57.135406Z","shell.execute_reply":"2021-09-06T13:50:57.150068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fitting the model with Training and testing data","metadata":{}},{"cell_type":"code","source":"call=callbacks.EarlyStopping( min_delta=0.0001, patience=15, verbose=1,restore_best_weights=True)\nmodel.fit(xtr,ytr,validation_data=(xval, yval), epochs=50,callbacks=[call])","metadata":{"papermill":{"duration":3280.752006,"end_time":"2021-09-05T17:54:06.222924","exception":false,"start_time":"2021-09-05T16:59:25.470918","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T13:51:00.586968Z","iopub.execute_input":"2021-09-06T13:51:00.587295Z","iopub.status.idle":"2021-09-06T13:54:36.7001Z","shell.execute_reply.started":"2021-09-06T13:51:00.587264Z","shell.execute_reply":"2021-09-06T13:54:36.699339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## More than 99% Accuracy","metadata":{}},{"cell_type":"markdown","source":"# Converting our test dataset","metadata":{}},{"cell_type":"code","source":"test.shape","metadata":{"papermill":{"duration":16.732174,"end_time":"2021-09-05T17:55:48.461043","exception":false,"start_time":"2021-09-05T17:55:31.728869","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T13:57:20.495318Z","iopub.execute_input":"2021-09-06T13:57:20.495702Z","iopub.status.idle":"2021-09-06T13:57:20.502334Z","shell.execute_reply.started":"2021-09-06T13:57:20.495659Z","shell.execute_reply":"2021-09-06T13:57:20.50142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.array(test).reshape(test.shape[0],28,28,1)/255","metadata":{"papermill":{"duration":16.706585,"end_time":"2021-09-05T17:56:21.972903","exception":false,"start_time":"2021-09-05T17:56:05.266318","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T13:57:32.374342Z","iopub.execute_input":"2021-09-06T13:57:32.374702Z","iopub.status.idle":"2021-09-06T13:57:32.512415Z","shell.execute_reply.started":"2021-09-06T13:57:32.374666Z","shell.execute_reply":"2021-09-06T13:57:32.511549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"papermill":{"duration":16.547699,"end_time":"2021-09-05T17:56:55.219906","exception":false,"start_time":"2021-09-05T17:56:38.672207","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T13:57:35.08096Z","iopub.execute_input":"2021-09-06T13:57:35.081288Z","iopub.status.idle":"2021-09-06T13:57:35.087451Z","shell.execute_reply.started":"2021-09-06T13:57:35.081252Z","shell.execute_reply":"2021-09-06T13:57:35.086192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting the results","metadata":{}},{"cell_type":"code","source":"ypred = model.predict(test)","metadata":{"papermill":{"duration":26.083005,"end_time":"2021-09-05T17:58:11.546093","exception":false,"start_time":"2021-09-05T17:57:45.463088","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T13:58:41.87927Z","iopub.execute_input":"2021-09-06T13:58:41.879591Z","iopub.status.idle":"2021-09-06T13:58:43.096Z","shell.execute_reply.started":"2021-09-06T13:58:41.879562Z","shell.execute_reply":"2021-09-06T13:58:43.095047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred.shape","metadata":{"papermill":{"duration":16.72919,"end_time":"2021-09-05T17:58:45.150554","exception":false,"start_time":"2021-09-05T17:58:28.421364","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T13:58:54.017521Z","iopub.execute_input":"2021-09-06T13:58:54.01788Z","iopub.status.idle":"2021-09-06T13:58:54.023608Z","shell.execute_reply.started":"2021-09-06T13:58:54.01785Z","shell.execute_reply":"2021-09-06T13:58:54.022815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = []\nfor i in range(len(ypred)):\n    pred.append(list(ypred[i]).index(max(ypred[i])))","metadata":{"papermill":{"duration":16.892962,"end_time":"2021-09-05T17:59:18.76138","exception":false,"start_time":"2021-09-05T17:59:01.868418","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T14:00:05.054456Z","iopub.execute_input":"2021-09-06T14:00:05.054996Z","iopub.status.idle":"2021-09-06T14:00:05.392296Z","shell.execute_reply.started":"2021-09-06T14:00:05.054934Z","shell.execute_reply":"2021-09-06T14:00:05.391225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'ImageId' : range(1,len(pred)+1),'Label' : pred})","metadata":{"papermill":{"duration":16.597238,"end_time":"2021-09-05T17:59:52.117822","exception":false,"start_time":"2021-09-05T17:59:35.520584","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T14:00:27.560404Z","iopub.execute_input":"2021-09-06T14:00:27.560775Z","iopub.status.idle":"2021-09-06T14:00:27.577456Z","shell.execute_reply.started":"2021-09-06T14:00:27.560739Z","shell.execute_reply":"2021-09-06T14:00:27.576535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"papermill":{"duration":16.716742,"end_time":"2021-09-05T18:00:25.491051","exception":false,"start_time":"2021-09-05T18:00:08.774309","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T14:00:42.342518Z","iopub.execute_input":"2021-09-06T14:00:42.342871Z","iopub.status.idle":"2021-09-06T14:00:42.353464Z","shell.execute_reply.started":"2021-09-06T14:00:42.342842Z","shell.execute_reply":"2021-09-06T14:00:42.352518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv',index=False)","metadata":{"papermill":{"duration":16.843034,"end_time":"2021-09-05T18:00:58.906908","exception":false,"start_time":"2021-09-05T18:00:42.063874","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-06T14:00:46.802167Z","iopub.execute_input":"2021-09-06T14:00:46.802505Z","iopub.status.idle":"2021-09-06T14:00:46.854569Z","shell.execute_reply.started":"2021-09-06T14:00:46.802473Z","shell.execute_reply":"2021-09-06T14:00:46.853804Z"},"trusted":true},"execution_count":null,"outputs":[]}]}