{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":132009,"databundleVersionId":15879574,"isSourceIdPinned":false}],"dockerImageVersionId":31287,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport catboost\nfrom sklearn.metrics import  roc_auc_score\nfrom sklearn.model_selection import StratifiedKFold\nimport gc\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:14:01.924792Z","iopub.execute_input":"2026-03-01T12:14:01.925457Z","iopub.status.idle":"2026-03-01T12:14:04.826809Z","shell.execute_reply.started":"2026-03-01T12:14:01.925423Z","shell.execute_reply":"2026-03-01T12:14:04.826227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_parquet('/kaggle/input/competitions/SBOR_VSOSH_SREZ_2/train.parquet')\ntest = pd.read_parquet('/kaggle/input/competitions/SBOR_VSOSH_SREZ_2/test.parquet')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:14:04.828252Z","iopub.execute_input":"2026-03-01T12:14:04.828656Z","iopub.status.idle":"2026-03-01T12:14:11.146601Z","shell.execute_reply.started":"2026-03-01T12:14:04.828629Z","shell.execute_reply":"2026-03-01T12:14:11.145958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_data = pd.read_csv('/kaggle/input/competitions/SBOR_VSOSH_SREZ_2/target_train.csv')\nsample_submission = pd.read_csv('/kaggle/input/competitions/SBOR_VSOSH_SREZ_2/sample_submission_vsosh.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:14:11.147633Z","iopub.execute_input":"2026-03-01T12:14:11.147862Z","iopub.status.idle":"2026-03-01T12:14:11.344461Z","shell.execute_reply.started":"2026-03-01T12:14:11.147842Z","shell.execute_reply":"2026-03-01T12:14:11.343877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.concat([train, test]).reset_index(drop = True)\nltr = len(train)\n\ndel train, test\ngc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:14:11.345841Z","iopub.execute_input":"2026-03-01T12:14:11.346158Z","iopub.status.idle":"2026-03-01T12:14:15.225182Z","shell.execute_reply.started":"2026-03-01T12:14:11.346135Z","shell.execute_reply":"2026-03-01T12:14:15.224534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = data.sort_values(['id', 'rn']).drop_duplicates('id', keep = 'last')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:14:15.225991Z","iopub.execute_input":"2026-03-01T12:14:15.226194Z","iopub.status.idle":"2026-03-01T12:14:18.309197Z","shell.execute_reply.started":"2026-03-01T12:14:15.226175Z","shell.execute_reply":"2026-03-01T12:14:18.308526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"grp_mean = data.sort_values(['id', 'rn']).groupby('id')[data.columns[2:]].mean()\ntrain_data = train_data.merge(grp_mean.reset_index(), on = 'id', how = 'left', suffixes = ('', '_mean'))\ndel grp_mean\ngc.collect()\n\ngrp_max = data.sort_values(['id', 'rn']).groupby('id')[data.columns[2:]].max()\ntrain_data = train_data.merge(grp_max.reset_index(), on = 'id', how = 'left', suffixes = ('', '_max'))\ndel grp_max\ngc.collect()\n\ngrp_min = data.sort_values(['id', 'rn']).groupby('id')[data.columns[2:]].min()\ntrain_data = train_data.merge(grp_min.reset_index(), on = 'id', how = 'left', suffixes = ('', '_min'))\ndel grp_min\ngc.collect()\n\ngrp_std = data.sort_values(['id', 'rn']).groupby('id')[data.columns[2:]].std()\ntrain_data = train_data.merge(grp_std.reset_index(), on = 'id', how = 'left', suffixes = ('', '_std'))\ndel grp_std\ngc.collect()\n\ngrp_sum = data.sort_values(['id', 'rn']).groupby('id')[data.columns[2:]].sum()\ntrain_data = train_data.merge(grp_sum.reset_index(), on = 'id', how = 'left', suffixes = ('', '_sum'))\ndel grp_sum\ngc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:14:18.310122Z","iopub.execute_input":"2026-03-01T12:14:18.310409Z","iopub.status.idle":"2026-03-01T12:15:14.734623Z","shell.execute_reply.started":"2026-03-01T12:14:18.310384Z","shell.execute_reply":"2026-03-01T12:15:14.733998Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['size'] = train_data['id'].map(data['id'].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:15:14.735487Z","iopub.execute_input":"2026-03-01T12:15:14.735734Z","iopub.status.idle":"2026-03-01T12:15:15.189720Z","shell.execute_reply.started":"2026-03-01T12:15:14.735702Z","shell.execute_reply":"2026-03-01T12:15:15.188894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = train_data.reset_index(drop = True)\ntrain_data['target'] = train_data['id'].map(target_data.set_index('id')['flag'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:15:15.190723Z","iopub.execute_input":"2026-03-01T12:15:15.191042Z","iopub.status.idle":"2026-03-01T12:15:18.121562Z","shell.execute_reply.started":"2026-03-01T12:15:15.191016Z","shell.execute_reply":"2026-03-01T12:15:18.120908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def cat_train(data, target, ltr, split_list, param, v_e = 0, iterations = 1000, cat_features = []):\n    pred = pd.DataFrame()\n    pred_val = np.zeros(ltr)\n    fi = np.zeros(data.shape[1])\n    score = []\n    bst_list = []\n    pred_train = pd.DataFrame()\n    j = 0\n\n    for i , (train_index, test_index) in enumerate(split_list):\n\n        tr = catboost.Pool(data.loc[train_index], target[train_index], cat_features = cat_features)\n        te = catboost.Pool(data.loc[test_index], target[test_index], cat_features = cat_features)\n        bst = catboost.train(tr, param,iterations=iterations,early_stopping_rounds = 400,verbose = 200,\n            eval_set = te)\n\n        bst_list += [bst]\n        fi += bst.feature_importances_\n        gc.collect()\n        del tr, te\n    return bst_list, fi","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:15:18.124195Z","iopub.execute_input":"2026-03-01T12:15:18.124459Z","iopub.status.idle":"2026-03-01T12:15:18.130613Z","shell.execute_reply.started":"2026-03-01T12:15:18.124435Z","shell.execute_reply":"2026-03-01T12:15:18.129899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def standart_split(data, target, n_splits = 5):\n    split_list = []\n    kf = StratifiedKFold(n_splits = n_splits, random_state=322, shuffle = True)\n    for train_index, test_index in kf.split(data, data[target]) :\n        split_list += [(train_index, test_index)]\n    return split_list","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:48:39.001004Z","iopub.execute_input":"2026-03-01T12:48:39.001618Z","iopub.status.idle":"2026-03-01T12:48:39.006225Z","shell.execute_reply.started":"2026-03-01T12:48:39.001586Z","shell.execute_reply":"2026-03-01T12:48:39.005452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"drop_cols = ['id']\ntarget = 'target'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:15:18.150395Z","iopub.execute_input":"2026-03-01T12:15:18.150632Z","iopub.status.idle":"2026-03-01T12:15:18.162370Z","shell.execute_reply.started":"2026-03-01T12:15:18.150611Z","shell.execute_reply":"2026-03-01T12:15:18.161724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_cols = [x for x in train_data.columns if x not in [target] + drop_cols]\nlen(train_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:15:18.163157Z","iopub.execute_input":"2026-03-01T12:15:18.163417Z","iopub.status.idle":"2026-03-01T12:15:18.175497Z","shell.execute_reply.started":"2026-03-01T12:15:18.163383Z","shell.execute_reply":"2026-03-01T12:15:18.174974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"params_cat = {\n    'loss_function' :'Logloss',\n    'task_type' : 'GPU',\n    'max_depth' : 6,\n    'grow_policy' : 'Depthwise',\n    'eval_metric' :'AUC',\n    'learning_rate' : 0.03,\n    'random_state' : 42 ,\n    'random_strength' : .5,\n    'border_count' :  150,\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:37:47.854750Z","iopub.execute_input":"2026-03-01T12:37:47.855314Z","iopub.status.idle":"2026-03-01T12:37:47.859243Z","shell.execute_reply.started":"2026-03-01T12:37:47.855285Z","shell.execute_reply":"2026-03-01T12:37:47.858464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"split_list = standart_split(train_data.loc[:1000000 - 1], target)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:48:50.905695Z","iopub.execute_input":"2026-03-01T12:48:50.906549Z","iopub.status.idle":"2026-03-01T12:48:50.976091Z","shell.execute_reply.started":"2026-03-01T12:48:50.906514Z","shell.execute_reply":"2026-03-01T12:48:50.975232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:48:51.138190Z","iopub.execute_input":"2026-03-01T12:48:51.138657Z","iopub.status.idle":"2026-03-01T12:48:51.223299Z","shell.execute_reply.started":"2026-03-01T12:48:51.138632Z","shell.execute_reply":"2026-03-01T12:48:51.222557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bst_list_catboost, fi = cat_train(train_data[train_cols], train_data[target], len(data), \n                                                    split_list, params_cat, iterations = 5000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:37:50.657321Z","iopub.execute_input":"2026-03-01T12:37:50.657645Z","iopub.status.idle":"2026-03-01T12:45:43.227043Z","shell.execute_reply.started":"2026-03-01T12:37:50.657617Z","shell.execute_reply":"2026-03-01T12:45:43.226274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predict = []\nfor bst in bst_list_catboost:\n    predict += [ bst.predict(train_data.loc[1000000:, train_cols]) ]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:45:43.228346Z","iopub.execute_input":"2026-03-01T12:45:43.228796Z","iopub.status.idle":"2026-03-01T12:46:28.668175Z","shell.execute_reply.started":"2026-03-01T12:45:43.228768Z","shell.execute_reply":"2026-03-01T12:46:28.667476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission['flag'] = np.mean(predict, 0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:46:28.953864Z","iopub.execute_input":"2026-03-01T12:46:28.954722Z","iopub.status.idle":"2026-03-01T12:46:28.962405Z","shell.execute_reply.started":"2026-03-01T12:46:28.954686Z","shell.execute_reply":"2026-03-01T12:46:28.961757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission.to_csv('author_solution.csv', index = None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-01T12:46:28.963300Z","iopub.execute_input":"2026-03-01T12:46:28.963585Z","iopub.status.idle":"2026-03-01T12:46:29.429689Z","shell.execute_reply.started":"2026-03-01T12:46:28.963552Z","shell.execute_reply":"2026-03-01T12:46:29.429094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}