{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-12-18T11:57:58.301203Z","iopub.execute_input":"2023-12-18T11:57:58.301605Z","iopub.status.idle":"2023-12-18T11:57:58.313581Z","shell.execute_reply.started":"2023-12-18T11:57:58.301572Z","shell.execute_reply":"2023-12-18T11:57:58.312107Z"},"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-12-18T11:57:58.315468Z","iopub.execute_input":"2023-12-18T11:57:58.315867Z","iopub.status.idle":"2023-12-18T11:58:03.853507Z","shell.execute_reply.started":"2023-12-18T11:57:58.315826Z","shell.execute_reply":"2023-12-18T11:58:03.851971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:58:03.855374Z","iopub.execute_input":"2023-12-18T11:58:03.855837Z","iopub.status.idle":"2023-12-18T11:58:03.886181Z","shell.execute_reply.started":"2023-12-18T11:58:03.855794Z","shell.execute_reply":"2023-12-18T11:58:03.885101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:58:03.887726Z","iopub.execute_input":"2023-12-18T11:58:03.888674Z","iopub.status.idle":"2023-12-18T11:58:03.895837Z","shell.execute_reply.started":"2023-12-18T11:58:03.888632Z","shell.execute_reply":"2023-12-18T11:58:03.894533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nnum = np.array(train.iloc[3,1:]).reshape(28,28)\nplt.imshow(num)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:58:03.899179Z","iopub.execute_input":"2023-12-18T11:58:03.899661Z","iopub.status.idle":"2023-12-18T11:58:04.158650Z","shell.execute_reply.started":"2023-12-18T11:58:03.899601Z","shell.execute_reply":"2023-12-18T11:58:04.157526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(train)\ny = np.array(test)\n\nX_train = x[:,1:]\nY_train = x[:,0]\nX_test = y","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:58:04.160000Z","iopub.execute_input":"2023-12-18T11:58:04.160434Z","iopub.status.idle":"2023-12-18T11:58:04.405366Z","shell.execute_reply.started":"2023-12-18T11:58:04.160393Z","shell.execute_reply":"2023-12-18T11:58:04.404299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-18T11:58:04.406938Z","iopub.execute_input":"2023-12-18T11:58:04.407382Z","iopub.status.idle":"2023-12-18T11:58:17.835701Z","shell.execute_reply.started":"2023-12-18T11:58:04.407341Z","shell.execute_reply":"2023-12-18T11:58:17.834530Z"},"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-12-18T11:58:17.836950Z","iopub.execute_input":"2023-12-18T11:58:17.839582Z","iopub.status.idle":"2023-12-18T11:58:18.010885Z","shell.execute_reply.started":"2023-12-18T11:58:17.839535Z","shell.execute_reply":"2023-12-18T11:58:18.009651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1,784)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:58:18.012038Z","iopub.execute_input":"2023-12-18T11:58:18.012353Z","iopub.status.idle":"2023-12-18T11:58:18.017235Z","shell.execute_reply.started":"2023-12-18T11:58:18.012326Z","shell.execute_reply":"2023-12-18T11:58:18.015706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:58:18.019042Z","iopub.execute_input":"2023-12-18T11:58:18.019386Z","iopub.status.idle":"2023-12-18T11:58:18.030998Z","shell.execute_reply.started":"2023-12-18T11:58:18.019356Z","shell.execute_reply":"2023-12-18T11:58:18.029902Z"},"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-12-18T11:58:18.032672Z","iopub.execute_input":"2023-12-18T11:58:18.033022Z","iopub.status.idle":"2023-12-18T11:58:18.177047Z","shell.execute_reply.started":"2023-12-18T11:58:18.032991Z","shell.execute_reply":"2023-12-18T11:58:18.176222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = knn.predict(X_test[105].reshape(1,-1))\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:58:18.177906Z","iopub.execute_input":"2023-12-18T11:58:18.178197Z","iopub.status.idle":"2023-12-18T11:58:18.393384Z","shell.execute_reply.started":"2023-12-18T11:58:18.178173Z","shell.execute_reply":"2023-12-18T11:58:18.392007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num = np.array(test.iloc[105,:]).reshape(28,28)\nplt.imshow(num)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:58:18.395335Z","iopub.execute_input":"2023-12-18T11:58:18.395900Z","iopub.status.idle":"2023-12-18T11:58:18.636824Z","shell.execute_reply.started":"2023-12-18T11:58:18.395832Z","shell.execute_reply":"2023-12-18T11:58:18.635644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = range(1, len(predictions) + 1)\ndf_submission = pd.DataFrame({'ImageId': image_ids, 'Label': predictions})\n\ndf_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:58:32.475181Z","iopub.execute_input":"2023-12-18T11:58:32.475554Z","iopub.status.idle":"2023-12-18T11:58:32.483455Z","shell.execute_reply.started":"2023-12-18T11:58:32.475525Z","shell.execute_reply":"2023-12-18T11:58:32.482331Z"},"trusted":true},"execution_count":null,"outputs":[]}]}