{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Handwritten Digit Classifier","metadata":{}},{"cell_type":"markdown","source":"# 1. Importing Dependencies","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras import models, layers\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-10-12T05:54:53.098463Z","iopub.execute_input":"2024-10-12T05:54:53.098793Z","iopub.status.idle":"2024-10-12T05:55:05.633299Z","shell.execute_reply.started":"2024-10-12T05:54:53.098766Z","shell.execute_reply":"2024-10-12T05:55:05.632233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Loading and Exploring the data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-12T05:55:05.634904Z","iopub.execute_input":"2024-10-12T05:55:05.635626Z","iopub.status.idle":"2024-10-12T05:55:09.100551Z","shell.execute_reply.started":"2024-10-12T05:55:05.635599Z","shell.execute_reply":"2024-10-12T05:55:09.099767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-10-12T05:55:09.101570Z","iopub.execute_input":"2024-10-12T05:55:09.101856Z","iopub.status.idle":"2024-10-12T05:55:09.130380Z","shell.execute_reply.started":"2024-10-12T05:55:09.101832Z","shell.execute_reply":"2024-10-12T05:55:09.129472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=train.drop(columns='label')","metadata":{"execution":{"iopub.status.busy":"2024-10-12T05:55:09.132298Z","iopub.execute_input":"2024-10-12T05:55:09.132571Z","iopub.status.idle":"2024-10-12T05:55:09.215427Z","shell.execute_reply.started":"2024-10-12T05:55:09.132548Z","shell.execute_reply":"2024-10-12T05:55:09.214551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image","metadata":{"execution":{"iopub.status.busy":"2024-10-12T05:55:09.216541Z","iopub.execute_input":"2024-10-12T05:55:09.216851Z","iopub.status.idle":"2024-10-12T05:55:09.240979Z","shell.execute_reply.started":"2024-10-12T05:55:09.216825Z","shell.execute_reply":"2024-10-12T05:55:09.240042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.iloc[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-10-12T05:55:09.241935Z","iopub.execute_input":"2024-10-12T05:55:09.242237Z","iopub.status.idle":"2024-10-12T05:55:09.247918Z","shell.execute_reply.started":"2024-10-12T05:55:09.242215Z","shell.execute_reply":"2024-10-12T05:55:09.247038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(6):\n    image1 =image.iloc[i].values\n    re_image = image1.reshape(28,28)\n    plt.imshow(re_image,cmap ='gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-12T05:55:40.944702Z","iopub.execute_input":"2024-10-12T05:55:40.945354Z","iopub.status.idle":"2024-10-12T05:55:42.273172Z","shell.execute_reply.started":"2024-10-12T05:55:40.945326Z","shell.execute_reply":"2024-10-12T05:55:42.272225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Pre-processing the data","metadata":{}},{"cell_type":"markdown","source":"Re-shaping 1 dimensional pixel values into 28 * 28 to feed into neural network","metadata":{}},{"cell_type":"code","source":"np_image = image.values\nre_image=np_image.reshape(-1,28,28)\nre_image.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:01.146566Z","iopub.execute_input":"2024-10-11T12:37:01.147175Z","iopub.status.idle":"2024-10-11T12:37:01.153542Z","shell.execute_reply.started":"2024-10-11T12:37:01.147142Z","shell.execute_reply":"2024-10-11T12:37:01.152690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalizing the data by dividing with maximum pixel value(255)","metadata":{}},{"cell_type":"code","source":"re_image = re_image/255","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:01.154569Z","iopub.execute_input":"2024-10-11T12:37:01.154949Z","iopub.status.idle":"2024-10-11T12:37:01.262631Z","shell.execute_reply.started":"2024-10-11T12:37:01.154913Z","shell.execute_reply":"2024-10-11T12:37:01.261867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['label']\ny","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:01.263718Z","iopub.execute_input":"2024-10-11T12:37:01.263988Z","iopub.status.idle":"2024-10-11T12:37:01.271288Z","shell.execute_reply.started":"2024-10-11T12:37:01.263965Z","shell.execute_reply":"2024-10-11T12:37:01.270356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=y.values.reshape(-1,1)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:01.275252Z","iopub.execute_input":"2024-10-11T12:37:01.275536Z","iopub.status.idle":"2024-10-11T12:37:01.281104Z","shell.execute_reply.started":"2024-10-11T12:37:01.275514Z","shell.execute_reply":"2024-10-11T12:37:01.280203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's encode the label using One Hot Encoder for training in neural network","metadata":{}},{"cell_type":"code","source":"\nencoder = OneHotEncoder()\ny_encoded = encoder.fit_transform(y)\nprint(y_encoded.toarray())","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:01.282290Z","iopub.execute_input":"2024-10-11T12:37:01.282820Z","iopub.status.idle":"2024-10-11T12:37:01.304171Z","shell.execute_reply.started":"2024-10-11T12:37:01.282775Z","shell.execute_reply":"2024-10-11T12:37:01.303331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = y_encoded.toarray()\nY[0]","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:01.305326Z","iopub.execute_input":"2024-10-11T12:37:01.305954Z","iopub.status.idle":"2024-10-11T12:37:01.312357Z","shell.execute_reply.started":"2024-10-11T12:37:01.305920Z","shell.execute_reply":"2024-10-11T12:37:01.311440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nx_train, x_test, y_train, y_test = train_test_split(re_image,Y,test_size=0.2,random_state = 42, stratify =Y)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:01.313495Z","iopub.execute_input":"2024-10-11T12:37:01.313842Z","iopub.status.idle":"2024-10-11T12:37:02.294442Z","shell.execute_reply.started":"2024-10-11T12:37:01.313812Z","shell.execute_reply":"2024-10-11T12:37:02.293420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Training the Model","metadata":{}},{"cell_type":"code","source":"classifier = models.Sequential()\nclassifier.add(Conv2D(32,kernel_size =(3,3),padding ='Same',activation ='relu',input_shape = (28,28,1)))\nclassifier.add(MaxPool2D(pool_size=(2,2)))\nclassifier.add(Conv2D(64,kernel_size =(3,3),padding ='Same',activation = 'relu'))\nclassifier.add(MaxPool2D(pool_size=(2,2)))\nclassifier.add(Conv2D(62,kernel_size =(3,3),padding ='Same',activation = 'relu'))\nclassifier.add(MaxPool2D(pool_size=(2,2)))\nclassifier.add(Flatten())\nclassifier.add(Dense(64, activation = 'relu'))\nclassifier.add(Dropout(0.3))\nclassifier.add(Dense(32,activation = 'relu'))\nclassifier.add(Dense(10,activation ='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:02.296099Z","iopub.execute_input":"2024-10-11T12:37:02.296521Z","iopub.status.idle":"2024-10-11T12:37:03.459406Z","shell.execute_reply.started":"2024-10-11T12:37:02.296417Z","shell.execute_reply":"2024-10-11T12:37:03.458616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier.compile(loss = 'categorical_crossentropy',optimizer = 'adam', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:03.460369Z","iopub.execute_input":"2024-10-11T12:37:03.460626Z","iopub.status.idle":"2024-10-11T12:37:03.472766Z","shell.execute_reply.started":"2024-10-11T12:37:03.460603Z","shell.execute_reply":"2024-10-11T12:37:03.471997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:03.473789Z","iopub.execute_input":"2024-10-11T12:37:03.474111Z","iopub.status.idle":"2024-10-11T12:37:03.480192Z","shell.execute_reply.started":"2024-10-11T12:37:03.474080Z","shell.execute_reply":"2024-10-11T12:37:03.479346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier.fit(x_train,y_train,epochs = 50,batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:37:03.481097Z","iopub.execute_input":"2024-10-11T12:37:03.481339Z","iopub.status.idle":"2024-10-11T12:39:15.260621Z","shell.execute_reply.started":"2024-10-11T12:37:03.481318Z","shell.execute_reply":"2024-10-11T12:39:15.259705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier.summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:15.261761Z","iopub.execute_input":"2024-10-11T12:39:15.262044Z","iopub.status.idle":"2024-10-11T12:39:15.292262Z","shell.execute_reply.started":"2024-10-11T12:39:15.262019Z","shell.execute_reply":"2024-10-11T12:39:15.291432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = classifier.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:15.293426Z","iopub.execute_input":"2024-10-11T12:39:15.293787Z","iopub.status.idle":"2024-10-11T12:39:17.070802Z","shell.execute_reply.started":"2024-10-11T12:39:15.293755Z","shell.execute_reply":"2024-10-11T12:39:17.069962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred[0]","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:17.072144Z","iopub.execute_input":"2024-10-11T12:39:17.072406Z","iopub.status.idle":"2024-10-11T12:39:17.079882Z","shell.execute_reply.started":"2024-10-11T12:39:17.072385Z","shell.execute_reply":"2024-10-11T12:39:17.078592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_categorical = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:17.081074Z","iopub.execute_input":"2024-10-11T12:39:17.081356Z","iopub.status.idle":"2024-10-11T12:39:17.090616Z","shell.execute_reply.started":"2024-10-11T12:39:17.081332Z","shell.execute_reply":"2024-10-11T12:39:17.089447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_categorical = np.argmax(y_test, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:17.091990Z","iopub.execute_input":"2024-10-11T12:39:17.092346Z","iopub.status.idle":"2024-10-11T12:39:17.101886Z","shell.execute_reply.started":"2024-10-11T12:39:17.092317Z","shell.execute_reply":"2024-10-11T12:39:17.100957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_categorical","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:17.103216Z","iopub.execute_input":"2024-10-11T12:39:17.104141Z","iopub.status.idle":"2024-10-11T12:39:17.114856Z","shell.execute_reply.started":"2024-10-11T12:39:17.104101Z","shell.execute_reply":"2024-10-11T12:39:17.113870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_categorical","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:17.116029Z","iopub.execute_input":"2024-10-11T12:39:17.116600Z","iopub.status.idle":"2024-10-11T12:39:17.133288Z","shell.execute_reply.started":"2024-10-11T12:39:17.116572Z","shell.execute_reply":"2024-10-11T12:39:17.132338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:17.134305Z","iopub.execute_input":"2024-10-11T12:39:17.134548Z","iopub.status.idle":"2024-10-11T12:39:17.145227Z","shell.execute_reply.started":"2024-10-11T12:39:17.134527Z","shell.execute_reply":"2024-10-11T12:39:17.144391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test_categorical,y_pred_categorical)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:17.146430Z","iopub.execute_input":"2024-10-11T12:39:17.146883Z","iopub.status.idle":"2024-10-11T12:39:17.158477Z","shell.execute_reply.started":"2024-10-11T12:39:17.146849Z","shell.execute_reply":"2024-10-11T12:39:17.157627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. Submission","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:17.159565Z","iopub.execute_input":"2024-10-11T12:39:17.159877Z","iopub.status.idle":"2024-10-11T12:39:19.390043Z","shell.execute_reply.started":"2024-10-11T12:39:17.159854Z","shell.execute_reply":"2024-10-11T12:39:19.389223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_shape=test.values.reshape(-1,28,28)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:19.391258Z","iopub.execute_input":"2024-10-11T12:39:19.391524Z","iopub.status.idle":"2024-10-11T12:39:19.395879Z","shell.execute_reply.started":"2024-10-11T12:39:19.391502Z","shell.execute_reply":"2024-10-11T12:39:19.395056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_shape = test_shape.reshape(-1,28,28)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:19.396908Z","iopub.execute_input":"2024-10-11T12:39:19.397185Z","iopub.status.idle":"2024-10-11T12:39:19.405968Z","shell.execute_reply.started":"2024-10-11T12:39:19.397154Z","shell.execute_reply":"2024-10-11T12:39:19.405226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_shape=test_shape/255","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:19.406793Z","iopub.execute_input":"2024-10-11T12:39:19.407079Z","iopub.status.idle":"2024-10-11T12:39:19.484324Z","shell.execute_reply.started":"2024-10-11T12:39:19.407046Z","shell.execute_reply":"2024-10-11T12:39:19.483373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_final= classifier.predict(test_shape)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:19.490808Z","iopub.execute_input":"2024-10-11T12:39:19.491119Z","iopub.status.idle":"2024-10-11T12:39:21.423310Z","shell.execute_reply.started":"2024-10-11T12:39:19.491093Z","shell.execute_reply":"2024-10-11T12:39:21.422461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_final.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:21.424585Z","iopub.execute_input":"2024-10-11T12:39:21.424895Z","iopub.status.idle":"2024-10-11T12:39:21.430949Z","shell.execute_reply.started":"2024-10-11T12:39:21.424870Z","shell.execute_reply":"2024-10-11T12:39:21.430031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:21.432257Z","iopub.execute_input":"2024-10-11T12:39:21.432915Z","iopub.status.idle":"2024-10-11T12:39:21.465412Z","shell.execute_reply.started":"2024-10-11T12:39:21.432890Z","shell.execute_reply":"2024-10-11T12:39:21.464565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_final_categorical = np.argmax(y_final, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:21.466408Z","iopub.execute_input":"2024-10-11T12:39:21.466718Z","iopub.status.idle":"2024-10-11T12:39:21.471353Z","shell.execute_reply.started":"2024-10-11T12:39:21.466693Z","shell.execute_reply":"2024-10-11T12:39:21.470501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Label']=y_final_categorical","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:21.472554Z","iopub.execute_input":"2024-10-11T12:39:21.472921Z","iopub.status.idle":"2024-10-11T12:39:21.481448Z","shell.execute_reply.started":"2024-10-11T12:39:21.472897Z","shell.execute_reply":"2024-10-11T12:39:21.480782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:21.482459Z","iopub.execute_input":"2024-10-11T12:39:21.482756Z","iopub.status.idle":"2024-10-11T12:39:21.503096Z","shell.execute_reply.started":"2024-10-11T12:39:21.482733Z","shell.execute_reply":"2024-10-11T12:39:21.502284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T12:39:21.504287Z","iopub.execute_input":"2024-10-11T12:39:21.504927Z","iopub.status.idle":"2024-10-11T12:39:21.546142Z","shell.execute_reply.started":"2024-10-11T12:39:21.504896Z","shell.execute_reply":"2024-10-11T12:39:21.545494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}