{"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-04-02T15:29:48.078645Z","iopub.execute_input":"2023-04-02T15:29:48.079147Z","iopub.status.idle":"2023-04-02T15:29:48.092256Z","shell.execute_reply.started":"2023-04-02T15:29:48.079106Z","shell.execute_reply":"2023-04-02T15:29:48.089777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2023-04-02T15:29:48.095487Z","iopub.execute_input":"2023-04-02T15:29:48.096354Z","iopub.status.idle":"2023-04-02T15:29:48.102676Z","shell.execute_reply.started":"2023-04-02T15:29:48.096304Z","shell.execute_reply":"2023-04-02T15:29:48.101333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-04-02T15:29:48.104564Z","iopub.execute_input":"2023-04-02T15:29:48.105448Z","iopub.status.idle":"2023-04-02T15:29:54.538654Z","shell.execute_reply.started":"2023-04-02T15:29:48.105401Z","shell.execute_reply":"2023-04-02T15:29:54.537715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-02T15:29:54.539894Z","iopub.execute_input":"2023-04-02T15:29:54.541185Z","iopub.status.idle":"2023-04-02T15:29:54.580333Z","shell.execute_reply.started":"2023-04-02T15:29:54.541139Z","shell.execute_reply":"2023-04-02T15:29:54.578836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.drop(\"label\", axis=1)\ny = train_df[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2023-04-02T15:29:54.584437Z","iopub.execute_input":"2023-04-02T15:29:54.584881Z","iopub.status.idle":"2023-04-02T15:29:54.796878Z","shell.execute_reply.started":"2023-04-02T15:29:54.584841Z","shell.execute_reply":"2023-04-02T15:29:54.795251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import GridSearchCV\nmodel = KNeighborsClassifier(weights=\"distance\", n_neighbors=3)\n\n# grid_params = {\n#     'weights': ['uniform', 'distance'],\n#     'n_neighbors': [1, 2, 3, 4, 5],\n# }\n\n# gs = GridSearchCV(model, \n#                   grid_params, \n#                   verbose=3, \n#                   cv=3)\n\nmodel.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2023-04-02T15:29:54.798618Z","iopub.execute_input":"2023-04-02T15:29:54.799079Z","iopub.status.idle":"2023-04-02T15:29:54.840880Z","shell.execute_reply.started":"2023-04-02T15:29:54.799040Z","shell.execute_reply":"2023-04-02T15:29:54.839394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_df)","metadata":{"execution":{"iopub.status.busy":"2023-04-02T15:29:54.842993Z","iopub.execute_input":"2023-04-02T15:29:54.843513Z","iopub.status.idle":"2023-04-02T15:30:46.827061Z","shell.execute_reply.started":"2023-04-02T15:29:54.843471Z","shell.execute_reply":"2023-04-02T15:30:46.825551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"Label\"] = y_pred","metadata":{"execution":{"iopub.status.busy":"2023-04-02T15:30:46.828534Z","iopub.execute_input":"2023-04-02T15:30:46.828864Z","iopub.status.idle":"2023-04-02T15:30:46.837577Z","shell.execute_reply.started":"2023-04-02T15:30:46.828833Z","shell.execute_reply":"2023-04-02T15:30:46.836152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_df[[\"Label\"]]\n\nsubmission_df.to_csv(\"submission.csv\", index_label=\"ImageId\")","metadata":{"execution":{"iopub.status.busy":"2023-04-02T15:30:46.840376Z","iopub.execute_input":"2023-04-02T15:30:46.841502Z","iopub.status.idle":"2023-04-02T15:30:47.024007Z","shell.execute_reply.started":"2023-04-02T15:30:46.841432Z","shell.execute_reply":"2023-04-02T15:30:47.022055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if __name__ == \"__main__\":    \n    copy_test = test_df.drop(\"Label\", axis = 1)\n    random_index = 5\n    digit = copy_test.iloc[[random_index]].values.reshape(28, 28)\n    predicted_digit = model.predict(copy_test.iloc[[random_index]])\n\n    plt.imshow(digit)\n\n    plt.show()\n\n    print(\"Predicted Value:\", predicted_digit[0])","metadata":{"execution":{"iopub.status.busy":"2023-04-02T15:33:59.518559Z","iopub.execute_input":"2023-04-02T15:33:59.519105Z","iopub.status.idle":"2023-04-02T15:34:00.113747Z","shell.execute_reply.started":"2023-04-02T15:33:59.519061Z","shell.execute_reply":"2023-04-02T15:34:00.112043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}