{"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":"import pandas as pd\nimport numpy as np\nfrom sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:15:53.900987Z","iopub.execute_input":"2021-12-08T12:15:53.902036Z","iopub.status.idle":"2021-12-08T12:15:55.132228Z","shell.execute_reply.started":"2021-12-08T12:15:53.901904Z","shell.execute_reply":"2021-12-08T12:15:55.131587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Считаем csv файлы в Pandas датафрейм\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:15:59.037918Z","iopub.execute_input":"2021-12-08T12:15:59.038496Z","iopub.status.idle":"2021-12-08T12:16:04.735082Z","shell.execute_reply.started":"2021-12-08T12:15:59.038444Z","shell.execute_reply":"2021-12-08T12:16:04.734250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Посмотрим на тренировочный датафрейм\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:16:08.127617Z","iopub.execute_input":"2021-12-08T12:16:08.127902Z","iopub.status.idle":"2021-12-08T12:16:08.153853Z","shell.execute_reply.started":"2021-12-08T12:16:08.127874Z","shell.execute_reply":"2021-12-08T12:16:08.153196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Узнаем число пропущенных значений \n# и какому признаку принадлежит больше всего пропусков\nsum(train.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:16:11.384916Z","iopub.execute_input":"2021-12-08T12:16:11.385226Z","iopub.status.idle":"2021-12-08T12:16:11.434079Z","shell.execute_reply.started":"2021-12-08T12:16:11.385193Z","shell.execute_reply":"2021-12-08T12:16:11.433107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Посмотрим на тестовый датафрейм\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:16:16.505102Z","iopub.execute_input":"2021-12-08T12:16:16.505400Z","iopub.status.idle":"2021-12-08T12:16:16.525227Z","shell.execute_reply.started":"2021-12-08T12:16:16.505371Z","shell.execute_reply":"2021-12-08T12:16:16.524197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Узнаем число пропущенных значений \n# и какому признаку принадлежит больше всего пропусков\nsum(test.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:16:19.083554Z","iopub.execute_input":"2021-12-08T12:16:19.084285Z","iopub.status.idle":"2021-12-08T12:16:19.120551Z","shell.execute_reply.started":"2021-12-08T12:16:19.084247Z","shell.execute_reply":"2021-12-08T12:16:19.119700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Пропущенных значений нет, потому можно продолжать работу с выборками","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:16:24.412921Z","iopub.execute_input":"2021-12-08T12:16:24.413456Z","iopub.status.idle":"2021-12-08T12:16:24.417192Z","shell.execute_reply.started":"2021-12-08T12:16:24.413417Z","shell.execute_reply":"2021-12-08T12:16:24.416275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Подготовим данные для обучения и теста\nX_train = train.drop(['label'], axis=1)\ny_train = train.label","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:16:27.001166Z","iopub.execute_input":"2021-12-08T12:16:27.001634Z","iopub.status.idle":"2021-12-08T12:16:27.120542Z","shell.execute_reply.started":"2021-12-08T12:16:27.001590Z","shell.execute_reply":"2021-12-08T12:16:27.119698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Обучим модель\nmodel = KNeighborsClassifier(n_neighbors = 25)\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:16:29.614300Z","iopub.execute_input":"2021-12-08T12:16:29.614592Z","iopub.status.idle":"2021-12-08T12:16:38.569387Z","shell.execute_reply.started":"2021-12-08T12:16:29.614563Z","shell.execute_reply":"2021-12-08T12:16:38.568548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Делаем предсказания\npredictions = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T12:16:41.993938Z","iopub.execute_input":"2021-12-08T12:16:41.994242Z","iopub.status.idle":"2021-12-08T12:50:32.870431Z","shell.execute_reply.started":"2021-12-08T12:16:41.994207Z","shell.execute_reply":"2021-12-08T12:50:32.869526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Выведем результат\nresult = pd.DataFrame({\n    \"ImageId\": range(1,len(predictions)+1),\n    \"Label\": predictions\n})\nresult.to_csv('digit_n1.csv', index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:02:59.857755Z","iopub.execute_input":"2021-12-08T13:02:59.858087Z","iopub.status.idle":"2021-12-08T13:02:59.919676Z","shell.execute_reply.started":"2021-12-08T13:02:59.858056Z","shell.execute_reply":"2021-12-08T13:02:59.918809Z"},"trusted":true},"execution_count":null,"outputs":[]}]}