{"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":"2021-08-11T13:25:29.456537Z","iopub.execute_input":"2021-08-11T13:25:29.457027Z","iopub.status.idle":"2021-08-11T13:25:29.472362Z","shell.execute_reply.started":"2021-08-11T13:25:29.456922Z","shell.execute_reply":"2021-08-11T13:25:29.471201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-08-11T13:25:29.488796Z","iopub.execute_input":"2021-08-11T13:25:29.489356Z","iopub.status.idle":"2021-08-11T13:25:33.500441Z","shell.execute_reply.started":"2021-08-11T13:25:29.48932Z","shell.execute_reply":"2021-08-11T13:25:33.49936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T13:25:33.503018Z","iopub.execute_input":"2021-08-11T13:25:33.503448Z","iopub.status.idle":"2021-08-11T13:25:33.537989Z","shell.execute_reply.started":"2021-08-11T13:25:33.503402Z","shell.execute_reply":"2021-08-11T13:25:33.536491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T13:25:33.539574Z","iopub.execute_input":"2021-08-11T13:25:33.540028Z","iopub.status.idle":"2021-08-11T13:25:33.592786Z","shell.execute_reply.started":"2021-08-11T13:25:33.539998Z","shell.execute_reply":"2021-08-11T13:25:33.591694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib as mpl\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-08-11T13:25:33.594592Z","iopub.execute_input":"2021-08-11T13:25:33.594893Z","iopub.status.idle":"2021-08-11T13:25:33.599101Z","shell.execute_reply.started":"2021-08-11T13:25:33.594866Z","shell.execute_reply":"2021-08-11T13:25:33.598417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.drop(labels=['label'], axis=1)\ny_train = train['label']","metadata":{"execution":{"iopub.status.busy":"2021-08-11T13:25:33.600385Z","iopub.execute_input":"2021-08-11T13:25:33.600769Z","iopub.status.idle":"2021-08-11T13:25:33.711256Z","shell.execute_reply.started":"2021-08-11T13:25:33.60074Z","shell.execute_reply":"2021-08-11T13:25:33.71017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"some_digit = X_train.iloc[0]\nsome_digit_image = some_digit.values.reshape(28,28)\nplt.imshow(some_digit_image, cmap=\"binary\")\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T13:25:33.71245Z","iopub.execute_input":"2021-08-11T13:25:33.712745Z","iopub.status.idle":"2021-08-11T13:25:33.798848Z","shell.execute_reply.started":"2021-08-11T13:25:33.712718Z","shell.execute_reply":"2021-08-11T13:25:33.797794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train[0])","metadata":{"execution":{"iopub.status.busy":"2021-08-11T13:25:33.800237Z","iopub.execute_input":"2021-08-11T13:25:33.800619Z","iopub.status.idle":"2021-08-11T13:25:33.805858Z","shell.execute_reply.started":"2021-08-11T13:25:33.80058Z","shell.execute_reply":"2021-08-11T13:25:33.80461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nmodel = KNeighborsClassifier(n_neighbors=10)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T13:25:33.808604Z","iopub.execute_input":"2021-08-11T13:25:33.809071Z","iopub.status.idle":"2021-08-11T13:25:34.880337Z","shell.execute_reply.started":"2021-08-11T13:25:33.80903Z","shell.execute_reply":"2021-08-11T13:25:34.879183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.model_selection import cross_val_score\n#cross_val_score(model, X_train, y_train, cv=3, scoring=\"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2021-08-11T18:14:55.925337Z","iopub.execute_input":"2021-08-11T18:14:55.925758Z","iopub.status.idle":"2021-08-11T18:14:55.929911Z","shell.execute_reply.started":"2021-08-11T18:14:55.925729Z","shell.execute_reply":"2021-08-11T18:14:55.928882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T18:35:41.833665Z","iopub.execute_input":"2021-08-11T18:35:41.834191Z","iopub.status.idle":"2021-08-11T18:35:52.117424Z","shell.execute_reply.started":"2021-08-11T18:35:41.834158Z","shell.execute_reply":"2021-08-11T18:35:52.116315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"creating a submission file","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-08-11T18:31:39.001982Z","iopub.execute_input":"2021-08-11T18:31:39.002451Z","iopub.status.idle":"2021-08-11T18:31:40.975262Z","shell.execute_reply.started":"2021-08-11T18:31:39.002405Z","shell.execute_reply":"2021-08-11T18:31:40.97414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T18:31:47.124106Z","iopub.execute_input":"2021-08-11T18:31:47.124455Z","iopub.status.idle":"2021-08-11T18:31:47.14345Z","shell.execute_reply.started":"2021-08-11T18:31:47.124427Z","shell.execute_reply":"2021-08-11T18:31:47.142294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T18:38:00.747608Z","iopub.execute_input":"2021-08-11T18:38:00.748002Z","iopub.status.idle":"2021-08-11T19:12:08.92643Z","shell.execute_reply.started":"2021-08-11T18:38:00.74797Z","shell.execute_reply":"2021-08-11T19:12:08.925467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[:5]","metadata":{"execution":{"iopub.status.busy":"2021-08-11T19:16:00.167376Z","iopub.execute_input":"2021-08-11T19:16:00.167901Z","iopub.status.idle":"2021-08-11T19:16:00.173219Z","shell.execute_reply.started":"2021-08-11T19:16:00.167871Z","shell.execute_reply":"2021-08-11T19:16:00.172599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predictions)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T19:36:00.122484Z","iopub.execute_input":"2021-08-11T19:36:00.123134Z","iopub.status.idle":"2021-08-11T19:36:00.128951Z","shell.execute_reply.started":"2021-08-11T19:36:00.123095Z","shell.execute_reply":"2021-08-11T19:36:00.127906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': np.arange(1, len(predictions) + 1), 'Label': predictions})\noutput.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T19:36:46.999588Z","iopub.execute_input":"2021-08-11T19:36:46.99996Z","iopub.status.idle":"2021-08-11T19:36:47.010733Z","shell.execute_reply.started":"2021-08-11T19:36:46.999928Z","shell.execute_reply":"2021-08-11T19:36:47.009998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2021-08-11T19:37:01.939112Z","iopub.execute_input":"2021-08-11T19:37:01.939597Z","iopub.status.idle":"2021-08-11T19:37:01.983429Z","shell.execute_reply.started":"2021-08-11T19:37:01.939564Z","shell.execute_reply":"2021-08-11T19:37:01.982277Z"},"trusted":true},"execution_count":null,"outputs":[]}]}