{"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":"import pandas as pd\nimport numpy as np\nimport xgboost as xgb\nimport copy\nfrom tqdm import tqdm\nimport gc\nimport jo_wilder\nfrom sklearn.model_selection import train_test_split, StratifiedKFold, cross_val_score\nfrom sklearn.metrics import f1_score\nimport pickle\nimport os\nimport warnings\nwarnings.filterwarnings('ignore')\nfrom scipy.stats import mode","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-13T06:41:25.744341Z","iopub.execute_input":"2023-03-13T06:41:25.744766Z","iopub.status.idle":"2023-03-13T06:41:26.379311Z","shell.execute_reply.started":"2023-03-13T06:41:25.744728Z","shell.execute_reply":"2023-03-13T06:41:26.378082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"info_dict = pickle.load(open(\"/kaggle/input/info-dict/info_dict.pkl\", \"rb\"))\ninfo_dict","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:26.381219Z","iopub.execute_input":"2023-03-13T06:41:26.381685Z","iopub.status.idle":"2023-03-13T06:41:26.815837Z","shell.execute_reply.started":"2023-03-13T06:41:26.381643Z","shell.execute_reply":"2023-03-13T06:41:26.814846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def type_casting(df):\n    for col in df.columns:\n        col_type = df[col].dtype.name\n        if ((col_type != 'datetime64[ns]') & (col_type != 'category')):\n            if (col_type != 'object'):\n                c_min = df[col].min()\n                c_max = df[col].max()\n\n                if str(col_type)[:3] == 'int':\n                    if c_min >= np.iinfo(np.uint8).min and c_max < np.iinfo(np.uint8).max:\n                        df[col] = df[col].astype(np.uint8)\n                    elif c_min >= np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                        df[col] = df[col].astype(np.int8)\n                    \n                    elif c_min >= np.iinfo(np.uint16).min and c_max < np.iinfo(np.uint16).max:\n                        df[col] = df[col].astype(np.uint16)\n                    elif c_min >= np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                        df[col] = df[col].astype(np.int16)\n                    \n                    elif c_min >= np.iinfo(np.uint32).min and c_max < np.iinfo(np.uint32).max:\n                        df[col] = df[col].astype(np.uint32)\n                    elif c_min >= np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                        df[col] = df[col].astype(np.int32)\n                    \n                    elif c_min >= np.iinfo(np.uint64).min and c_max < np.iinfo(np.uint64).max:\n                        df[col] = df[col].astype(np.uint64)\n                    elif c_min >= np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                        df[col] = df[col].astype(np.int64)\n\n                else:\n                    if c_min >= np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                        df[col] = df[col].astype(np.float16)\n                    elif c_min >= np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                        df[col] = df[col].astype(np.float32)\n                    else:\n                        pass\n            else:\n                df[col] = df[col].astype('category')\n    \n    return df\n","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:26.817528Z","iopub.execute_input":"2023-03-13T06:41:26.818108Z","iopub.status.idle":"2023-03-13T06:41:26.833344Z","shell.execute_reply.started":"2023-03-13T06:41:26.818073Z","shell.execute_reply":"2023-03-13T06:41:26.832412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_test(df):\n    df['page'].fillna(-1, inplace=True)\n    df['room_coor_x'].fillna(0.0, inplace=True)\n    df['room_coor_y'].fillna(0.0, inplace=True)\n    df['screen_coor_x'].fillna(0.0, inplace=True)\n    df['screen_coor_y'].fillna(0.0, inplace=True)\n    df['hover_duration'].fillna(-1.0, inplace=True)\n    df['fqid'].fillna('unknown', inplace=True)\n    df['text_fqid'].fillna('unknown', inplace=True)\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:26.834599Z","iopub.execute_input":"2023-03-13T06:41:26.835156Z","iopub.status.idle":"2023-03-13T06:41:26.851186Z","shell.execute_reply.started":"2023-03-13T06:41:26.835123Z","shell.execute_reply":"2023-03-13T06:41:26.850143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_memory_usage_test(df):\n    \n    df = preprocess_test(df)\n#     df = type_casting(df)\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:27.788778Z","iopub.execute_input":"2023-03-13T06:41:27.789417Z","iopub.status.idle":"2023-03-13T06:41:27.794061Z","shell.execute_reply.started":"2023-03-13T06:41:27.789379Z","shell.execute_reply":"2023-03-13T06:41:27.793085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_engineer_test(df):\n    test_df = reduce_memory_usage_test(df)\n    \n    test_df['event_name'] = test_df['event_name'].map(info_dict['event_name_dict']).astype(np.uint8)\n    test_df['name'] = test_df['name'].map(info_dict['name_dict']).astype(np.uint8)\n    test_df['room_fqid'] = test_df['room_fqid'].map(info_dict['room_fqid_dict']).astype(np.uint8)\n    test_df['fqid'] = test_df['fqid'].map(info_dict['fqid_dict']).astype(np.uint8)\n    test_df['text_fqid'] = test_df['text_fqid'].map(info_dict['text_fqid_dict']).astype(np.uint8)\n    \n    return test_df","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:28.313362Z","iopub.execute_input":"2023-03-13T06:41:28.313773Z","iopub.status.idle":"2023-03-13T06:41:28.320937Z","shell.execute_reply.started":"2023-03-13T06:41:28.313736Z","shell.execute_reply":"2023-03-13T06:41:28.319936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cols = ['session_id', 'elapsed_time', 'event_name', 'name', 'level', 'page', \n              'room_coor_x', 'room_coor_y', 'screen_coor_x', 'screen_coor_y', 'hover_duration', \n              'fqid', 'text_fqid', 'room_fqid', 'level_group']","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:28.998240Z","iopub.execute_input":"2023-03-13T06:41:28.999263Z","iopub.status.idle":"2023-03-13T06:41:29.003873Z","shell.execute_reply.started":"2023-03-13T06:41:28.999225Z","shell.execute_reply":"2023-03-13T06:41:29.002954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = ['elapsed_time',\n        'event_name',\n        'name',\n        'level',\n        'page',\n        'room_coor_x',\n        'room_coor_y',\n        'screen_coor_x',\n        'screen_coor_y',\n        'hover_duration',\n        'room_fqid',\n        'fqid',\n        'text_fqid'\n       ]","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:30.262653Z","iopub.execute_input":"2023-03-13T06:41:30.265196Z","iopub.status.idle":"2023-03-13T06:41:30.270022Z","shell.execute_reply.started":"2023-03-13T06:41:30.265153Z","shell.execute_reply":"2023-03-13T06:41:30.269132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env = jo_wilder.make_env()\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:31.370533Z","iopub.execute_input":"2023-03-13T06:41:31.371199Z","iopub.status.idle":"2023-03-13T06:41:31.375604Z","shell.execute_reply.started":"2023-03-13T06:41:31.371162Z","shell.execute_reply":"2023-03-13T06:41:31.374743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"limits = {'0-4': (1, 3), '5-12': (4, 13), '13-22': (14, 18)}\n\n\nfor (sample_submission, test) in iter_test:\n    \n    # FEATURE ENGINEER TEST DATA\n    df = feature_engineer_test(test[train_cols])\n    \n    unique_level = df['level_group'].unique()[0]\n    \n    a, b = limits[unique_level]\n\n    for i in range(a, b+1):\n        # Read the model \n        xgb_model = info_dict['models'][f'{unique_level}_q{i}']\n\n        pred = mode(xgb_model.predict(df[cols].astype(np.float32)), axis=None)[0][0]\n\n        \n        mask = sample_submission.session_id.str.contains(f'q{i}')\n        sample_submission.loc[mask, 'correct'] = pred\n    \n    env.predict(sample_submission)\n    \n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:32.157581Z","iopub.execute_input":"2023-03-13T06:41:32.158440Z","iopub.status.idle":"2023-03-13T06:41:33.631336Z","shell.execute_reply.started":"2023-03-13T06:41:32.158397Z","shell.execute_reply":"2023-03-13T06:41:33.630228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/working/submission.csv\")\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:36.822852Z","iopub.execute_input":"2023-03-13T06:41:36.823268Z","iopub.status.idle":"2023-03-13T06:41:36.845946Z","shell.execute_reply.started":"2023-03-13T06:41:36.823236Z","shell.execute_reply":"2023-03-13T06:41:36.844777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.correct.mean()","metadata":{"execution":{"iopub.status.busy":"2023-03-13T06:41:38.508163Z","iopub.execute_input":"2023-03-13T06:41:38.508821Z","iopub.status.idle":"2023-03-13T06:41:38.518904Z","shell.execute_reply.started":"2023-03-13T06:41:38.508784Z","shell.execute_reply":"2023-03-13T06:41:38.517876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}