{"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":"# <font color='Yellow'>Importing Libraries</font>","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom tqdm import tqdm\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-18T05:25:43.053289Z","iopub.execute_input":"2022-12-18T05:25:43.053835Z","iopub.status.idle":"2022-12-18T05:25:45.635206Z","shell.execute_reply.started":"2022-12-18T05:25:43.053733Z","shell.execute_reply":"2022-12-18T05:25:45.634076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <font color='Yellow'>Loading Data</font>","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:45.637191Z","iopub.execute_input":"2022-12-18T05:25:45.638498Z","iopub.status.idle":"2022-12-18T05:25:49.945873Z","shell.execute_reply.started":"2022-12-18T05:25:45.638452Z","shell.execute_reply":"2022-12-18T05:25:49.944608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:49.947341Z","iopub.execute_input":"2022-12-18T05:25:49.948065Z","iopub.status.idle":"2022-12-18T05:25:49.972581Z","shell.execute_reply.started":"2022-12-18T05:25:49.948013Z","shell.execute_reply":"2022-12-18T05:25:49.971188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:49.974205Z","iopub.execute_input":"2022-12-18T05:25:49.975368Z","iopub.status.idle":"2022-12-18T05:25:49.983617Z","shell.execute_reply.started":"2022-12-18T05:25:49.975301Z","shell.execute_reply":"2022-12-18T05:25:49.982426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training Data :\n#### 42000 Rows & 785 Columns\n\n#### 785 Columns = 1 Label + 28x28 pixel","metadata":{}},{"cell_type":"code","source":"img = train.iloc[0, 1:]\nimg = np.array(img).reshape(28,28)\ntitle = train['label'][0]\nplt.imshow(img)\nplt.title('Image of : ' + str(title))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:49.987753Z","iopub.execute_input":"2022-12-18T05:25:49.988852Z","iopub.status.idle":"2022-12-18T05:25:50.204022Z","shell.execute_reply.started":"2022-12-18T05:25:49.988810Z","shell.execute_reply":"2022-12-18T05:25:50.202787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = train.iloc[91, 1:]\nimg = np.array(img).reshape(28,28)\ntitle = train['label'][91]\nplt.imshow(img)\nplt.title('Image of : ' + str(title))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.205745Z","iopub.execute_input":"2022-12-18T05:25:50.206241Z","iopub.status.idle":"2022-12-18T05:25:50.413337Z","shell.execute_reply.started":"2022-12-18T05:25:50.206194Z","shell.execute_reply":"2022-12-18T05:25:50.411956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Grey channel distribution\n\nplt.hist(np.array(train.iloc[91, 1:]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.414732Z","iopub.execute_input":"2022-12-18T05:25:50.415087Z","iopub.status.idle":"2022-12-18T05:25:50.624615Z","shell.execute_reply.started":"2022-12-18T05:25:50.415055Z","shell.execute_reply":"2022-12-18T05:25:50.623360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.626227Z","iopub.execute_input":"2022-12-18T05:25:50.626717Z","iopub.status.idle":"2022-12-18T05:25:50.647118Z","shell.execute_reply.started":"2022-12-18T05:25:50.626681Z","shell.execute_reply":"2022-12-18T05:25:50.645772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.648416Z","iopub.execute_input":"2022-12-18T05:25:50.648864Z","iopub.status.idle":"2022-12-18T05:25:50.657517Z","shell.execute_reply.started":"2022-12-18T05:25:50.648827Z","shell.execute_reply":"2022-12-18T05:25:50.656191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values_count = train.isnull().sum()\nmissing_values_count","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.659323Z","iopub.execute_input":"2022-12-18T05:25:50.659771Z","iopub.status.idle":"2022-12-18T05:25:50.717767Z","shell.execute_reply.started":"2022-12-18T05:25:50.659733Z","shell.execute_reply":"2022-12-18T05:25:50.716631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <font color='Yellow'>Preparing Training & Testing Data</font>","metadata":{}},{"cell_type":"code","source":"x = np.array(train)\ny = np.array(test)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.719158Z","iopub.execute_input":"2022-12-18T05:25:50.719545Z","iopub.status.idle":"2022-12-18T05:25:50.892397Z","shell.execute_reply.started":"2022-12-18T05:25:50.719512Z","shell.execute_reply":"2022-12-18T05:25:50.891157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x[:, 1:]\ny_train = x[:, 0]\nx_test = y","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.893937Z","iopub.execute_input":"2022-12-18T05:25:50.894345Z","iopub.status.idle":"2022-12-18T05:25:50.899943Z","shell.execute_reply.started":"2022-12-18T05:25:50.894311Z","shell.execute_reply":"2022-12-18T05:25:50.898731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(-1,28,28,1)                            # -1 in reshape means, it asks Python to decide the sizw/shape","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.901832Z","iopub.execute_input":"2022-12-18T05:25:50.902352Z","iopub.status.idle":"2022-12-18T05:25:50.910192Z","shell.execute_reply.started":"2022-12-18T05:25:50.902301Z","shell.execute_reply":"2022-12-18T05:25:50.909115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <font color='Cyan'>Generating Image Data</font>","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n                rotation_range=15,\n                zoom_range=0.1,\n                shear_range=0.1,\n                height_shift_range=0.1\n            )","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.914970Z","iopub.execute_input":"2022-12-18T05:25:50.915478Z","iopub.status.idle":"2022-12-18T05:25:50.923549Z","shell.execute_reply.started":"2022-12-18T05:25:50.915442Z","shell.execute_reply":"2022-12-18T05:25:50.922588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:50.924809Z","iopub.execute_input":"2022-12-18T05:25:50.925132Z","iopub.status.idle":"2022-12-18T05:25:51.049172Z","shell.execute_reply.started":"2022-12-18T05:25:50.925103Z","shell.execute_reply":"2022-12-18T05:25:51.047937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(-1,784)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:51.051063Z","iopub.execute_input":"2022-12-18T05:25:51.051476Z","iopub.status.idle":"2022-12-18T05:25:51.057303Z","shell.execute_reply.started":"2022-12-18T05:25:51.051438Z","shell.execute_reply":"2022-12-18T05:25:51.056000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <font color='Cyan'>Training Model : Image Data</font>","metadata":{}},{"cell_type":"code","source":"knn = KNeighborsClassifier(5)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:51.058792Z","iopub.execute_input":"2022-12-18T05:25:51.059362Z","iopub.status.idle":"2022-12-18T05:25:51.067984Z","shell.execute_reply.started":"2022-12-18T05:25:51.059311Z","shell.execute_reply":"2022-12-18T05:25:51.066624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:51.069868Z","iopub.execute_input":"2022-12-18T05:25:51.071087Z","iopub.status.idle":"2022-12-18T05:25:51.089357Z","shell.execute_reply.started":"2022-12-18T05:25:51.071031Z","shell.execute_reply":"2022-12-18T05:25:51.088095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <font color='Cyan'>Predicting Image Data</font>","metadata":{}},{"cell_type":"code","source":"prediction = knn.predict(y[100].reshape(1,-1))                           # predicting 100th row\nprint(int(prediction))","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:51.090669Z","iopub.execute_input":"2022-12-18T05:25:51.091028Z","iopub.status.idle":"2022-12-18T05:25:51.269762Z","shell.execute_reply.started":"2022-12-18T05:25:51.090995Z","shell.execute_reply":"2022-12-18T05:25:51.264664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np.array(y[100,:]).reshape(28,28))                            # plotting 100th row\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:51.272228Z","iopub.execute_input":"2022-12-18T05:25:51.273375Z","iopub.status.idle":"2022-12-18T05:25:51.460773Z","shell.execute_reply.started":"2022-12-18T05:25:51.273302Z","shell.execute_reply":"2022-12-18T05:25:51.459731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = knn.predict(np.reshape(x_test[27365,], (1,-1)))                 # predicting 27365th row\nprint(int(prediction))","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:51.462653Z","iopub.execute_input":"2022-12-18T05:25:51.463506Z","iopub.status.idle":"2022-12-18T05:25:51.634685Z","shell.execute_reply.started":"2022-12-18T05:25:51.463459Z","shell.execute_reply":"2022-12-18T05:25:51.632704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np.array(np.reshape(x_test[27365,], (28,28))))                    # plotting 27365th row\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:51.637955Z","iopub.execute_input":"2022-12-18T05:25:51.639160Z","iopub.status.idle":"2022-12-18T05:25:51.824344Z","shell.execute_reply.started":"2022-12-18T05:25:51.639084Z","shell.execute_reply":"2022-12-18T05:25:51.822688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = knn.predict(x_test[9999,:].reshape(1,-1))                         # predicting 9999th row\nprint(int(prediction))","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:51.826809Z","iopub.execute_input":"2022-12-18T05:25:51.827405Z","iopub.status.idle":"2022-12-18T05:25:52.001995Z","shell.execute_reply.started":"2022-12-18T05:25:51.827350Z","shell.execute_reply":"2022-12-18T05:25:52.000227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np.array(np.reshape(y[9999,], (28,28))))                          # plotting 9999th row\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:52.004927Z","iopub.execute_input":"2022-12-18T05:25:52.006200Z","iopub.status.idle":"2022-12-18T05:25:52.220171Z","shell.execute_reply.started":"2022-12-18T05:25:52.006128Z","shell.execute_reply":"2022-12-18T05:25:52.219285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId = []\nLabel = []\n\nfor i in tqdm(range(y.shape[0])):\n    \n    ImageId.append(i+1)\n    \n    l = knn.predict(y[i,:].reshape(1,-1))\n    l = int(l)\n    \n    Label.append(l)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T05:25:52.221625Z","iopub.execute_input":"2022-12-18T05:25:52.222214Z","iopub.status.idle":"2022-12-18T06:59:40.749125Z","shell.execute_reply.started":"2022-12-18T05:25:52.222179Z","shell.execute_reply":"2022-12-18T06:59:40.747506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'ImageId' : ImageId, 'Label' : Label})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T06:59:40.771307Z","iopub.execute_input":"2022-12-18T06:59:40.772516Z","iopub.status.idle":"2022-12-18T06:59:40.843633Z","shell.execute_reply.started":"2022-12-18T06:59:40.772449Z","shell.execute_reply":"2022-12-18T06:59:40.842026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('Digits.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T07:00:25.315119Z","iopub.execute_input":"2022-12-18T07:00:25.315536Z","iopub.status.idle":"2022-12-18T07:00:25.352391Z","shell.execute_reply.started":"2022-12-18T07:00:25.315498Z","shell.execute_reply":"2022-12-18T07:00:25.351244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"op = pd.read_csv('Digits.csv')\nop.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T07:00:27.322101Z","iopub.execute_input":"2022-12-18T07:00:27.322514Z","iopub.status.idle":"2022-12-18T07:00:27.339088Z","shell.execute_reply.started":"2022-12-18T07:00:27.322480Z","shell.execute_reply":"2022-12-18T07:00:27.338111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}