{"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\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import GridSearchCV","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2021-11-06T09:09:04.853347Z","iopub.execute_input":"2021-11-06T09:09:04.854070Z","iopub.status.idle":"2021-11-06T09:09:05.963752Z","shell.execute_reply.started":"2021-11-06T09:09:04.854018Z","shell.execute_reply":"2021-11-06T09:09:05.963032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('../input/digit-recognizer/train.csv') #прочитали тренировачную выборку\ntest_data = pd.read_csv('../input/digit-recognizer/test.csv') #прочитали тестовую выборку\n\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-06T09:09:51.391630Z","iopub.execute_input":"2021-11-06T09:09:51.392276Z","iopub.status.idle":"2021-11-06T09:09:57.203689Z","shell.execute_reply.started":"2021-11-06T09:09:51.392234Z","shell.execute_reply":"2021-11-06T09:09:57.202861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_data['label'] #выделим целевую переменную\nX = train_data.drop('label', axis=1) #отбросим целевую переменную из тренировачной выборки","metadata":{"execution":{"iopub.status.busy":"2021-11-06T09:10:03.750300Z","iopub.execute_input":"2021-11-06T09:10:03.750576Z","iopub.status.idle":"2021-11-06T09:10:03.871118Z","shell.execute_reply.started":"2021-11-06T09:10:03.750541Z","shell.execute_reply":"2021-11-06T09:10:03.870246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#попробуем подобрать оптимальные параметры (количество деревьев в лесу)\nforrest_params = dict(     \n    n_estimators = [50, 150, 200]\n)\nforrest = RandomForestClassifier()","metadata":{"execution":{"iopub.status.busy":"2021-11-06T09:10:08.290735Z","iopub.execute_input":"2021-11-06T09:10:08.291012Z","iopub.status.idle":"2021-11-06T09:10:08.295417Z","shell.execute_reply.started":"2021-11-06T09:10:08.290980Z","shell.execute_reply":"2021-11-06T09:10:08.294653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#запускаем перебор параметров\nforest_cv = GridSearchCV(estimator=forrest, param_grid=forrest_params, cv=5) \nforest_cv.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T09:10:16.304276Z","iopub.execute_input":"2021-11-06T09:10:16.304947Z","iopub.status.idle":"2021-11-06T09:19:17.544493Z","shell.execute_reply.started":"2021-11-06T09:10:16.304906Z","shell.execute_reply":"2021-11-06T09:19:17.543559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Наилучший рузльтат: {}\".format(forest_cv.best_score_))\nprint(\"Оптимальные параметры: {}\".format(forest_cv.best_estimator_))","metadata":{"execution":{"iopub.status.busy":"2021-11-06T09:19:17.545990Z","iopub.execute_input":"2021-11-06T09:19:17.546187Z","iopub.status.idle":"2021-11-06T09:19:17.552580Z","shell.execute_reply.started":"2021-11-06T09:19:17.546163Z","shell.execute_reply":"2021-11-06T09:19:17.551760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#делаем предсказания на лучшей получившейся модели\nforest_pred = forest_cv.predict(test_data)\n#оформляем файл с ответами\nforest_pred_csv = pd.DataFrame({\"ImageId\": range(1,len(forest_pred)+1),\"Label\": forest_pred})\nforest_pred_csv.to_csv('forest_submission.csv', index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T09:20:00.372887Z","iopub.execute_input":"2021-11-06T09:20:00.373370Z","iopub.status.idle":"2021-11-06T09:20:02.707147Z","shell.execute_reply.started":"2021-11-06T09:20:00.373340Z","shell.execute_reply":"2021-11-06T09:20:02.706255Z"},"trusted":true},"execution_count":null,"outputs":[]}]}