{"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"}],"dockerImageVersionId":30839,"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","trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:07:17.506735Z","iopub.execute_input":"2025-02-06T22:07:17.507034Z","iopub.status.idle":"2025-02-06T22:07:18.540751Z","shell.execute_reply.started":"2025-02-06T22:07:17.507009Z","shell.execute_reply":"2025-02-06T22:07:18.539487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:07:57.131377Z","iopub.execute_input":"2025-02-06T22:07:57.131699Z","iopub.status.idle":"2025-02-06T22:08:02.462620Z","shell.execute_reply.started":"2025-02-06T22:07:57.131674Z","shell.execute_reply":"2025-02-06T22:08:02.461692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:08:34.447555Z","iopub.execute_input":"2025-02-06T22:08:34.447939Z","iopub.status.idle":"2025-02-06T22:08:34.454322Z","shell.execute_reply.started":"2025-02-06T22:08:34.447911Z","shell.execute_reply":"2025-02-06T22:08:34.453404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:08:39.950372Z","iopub.execute_input":"2025-02-06T22:08:39.950816Z","iopub.status.idle":"2025-02-06T22:08:39.956660Z","shell.execute_reply.started":"2025-02-06T22:08:39.950747Z","shell.execute_reply":"2025-02-06T22:08:39.955867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:11:57.316309Z","iopub.execute_input":"2025-02-06T22:11:57.316619Z","iopub.status.idle":"2025-02-06T22:11:57.336586Z","shell.execute_reply.started":"2025-02-06T22:11:57.316595Z","shell.execute_reply":"2025-02-06T22:11:57.335723Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:10:42.279730Z","iopub.execute_input":"2025-02-06T22:10:42.280156Z","iopub.status.idle":"2025-02-06T22:10:42.298318Z","shell.execute_reply.started":"2025-02-06T22:10:42.280123Z","shell.execute_reply":"2025-02-06T22:10:42.296938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:11:35.322838Z","iopub.execute_input":"2025-02-06T22:11:35.323143Z","iopub.status.idle":"2025-02-06T22:11:35.327553Z","shell.execute_reply.started":"2025-02-06T22:11:35.323122Z","shell.execute_reply":"2025-02-06T22:11:35.326315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(train_df.iloc[22775, 1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:12:51.811448Z","iopub.execute_input":"2025-02-06T22:12:51.811806Z","iopub.status.idle":"2025-02-06T22:12:52.082480Z","shell.execute_reply.started":"2025-02-06T22:12:51.811753Z","shell.execute_reply":"2025-02-06T22:12:52.081595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=train_df.iloc[:, 1:]\ny=train_df.iloc[:, 0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:13:42.300738Z","iopub.execute_input":"2025-02-06T22:13:42.301110Z","iopub.status.idle":"2025-02-06T22:13:42.306216Z","shell.execute_reply.started":"2025-02-06T22:13:42.301081Z","shell.execute_reply":"2025-02-06T22:13:42.305096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test=train_test_split(x, y, test_size=0.2, random_state=32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:20:35.827539Z","iopub.execute_input":"2025-02-06T22:20:35.827958Z","iopub.status.idle":"2025-02-06T22:20:36.067977Z","shell.execute_reply.started":"2025-02-06T22:20:35.827929Z","shell.execute_reply":"2025-02-06T22:20:36.067058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:15:55.755402Z","iopub.execute_input":"2025-02-06T22:15:55.755740Z","iopub.status.idle":"2025-02-06T22:15:55.963140Z","shell.execute_reply.started":"2025-02-06T22:15:55.755711Z","shell.execute_reply":"2025-02-06T22:15:55.962292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:16:10.951299Z","iopub.execute_input":"2025-02-06T22:16:10.951637Z","iopub.status.idle":"2025-02-06T22:16:10.956149Z","shell.execute_reply.started":"2025-02-06T22:16:10.951612Z","shell.execute_reply":"2025-02-06T22:16:10.955090Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:20:41.194254Z","iopub.execute_input":"2025-02-06T22:20:41.194599Z","iopub.status.idle":"2025-02-06T22:20:41.319300Z","shell.execute_reply.started":"2025-02-06T22:20:41.194574Z","shell.execute_reply":"2025-02-06T22:20:41.318235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:20:49.631349Z","iopub.execute_input":"2025-02-06T22:20:49.631694Z","iopub.status.idle":"2025-02-06T22:21:01.805794Z","shell.execute_reply.started":"2025-02-06T22:20:49.631667Z","shell.execute_reply":"2025-02-06T22:21:01.804879Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test, y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:21:01.806957Z","iopub.execute_input":"2025-02-06T22:21:01.807222Z","iopub.status.idle":"2025-02-06T22:21:01.814455Z","shell.execute_reply.started":"2025-02-06T22:21:01.807200Z","shell.execute_reply":"2025-02-06T22:21:01.813521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction=knn.predict(test_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:21:07.215457Z","iopub.execute_input":"2025-02-06T22:21:07.215862Z","iopub.status.idle":"2025-02-06T22:21:48.166946Z","shell.execute_reply.started":"2025-02-06T22:21:07.215831Z","shell.execute_reply":"2025-02-06T22:21:48.165559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': np.arange(1, len(prediction) + 1),  \n    'Label': prediction\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:23:35.285112Z","iopub.execute_input":"2025-02-06T22:23:35.285485Z","iopub.status.idle":"2025-02-06T22:23:35.291397Z","shell.execute_reply.started":"2025-02-06T22:23:35.285450Z","shell.execute_reply":"2025-02-06T22:23:35.290116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:23:47.374060Z","iopub.execute_input":"2025-02-06T22:23:47.374420Z","iopub.status.idle":"2025-02-06T22:23:47.382946Z","shell.execute_reply.started":"2025-02-06T22:23:47.374388Z","shell.execute_reply":"2025-02-06T22:23:47.382041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-06T22:24:43.814851Z","iopub.execute_input":"2025-02-06T22:24:43.815320Z","iopub.status.idle":"2025-02-06T22:24:43.847168Z","shell.execute_reply.started":"2025-02-06T22:24:43.815290Z","shell.execute_reply":"2025-02-06T22:24:43.846249Z"}},"outputs":[],"execution_count":null}]}