{"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-12-08T21:06:52.447351Z","iopub.execute_input":"2021-12-08T21:06:52.447750Z","iopub.status.idle":"2021-12-08T21:06:52.461133Z","shell.execute_reply.started":"2021-12-08T21:06:52.447697Z","shell.execute_reply":"2021-12-08T21:06:52.460220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    import google.colab\n    IN_COLAB = True\nexcept:\n    IN_COLAB = False \nimport matplotlib.pyplot as plt\nfrom matplotlib import gridspec\nimport seaborn as sns\nimport pandas as pd\nfrom tqdm.notebook import tqdm\n\nfrom scipy.special import softmax\nfrom scipy.spatial.distance import cdist\nimport numpy as np\nimport torch\n\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.svm import SVC, SVR\nfrom sklearn.metrics import classification_report\nfrom sklearn.model_selection import KFold, ParameterGrid\nfrom sklearn.datasets import make_classification, make_regression\nfrom sklearn.model_selection import train_test_split\n\nfrom mlxtend.plotting import plot_decision_regions \nfrom sklearn.decomposition import PCA \nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.datasets import make_classification \n\nfrom catboost import CatBoostRegressor \n","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:06:52.463210Z","iopub.execute_input":"2021-12-08T21:06:52.463583Z","iopub.status.idle":"2021-12-08T21:06:52.473157Z","shell.execute_reply.started":"2021-12-08T21:06:52.463545Z","shell.execute_reply":"2021-12-08T21:06:52.472479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Читаем данные","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/fall-ml2-mipt-2021/train.csv', index_col=0)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:06:52.474130Z","iopub.execute_input":"2021-12-08T21:06:52.474934Z","iopub.status.idle":"2021-12-08T21:06:56.672846Z","shell.execute_reply.started":"2021-12-08T21:06:52.474886Z","shell.execute_reply":"2021-12-08T21:06:56.671999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:06:56.674304Z","iopub.execute_input":"2021-12-08T21:06:56.674643Z","iopub.status.idle":"2021-12-08T21:06:56.706719Z","shell.execute_reply.started":"2021-12-08T21:06:56.674601Z","shell.execute_reply":"2021-12-08T21:06:56.705831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Преобразование данных","metadata":{}},{"cell_type":"code","source":"X = data.loc[:, 'x_1':'x_300'].to_numpy()\nY =  data.loc[:, 'Category'].to_numpy()\nX_train, X_test, Y_train, Y_test = train_test_split(X, Y, \n                                                    test_size=9000, \n                                                    random_state=0) \n\nprint('Размер выборки составляет l={} объектов.'.format(len(data))) \nfor k in np.unique(Y):\n    plt.plot(X[Y==k, 0], X[Y==k, 1], 'o')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:06:56.709910Z","iopub.execute_input":"2021-12-08T21:06:56.710895Z","iopub.status.idle":"2021-12-08T21:06:57.460475Z","shell.execute_reply.started":"2021-12-08T21:06:56.710846Z","shell.execute_reply":"2021-12-08T21:06:57.459435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Обучаем финальную модуль","metadata":{}},{"cell_type":"code","source":"from catboost import CatBoostClassifier      \nmodel_cat_boost = CatBoostClassifier(iterations=1000)\n# Fit model\n_ = model_cat_boost.fit(X_train, Y_train)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:06:57.462057Z","iopub.execute_input":"2021-12-08T21:06:57.462396Z","iopub.status.idle":"2021-12-08T21:08:26.234416Z","shell.execute_reply.started":"2021-12-08T21:06:57.462352Z","shell.execute_reply":"2021-12-08T21:08:26.233461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Тестируем финальную модель","metadata":{}},{"cell_type":"code","source":"print(model_cat_boost.score(X_test, Y_test)) \nprint(model_cat_boost.score(X_train, Y_train))","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:08:26.235805Z","iopub.execute_input":"2021-12-08T21:08:26.236105Z","iopub.status.idle":"2021-12-08T21:08:39.966931Z","shell.execute_reply.started":"2021-12-08T21:08:26.236063Z","shell.execute_reply":"2021-12-08T21:08:39.965958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = model_cat_boost.fit(X, Y)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:08:39.968726Z","iopub.execute_input":"2021-12-08T21:08:39.969105Z","iopub.status.idle":"2021-12-08T21:10:00.958270Z","shell.execute_reply.started":"2021-12-08T21:08:39.969061Z","shell.execute_reply":"2021-12-08T21:10:00.957408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model_cat_boost.score(X_test, Y_test)) \nprint(model_cat_boost.score(X_train, Y_train))","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:10:00.959362Z","iopub.execute_input":"2021-12-08T21:10:00.959815Z","iopub.status.idle":"2021-12-08T21:10:15.093801Z","shell.execute_reply.started":"2021-12-08T21:10:00.959775Z","shell.execute_reply":"2021-12-08T21:10:15.092888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test = pd.read_csv('/kaggle/input/fall-ml2-mipt-2021/test.csv', index_col=0)   \nX_object = data_test.loc[:, 'x_1':'x_300'].to_numpy()\n \npredict = [int(i) for i in model_cat_boost.predict(X_object)]\n\ntest_data_answer = pd.DataFrame(predict, columns=['Category'])","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:10:15.095168Z","iopub.execute_input":"2021-12-08T21:10:15.095397Z","iopub.status.idle":"2021-12-08T21:10:15.595944Z","shell.execute_reply.started":"2021-12-08T21:10:15.095368Z","shell.execute_reply":"2021-12-08T21:10:15.595040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_answer.head()  \nprint(len(test_data_answer  ))","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:10:15.597309Z","iopub.execute_input":"2021-12-08T21:10:15.597633Z","iopub.status.idle":"2021-12-08T21:10:15.602233Z","shell.execute_reply.started":"2021-12-08T21:10:15.597604Z","shell.execute_reply":"2021-12-08T21:10:15.601278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_answer.to_csv('/kaggle/working/predict.csv', index=True, index_label='Id') ","metadata":{"execution":{"iopub.status.busy":"2021-12-08T21:10:15.603445Z","iopub.execute_input":"2021-12-08T21:10:15.603665Z","iopub.status.idle":"2021-12-08T21:10:15.640750Z","shell.execute_reply.started":"2021-12-08T21:10:15.603637Z","shell.execute_reply":"2021-12-08T21:10:15.639966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Отправляем модель в лидерборд","metadata":{}}]}