{"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-01-03T07:01:20.257020Z","iopub.execute_input":"2023-01-03T07:01:20.258675Z","iopub.status.idle":"2023-01-03T07:01:20.297403Z","shell.execute_reply.started":"2023-01-03T07:01:20.258527Z","shell.execute_reply":"2023-01-03T07:01:20.295443Z"},"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-01-03T07:01:20.299759Z","iopub.execute_input":"2023-01-03T07:01:20.300915Z","iopub.status.idle":"2023-01-03T07:01:27.751655Z","shell.execute_reply.started":"2023-01-03T07:01:20.300865Z","shell.execute_reply":"2023-01-03T07:01:27.750119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:27.753819Z","iopub.execute_input":"2023-01-03T07:01:27.754238Z","iopub.status.idle":"2023-01-03T07:01:27.788828Z","shell.execute_reply.started":"2023-01-03T07:01:27.754205Z","shell.execute_reply":"2023-01-03T07:01:27.787702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:27.791695Z","iopub.execute_input":"2023-01-03T07:01:27.792139Z","iopub.status.idle":"2023-01-03T07:01:27.801741Z","shell.execute_reply.started":"2023-01-03T07:01:27.792091Z","shell.execute_reply":"2023-01-03T07:01:27.800417Z"},"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-01-03T07:14:26.364946Z","iopub.execute_input":"2023-01-03T07:14:26.365470Z","iopub.status.idle":"2023-01-03T07:14:26.377933Z","shell.execute_reply.started":"2023-01-03T07:14:26.365427Z","shell.execute_reply":"2023-01-03T07:14:26.376089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:27.818241Z","iopub.execute_input":"2023-01-03T07:01:27.818597Z","iopub.status.idle":"2023-01-03T07:01:27.830958Z","shell.execute_reply.started":"2023-01-03T07:01:27.818565Z","shell.execute_reply":"2023-01-03T07:01:27.828469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(number)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:27.833024Z","iopub.execute_input":"2023-01-03T07:01:27.833601Z","iopub.status.idle":"2023-01-03T07:01:28.124206Z","shell.execute_reply.started":"2023-01-03T07:01:27.833542Z","shell.execute_reply":"2023-01-03T07:01:28.122095Z"},"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-01-03T07:01:28.126097Z","iopub.execute_input":"2023-01-03T07:01:28.126636Z","iopub.status.idle":"2023-01-03T07:01:28.134716Z","shell.execute_reply.started":"2023-01-03T07:01:28.126597Z","shell.execute_reply":"2023-01-03T07:01:28.133042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(number4)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:28.141459Z","iopub.execute_input":"2023-01-03T07:01:28.142026Z","iopub.status.idle":"2023-01-03T07:01:28.391908Z","shell.execute_reply.started":"2023-01-03T07:01:28.141975Z","shell.execute_reply":"2023-01-03T07:01:28.390286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = train.iloc[3,1:]","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:28.393876Z","iopub.execute_input":"2023-01-03T07:01:28.394386Z","iopub.status.idle":"2023-01-03T07:01:28.402935Z","shell.execute_reply.started":"2023-01-03T07:01:28.394343Z","shell.execute_reply":"2023-01-03T07:01:28.400800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(arr)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:28.406300Z","iopub.execute_input":"2023-01-03T07:01:28.407143Z","iopub.status.idle":"2023-01-03T07:01:28.671129Z","shell.execute_reply.started":"2023-01-03T07:01:28.407084Z","shell.execute_reply":"2023-01-03T07:01:28.669749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(arr)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:28.673079Z","iopub.execute_input":"2023-01-03T07:01:28.674590Z","iopub.status.idle":"2023-01-03T07:01:28.683706Z","shell.execute_reply.started":"2023-01-03T07:01:28.674523Z","shell.execute_reply":"2023-01-03T07:01:28.681808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min(arr)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:28.685221Z","iopub.execute_input":"2023-01-03T07:01:28.685632Z","iopub.status.idle":"2023-01-03T07:01:28.704158Z","shell.execute_reply.started":"2023-01-03T07:01:28.685594Z","shell.execute_reply":"2023-01-03T07:01:28.702440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.mean(arr)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:28.708834Z","iopub.execute_input":"2023-01-03T07:01:28.709442Z","iopub.status.idle":"2023-01-03T07:01:28.723407Z","shell.execute_reply.started":"2023-01-03T07:01:28.709393Z","shell.execute_reply":"2023-01-03T07:01:28.721433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Missing Values\n","metadata":{}},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:28.725278Z","iopub.execute_input":"2023-01-03T07:01:28.725907Z","iopub.status.idle":"2023-01-03T07:01:28.794072Z","shell.execute_reply.started":"2023-01-03T07:01:28.725848Z","shell.execute_reply":"2023-01-03T07:01:28.792482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(train)\ny = np.array(test)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:28.795829Z","iopub.execute_input":"2023-01-03T07:01:28.796274Z","iopub.status.idle":"2023-01-03T07:01:29.098775Z","shell.execute_reply.started":"2023-01-03T07:01:28.796234Z","shell.execute_reply":"2023-01-03T07:01:29.096997Z"},"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-01-03T07:01:29.101770Z","iopub.execute_input":"2023-01-03T07:01:29.102606Z","iopub.status.idle":"2023-01-03T07:01:29.111373Z","shell.execute_reply.started":"2023-01-03T07:01:29.102539Z","shell.execute_reply":"2023-01-03T07:01:29.109296Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:29.113717Z","iopub.execute_input":"2023-01-03T07:01:29.114308Z","iopub.status.idle":"2023-01-03T07:01:37.458067Z","shell.execute_reply.started":"2023-01-03T07:01:29.114242Z","shell.execute_reply":"2023-01-03T07:01:37.455828Z"},"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-01-03T07:01:37.460137Z","iopub.execute_input":"2023-01-03T07:01:37.461499Z","iopub.status.idle":"2023-01-03T07:01:37.649783Z","shell.execute_reply.started":"2023-01-03T07:01:37.461437Z","shell.execute_reply":"2023-01-03T07:01:37.648340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1,784)\nX_train","metadata":{"execution":{"iopub.status.busy":"2023-01-03T07:01:37.651597Z","iopub.execute_input":"2023-01-03T07:01:37.651996Z","iopub.status.idle":"2023-01-03T07:01:37.662940Z","shell.execute_reply.started":"2023-01-03T07:01:37.651960Z","shell.execute_reply":"2023-01-03T07:01:37.661157Z"},"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-01-03T07:01:37.664581Z","iopub.execute_input":"2023-01-03T07:01:37.664981Z","iopub.status.idle":"2023-01-03T07:01:38.224133Z","shell.execute_reply.started":"2023-01-03T07:01:37.664950Z","shell.execute_reply":"2023-01-03T07:01:38.222680Z"},"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-01-03T07:01:38.226417Z","iopub.execute_input":"2023-01-03T07:01:38.227052Z","iopub.status.idle":"2023-01-03T07:01:38.248224Z","shell.execute_reply.started":"2023-01-03T07:01:38.227000Z","shell.execute_reply":"2023-01-03T07:01:38.246543Z"},"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-01-03T07:33:05.833356Z","iopub.execute_input":"2023-01-03T07:33:05.833774Z","iopub.status.idle":"2023-01-03T07:33:06.068054Z","shell.execute_reply.started":"2023-01-03T07:33:05.833742Z","shell.execute_reply":"2023-01-03T07:33:06.065948Z"},"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-01-03T07:33:06.996725Z","iopub.execute_input":"2023-01-03T07:33:06.997219Z","iopub.status.idle":"2023-01-03T07:33:07.250750Z","shell.execute_reply.started":"2023-01-03T07:33:06.997141Z","shell.execute_reply":"2023-01-03T07:33:07.248453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}