{"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":"This notebook was copied with edits from VADIM KAMAEV's notebook :\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\nimport lightgbm as lgb\nimport pickle\nimport sys","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:19:52.185036Z","iopub.execute_input":"2023-06-16T23:19:52.185367Z","iopub.status.idle":"2023-06-16T23:19:52.190259Z","shell.execute_reply.started":"2023-06-16T23:19:52.185340Z","shell.execute_reply":"2023-06-16T23:19:52.189475Z"},"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":"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-16T23:19:52.208653Z","iopub.execute_input":"2023-06-16T23:19:52.209234Z","iopub.status.idle":"2023-06-16T23:19:52.215888Z","shell.execute_reply.started":"2023-06-16T23:19:52.209203Z","shell.execute_reply":"2023-06-16T23:19:52.215166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-16T23:19:52.217491Z","iopub.execute_input":"2023-06-16T23:19:52.217993Z","iopub.status.idle":"2023-06-16T23:19:53.041218Z","shell.execute_reply.started":"2023-06-16T23:19:52.217965Z","shell.execute_reply":"2023-06-16T23:19:53.039991Z"},"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-16T23:19:53.042553Z","iopub.execute_input":"2023-06-16T23:19:53.042820Z","iopub.status.idle":"2023-06-16T23:19:53.093143Z","shell.execute_reply.started":"2023-06-16T23:19:53.042795Z","shell.execute_reply":"2023-06-16T23:19:53.091652Z"},"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":"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-16T23:19:53.095645Z","iopub.execute_input":"2023-06-16T23:19:53.095950Z","iopub.status.idle":"2023-06-16T23:19:53.102298Z","shell.execute_reply.started":"2023-06-16T23:19:53.095925Z","shell.execute_reply":"2023-06-16T23:19:53.101103Z"},"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-16T23:19:53.103559Z","iopub.execute_input":"2023-06-16T23:19:53.104727Z","iopub.status.idle":"2023-06-16T23:19:53.121213Z","shell.execute_reply.started":"2023-06-16T23:19:53.104675Z","shell.execute_reply":"2023-06-16T23:19:53.120070Z"},"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-16T23:19:53.122538Z","iopub.execute_input":"2023-06-16T23:19:53.123069Z","iopub.status.idle":"2023-06-16T23:19:53.137438Z","shell.execute_reply.started":"2023-06-16T23:19:53.123035Z","shell.execute_reply":"2023-06-16T23:19:53.136365Z"},"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-16T23:19:53.139074Z","iopub.execute_input":"2023-06-16T23:19:53.139665Z","iopub.status.idle":"2023-06-16T23:19:53.156473Z","shell.execute_reply.started":"2023-06-16T23:19:53.139635Z","shell.execute_reply":"2023-06-16T23:19:53.154643Z"},"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-16T23:19:53.158251Z","iopub.execute_input":"2023-06-16T23:19:53.158673Z","iopub.status.idle":"2023-06-16T23:19:53.175442Z","shell.execute_reply.started":"2023-06-16T23:19:53.158641Z","shell.execute_reply":"2023-06-16T23:19:53.174226Z"},"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-16T23:19:53.176754Z","iopub.execute_input":"2023-06-16T23:19:53.177061Z","iopub.status.idle":"2023-06-16T23:19:53.188435Z","shell.execute_reply.started":"2023-06-16T23:19:53.177031Z","shell.execute_reply":"2023-06-16T23:19:53.187081Z"},"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-16T23:19:53.191872Z","iopub.execute_input":"2023-06-16T23:19:53.192133Z","iopub.status.idle":"2023-06-16T23:19:53.204761Z","shell.execute_reply.started":"2023-06-16T23:19:53.192110Z","shell.execute_reply":"2023-06-16T23:19:53.203465Z"},"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-16T23:19:53.205904Z","iopub.execute_input":"2023-06-16T23:19:53.206155Z","iopub.status.idle":"2023-06-16T23:19:53.217305Z","shell.execute_reply.started":"2023-06-16T23:19:53.206133Z","shell.execute_reply":"2023-06-16T23:19:53.216642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom xgboost import XGBClassifier\n\nfrom sklearn.neural_network import MLPClassifier\n\ndef 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            booster = 'gbtree',\n            learning_rate=0.02,\n            max_depth= 4,\n            n_estimators= 300,\n            subsample = 0.8, \n            colsample_bytree = 0.5,\n            eval_metric = 'logloss',\n            alpha = 8,  \n            objective = 'binary:logistic',\n            seed = 42\n          \n        )\n        xgb_model.fit(train_x.astype('float32'), train_y['correct'])\n        models[f'{q}_xgboost'] = xgb_model\n\n\n    print('***')\n\n    return models","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:19:53.218270Z","iopub.execute_input":"2023-06-16T23:19:53.218664Z","iopub.status.idle":"2023-06-16T23:19:53.233880Z","shell.execute_reply.started":"2023-06-16T23:19:53.218640Z","shell.execute_reply":"2023-06-16T23:19:53.232190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = {}\nbest_threshold = 0.63","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:19:53.235226Z","iopub.execute_input":"2023-06-16T23:19:53.236187Z","iopub.status.idle":"2023-06-16T23:19:53.250660Z","shell.execute_reply.started":"2023-06-16T23:19:53.236151Z","shell.execute_reply":"2023-06-16T23:19:53.249247Z"},"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-16T23:19:53.252445Z","iopub.execute_input":"2023-06-16T23:19:53.252817Z","iopub.status.idle":"2023-06-16T23:19:53.264012Z","shell.execute_reply.started":"2023-06-16T23:19:53.252788Z","shell.execute_reply":"2023-06-16T23:19:53.262893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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# list_kol_f = {1:140,3:110}\n\nmodels = create_model(df0_4, quests_0_4, models, list_kol_f)\ndel df0_4","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:19:53.265698Z","iopub.execute_input":"2023-06-16T23:19:53.266017Z","iopub.status.idle":"2023-06-16T23:21:58.587419Z","shell.execute_reply.started":"2023-06-16T23:19:53.265990Z","shell.execute_reply":"2023-06-16T23:21:58.586787Z"},"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\n# list_kol_f = {4:110, 5:220, 6:120, 7:110, 8:110, 9:100, 10:140, 11:120}\n\nmodels = create_model(df5_12, quests_5_12, models, list_kol_f)\ndel df5_12","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:21:58.588644Z","iopub.execute_input":"2023-06-16T23:21:58.589096Z","iopub.status.idle":"2023-06-16T23:23:09.993680Z","shell.execute_reply.started":"2023-06-16T23:21:58.589071Z","shell.execute_reply":"2023-06-16T23:23:09.992833Z"},"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# list_kol_f = {14: 160, 15:160, 16:105, 17:140}\n\nmodels = create_model(df13_22, quests_13_22, models, list_kol_f)\ndel df13_22","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:23:16.874946Z","iopub.execute_input":"2023-06-16T23:23:16.875318Z","iopub.status.idle":"2023-06-16T23:25:44.897607Z","shell.execute_reply.started":"2023-06-16T23:23:16.875288Z","shell.execute_reply":"2023-06-16T23:25:44.896203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Saving a Model\n# import joblib\n\n# for q in quests_0_4 + quests_5_12 + quests_13_22:\n#     joblib.dump(models[str(q)+\"_mlp\"], f'mlp_model_{q}.joblib')","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:25:58.770598Z","iopub.execute_input":"2023-06-16T23:25:58.770972Z","iopub.status.idle":"2023-06-16T23:25:58.775845Z","shell.execute_reply.started":"2023-06-16T23:25:58.770944Z","shell.execute_reply":"2023-06-16T23:25:58.774854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for q in quests_0_4 + quests_5_12 + quests_13_22:\n#     models[str(q)+\"_catboost\"].save_model(f'cat_model_{q}.bin')","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:25:58.780353Z","iopub.execute_input":"2023-06-16T23:25:58.780914Z","iopub.status.idle":"2023-06-16T23:25:58.791914Z","shell.execute_reply.started":"2023-06-16T23:25:58.780877Z","shell.execute_reply":"2023-06-16T23:25:58.790687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-16T23:25:58.793688Z","iopub.execute_input":"2023-06-16T23:25:58.794269Z","iopub.status.idle":"2023-06-16T23:25:58.806033Z","shell.execute_reply.started":"2023-06-16T23:25:58.794224Z","shell.execute_reply":"2023-06-16T23:25:58.805269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for q in quests_0_4 + quests_5_12 + quests_13_22:\n#     models[str(q)+\"_lgb\"].save_model(f'lgb_model_{q}.bin')","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:25:58.807403Z","iopub.execute_input":"2023-06-16T23:25:58.807949Z","iopub.status.idle":"2023-06-16T23:25:58.820145Z","shell.execute_reply.started":"2023-06-16T23:25:58.807920Z","shell.execute_reply":"2023-06-16T23:25:58.819377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model Reading\nimport joblib\ndir = '/kaggle/input/my-model'\nfor q in quests_0_4 + quests_5_12 + quests_13_22:\n    for i in [\"_catboost\",\"_xgboost\",\"_mlp\"] :\n#             models[f'{str(q)+i}'] =  CatBoostClassifier().load_model(dir+f'cat_model_{q}.bin')\n\n            if i == \"_catboost\" :\n                models[f'{str(q)+i}'] = CatBoostClassifier().load_model(dir+f'/cat_model_{q}.bin')\n#             elif i == \"_xgboost\":\n#                 models[f'{str(q)+i}'] = XGBClassifier().load_model(dir+f'/xgb_model_{q}.bin')            \n            elif i == \"_mlp\" :\n                models[f'{str(q)+i}'] = joblib.load(dir+f'/mlp_model_{q}.joblib')","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:25:58.822057Z","iopub.execute_input":"2023-06-16T23:25:58.822657Z","iopub.status.idle":"2023-06-16T23:26:00.992731Z","shell.execute_reply.started":"2023-06-16T23:25:58.822626Z","shell.execute_reply":"2023-06-16T23:26:00.991115Z"},"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-16T23:26:00.996886Z","iopub.execute_input":"2023-06-16T23:26:00.997210Z","iopub.status.idle":"2023-06-16T23:26:01.037836Z","shell.execute_reply.started":"2023-06-16T23:26:00.997185Z","shell.execute_reply":"2023-06-16T23:26:01.036891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:26:01.041260Z","iopub.execute_input":"2023-06-16T23:26:01.042264Z","iopub.status.idle":"2023-06-16T23:26:01.048458Z","shell.execute_reply.started":"2023-06-16T23:26:01.042210Z","shell.execute_reply":"2023-06-16T23:26:01.047602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_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#         new_train = feature_quest(new_train, old_train, q, kol_f)\n        \n        end4 = time.time() - start4\n        g_end4 += end4\n        \n        start5 = time.time()        \n        \n        for i in [\"_catboost\",\"_xgboost\",\"_mlp\"] :\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            #elif i == \"_lgbm\":\n            #    p3 = clf.predict_proba(new_train.astype('float32'))[:,1] \n            elif i == \"_mlp\" :\n                p4 = clf.predict_proba(new_train.astype('float32').fillna(0))[:,1]\n\n        p = 0.93*p1 + 0.03*p2 + 0.04*p4  # one can experiment with different weights and see if results improve\n        \n        end5 = time.time() - start5\n        g_end5 += end5\n             \n        \n        mask = sam_sub.question == q \n        x = int(p[0]>best_threshold)\n        sam_sub.loc[mask,'correct'] = x      \n        \n        \n    sam_sub = sam_sub[['session_id', 'correct']]      \n    env.predict(sam_sub)","metadata":{"execution":{"iopub.status.busy":"2023-06-16T23:26:01.049756Z","iopub.execute_input":"2023-06-16T23:26:01.054029Z","iopub.status.idle":"2023-06-16T23:26:01.855555Z","shell.execute_reply.started":"2023-06-16T23:26:01.053992Z","shell.execute_reply":"2023-06-16T23:26:01.854136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA 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-16T23:26:01.856701Z","iopub.status.idle":"2023-06-16T23:26:01.857271Z","shell.execute_reply.started":"2023-06-16T23:26:01.857087Z","shell.execute_reply":"2023-06-16T23:26:01.857107Z"},"trusted":true},"execution_count":null,"outputs":[]}]}