{"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-11-27T11:00:47.088085Z","iopub.execute_input":"2021-11-27T11:00:47.088849Z","iopub.status.idle":"2021-11-27T11:00:47.121080Z","shell.execute_reply.started":"2021-11-27T11:00:47.088728Z","shell.execute_reply":"2021-11-27T11:00:47.120414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import classification_report\n\nfrom sklearn.model_selection import cross_val_score, RepeatedStratifiedKFold","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:07:11.860081Z","iopub.execute_input":"2021-11-27T11:07:11.861064Z","iopub.status.idle":"2021-11-27T11:07:11.867488Z","shell.execute_reply.started":"2021-11-27T11:07:11.860985Z","shell.execute_reply":"2021-11-27T11:07:11.866244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Читаем данные","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/fall-ml2-mipt-2021/train.csv', index_col=0)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:03:49.291188Z","iopub.execute_input":"2021-11-27T11:03:49.292127Z","iopub.status.idle":"2021-11-27T11:03:53.824876Z","shell.execute_reply.started":"2021-11-27T11:03:49.292073Z","shell.execute_reply":"2021-11-27T11:03:53.823683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:03:53.827369Z","iopub.execute_input":"2021-11-27T11:03:53.827723Z","iopub.status.idle":"2021-11-27T11:03:53.862903Z","shell.execute_reply.started":"2021-11-27T11:03:53.827678Z","shell.execute_reply":"2021-11-27T11:03:53.861725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Преобразование данных","metadata":{}},{"cell_type":"code","source":"X = train_data.iloc[:, train_data.columns != 'Category'].values\ny = train_data.iloc[:, train_data.columns == 'Category'].values.reshape(-1)\n\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:06:28.580175Z","iopub.execute_input":"2021-11-27T11:06:28.580596Z","iopub.status.idle":"2021-11-27T11:06:28.744023Z","shell.execute_reply.started":"2021-11-27T11:06:28.580556Z","shell.execute_reply":"2021-11-27T11:06:28.742889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LogisticRegression(solver='liblinear', max_iter=10)\n\ncv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=0)\nn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, verbose=1, n_jobs=-1)\nprint(f'SCORE: {np.mean(n_scores)} +- {np.std(n_scores)}')","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:16:28.744330Z","iopub.execute_input":"2021-11-27T11:16:28.744620Z","iopub.status.idle":"2021-11-27T11:17:40.522984Z","shell.execute_reply.started":"2021-11-27T11:16:28.744589Z","shell.execute_reply":"2021-11-27T11:17:40.521753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Обучаем финальную модуль","metadata":{}},{"cell_type":"code","source":"model = LogisticRegression(solver='liblinear', max_iter=10)\n\nmodel.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:19:12.384389Z","iopub.execute_input":"2021-11-27T11:19:12.385159Z","iopub.status.idle":"2021-11-27T11:19:19.677913Z","shell.execute_reply.started":"2021-11-27T11:19:12.385119Z","shell.execute_reply":"2021-11-27T11:19:19.676872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Тестируем финальную модель","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/fall-ml2-mipt-2021/test.csv', index_col=0)\n\nX_test = test_data.iloc[:, test_data.columns != 'Category'].values\n\nX_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:21:15.428044Z","iopub.execute_input":"2021-11-27T11:21:15.428369Z","iopub.status.idle":"2021-11-27T11:21:15.942718Z","shell.execute_reply.started":"2021-11-27T11:21:15.428337Z","shell.execute_reply":"2021-11-27T11:21:15.941669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test)\n\ntest_data_answer = pd.DataFrame(predict, columns=['Category'])","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:22:28.594422Z","iopub.execute_input":"2021-11-27T11:22:28.594972Z","iopub.status.idle":"2021-11-27T11:22:28.610929Z","shell.execute_reply.started":"2021-11-27T11:22:28.594922Z","shell.execute_reply":"2021-11-27T11:22:28.609791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_answer.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:22:29.698669Z","iopub.execute_input":"2021-11-27T11:22:29.698954Z","iopub.status.idle":"2021-11-27T11:22:29.708472Z","shell.execute_reply.started":"2021-11-27T11:22:29.698923Z","shell.execute_reply":"2021-11-27T11:22:29.707644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Отправляем модель в лидерборд","metadata":{}},{"cell_type":"code","source":"test_data_answer.to_csv('/kaggle/working/predict.csv', index=True, index_label='Id')","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:23:53.164380Z","iopub.execute_input":"2021-11-27T11:23:53.165307Z","iopub.status.idle":"2021-11-27T11:23:53.203311Z","shell.execute_reply.started":"2021-11-27T11:23:53.165259Z","shell.execute_reply":"2021-11-27T11:23:53.202507Z"},"trusted":true},"execution_count":null,"outputs":[]}]}