{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-09-18T10:45:58.467750Z","iopub.execute_input":"2025-09-18T10:45:58.468009Z","iopub.status.idle":"2025-09-18T10:45:58.475003Z","shell.execute_reply.started":"2025-09-18T10:45:58.467991Z","shell.execute_reply":"2025-09-18T10:45:58.474488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T10:45:58.476160Z","iopub.execute_input":"2025-09-18T10:45:58.476766Z","iopub.status.idle":"2025-09-18T10:46:00.218891Z","shell.execute_reply.started":"2025-09-18T10:45:58.476743Z","shell.execute_reply":"2025-09-18T10:46:00.218161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()[train.isnull().sum() > 0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T10:46:00.220028Z","iopub.execute_input":"2025-09-18T10:46:00.220254Z","iopub.status.idle":"2025-09-18T10:46:00.289852Z","shell.execute_reply.started":"2025-09-18T10:46:00.220238Z","shell.execute_reply":"2025-09-18T10:46:00.289143Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T10:46:00.290645Z","iopub.execute_input":"2025-09-18T10:46:00.291409Z","iopub.status.idle":"2025-09-18T10:46:00.302184Z","shell.execute_reply.started":"2025-09-18T10:46:00.291384Z","shell.execute_reply":"2025-09-18T10:46:00.301471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import GridSearchCV\ny=train['label']\ntrain=train.drop(columns='label')\nX=train[:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T10:46:00.303964Z","iopub.execute_input":"2025-09-18T10:46:00.304165Z","iopub.status.idle":"2025-09-18T10:46:00.399871Z","shell.execute_reply.started":"2025-09-18T10:46:00.304150Z","shell.execute_reply":"2025-09-18T10:46:00.399099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn=KNeighborsClassifier()\nparam_grid = {\n    'n_neighbors': [3, 5, 7, 9, 11],\n    'weights': ['uniform', 'distance'],\n    'metric': ['euclidean', 'manhattan']\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T10:46:00.400608Z","iopub.execute_input":"2025-09-18T10:46:00.400822Z","iopub.status.idle":"2025-09-18T10:46:00.404953Z","shell.execute_reply.started":"2025-09-18T10:46:00.400805Z","shell.execute_reply":"2025-09-18T10:46:00.404098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"grid = GridSearchCV(knn, param_grid, cv=3, scoring='accuracy', n_jobs=-1)\ngrid.fit(X,y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T10:46:00.405735Z","iopub.execute_input":"2025-09-18T10:46:00.405932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"grid.best_score_","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()[train.isnull().sum() > 0]","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred=grid.predict(test[:])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"count=test.shape[0]\nImageId=list(range(1,count+1))\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.DataFrame({\n    'ImageId':ImageId,\n    'Label': pred\n})\ndf.to_csv('sample_submission.csv',index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}