{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-19T19:54:21.943814Z","iopub.execute_input":"2023-04-19T19:54:21.944171Z","iopub.status.idle":"2023-04-19T19:54:22.002073Z","shell.execute_reply.started":"2023-04-19T19:54:21.944140Z","shell.execute_reply":"2023-04-19T19:54:22.001174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Importing required libraries\nimport numpy as np , gc\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as mpatches\nimport sklearn\nimport seaborn as sns\nfrom sklearn.metrics import confusion_matrix\nimport random\nfrom sklearn.model_selection import KFold, GroupKFold\nfrom xgboost import XGBClassifier\nfrom sklearn.metrics import f1_score\nimport pandas as pd\nimport pyarrow.parquet as pq","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:54:22.003289Z","iopub.execute_input":"2023-04-19T19:54:22.003605Z","iopub.status.idle":"2023-04-19T19:54:22.855749Z","shell.execute_reply.started":"2023-04-19T19:54:22.003570Z","shell.execute_reply":"2023-04-19T19:54:22.854782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CATS = ['event_name', 'fqid', 'room_fqid', 'text']\nNUMS = ['elapsed_time','level','page','room_coor_x', 'room_coor_y', \n        'screen_coor_x', 'screen_coor_y', 'hover_duration']\n\n# https://www.kaggle.com/code/kimtaehun/lightgbm-baseline-with-aggregated-log-data\nEVENTS = ['navigate_click','person_click','cutscene_click','object_click',\n          'map_hover','notification_click','map_click','observation_click',\n          'checkpoint']","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:54:22.856903Z","iopub.execute_input":"2023-04-19T19:54:22.857190Z","iopub.status.idle":"2023-04-19T19:54:22.862986Z","shell.execute_reply.started":"2023-04-19T19:54:22.857161Z","shell.execute_reply":"2023-04-19T19:54:22.861980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_engineer(train_df):\n    \n    dfs = []\n    for c in CATS:\n        tmp = train_df.groupby(['session_id','level_group'])[c].agg('nunique')\n        tmp.name = tmp.name + '_nunique'\n        dfs.append(tmp)\n    for c in NUMS:\n        tmp = train_df.groupby(['session_id','level_group'])[c].agg('mean')\n        tmp.name = tmp.name + '_mean'\n        dfs.append(tmp)\n    for c in NUMS:\n        tmp = train_df.groupby(['session_id','level_group'])[c].agg('std')\n        tmp.name = tmp.name + '_std'\n        dfs.append(tmp)\n    for c in EVENTS: \n        train_df[c] = (train_df.event_name == c).astype('int8')\n    for c in EVENTS + ['elapsed_time']:\n        tmp = train_df.groupby(['session_id','level_group'])[c].agg('sum')\n        tmp.name = tmp.name + '_sum'\n        dfs.append(tmp)\n    train_df = train_df.drop(EVENTS,axis=1)\n        \n    df = pd.concat(dfs,axis=1)\n    df = df.fillna(-1)\n    df = df.reset_index()\n    df = df.set_index('session_id')\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:54:22.865032Z","iopub.execute_input":"2023-04-19T19:54:22.865396Z","iopub.status.idle":"2023-04-19T19:54:22.875965Z","shell.execute_reply.started":"2023-04-19T19:54:22.865366Z","shell.execute_reply":"2023-04-19T19:54:22.874809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#model_list  = XGBClassifier()\n\n# Save each model to list\nmodels= []\nfor t in range(1, 19):\n    # Includes XGBClassifier() inside so it loads the new model on every loop\n    model = XGBClassifier()\n    model.load_model(f\"/kaggle/input/student-performance-notebook/XGB_question{t}.xgb\")\n    models.append(model)\nprint(models)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:54:22.877636Z","iopub.execute_input":"2023-04-19T19:54:22.878239Z","iopub.status.idle":"2023-04-19T19:54:23.261469Z","shell.execute_reply.started":"2023-04-19T19:54:22.878175Z","shell.execute_reply":"2023-04-19T19:54:23.260631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(models)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:54:23.262649Z","iopub.execute_input":"2023-04-19T19:54:23.263152Z","iopub.status.idle":"2023-04-19T19:54:23.269271Z","shell.execute_reply.started":"2023-04-19T19:54:23.263119Z","shell.execute_reply":"2023-04-19T19:54:23.268439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# IMPORT KAGGLE API\nimport jo_wilder\nenv = jo_wilder.make_env()\niter_test = env.iter_test()\n\n# CLEAR MEMORY\nimport gc\n#del targets, df, oof, true\n#_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:54:23.270566Z","iopub.execute_input":"2023-04-19T19:54:23.271092Z","iopub.status.idle":"2023-04-19T19:54:23.282784Z","shell.execute_reply.started":"2023-04-19T19:54:23.271059Z","shell.execute_reply":"2023-04-19T19:54:23.281826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"limits = {'0-4':(1,4), '5-12':(4,14), '13-22':(14,19)}\n\nfor (test, sample_submission) in iter_test:\n    \n    # FEATURE ENGINEER TEST DATA\n    df = feature_engineer(test)\n    FEATURES = [c for c in df.columns if c != 'level_group']\n    print(FEATURES)\n    # INFER TEST DATA\n    grp = test.level_group.values[0]\n    a,b = limits[grp]\n    for t in range(a,b):\n        clf = models[t-1]\n        p = clf.predict_proba(df[FEATURES].astype('float32'))[0,1]\n        mask = sample_submission.session_id.str.contains(f'q{t}')\n        sample_submission.loc[mask,'correct'] = int( p > .63 )\n    \n    env.predict(sample_submission)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:54:23.284314Z","iopub.execute_input":"2023-04-19T19:54:23.284895Z","iopub.status.idle":"2023-04-19T19:54:24.148438Z","shell.execute_reply.started":"2023-04-19T19:54:23.284846Z","shell.execute_reply":"2023-04-19T19:54:24.147547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('submission.csv')\nprint( df.shape )\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:55:17.137663Z","iopub.execute_input":"2023-04-19T19:55:17.138694Z","iopub.status.idle":"2023-04-19T19:55:17.158043Z","shell.execute_reply.started":"2023-04-19T19:55:17.138653Z","shell.execute_reply":"2023-04-19T19:55:17.157266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.correct.mean())","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:58:17.548414Z","iopub.execute_input":"2023-04-19T19:58:17.548829Z","iopub.status.idle":"2023-04-19T19:58:17.554630Z","shell.execute_reply.started":"2023-04-19T19:58:17.548776Z","shell.execute_reply":"2023-04-19T19:58:17.553485Z"},"trusted":true},"execution_count":null,"outputs":[]}]}