{"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":"markdown","source":"![jo_wilder](https://content.pbswisconsineducation.org/wp-content/uploads/2021/06/26141330/jw-playthegame-howtoplay-1.jpg)","metadata":{}},{"cell_type":"markdown","source":"# Jo Wilder Educational Game  \n### Aalf solution (240 private place from 2100 teams).","metadata":{}},{"cell_type":"markdown","source":"Special thanks to CHRIS DEOTTE and VADIM KAMAEV for these notebooks:  \n\nhttps://www.kaggle.com/code/cdeotte/game-room-click-eda\n\nhttps://www.kaggle.com/code/vadimkamaev/catboost-new/notebook","metadata":{}},{"cell_type":"code","source":"import pandas as pd, numpy as np\nfrom catboost import CatBoostClassifier\nfrom xgboost import XGBClassifier\nimport pickle\nimport sys","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:18.336938Z","iopub.execute_input":"2023-06-27T10:18:18.337368Z","iopub.status.idle":"2023-06-27T10:18:19.665999Z","shell.execute_reply.started":"2023-06-27T10:18:18.337334Z","shell.execute_reply":"2023-06-27T10:18:19.664813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Train Data and Labels","metadata":{"papermill":{"duration":0.004542,"end_time":"2023-02-07T00:59:59.189777","exception":false,"start_time":"2023-02-07T00:59:59.185235","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"We change the data types to be less memory-intensive, as we have a limitation of 8GB RAM.","metadata":{}},{"cell_type":"code","source":"dtypes = {\"session_id\": 'int64',\n          \"index\": np.int16,\n          \"elapsed_time\": np.int32,\n          \"event_name\": 'category',\n          \"name\": 'category',\n          \"level\": np.int8,\n          \"page\": np.float16,\n          \"room_coor_x\": np.float16,\n          \"room_coor_y\": np.float16,\n          \"screen_coor_x\": np.float16,\n          \"screen_coor_y\": np.float16,\n          \"hover_duration\": np.float32,\n          \"text\": 'category',\n          \"fqid\": 'category',\n          \"room_fqid\": 'category',\n          \"text_fqid\": 'category',\n          \"fullscreen\": np.int8,\n          \"hq\": np.int8,\n          \"music\": np.int8,\n          \"level_group\": 'category'\n          }\nuse_col = ['session_id', 'index', 'elapsed_time', 'event_name', 'name', 'level', 'page',\n           'room_coor_x', 'room_coor_y', 'hover_duration', 'text', 'fqid', 'room_fqid', 'text_fqid', 'level_group']","metadata":{"papermill":{"duration":59.284316,"end_time":"2023-02-07T01:00:58.478743","exception":false,"start_time":"2023-02-07T00:59:59.194427","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-27T10:18:19.668095Z","iopub.execute_input":"2023-06-27T10:18:19.668436Z","iopub.status.idle":"2023-06-27T10:18:19.678130Z","shell.execute_reply.started":"2023-06-27T10:18:19.668406Z","shell.execute_reply":"2023-06-27T10:18:19.677212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reading labels for the target and retrieving preprocessed and prepared features from PyCharm.","metadata":{}},{"cell_type":"code","source":"targets = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train_labels.csv')\ntargets['session'] = targets.session_id.apply(lambda x: int(x.split('_')[0]) )\ntargets['q'] = targets.session_id.apply(lambda x: int(x.split('_')[-1][1:]) )\nprint( targets.shape )\ntargets.head()","metadata":{"papermill":{"duration":0.598155,"end_time":"2023-02-07T01:00:59.082015","exception":false,"start_time":"2023-02-07T01:00:58.48386","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-27T10:18:22.713683Z","iopub.execute_input":"2023-06-27T10:18:22.714638Z","iopub.status.idle":"2023-06-27T10:18:24.489913Z","shell.execute_reply.started":"2023-06-27T10:18:22.714594Z","shell.execute_reply":"2023-06-27T10:18:24.488883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_df = pd.read_csv('/kaggle/input/featur/feature_sort.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:24.493986Z","iopub.execute_input":"2023-06-27T10:18:24.494328Z","iopub.status.idle":"2023-06-27T10:18:24.656129Z","shell.execute_reply.started":"2023-06-27T10:18:24.494297Z","shell.execute_reply":"2023-06-27T10:18:24.655164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Engineer","metadata":{"papermill":{"duration":0.005196,"end_time":"2023-02-07T01:00:59.092865","exception":false,"start_time":"2023-02-07T01:00:59.087669","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"A lot of Vadim's feature engineer","metadata":{}},{"cell_type":"code","source":"def delt_time_def(df):\n    df.sort_values(by=['session_id', 'elapsed_time'], inplace=True)\n    df['d_time'] = df['elapsed_time'].diff(1)\n    df['d_time'].fillna(0, inplace=True)\n    df['delt_time'] = df['d_time'].clip(0, 103000)\n    df['delt_time_next'] = df['delt_time'].shift(-1)\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:25.434864Z","iopub.execute_input":"2023-06-27T10:18:25.437615Z","iopub.status.idle":"2023-06-27T10:18:25.446169Z","shell.execute_reply.started":"2023-06-27T10:18:25.437567Z","shell.execute_reply":"2023-06-27T10:18:25.445110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_engineer(train, kol_f):\n    global kol_col, kol_col_max\n    kol_col = 9\n    kol_col_max = 11+kol_f*2\n    col = [i for i in range(0,kol_col_max)]\n    new_train = pd.DataFrame(index=train['session_id'].unique(), columns=col, dtype=np.float16)  \n    new_train[10] = new_train.index # \"session_id\"    \n\n    new_train[0] = train.groupby(['session_id'])['d_time'].quantile(q=0.3)\n    new_train[1] = train.groupby(['session_id'])['d_time'].quantile(q=0.8)\n    new_train[2] = train.groupby(['session_id'])['d_time'].quantile(q=0.5)\n    new_train[3] = train.groupby(['session_id'])['d_time'].quantile(q=0.65)\n    new_train[4] = train.groupby(['session_id'])['hover_duration'].agg('mean')\n    new_train[5] = train.groupby(['session_id'])['hover_duration'].agg('std')    \n    new_train[6] = new_train[10].apply(lambda x: int(str(x)[:2])).astype(np.uint8) # \"year\"\n    new_train[7] = new_train[10].apply(lambda x: int(str(x)[2:4])+1).astype(np.uint8) # \"month\"\n    new_train[8] = new_train[10].apply(lambda x: int(str(x)[4:6])).astype(np.uint8) # \"day\"\n    new_train[9] = new_train[10].apply(lambda x: int(str(x)[6:8])).astype(np.uint8) + new_train[10].apply(lambda x: int(str(x)[8:10])).astype(np.uint8)/60\n    new_train[10] = 0\n    new_train = new_train.fillna(-1)\n    \n    return new_train","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:25.756241Z","iopub.execute_input":"2023-06-27T10:18:25.757003Z","iopub.status.idle":"2023-06-27T10:18:25.772619Z","shell.execute_reply.started":"2023-06-27T10:18:25.756963Z","shell.execute_reply":"2023-06-27T10:18:25.771514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_next_t(row_f, new_train, train, gran_1, gran_2, i):\n    global kol_col\n    kol_col +=1\n    col1 = row_f['col1']\n    val1 = row_f['val1']\n    maska = (train[col1] == val1)\n    if row_f['kol_col'] == 1:       \n        new_train[kol_col] = train[maska].groupby(['session_id'])['delt_time_next'].sum()\n        if gran_1:\n            kol_col +=1\n            new_train[kol_col] = train[maska].groupby(['session_id'])['delt_time'].mean()\n        if gran_2:\n            kol_col +=1\n            new_train[kol_col] = train[maska].groupby(['session_id'])['index'].count()          \n    elif row_f['kol_col'] == 2: \n        col2 = row_f['col2']\n        val2 = row_f['val2']\n        maska = maska & (train[col2] == val2)        \n        new_train[kol_col] = train[maska].groupby(['session_id'])['delt_time_next'].sum()\n        if gran_1:\n            kol_col +=1\n            new_train[kol_col] = train[maska].groupby(['session_id'])['delt_time'].mean()\n        if gran_2:\n            kol_col +=1\n            new_train[kol_col] = train[maska].groupby(['session_id'])['index'].count()\n    return new_train","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:26.013335Z","iopub.execute_input":"2023-06-27T10:18:26.015952Z","iopub.status.idle":"2023-06-27T10:18:26.029860Z","shell.execute_reply.started":"2023-06-27T10:18:26.015906Z","shell.execute_reply":"2023-06-27T10:18:26.028765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_next_t_otvet(row_f, new_train, train, gran_1, gran_2, i):\n    global kol_col\n    kol_col +=1\n    col1 = row_f['col1']\n    val1 = row_f['val1']\n    maska = (train[col1] == val1)\n    if row_f['kol_col'] == 1:      \n        new_train[kol_col] = train[maska]['delt_time_next'].sum()\n        if gran_1:\n            kol_col +=1\n            new_train[kol_col] = train[maska]['delt_time'].mean()\n        if gran_2:\n            kol_col +=1\n            new_train[kol_col] = train[maska]['index'].count()          \n    elif row_f['kol_col'] == 2: \n        col2 = row_f['col2']\n        val2 = row_f['val2']\n        maska = maska & (train[col2] == val2)        \n        new_train[kol_col] = train[maska]['delt_time_next'].sum()\n        if gran_1:\n            kol_col +=1\n            new_train[kol_col] = train[maska]['delt_time'].mean()\n        if gran_2:\n            kol_col +=1\n            new_train[kol_col] = train[maska]['index'].count()\n    return new_train","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:26.622193Z","iopub.execute_input":"2023-06-27T10:18:26.624819Z","iopub.status.idle":"2023-06-27T10:18:26.637774Z","shell.execute_reply.started":"2023-06-27T10:18:26.624774Z","shell.execute_reply":"2023-06-27T10:18:26.636657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def experiment_feature_next_t_otvet(row_f, new_train, train, gran_1, gran_2, i):\n    global kol_col\n    kol_col +=1\n    if row_f['kol_col'] == 1: \n        maska = train[row_f['col1']] == row_f['val1']\n        new_train[kol_col] = train[maska]['delt_time_next'].sum()\n        if gran_1:\n            kol_col +=1\n            new_train[kol_col] = train[maska]['delt_time'].mean()\n        if gran_2:\n            kol_col +=1\n            new_train[kol_col] = train[maska]['index'].count()          \n    elif row_f['kol_col'] == 2: \n        col2 = row_f['col2']\n        val2 = row_f['val2']\n        maska = (train[col1] == val1) & (train[col2] == val2)        \n        new_train[kol_col] = train[maska]['delt_time_next'].sum()\n        if gran_1:\n            kol_col +=1\n            new_train[kol_col] = train[maska]['delt_time'].mean()\n        if gran_2:\n            kol_col +=1\n            new_train[kol_col] = train[maska]['index'].count()\n    return new_train","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:26.832295Z","iopub.execute_input":"2023-06-27T10:18:26.834801Z","iopub.status.idle":"2023-06-27T10:18:26.847351Z","shell.execute_reply.started":"2023-06-27T10:18:26.834758Z","shell.execute_reply":"2023-06-27T10:18:26.846369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_quest_otvet(new_train, train, quest, kol_f):\n    global kol_col\n    kol_col = 9\n    g1 = 0.7 \n    g2 = 0.3 \n\n    feature_q = feature_df[feature_df['quest'] == quest].copy()\n    feature_q.reset_index(drop=True, inplace=True)\n    \n    gran1 = round(kol_f * g1)\n    gran2 = round(kol_f * g2)    \n    for i in range(0, kol_f):         \n        row_f = feature_q.loc[i]\n        new_train = feature_next_t_otvet(row_f, new_train, train, i < gran1, i <  gran2, i) \n    col = [i for i in range(0,kol_col+1)]\n    return new_train[col]","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:27.209968Z","iopub.execute_input":"2023-06-27T10:18:27.212727Z","iopub.status.idle":"2023-06-27T10:18:27.223400Z","shell.execute_reply.started":"2023-06-27T10:18:27.212680Z","shell.execute_reply":"2023-06-27T10:18:27.222538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_engineer_new(new_train, train, feature_q, kol_f):\n    g1 = 0.7 \n    g2 = 0.3 \n    gran1 = round(kol_f * g1)\n    gran2 = round(kol_f * g2)    \n    for i in range(0, kol_f): \n        row_f = feature_q.loc[i]       \n        new_train = feature_next_t(row_f, new_train, train, i < gran1, i <  gran2, i)         \n    return new_train","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:27.768282Z","iopub.execute_input":"2023-06-27T10:18:27.769023Z","iopub.status.idle":"2023-06-27T10:18:27.779390Z","shell.execute_reply.started":"2023-06-27T10:18:27.768986Z","shell.execute_reply":"2023-06-27T10:18:27.778248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_quest(new_train, train, quest, kol_f):\n    global kol_col\n    kol_col = 9\n    feature_q = feature_df[feature_df['quest'] == quest].copy()\n    feature_q.reset_index(drop=True, inplace=True)\n    new_train = feature_engineer_new(new_train, train, feature_q, kol_f)\n    col = [i for i in range(0,kol_col+1)]\n    return new_train[col]","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:28.687066Z","iopub.execute_input":"2023-06-27T10:18:28.687451Z","iopub.status.idle":"2023-06-27T10:18:28.696467Z","shell.execute_reply.started":"2023-06-27T10:18:28.687419Z","shell.execute_reply":"2023-06-27T10:18:28.695131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Models","metadata":{}},{"cell_type":"markdown","source":"The optimal models for achieving a public score of 0.701 and the public bronze medal were CatBoost with 318 trees and a depth of 6, and XGBoost with 313 trees and a depth of 6.  \n\nWe trained CatBoost in another notebook, so we will load its weights later. However, we will train XGBoost directly here.","metadata":{}},{"cell_type":"code","source":"def create_model(old_train, quests, models, list_kol_f):\n\n    kol_quest = len(quests)\n    \n    # Iterate through questions\n    for q in quests:\n        print('### quest ', q, end='')\n        new_train = feature_engineer(old_train, list_kol_f[q])\n        train_x = feature_quest(new_train, old_train, q, list_kol_f[q])\n        print(' ---- ', 'train_q.shape = ', train_x.shape)\n\n        # TRAIN DATA\n        train_users = train_x.index.values\n        train_y = targets.loc[targets.q == q].set_index('session').loc[train_users]\n    \n     \n        # XGBoost model\n        xgb_model = XGBClassifier(       \n            learning_rate = 0.045,\n            max_depth = 6,\n            n_estimators = 313        \n        )\n        \n        xgb_model.fit(train_x.astype('float32'), train_y['correct'])\n        models[f'{q}_xgboost'] = xgb_model\n\n    print('***')\n\n    return models","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:29.846297Z","iopub.execute_input":"2023-06-27T10:18:29.846827Z","iopub.status.idle":"2023-06-27T10:18:29.861999Z","shell.execute_reply.started":"2023-06-27T10:18:29.846786Z","shell.execute_reply":"2023-06-27T10:18:29.860904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I see that in the public notebooks, the best threshold was found to be 0.63.  \n\nI experimented with different thresholds and found that 0.623 is actually more optimal and provides better predictions on the public test set.","metadata":{}},{"cell_type":"code","source":"models = {}\nbest_threshold = 0.623","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:31.092396Z","iopub.execute_input":"2023-06-27T10:18:31.092863Z","iopub.status.idle":"2023-06-27T10:18:31.097566Z","shell.execute_reply.started":"2023-06-27T10:18:31.092830Z","shell.execute_reply":"2023-06-27T10:18:31.096665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_kol_f = {\n    1:140,3:110,\n    4:120, 5:220, 6:130, 7:110, 8:110, 9:100, 10:140, 11:120,\n    14: 160, 15:160, 16:130, 17:140             \n             }","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:31.308635Z","iopub.execute_input":"2023-06-27T10:18:31.309154Z","iopub.status.idle":"2023-06-27T10:18:31.321847Z","shell.execute_reply.started":"2023-06-27T10:18:31.309108Z","shell.execute_reply":"2023-06-27T10:18:31.320685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## XGboost training","metadata":{}},{"cell_type":"code","source":"df0_4 = pd.read_csv('/kaggle/input/featur/train_0_4t.csv', dtype=dtypes) \nkol_lvl = (df0_4 .groupby(['session_id'])['level'].agg('nunique') < 5)\nlist_session = kol_lvl[kol_lvl].index\ndf0_4  = df0_4 [~df0_4 ['session_id'].isin(list_session)]\ndf0_4 = delt_time_def(df0_4)\n\nquests_0_4 = [1, 3] \n\nmodels = create_model(df0_4, quests_0_4, models, list_kol_f)\ndel df0_4","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:18:33.242209Z","iopub.execute_input":"2023-06-27T10:18:33.242619Z","iopub.status.idle":"2023-06-27T10:25:07.389210Z","shell.execute_reply.started":"2023-06-27T10:18:33.242586Z","shell.execute_reply":"2023-06-27T10:25:07.388149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df5_12 = pd.read_csv('/kaggle/input/featur/train_5_12t.csv', dtype=dtypes)\nkol_lvl = (df5_12.groupby(['session_id'])['level'].agg('nunique') < 8)\nlist_session = kol_lvl[kol_lvl].index\ndf5_12 = df5_12[~df5_12['session_id'].isin(list_session)]\ndf5_12 = delt_time_def(df5_12)\nquests_5_12 = [4, 5, 6, 7, 8, 9, 10, 11] \n\nmodels = create_model(df5_12, quests_5_12, models, list_kol_f)\ndel df5_12","metadata":{"execution":{"iopub.status.busy":"2023-06-25T16:48:37.495713Z","iopub.execute_input":"2023-06-25T16:48:37.496503Z","iopub.status.idle":"2023-06-25T17:13:50.689522Z","shell.execute_reply.started":"2023-06-25T16:48:37.496454Z","shell.execute_reply":"2023-06-25T17:13:50.68835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df13_22 = pd.read_csv('/kaggle/input/featur/train_13_22t.csv', dtype=dtypes) \nkol_lvl = (df13_22 .groupby(['session_id'])['level'].agg('nunique') < 10)\nlist_session = kol_lvl[kol_lvl].index\ndf13_22  = df13_22 [~df13_22 ['session_id'].isin(list_session)]\ndf13_22 = delt_time_def(df13_22)\n\nquests_13_22 = [14, 15, 16, 17] \n\nmodels = create_model(df13_22, quests_13_22, models, list_kol_f)\ndel df13_22","metadata":{"execution":{"iopub.status.busy":"2023-06-25T17:13:50.694253Z","iopub.execute_input":"2023-06-25T17:13:50.696707Z","iopub.status.idle":"2023-06-25T17:29:11.329435Z","shell.execute_reply.started":"2023-06-25T17:13:50.696651Z","shell.execute_reply":"2023-06-25T17:29:11.327998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Saving the xgboost models.","metadata":{}},{"cell_type":"code","source":"for q in quests_0_4 + quests_5_12 + quests_13_22:\n    models[str(q)+\"_xgboost\"].save_model(f'xgb_model_{q}.bin')","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:26:50.311990Z","iopub.execute_input":"2023-06-27T10:26:50.312440Z","iopub.status.idle":"2023-06-27T10:26:50.331034Z","shell.execute_reply.started":"2023-06-27T10:26:50.312407Z","shell.execute_reply":"2023-06-27T10:26:50.329456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Loading catboost models from kaggle dataset folder.","metadata":{}},{"cell_type":"code","source":"import joblib\ndir = '/kaggle/input/catboost-318'\nfor q in quests_0_4 + quests_5_12 + quests_13_22:\n    for i in [\"_catboost\",\"_xgboost\"]:\n        if i == \"_catboost\":\n            models[f'{str(q)+i}'] = CatBoostClassifier().load_model(dir+f'/cat_model_{q}.bin')","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:27:38.714916Z","iopub.execute_input":"2023-06-27T10:27:38.715399Z","iopub.status.idle":"2023-06-27T10:27:38.770176Z","shell.execute_reply.started":"2023-06-27T10:27:38.715363Z","shell.execute_reply":"2023-06-27T10:27:38.768860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models","metadata":{"execution":{"iopub.status.busy":"2023-06-27T10:27:41.712476Z","iopub.execute_input":"2023-06-27T10:27:41.712945Z","iopub.status.idle":"2023-06-27T10:27:41.730392Z","shell.execute_reply.started":"2023-06-27T10:27:41.712910Z","shell.execute_reply":"2023-06-27T10:27:41.729409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Infer Test Data","metadata":{}},{"cell_type":"code","source":"import jo_wilder\n\ntry:\n    jo_wilder.make_env.__called__ = False\n    env.__called__ = False\n    type(env)._state = type(type(env)._state).__dict__['INIT']\nexcept:\n    pass\n\nenv = jo_wilder.make_env()\niter_test = env.iter_test()    ","metadata":{"execution":{"iopub.status.busy":"2023-06-25T17:29:11.796225Z","iopub.execute_input":"2023-06-25T17:29:11.796694Z","iopub.status.idle":"2023-06-25T17:29:11.826215Z","shell.execute_reply.started":"2023-06-25T17:29:11.796659Z","shell.execute_reply":"2023-06-25T17:29:11.824809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making predictions","metadata":{}},{"cell_type":"markdown","source":"Here I experimented with different weights for the models. \n\nSince the CatBoost score separately was 0.700, adding 3% weight to XGBoost was the perfect complement, resulting in my highest public leaderboard score of 0.701.  \n\nFor the remaining 2 submissions in the competition, I used Stratified K-fold with 5 folds and a combination of CatBoost, XGBoost, and MLP with a lower score than in this notebook (both on the public and private leaderboards).","metadata":{}},{"cell_type":"code","source":"import time\ng_end4 = 0\ng_end5 = 0\n\nlist_q = {'0-4':quests_0_4, '5-12':quests_5_12, '13-22':quests_13_22}\nfor (test, sam_sub) in iter_test:\n    sam_sub['question'] = [int(label.split('_')[1][1:]) for label in sam_sub['session_id']]    \n    grp = test.level_group.values[0]   \n    sam_sub['correct'] = 1\n    sam_sub.loc[sam_sub.question.isin([5, 8, 10, 13, 15]), 'correct'] = 0  \n    old_train = delt_time_def(test[test.level_group == grp])\n       \n    for q in list_q[grp]:\n        \n        start4 = time.time()\n        new_train = feature_engineer(old_train, list_kol_f[q])\n        new_train = feature_quest_otvet(new_train, old_train, q, list_kol_f[q])\n        \n        end4 = time.time() - start4\n        g_end4 += end4\n        \n        start5 = time.time()        \n        \n        for i in [\"_catboost\",\"_xgboost\"]:\n            clf = models[f'{str(q)+i}']\n            if i == \"_catboost\" :\n                p1 = clf.predict_proba(new_train.astype('float32'))[:,1]\n            elif i == \"_xgboost\":\n                p2 = clf.predict_proba(new_train.astype('float32'))[:,1]         \n\n        p = 0.97*p1 + 0.03*p2\n        \n        end5 = time.time() - start5\n        g_end5 += end5\n             \n        mask = sam_sub.question == q \n        x = int(p[0]>best_threshold)\n        sam_sub.loc[mask,'correct'] = x      \n           \n    sam_sub = sam_sub[['session_id', 'correct']]      \n    env.predict(sam_sub)","metadata":{"execution":{"iopub.status.busy":"2023-06-25T17:29:11.847928Z","iopub.execute_input":"2023-06-25T17:29:11.848338Z","iopub.status.idle":"2023-06-25T17:29:24.926195Z","shell.execute_reply.started":"2023-06-25T17:29:11.848302Z","shell.execute_reply":"2023-06-25T17:29:24.925109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission.csv","metadata":{"papermill":{"duration":0.011427,"end_time":"2023-02-07T01:02:45.502331","exception":false,"start_time":"2023-02-07T01:02:45.490904","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df = pd.read_csv('submission.csv')\nprint( df.shape )\ndf.head(60)","metadata":{"papermill":{"duration":0.027432,"end_time":"2023-02-07T01:02:45.541022","exception":false,"start_time":"2023-02-07T01:02:45.51359","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-25T17:29:24.927704Z","iopub.execute_input":"2023-06-25T17:29:24.928045Z","iopub.status.idle":"2023-06-25T17:29:24.949604Z","shell.execute_reply.started":"2023-06-25T17:29:24.928012Z","shell.execute_reply":"2023-06-25T17:29:24.948532Z"},"trusted":true},"execution_count":null,"outputs":[]}]}