{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-30T21:59:12.599748Z","iopub.execute_input":"2023-10-30T21:59:12.600815Z","iopub.status.idle":"2023-10-30T21:59:12.607553Z","shell.execute_reply.started":"2023-10-30T21:59:12.600779Z","shell.execute_reply":"2023-10-30T21:59:12.606590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","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":"2023-10-30T21:54:18.646297Z","iopub.execute_input":"2023-10-30T21:54:18.646726Z","iopub.status.idle":"2023-10-30T21:54:23.980156Z","shell.execute_reply.started":"2023-10-30T21:54:18.646693Z","shell.execute_reply":"2023-10-30T21:54:23.979189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T21:54:33.679333Z","iopub.execute_input":"2023-10-30T21:54:33.679728Z","iopub.status.idle":"2023-10-30T21:54:33.704463Z","shell.execute_reply.started":"2023-10-30T21:54:33.679695Z","shell.execute_reply":"2023-10-30T21:54:33.703445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-30T21:57:03.401222Z","iopub.execute_input":"2023-10-30T21:57:03.401723Z","iopub.status.idle":"2023-10-30T21:57:03.410603Z","shell.execute_reply.started":"2023-10-30T21:57:03.401682Z","shell.execute_reply":"2023-10-30T21:57:03.408990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-30T21:57:14.839298Z","iopub.execute_input":"2023-10-30T21:57:14.839664Z","iopub.status.idle":"2023-10-30T21:57:14.846165Z","shell.execute_reply.started":"2023-10-30T21:57:14.839632Z","shell.execute_reply":"2023-10-30T21:57:14.845164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num = np.array(train.iloc[3, 1:]).reshape(28,28)\nplt.imshow(num)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:10:48.271940Z","iopub.execute_input":"2023-10-30T22:10:48.272317Z","iopub.status.idle":"2023-10-30T22:10:48.475222Z","shell.execute_reply.started":"2023-10-30T22:10:48.272287Z","shell.execute_reply":"2023-10-30T22:10:48.474190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = train.iloc[3, 1:]","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:10:48.476807Z","iopub.execute_input":"2023-10-30T22:10:48.477107Z","iopub.status.idle":"2023-10-30T22:10:48.482226Z","shell.execute_reply.started":"2023-10-30T22:10:48.477080Z","shell.execute_reply":"2023-10-30T22:10:48.481271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(arr)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:01:34.346753Z","iopub.execute_input":"2023-10-30T22:01:34.347167Z","iopub.status.idle":"2023-10-30T22:01:34.636730Z","shell.execute_reply.started":"2023-10-30T22:01:34.347135Z","shell.execute_reply":"2023-10-30T22:01:34.635533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(arr)\n# 256 int of RGB Scale (2^8)\n# R G B -> 3 channels\n# Grey Scaled -> 1 channel (0- black, 255 - white)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:06:09.068176Z","iopub.execute_input":"2023-10-30T22:06:09.068559Z","iopub.status.idle":"2023-10-30T22:06:09.075223Z","shell.execute_reply.started":"2023-10-30T22:06:09.068531Z","shell.execute_reply":"2023-10-30T22:06:09.074307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_val_count = train.isnull().sum()\nmissing_val_count","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:08:48.912952Z","iopub.execute_input":"2023-10-30T22:08:48.913438Z","iopub.status.idle":"2023-10-30T22:08:48.969517Z","shell.execute_reply.started":"2023-10-30T22:08:48.913398Z","shell.execute_reply":"2023-10-30T22:08:48.968332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{}},{"cell_type":"code","source":"x = np.array(train)\ny = np.array(test)\n\nX_train = x[:, 1:]\nY_train = x[:, 0]\nY_test = y[:, 0]\nX_test = y","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:38:41.126861Z","iopub.execute_input":"2023-10-30T22:38:41.127240Z","iopub.status.idle":"2023-10-30T22:38:41.291075Z","shell.execute_reply.started":"2023-10-30T22:38:41.127210Z","shell.execute_reply":"2023-10-30T22:38:41.290032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Rotate Image to  create new image -> Shear it (Tilting)\n# Zoom \n# Shift the image\n# Keras -> ImageDataGenerator -> (a,x,y,z) -> input reshape\n\nfrom keras.preprocessing.image import ImageDataGenerator\nX_train = X_train.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:17:48.415512Z","iopub.execute_input":"2023-10-30T22:17:48.415876Z","iopub.status.idle":"2023-10-30T22:17:48.421514Z","shell.execute_reply.started":"2023-10-30T22:17:48.415849Z","shell.execute_reply":"2023-10-30T22:17:48.420408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range = 15,\n    height_shift_range = 0.1,\n    zoom_range = 0.1,\n    shear_range = 0.1,\n)\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:19:04.070539Z","iopub.execute_input":"2023-10-30T22:19:04.070945Z","iopub.status.idle":"2023-10-30T22:19:04.190630Z","shell.execute_reply.started":"2023-10-30T22:19:04.070914Z","shell.execute_reply":"2023-10-30T22:19:04.189524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1,784)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:22:32.155219Z","iopub.execute_input":"2023-10-30T22:22:32.155633Z","iopub.status.idle":"2023-10-30T22:22:32.161121Z","shell.execute_reply.started":"2023-10-30T22:22:32.155589Z","shell.execute_reply":"2023-10-30T22:22:32.159810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Apply ML Model","metadata":{}},{"cell_type":"code","source":"# KNN Model","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:20:11.009300Z","iopub.execute_input":"2023-10-30T22:20:11.009795Z","iopub.status.idle":"2023-10-30T22:20:11.015892Z","shell.execute_reply.started":"2023-10-30T22:20:11.009757Z","shell.execute_reply":"2023-10-30T22:20:11.014189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:21:21.382025Z","iopub.execute_input":"2023-10-30T22:21:21.383281Z","iopub.status.idle":"2023-10-30T22:21:21.686137Z","shell.execute_reply.started":"2023-10-30T22:21:21.383224Z","shell.execute_reply":"2023-10-30T22:21:21.685183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors = 5000)\nknn.fit(X_train, Y_train)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:48:42.306461Z","iopub.execute_input":"2023-10-30T22:48:42.306847Z","iopub.status.idle":"2023-10-30T22:48:42.473245Z","shell.execute_reply.started":"2023-10-30T22:48:42.306818Z","shell.execute_reply":"2023-10-30T22:48:42.472363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"int(knn.predict(X_test[153].reshape(1,-1)))","metadata":{"execution":{"iopub.status.busy":"2023-10-30T23:01:51.327216Z","iopub.execute_input":"2023-10-30T23:01:51.328308Z","iopub.status.idle":"2023-10-30T23:01:51.486747Z","shell.execute_reply.started":"2023-10-30T23:01:51.328274Z","shell.execute_reply":"2023-10-30T23:01:51.485666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num = np.array(test.iloc[153, :]).reshape(28,28)\nplt.imshow(num)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T23:01:47.429764Z","iopub.execute_input":"2023-10-30T23:01:47.430148Z","iopub.status.idle":"2023-10-30T23:01:47.643840Z","shell.execute_reply.started":"2023-10-30T23:01:47.430117Z","shell.execute_reply":"2023-10-30T23:01:47.642904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = knn.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:48:45.534388Z","iopub.execute_input":"2023-10-30T22:48:45.534757Z","iopub.status.idle":"2023-10-30T22:49:52.939549Z","shell.execute_reply.started":"2023-10-30T22:48:45.534728Z","shell.execute_reply":"2023-10-30T22:49:52.938409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy = accuracy_score(Y_test, Y_pred)\nprint(f'Accuracy: {accuracy * 1000:.2f}%')","metadata":{"execution":{"iopub.status.busy":"2023-10-30T22:49:58.841182Z","iopub.execute_input":"2023-10-30T22:49:58.841564Z","iopub.status.idle":"2023-10-30T22:49:58.849153Z","shell.execute_reply.started":"2023-10-30T22:49:58.841534Z","shell.execute_reply":"2023-10-30T22:49:58.848009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Accuracy: 89.57%","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}