{"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)\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-06-18T06:06:09.019678Z","iopub.execute_input":"2023-06-18T06:06:09.020129Z","iopub.status.idle":"2023-06-18T06:06:09.030435Z","shell.execute_reply.started":"2023-06-18T06:06:09.020093Z","shell.execute_reply":"2023-06-18T06:06:09.028983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-06-18T06:06:13.864235Z","iopub.execute_input":"2023-06-18T06:06:13.864958Z","iopub.status.idle":"2023-06-18T06:06:20.614812Z","shell.execute_reply.started":"2023-06-18T06:06:13.864913Z","shell.execute_reply":"2023-06-18T06:06:20.613741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:06:26.204182Z","iopub.execute_input":"2023-06-18T06:06:26.204564Z","iopub.status.idle":"2023-06-18T06:06:26.243754Z","shell.execute_reply.started":"2023-06-18T06:06:26.204535Z","shell.execute_reply":"2023-06-18T06:06:26.242822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:06:35.885195Z","iopub.execute_input":"2023-06-18T06:06:35.885785Z","iopub.status.idle":"2023-06-18T06:06:35.892287Z","shell.execute_reply.started":"2023-06-18T06:06:35.885755Z","shell.execute_reply":"2023-06-18T06:06:35.891115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"number = np.array(train.iloc[0,1:]).reshape(28,28)\nnumber[10]","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:06:49.605159Z","iopub.execute_input":"2023-06-18T06:06:49.605596Z","iopub.status.idle":"2023-06-18T06:06:49.612818Z","shell.execute_reply.started":"2023-06-18T06:06:49.605563Z","shell.execute_reply":"2023-06-18T06:06:49.611980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:07:04.400172Z","iopub.execute_input":"2023-06-18T06:07:04.401224Z","iopub.status.idle":"2023-06-18T06:07:04.405862Z","shell.execute_reply.started":"2023-06-18T06:07:04.401186Z","shell.execute_reply":"2023-06-18T06:07:04.404483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(number)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:07:16.840126Z","iopub.execute_input":"2023-06-18T06:07:16.840921Z","iopub.status.idle":"2023-06-18T06:07:17.170283Z","shell.execute_reply.started":"2023-06-18T06:07:16.840858Z","shell.execute_reply":"2023-06-18T06:07:17.169116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"number4 = np.array(train.iloc[3,1:]).reshape(28,28)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:07:35.605073Z","iopub.execute_input":"2023-06-18T06:07:35.606123Z","iopub.status.idle":"2023-06-18T06:07:35.612856Z","shell.execute_reply.started":"2023-06-18T06:07:35.606084Z","shell.execute_reply":"2023-06-18T06:07:35.611617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(number4)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:07:46.150074Z","iopub.execute_input":"2023-06-18T06:07:46.150476Z","iopub.status.idle":"2023-06-18T06:07:46.434932Z","shell.execute_reply.started":"2023-06-18T06:07:46.150445Z","shell.execute_reply":"2023-06-18T06:07:46.433646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = train.iloc[3,1:]\n","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:08:03.524983Z","iopub.execute_input":"2023-06-18T06:08:03.525371Z","iopub.status.idle":"2023-06-18T06:08:03.530649Z","shell.execute_reply.started":"2023-06-18T06:08:03.525342Z","shell.execute_reply":"2023-06-18T06:08:03.529590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(arr)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:08:26.239331Z","iopub.execute_input":"2023-06-18T06:08:26.239737Z","iopub.status.idle":"2023-06-18T06:08:26.539956Z","shell.execute_reply.started":"2023-06-18T06:08:26.239707Z","shell.execute_reply":"2023-06-18T06:08:26.538697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(arr)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:08:38.045383Z","iopub.execute_input":"2023-06-18T06:08:38.045862Z","iopub.status.idle":"2023-06-18T06:08:38.056635Z","shell.execute_reply.started":"2023-06-18T06:08:38.045825Z","shell.execute_reply":"2023-06-18T06:08:38.055226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min(arr)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:08:50.820017Z","iopub.execute_input":"2023-06-18T06:08:50.820462Z","iopub.status.idle":"2023-06-18T06:08:50.829403Z","shell.execute_reply.started":"2023-06-18T06:08:50.820432Z","shell.execute_reply":"2023-06-18T06:08:50.827500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.mean(arr)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:09:00.429267Z","iopub.execute_input":"2023-06-18T06:09:00.429677Z","iopub.status.idle":"2023-06-18T06:09:00.437451Z","shell.execute_reply.started":"2023-06-18T06:09:00.429648Z","shell.execute_reply":"2023-06-18T06:09:00.436216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Missing Values","metadata":{}},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:09:31.965437Z","iopub.execute_input":"2023-06-18T06:09:31.965855Z","iopub.status.idle":"2023-06-18T06:09:32.001579Z","shell.execute_reply.started":"2023-06-18T06:09:31.965821Z","shell.execute_reply":"2023-06-18T06:09:32.000396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(train)\ny = np.array(test)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:09:43.220417Z","iopub.execute_input":"2023-06-18T06:09:43.221164Z","iopub.status.idle":"2023-06-18T06:09:43.455976Z","shell.execute_reply.started":"2023-06-18T06:09:43.221131Z","shell.execute_reply":"2023-06-18T06:09:43.454621Z"},"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":"2023-06-18T06:09:54.200070Z","iopub.execute_input":"2023-06-18T06:09:54.200501Z","iopub.status.idle":"2023-06-18T06:09:54.206233Z","shell.execute_reply.started":"2023-06-18T06:09:54.200468Z","shell.execute_reply":"2023-06-18T06:09:54.204880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Engineering","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nX_train = X_train.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:10:18.030077Z","iopub.execute_input":"2023-06-18T06:10:18.030468Z","iopub.status.idle":"2023-06-18T06:10:26.739342Z","shell.execute_reply.started":"2023-06-18T06:10:18.030437Z","shell.execute_reply":"2023-06-18T06:10:26.738458Z"},"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-06-18T06:10:39.715237Z","iopub.execute_input":"2023-06-18T06:10:39.716427Z","iopub.status.idle":"2023-06-18T06:10:39.862994Z","shell.execute_reply.started":"2023-06-18T06:10:39.716391Z","shell.execute_reply":"2023-06-18T06:10:39.861752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1,784)\nX_train\n","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:10:51.320091Z","iopub.execute_input":"2023-06-18T06:10:51.320517Z","iopub.status.idle":"2023-06-18T06:10:51.328952Z","shell.execute_reply.started":"2023-06-18T06:10:51.320484Z","shell.execute_reply":"2023-06-18T06:10:51.327809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:11:28.164213Z","iopub.execute_input":"2023-06-18T06:11:28.164605Z","iopub.status.idle":"2023-06-18T06:11:29.401562Z","shell.execute_reply.started":"2023-06-18T06:11:28.164575Z","shell.execute_reply":"2023-06-18T06:11:29.400299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors=5)\nknn.fit(X_train,Y_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:11:39.999983Z","iopub.execute_input":"2023-06-18T06:11:40.000413Z","iopub.status.idle":"2023-06-18T06:11:40.147153Z","shell.execute_reply.started":"2023-06-18T06:11:40.000378Z","shell.execute_reply":"2023-06-18T06:11:40.145917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"int(knn.predict(X_test[60].reshape(1,-1)))","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:11:50.259989Z","iopub.execute_input":"2023-06-18T06:11:50.260387Z","iopub.status.idle":"2023-06-18T06:11:50.457836Z","shell.execute_reply.started":"2023-06-18T06:11:50.260357Z","shell.execute_reply":"2023-06-18T06:11:50.456381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num = np.array(test.iloc[60,:]).reshape(28,28)\nplt.imshow(num)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T06:12:02.104676Z","iopub.execute_input":"2023-06-18T06:12:02.105057Z","iopub.status.idle":"2023-06-18T06:12:02.392832Z","shell.execute_reply.started":"2023-06-18T06:12:02.105028Z","shell.execute_reply":"2023-06-18T06:12:02.391755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}