{"metadata":{"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45533,"databundleVersionId":5748852,"sourceType":"competition"},{"sourceId":5675701,"sourceType":"datasetVersion","datasetId":3244175}],"dockerImageVersionId":30458,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.12"},"papermill":{"default_parameters":{},"duration":1072.879958,"end_time":"2023-12-15T15:15:39.274601","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-12-15T14:57:46.394643","version":"2.4.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd, numpy as np\nfrom catboost import CatBoostClassifier\nfrom sklearn.model_selection import GridSearchCV\nimport pickle\nimport sys\nimport xgboost as xgb\n","metadata":{"execution":{"iopub.execute_input":"2023-12-15T14:57:57.513315Z","iopub.status.busy":"2023-12-15T14:57:57.512910Z","iopub.status.idle":"2023-12-15T14:57:59.018608Z","shell.execute_reply":"2023-12-15T14:57:59.017544Z"},"papermill":{"duration":1.5196,"end_time":"2023-12-15T14:57:59.021836","exception":false,"start_time":"2023-12-15T14:57:57.502236","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Train Data and Labels","metadata":{"papermill":{"duration":0.007596,"end_time":"2023-12-15T14:57:59.037636","exception":false,"start_time":"2023-12-15T14:57:59.030040","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":{"execution":{"iopub.execute_input":"2023-12-15T14:57:59.055392Z","iopub.status.busy":"2023-12-15T14:57:59.054747Z","iopub.status.idle":"2023-12-15T14:57:59.062230Z","shell.execute_reply":"2023-12-15T14:57:59.061023Z"},"papermill":{"duration":0.019147,"end_time":"2023-12-15T14:57:59.064609","exception":false,"start_time":"2023-12-15T14:57:59.045462","status":"completed"},"tags":[]},"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:]) )\n# print( targets.shape )\n# targets.head()","metadata":{"execution":{"iopub.execute_input":"2023-12-15T14:57:59.082764Z","iopub.status.busy":"2023-12-15T14:57:59.081984Z","iopub.status.idle":"2023-12-15T14:58:00.292296Z","shell.execute_reply":"2023-12-15T14:58:00.291232Z"},"papermill":{"duration":1.222346,"end_time":"2023-12-15T14:58:00.294862","exception":false,"start_time":"2023-12-15T14:57:59.072516","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_df = pd.read_csv('/kaggle/input/featur/feature_sort.csv')","metadata":{"execution":{"iopub.execute_input":"2023-12-15T14:58:00.314263Z","iopub.status.busy":"2023-12-15T14:58:00.311827Z","iopub.status.idle":"2023-12-15T14:58:00.454151Z","shell.execute_reply":"2023-12-15T14:58:00.453181Z"},"papermill":{"duration":0.154078,"end_time":"2023-12-15T14:58:00.456791","exception":false,"start_time":"2023-12-15T14:58:00.302713","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Engineer","metadata":{"papermill":{"duration":0.007449,"end_time":"2023-12-15T14:58:00.472327","exception":false,"start_time":"2023-12-15T14:58:00.464878","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.execute_input":"2023-12-15T14:58:00.490273Z","iopub.status.busy":"2023-12-15T14:58:00.489225Z","iopub.status.idle":"2023-12-15T14:58:00.495689Z","shell.execute_reply":"2023-12-15T14:58:00.494754Z"},"papermill":{"duration":0.017789,"end_time":"2023-12-15T14:58:00.497923","exception":false,"start_time":"2023-12-15T14:58:00.480134","status":"completed"},"tags":[]},"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.execute_input":"2023-12-15T14:58:00.516774Z","iopub.status.busy":"2023-12-15T14:58:00.516053Z","iopub.status.idle":"2023-12-15T14:58:00.529817Z","shell.execute_reply":"2023-12-15T14:58:00.528774Z"},"papermill":{"duration":0.025622,"end_time":"2023-12-15T14:58:00.532400","exception":false,"start_time":"2023-12-15T14:58:00.506778","status":"completed"},"tags":[]},"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.execute_input":"2023-12-15T14:58:00.551004Z","iopub.status.busy":"2023-12-15T14:58:00.550215Z","iopub.status.idle":"2023-12-15T14:58:00.560251Z","shell.execute_reply":"2023-12-15T14:58:00.559010Z"},"papermill":{"duration":0.022468,"end_time":"2023-12-15T14:58:00.562795","exception":false,"start_time":"2023-12-15T14:58:00.540327","status":"completed"},"tags":[]},"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.execute_input":"2023-12-15T14:58:00.580808Z","iopub.status.busy":"2023-12-15T14:58:00.580013Z","iopub.status.idle":"2023-12-15T14:58:00.588978Z","shell.execute_reply":"2023-12-15T14:58:00.588028Z"},"papermill":{"duration":0.020639,"end_time":"2023-12-15T14:58:00.591333","exception":false,"start_time":"2023-12-15T14:58:00.570694","status":"completed"},"tags":[]},"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.execute_input":"2023-12-15T14:58:00.609558Z","iopub.status.busy":"2023-12-15T14:58:00.608785Z","iopub.status.idle":"2023-12-15T14:58:00.617895Z","shell.execute_reply":"2023-12-15T14:58:00.616790Z"},"papermill":{"duration":0.02097,"end_time":"2023-12-15T14:58:00.620369","exception":false,"start_time":"2023-12-15T14:58:00.599399","status":"completed"},"tags":[]},"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.execute_input":"2023-12-15T14:58:00.638470Z","iopub.status.busy":"2023-12-15T14:58:00.637716Z","iopub.status.idle":"2023-12-15T14:58:00.644810Z","shell.execute_reply":"2023-12-15T14:58:00.643980Z"},"papermill":{"duration":0.018811,"end_time":"2023-12-15T14:58:00.647027","exception":false,"start_time":"2023-12-15T14:58:00.628216","status":"completed"},"tags":[]},"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.execute_input":"2023-12-15T14:58:00.664993Z","iopub.status.busy":"2023-12-15T14:58:00.664200Z","iopub.status.idle":"2023-12-15T14:58:00.670178Z","shell.execute_reply":"2023-12-15T14:58:00.669317Z"},"papermill":{"duration":0.017595,"end_time":"2023-12-15T14:58:00.672396","exception":false,"start_time":"2023-12-15T14:58:00.654801","status":"completed"},"tags":[]},"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.execute_input":"2023-12-15T14:58:00.690119Z","iopub.status.busy":"2023-12-15T14:58:00.689362Z","iopub.status.idle":"2023-12-15T14:58:00.695556Z","shell.execute_reply":"2023-12-15T14:58:00.694620Z"},"papermill":{"duration":0.017698,"end_time":"2023-12-15T14:58:00.697904","exception":false,"start_time":"2023-12-15T14:58:00.680206","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = {}\nbest_threshold = 0.63","metadata":{"execution":{"iopub.execute_input":"2023-12-15T14:58:00.715789Z","iopub.status.busy":"2023-12-15T14:58:00.715023Z","iopub.status.idle":"2023-12-15T14:58:00.719741Z","shell.execute_reply":"2023-12-15T14:58:00.718833Z"},"papermill":{"duration":0.0162,"end_time":"2023-12-15T14:58:00.721874","exception":false,"start_time":"2023-12-15T14:58:00.705674","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_kol_f = {\n    1:140,3:110,\n    4:110, 5:220, 6:120, 7:110, 8:110, 9:100, 10:120, 11:120,\n    14: 110, 15:160, 16:105, 17:140             \n             }","metadata":{"execution":{"iopub.execute_input":"2023-12-15T14:58:00.739455Z","iopub.status.busy":"2023-12-15T14:58:00.738734Z","iopub.status.idle":"2023-12-15T14:58:00.743973Z","shell.execute_reply":"2023-12-15T14:58:00.743057Z"},"papermill":{"duration":0.016531,"end_time":"2023-12-15T14:58:00.746147","exception":false,"start_time":"2023-12-15T14:58:00.729616","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create model with best parameters","metadata":{"papermill":{"duration":0.007466,"end_time":"2023-12-15T14:58:00.761345","exception":false,"start_time":"2023-12-15T14:58:00.753879","status":"completed"},"tags":[]}},{"cell_type":"code","source":"Depth = 7\nlr = 0.05\nIter = 60\n\nbest_models = {'1': {'lambda': 0.46054517645273085,\n  'alpha': 0.309558813625558,\n  'colsample_bytree': 0.9709961001194,\n  'subsample': 0.9052814877282959,\n  'learning_rate': 0.08686131203926269,\n  'n_estimators': 190,\n  'max_depth': 6,\n  'random_state': 43,\n  'min_child_weight': 178},\n '3': {'lambda': 6.2609010302722545,\n  'alpha': 0.008829712142944927,\n  'colsample_bytree': 0.5078281069068661,\n  'subsample': 0.44876173983966705,\n  'learning_rate': 0.034607737521525656,\n  'n_estimators': 78,\n  'max_depth': 6,\n  'random_state': 43,\n  'min_child_weight': 59},\n '4': {'lambda': 0.002196089697117182,\n  'alpha': 0.4703891420410998,\n  'colsample_bytree': 0.40546996430905335,\n  'subsample': 0.5472557114670465,\n  'learning_rate': 0.06645703968198914,\n  'n_estimators': 193,\n  'max_depth': 11,\n  'random_state': 43,\n  'min_child_weight': 117},\n '5': {'lambda': 0.4016076024798144,\n  'alpha': 0.1487324337790175,\n  'colsample_bytree': 0.3205167435878081,\n  'subsample': 0.6884038266869779,\n  'learning_rate': 0.091678720357652,\n  'n_estimators': 170,\n  'max_depth': 6,\n  'random_state': 43,\n  'min_child_weight': 193},\n '6': {'lambda': 1.2872639184328254,\n  'alpha': 0.060756540772177806,\n  'colsample_bytree': 0.6920337325359551,\n  'subsample': 0.6257752628204902,\n  'learning_rate': 0.045563802597348414,\n  'n_estimators': 146,\n  'max_depth': 5,\n  'random_state': 43,\n  'min_child_weight': 26},\n '7': {'lambda': 0.011770641180749943,\n  'alpha': 0.10586519922535292,\n  'colsample_bytree': 0.6400326168356286,\n  'subsample': 0.8462498734950723,\n  'learning_rate': 0.054388047649770024,\n  'n_estimators': 103,\n  'max_depth': 9,\n  'random_state': 43,\n  'min_child_weight': 30},\n '8': {'lambda': 0.030668823995119495,\n  'alpha': 1.1125013487633146,\n  'colsample_bytree': 0.8629095355208878,\n  'subsample': 0.578262211729344,\n  'learning_rate': 0.09931266352240624,\n  'n_estimators': 134,\n  'max_depth': 12,\n  'random_state': 43,\n  'min_child_weight': 227},\n '9': {'lambda': 1.5426806141512248,\n  'alpha': 0.004072987773410624,\n  'colsample_bytree': 0.445090005358303,\n  'subsample': 0.8948075773729335,\n  'learning_rate': 0.030438014747969238,\n  'n_estimators': 200,\n  'max_depth': 11,\n  'random_state': 43,\n  'min_child_weight': 130},\n '10': {'lambda': 0.0553495445787293,\n  'alpha': 5.788509297588221,\n  'colsample_bytree': 0.5399990122978464,\n  'subsample': 0.7088493612881469,\n  'learning_rate': 0.06727351053609328,\n  'n_estimators': 173,\n  'max_depth': 5,\n  'random_state': 43,\n  'min_child_weight': 230},\n '11': {'lambda': 0.8019596519294295,\n  'alpha': 0.03925571758705697,\n  'colsample_bytree': 0.5216842034997399,\n  'subsample': 0.9266073303947991,\n  'learning_rate': 0.09735759359169276,\n  'n_estimators': 184,\n  'max_depth': 5,\n  'random_state': 43,\n  'min_child_weight': 280},\n '14': {'lambda': 3.1612189232216377,\n  'alpha': 0.002595500561666517,\n  'colsample_bytree': 0.5373445714004844,\n  'subsample': 0.7231861712486606,\n  'learning_rate': 0.0829129781675211,\n  'n_estimators': 86,\n  'max_depth': 5,\n  'random_state': 43,\n  'min_child_weight': 53},\n '15': {'lambda': 4.08305659058426,\n  'alpha': 0.04376799826247082,\n  'colsample_bytree': 0.7756749380431447,\n  'subsample': 0.7620157557114705,\n  'learning_rate': 0.07326787669639384,\n  'n_estimators': 143,\n  'max_depth': 11,\n  'random_state': 43,\n  'min_child_weight': 193},\n '16': {'lambda': 0.0013552889932012958,\n  'alpha': 0.03945051058600412,\n  'colsample_bytree': 0.9753446984501696,\n  'subsample': 0.4169089569935959,\n  'learning_rate': 0.06346024696190851,\n  'n_estimators': 171,\n  'max_depth': 11,\n  'random_state': 43,\n  'min_child_weight': 255},\n '17': {'lambda': 0.06376234358327733,\n  'alpha': 0.8555674514711957,\n  'colsample_bytree': 0.3105210407135366,\n  'subsample': 0.7322592658249376,\n  'learning_rate': 0.08269861154443374,\n  'n_estimators': 146,\n  'max_depth': 6,\n  'random_state': 43,\n  'min_child_weight': 293}}\n\ndef create_model(old_train, quests, models, list_kol_f):\n    \n    kol_quest = len(quests)\n    # ITERATE THRU 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        model = xgb.XGBClassifier(**best_models[f'{q}'])  \n\n        model.fit(train_x.astype('float32'), train_y['correct'], verbose=False)\n\n        # SAVE MODEL, PREDICT VALID OOF\n        models[f'{q}'] = model\n    print('***')\n    \n    return models","metadata":{"execution":{"iopub.execute_input":"2023-12-15T14:58:00.778784Z","iopub.status.busy":"2023-12-15T14:58:00.778375Z","iopub.status.idle":"2023-12-15T14:58:00.800123Z","shell.execute_reply":"2023-12-15T14:58:00.798996Z"},"papermill":{"duration":0.033659,"end_time":"2023-12-15T14:58:00.802692","exception":false,"start_time":"2023-12-15T14:58:00.769033","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = {}\nbest_threshold = 0.63\n\n#score0.7\nlist_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.execute_input":"2023-12-15T14:58:00.820747Z","iopub.status.busy":"2023-12-15T14:58:00.819968Z","iopub.status.idle":"2023-12-15T14:58:00.825569Z","shell.execute_reply":"2023-12-15T14:58:00.824627Z"},"papermill":{"duration":0.017309,"end_time":"2023-12-15T14:58:00.827936","exception":false,"start_time":"2023-12-15T14:58:00.810627","status":"completed"},"tags":[]},"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.execute_input":"2023-12-15T14:58:00.845891Z","iopub.status.busy":"2023-12-15T14:58:00.845117Z","iopub.status.idle":"2023-12-15T15:00:23.837911Z","shell.execute_reply":"2023-12-15T15:00:23.836912Z"},"papermill":{"duration":143.005211,"end_time":"2023-12-15T15:00:23.840983","exception":false,"start_time":"2023-12-15T14:58:00.835772","status":"completed"},"tags":[]},"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:120, 11:120}\n\nmodels = create_model(df5_12, quests_5_12, models, list_kol_f)\ndel df5_12","metadata":{"execution":{"iopub.execute_input":"2023-12-15T15:00:23.859664Z","iopub.status.busy":"2023-12-15T15:00:23.858873Z","iopub.status.idle":"2023-12-15T15:09:27.928811Z","shell.execute_reply":"2023-12-15T15:09:27.927909Z"},"papermill":{"duration":544.090227,"end_time":"2023-12-15T15:09:27.939460","exception":false,"start_time":"2023-12-15T15:00:23.849233","status":"completed"},"tags":[]},"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: 110, 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.execute_input":"2023-12-15T15:09:27.959536Z","iopub.status.busy":"2023-12-15T15:09:27.958787Z","iopub.status.idle":"2023-12-15T15:15:27.166279Z","shell.execute_reply":"2023-12-15T15:15:27.164648Z"},"papermill":{"duration":359.221032,"end_time":"2023-12-15T15:15:27.169405","exception":false,"start_time":"2023-12-15T15:09:27.948373","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Infer Test Data**","metadata":{"papermill":{"duration":0.00936,"end_time":"2023-12-15T15:15:27.246393","exception":false,"start_time":"2023-12-15T15:15:27.237033","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.043327,"end_time":"2023-12-15T15:15:27.299179","exception":false,"start_time":"2023-12-15T15:15:27.255852","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-12-21T18:13:26.350935Z","iopub.execute_input":"2023-12-21T18:13:26.351555Z","iopub.status.idle":"2023-12-21T18:13:26.360349Z","shell.execute_reply.started":"2023-12-21T18:13:26.351510Z","shell.execute_reply":"2023-12-21T18:13:26.358865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time","metadata":{"execution":{"iopub.execute_input":"2023-12-15T15:15:27.321188Z","iopub.status.busy":"2023-12-15T15:15:27.320365Z","iopub.status.idle":"2023-12-15T15:15:27.325125Z","shell.execute_reply":"2023-12-15T15:15:27.324217Z"},"papermill":{"duration":0.018193,"end_time":"2023-12-15T15:15:27.327292","exception":false,"start_time":"2023-12-15T15:15:27.309099","status":"completed"},"tags":[]},"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        clf = models[f'{q}']\n        p = clf.predict_proba(new_train.astype('float32'))[:,1]        \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    print(sam_sub)\n    env.predict(sam_sub)","metadata":{"execution":{"iopub.execute_input":"2023-12-15T15:15:27.348950Z","iopub.status.busy":"2023-12-15T15:15:27.348144Z","iopub.status.idle":"2023-12-15T15:15:38.141650Z","shell.execute_reply":"2023-12-15T15:15:38.140778Z"},"papermill":{"duration":10.807137,"end_time":"2023-12-15T15:15:38.144039","exception":false,"start_time":"2023-12-15T15:15:27.336902","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA submission.csv","metadata":{"papermill":{"duration":0.010056,"end_time":"2023-12-15T15:15:38.164642","exception":false,"start_time":"2023-12-15T15:15:38.154586","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df = pd.read_csv('submission.csv')\nprint( df.shape )\ndf.head(60)","metadata":{"execution":{"iopub.execute_input":"2023-12-15T15:15:38.187340Z","iopub.status.busy":"2023-12-15T15:15:38.186408Z","iopub.status.idle":"2023-12-15T15:15:38.210648Z","shell.execute_reply":"2023-12-15T15:15:38.209786Z"},"papermill":{"duration":0.038735,"end_time":"2023-12-15T15:15:38.213486","exception":false,"start_time":"2023-12-15T15:15:38.174751","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}