{"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)\nimport polars as pl\nimport json\nimport xgboost as xgb\nfrom tqdm import tqdm\nimport gc\nimport sklearn\nimport catboost as cat\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-06-22T10:12:54.190745Z","iopub.execute_input":"2023-06-22T10:12:54.191176Z","iopub.status.idle":"2023-06-22T10:12:55.477359Z","shell.execute_reply.started":"2023-06-22T10:12:54.191146Z","shell.execute_reply":"2023-06-22T10:12:55.476457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_mapping = {\"room_fqid\": ['tunic.historicalsociety.closet',\n                                  'tunic.historicalsociety.basement',\n                                  'tunic.historicalsociety.entry',\n                                  'tunic.historicalsociety.collection',\n                                  'tunic.historicalsociety.stacks', 'tunic.kohlcenter.halloffame',\n                                  'tunic.capitol_0.hall', 'tunic.historicalsociety.closet_dirty',\n                                  'tunic.historicalsociety.frontdesk',\n                                  'tunic.humanecology.frontdesk', 'tunic.drycleaner.frontdesk',\n                                  'tunic.library.frontdesk', 'tunic.library.microfiche',\n                                  'tunic.capitol_1.hall', 'tunic.historicalsociety.cage',\n                                  'tunic.historicalsociety.collection_flag', 'tunic.wildlife.center',\n                                  'tunic.flaghouse.entry', 'tunic.capitol_2.hall'],\n                   \"text_fqid\":  ['tunic.historicalsociety.closet.intro',\n                                   'tunic.historicalsociety.closet.gramps.intro_0_cs_0',\n                                   'tunic.historicalsociety.closet.teddy.intro_0_cs_0',\n                                   'tunic.historicalsociety.closet.teddy.intro_0_cs_5',\n                                   'tunic.historicalsociety.closet.photo',\n                                   'tunic.historicalsociety.closet.notebook',\n                                   'tunic.historicalsociety.closet.retirement_letter.hub',\n                                   'tunic.historicalsociety.basement.janitor',\n                                   'tunic.historicalsociety.entry.groupconvo',\n                                   'tunic.historicalsociety.entry.boss.talktogramps',\n                                   'tunic.historicalsociety.entry.wells.talktogramps',\n                                   'tunic.historicalsociety.collection.cs',\n                                   'tunic.historicalsociety.collection.tunic.slip',\n                                   'tunic.historicalsociety.collection.gramps.found',\n                                   'tunic.historicalsociety.stacks.outtolunch',\n                                   'tunic.kohlcenter.halloffame.plaque.face.date',\n                                   'tunic.kohlcenter.halloffame.togrampa',\n                                   'tunic.capitol_0.hall.boss.talktogramps',\n                                   'tunic.historicalsociety.closet_dirty.what_happened',\n                                   'tunic.historicalsociety.closet_dirty.gramps.helpclean',\n                                   'tunic.historicalsociety.closet_dirty.trigger_scarf',\n                                   'tunic.historicalsociety.closet_dirty.trigger_coffee',\n                                   'tunic.historicalsociety.closet_dirty.gramps.news',\n                                   'tunic.historicalsociety.frontdesk.archivist.hello',\n                                   'tunic.historicalsociety.frontdesk.archivist.need_glass_0',\n                                   'tunic.historicalsociety.frontdesk.magnify',\n                                   'tunic.historicalsociety.frontdesk.archivist.have_glass',\n                                   'tunic.historicalsociety.frontdesk.archivist.have_glass_recap',\n                                   'tunic.humanecology.frontdesk.worker.intro',\n                                   'tunic.humanecology.frontdesk.businesscards.card_bingo.bingo',\n                                   'tunic.humanecology.frontdesk.worker.badger',\n                                   'tunic.drycleaner.frontdesk.worker.hub',\n                                   'tunic.drycleaner.frontdesk.logbook.page.bingo',\n                                   'tunic.drycleaner.frontdesk.worker.done',\n                                   'tunic.library.frontdesk.worker.hello',\n                                   'tunic.library.microfiche.reader.paper2.bingo',\n                                   'tunic.library.frontdesk.wellsbadge.hub',\n                                   'tunic.library.frontdesk.worker.wells',\n                                   'tunic.capitol_1.hall.boss.haveyougotit',\n                                   'tunic.historicalsociety.frontdesk.archivist.newspaper',\n                                   'tunic.historicalsociety.stacks.journals.pic_2.bingo',\n                                   'tunic.historicalsociety.basement.ch3start',\n                                   'tunic.historicalsociety.basement.gramps.whatdo',\n                                   'tunic.historicalsociety.basement.seescratches',\n                                   'tunic.historicalsociety.cage.glasses.beforeteddy',\n                                   'tunic.historicalsociety.cage.teddy.trapped',\n                                   'tunic.historicalsociety.cage.glasses.afterteddy',\n                                   'tunic.historicalsociety.entry.directory.closeup.archivist',\n                                   'tunic.historicalsociety.frontdesk.key',\n                                   'tunic.historicalsociety.cage.unlockdoor',\n                                   'tunic.historicalsociety.cage.confrontation',\n                                   'tunic.historicalsociety.basement.savedteddy',\n                                   'tunic.historicalsociety.collection_flag.gramps.flag',\n                                   'tunic.historicalsociety.entry.groupconvo_flag',\n                                   'tunic.historicalsociety.entry.wells.flag',\n                                   'tunic.historicalsociety.entry.boss.flag',\n                                   'tunic.historicalsociety.entry.wells.flag_recap',\n                                   'tunic.historicalsociety.entry.boss.flag_recap',\n                                   'tunic.wildlife.center.coffee',\n                                   'tunic.wildlife.center.wells.animals',\n                                   'tunic.wildlife.center.wells.animals2',\n                                   'tunic.wildlife.center.crane_ranger.crane',\n                                   'tunic.wildlife.center.remove_cup',\n                                   'tunic.wildlife.center.expert.removed_cup',\n                                   'tunic.wildlife.center.tracks.hub.deer',\n                                   'tunic.wildlife.center.expert.recap',\n                                   'tunic.wildlife.center.wells.nodeer',\n                                   'tunic.flaghouse.entry.flag_girl.hello',\n                                   'tunic.flaghouse.entry.colorbook',\n                                   'tunic.flaghouse.entry.flag_girl.symbol',\n                                   'tunic.flaghouse.entry.flag_girl.symbol_recap',\n                                   'tunic.library.frontdesk.worker.flag',\n                                   'tunic.library.microfiche.reader_flag.paper2.bingo',\n                                   'tunic.library.frontdesk.worker.nelson',\n                                   'tunic.historicalsociety.frontdesk.archivist_glasses.confrontation',\n                                   'tunic.historicalsociety.stacks.journals_flag.pic_0.bingo',\n                                   'tunic.historicalsociety.entry.block_tocollection',\n                                   'tunic.historicalsociety.closet_dirty.gramps.archivist',\n                                   'tunic.historicalsociety.cage.lockeddoor',\n                                   'tunic.capitol_2.hall.boss.haveyougotit',\n                                   'tunic.drycleaner.frontdesk.worker.done2',\n                                   'tunic.library.frontdesk.worker.preflag',\n                                   'tunic.historicalsociety.closet_dirty.photo',\n                                   'tunic.historicalsociety.collection_flag.gramps.recap',\n                                   'tunic.wildlife.center.wells.nodeer_recap',\n                                   'tunic.library.frontdesk.worker.flag_recap',\n                                   'tunic.historicalsociety.frontdesk.archivist_glasses.confrontation_recap',\n                                   'tunic.historicalsociety.collection.tunic',\n                                   'tunic.historicalsociety.collection.gramps.lost',\n                                   'tunic.humanecology.frontdesk.block_0',\n                                   'tunic.library.frontdesk.worker.wells_recap',\n                                   'tunic.capitol_1.hall.chap2_finale_c',\n                                   'tunic.capitol_1.hall.boss.writeitup',\n                                   'tunic.historicalsociety.stacks.journals_flag.pic_1.bingo',\n                                   'tunic.historicalsociety.frontdesk.block_magnify',\n                                   'tunic.historicalsociety.closet.doorblock',\n                                   'tunic.historicalsociety.stacks.journals_flag.pic_2.bingo',\n                                   'tunic.capitol_2.hall.chap4_finale_c',\n                                   'tunic.historicalsociety.closet_dirty.door_block_clean',\n                                   'tunic.historicalsociety.closet_dirty.door_block_talk',\n                                   'tunic.historicalsociety.stacks.block',\n                                   'tunic.flaghouse.entry.flag_girl.hello_recap',\n                                   'tunic.historicalsociety.entry.block_tomap2',\n                                   'tunic.historicalsociety.frontdesk.archivist.newspaper_recap',\n                                   'tunic.historicalsociety.basement.gramps.seeyalater',\n                                   'tunic.library.frontdesk.worker.hello_short',\n                                   'tunic.historicalsociety.closet_dirty.gramps.nothing',\n                                   'tunic.historicalsociety.cage.need_glasses',\n                                   'tunic.library.frontdesk.block_nelson',\n                                   'tunic.library.frontdesk.worker.nelson_recap',\n                                   'tunic.historicalsociety.frontdesk.archivist.need_glass_1',\n                                   'tunic.library.frontdesk.worker.droppedbadge',\n                                   'tunic.drycleaner.frontdesk.worker.takealook',\n                                   'tunic.historicalsociety.entry.block_tomap1',\n                                   'tunic.capitol_0.hall.chap1_finale_c',\n                                   'tunic.historicalsociety.frontdesk.archivist.foundtheodora',\n                                   'tunic.library.frontdesk.block_badge',\n                                   'tunic.kohlcenter.halloffame.block_0',\n                                   'tunic.drycleaner.frontdesk.block_0',\n                                   'tunic.library.microfiche.block_0',\n                                   'tunic.historicalsociety.collection.gramps.look_0',\n                                   'tunic.wildlife.center.fox.concern'],\n                   \"fqid\": ['intro', 'gramps', 'teddy', 'photo', 'notebook',\n                            'retirement_letter', 'tobasement', 'janitor', 'toentry',\n                            'groupconvo', 'report', 'boss', 'wells', 'directory',\n                            'tocollection', 'cs', 'tunic', 'tunic.hub.slip', 'tostacks',\n                            'outtolunch', 'tocloset', 'tomap', 'tunic.historicalsociety',\n                            'tunic.kohlcenter', 'plaque', 'plaque.face.date', 'togrampa',\n                            'tunic.capitol_0', 'chap1_finale', 'chap1_finale_c',\n                            'tocloset_dirty', 'what_happened', 'trigger_scarf',\n                            'trigger_coffee', 'tunic.capitol_1', 'tofrontdesk', 'archivist',\n                            'magnify', 'tunic.humanecology', 'worker', 'businesscards',\n                            'businesscards.card_0.next', 'businesscards.card_1.next',\n                            'businesscards.card_bingo.next', 'businesscards.card_bingo.bingo',\n                            'tohallway', 'tunic.drycleaner', 'logbook', 'logbook.page.bingo',\n                            'tunic.library', 'tomicrofiche', 'reader', 'reader.paper0.next',\n                            'reader.paper1.next', 'reader.paper2.bingo', 'wellsbadge',\n                            'journals', 'journals.hub.topics', 'journals.pic_0.next',\n                            'journals.pic_1.next', 'journals.pic_2.bingo', 'chap2_finale_c',\n                            'ch3start', 'seescratches', 'tocage', 'glasses',\n                            'directory.closeup.archivist', 'key', 'unlockdoor',\n                            'confrontation', 'savedteddy', 'tocollectionflag',\n                            'groupconvo_flag', 'tunic.capitol_2', 'tunic.wildlife', 'coffee',\n                            'crane_ranger', 'remove_cup', 'expert', 'tracks',\n                            'tracks.hub.deer', 'tunic.flaghouse', 'flag_girl', 'colorbook',\n                            'reader_flag', 'reader_flag.paper0.next',\n                            'reader_flag.paper1.next', 'reader_flag.paper2.bingo',\n                            'archivist_glasses', 'journals_flag',\n                            'journals_flag.hub.topics_old', 'journals_flag.hub.topics',\n                            'journals_flag.pic_0.bingo', 'journals_flag.pic_0.next',\n                            'chap4_finale_c', 'block_tocollection', 'reader.paper2.next',\n                            'journals.pic_2.next', 'lockeddoor', 'reader.paper2.prev',\n                            'reader.paper0.prev', 'reader_flag.paper1.prev',\n                            'journals_flag.pic_0_old.next', 'journals_flag.pic_1_old.next',\n                            'reader.paper1.prev', 'block_0', 'journals_flag.pic_1.bingo',\n                            'block_magnify', 'journals_flag.pic_1.next', 'doorblock',\n                            'journals_flag.pic_2.bingo', 'journals_flag.pic_2.next',\n                            'door_block_clean', 'door_block_talk', 'block',\n                            'reader_flag.paper2.next', 'block_tomap2',\n                            'reader_flag.paper0.prev', 'journals_flag.pic_2_old.next',\n                            'need_glasses', 'block_nelson', 'block_tomap1',\n                            'reader_flag.paper2.prev', 'block_badge', 'fox'],\n                   \"event_name\": ['cutscene_click', 'person_click', 'navigate_click',\n                                  'observation_click', 'notification_click', 'object_click',\n                                  'object_hover', 'map_hover', 'map_click', 'checkpoint','notebook_click'],\n                    \"name\": ['basic', 'close', 'open', 'prev', 'next']\n                   }\ncoordinates = [\"room_coor_x\", \"room_coor_y\", \"screen_coor_x\", \"screen_coor_y\"\n]\n\nCATS = ['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', \"elapsed_time_diff\"]\n\nfeature_generation = [\n    *[pl.col(cat).n_unique().alias(f\"{cat}_nunique\") for cat in CATS],\n    \n    *[pl.col(cat).unique_counts().mean().alias(f\"{cat}_unique_count_mean\") for cat in CATS],\n    *[pl.col(cat).unique_counts().std().alias(f\"{cat}_unique_count_std\") for cat in CATS],\n    *[pl.col(cat).unique_counts().median().alias(f\"{cat}_unique_count_median\") for cat in CATS],\n    *[pl.col(cat).unique_counts().max().alias(f\"{cat}_unique_count_max\") for cat in CATS],\n    *[pl.col(cat).unique_counts().min().alias(f\"{cat}_unique_count_min\") for cat in CATS],\n    \n    pl.col('session_id').count().alias(f\"number_of_clicks\"),\n    \n    *[pl.col(num).mean().alias(f\"{num}_mean\") for num in NUMS],\n    *[pl.col(num).std().alias(f\"{num}_std\") for num in NUMS],\n    *[pl.col(num).median().alias(f\"{num}_median\") for num in NUMS],\n    *[pl.col(num).max().alias(f\"{num}_max\") for num in NUMS],\n    *[pl.col(num).min().alias(f\"{num}_min\") for num in NUMS],\n     \n    *[pl.col(\"elapsed_time_diff\")\\\n           .filter(pl.col(feature_column) == f).median().alias(f'elapsed_time_diff_median_{f}')\\\n                for feature_column in features_mapping for f in features_mapping[feature_column]],\n    *[pl.col(\"elapsed_time_diff\")\\\n          .filter(pl.col(feature_column) == f).mean().alias(f'elapsed_time_diff_mean_{f}')\\\n               for feature_column in features_mapping for f in features_mapping[feature_column]],\n    *[pl.col(\"elapsed_time_diff\")\\\n         .filter(pl.col(feature_column) == f).max().alias(f'elapsed_time_diff_max_{f}')\\\n               for feature_column in features_mapping for f in features_mapping[feature_column]],\n    *[pl.col(\"elapsed_time_diff\")\\\n         .filter(pl.col(feature_column) == f).sum().alias(f'elapsed_time_diff_sum_{f}')\\\n               for feature_column in features_mapping for f in features_mapping[feature_column]],\n    *[pl.col(\"elapsed_time_diff\")\\\n         .filter(pl.col(feature_column) == f).min().alias(f'elapsed_time_diff_min_{f}')\\\n               for feature_column in features_mapping for f in features_mapping[feature_column]],\n    *[pl.col(\"elapsed_time_diff\")\\\n           .filter(pl.col(feature_column) == f).var().alias(f'elapsed_time_diff_var_{f}')\\\n               for feature_column in features_mapping for f in features_mapping[feature_column]],\n    *[pl.col('session_id').filter(pl.col(feature_column) == str(f)).count().alias(f\"number_of_clicks_{f}\")\n          for feature_column in features_mapping for f in features_mapping[feature_column]],  \n]\n\nlevels = [\"0-4\", \"5-12\", \"13-22\"]\n\nquestion_levels = [range(1,4), range(4,14), range(14,19)]\n\nANSWERS = [[f\"answers_{i}\" for i in question_range] for question_range in question_levels]","metadata":{"execution":{"iopub.status.busy":"2023-06-22T10:12:55.479166Z","iopub.execute_input":"2023-06-22T10:12:55.479721Z","iopub.status.idle":"2023-06-22T10:12:55.573453Z","shell.execute_reply.started":"2023-06-22T10:12:55.479689Z","shell.execute_reply":"2023-06-22T10:12:55.572584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def right_answers(question):\n    targets = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train_labels.csv')\n    targets['session'] = targets.session_id.apply(lambda x: int(x.split('_')[0]) )\n    targets['q'] = targets.session_id.apply(lambda x: int(x.split('_')[-1][1:]) )\n    return targets.loc[targets[\"q\"] == question][[\"session\", \"correct\"]].set_index(\"session\")\n\ndef preprocessing_2(test):\n    test.sort_values(by = [\"session_id\", \"elapsed_time\"], inplace=True)\n    return pl.DataFrame(test)\\\n        .with_columns((pl.col(\"elapsed_time\") - pl.col(\"elapsed_time\").shift(1))\n                          .fill_null(0)\n                          .clip(0, 1e9)\n                          .over([\"session_id\", \"level\"])\n                          .alias(\"elapsed_time_diff\"),\n                      pl.col(\"fqid\").fill_null(\"fqid_None\"),\n                      pl.col(\"text_fqid\").fill_null(\"text_fqid_None\"))\\\n        .groupby([\"session_id\", \"level_group\"], maintain_order=True).agg(feature_generation).to_pandas()\\\n            .set_index(\"session_id\").sort_index()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T10:12:56.858888Z","iopub.execute_input":"2023-06-22T10:12:56.859629Z","iopub.status.idle":"2023-06-22T10:12:56.869514Z","shell.execute_reply.started":"2023-06-22T10:12:56.859583Z","shell.execute_reply":"2023-06-22T10:12:56.868450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtypes={ \n    'elapsed_time':np.int32,\n    'event_name':'category',\n    'name':'category',\n    'level':np.uint8,\n    'page':'category',\n    'room_coor_x':np.float32,\n    'room_coor_y':np.float32,\n    'screen_coor_x':np.float32,\n    'screen_coor_y':np.float32,\n    'hover_duration':np.float32,\n     'text':'category',\n     'fqid':'category',\n     'room_fqid':'category',\n     'text_fqid':'category',\n     'fullscreen':'category',\n     'hq':'category',\n     'music':'category',\n     'level_group':'category'}\n\ndef train_retrieve():\n    train_iter = pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\", dtype=dtypes, chunksize=20000000)\n    for i, train_data_part in tqdm(enumerate(train_iter)):\n        gc.collect()\n        print(i)\n        part = preprocessing_2(train_data_part)\n        for level in levels:\n            part.loc[part[\"level_group\"] == level].drop(\"level_group\", axis=1).to_csv(f\"preprocessed_train_{level}.csv\", mode='a', header=(i == 0))\n        del part\n    gc.collect()\n    return 0\n\ntrain_retrieve()\nf1 = []","metadata":{"execution":{"iopub.status.busy":"2023-06-22T10:12:57.514916Z","iopub.execute_input":"2023-06-22T10:12:57.515820Z","iopub.status.idle":"2023-06-22T10:13:58.428473Z","shell.execute_reply.started":"2023-06-22T10:12:57.515779Z","shell.execute_reply":"2023-06-22T10:13:58.425628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\npreds = []\ntrain_preds = []\nlevel_y_test = []\nlevel_y_train = []\ncorrs = [0]\nmodels = []\ndrop = {\"0-4\": [], \"5-12\": [], \"13-22\": []}\nfor level, questions in enumerate(question_levels):\n    gc.collect()\n    print(f\"Level {level}\")\n    base_dataset = pd.read_csv(f\"/kaggle/working/preprocessed_train_{levels[level]}.csv\")\n    base_dataset = base_dataset.drop_duplicates(subset=[\"session_id\"]).set_index(\"session_id\")\n    null = base_dataset.isnull().sum().sort_values(ascending=False) / len(base_dataset)\n    drop[levels[level]] = list(null[null>0.9].index)\n    for col in base_dataset.columns:\n        if base_dataset[col].nunique()==1:\n            drop[levels[level]].append(col)\n    dataset = base_dataset.drop(drop[levels[level]], axis=1).fillna(-1).sort_index()\n    del base_dataset\n    gc.collect()\n    for i in questions:\n        start = time.time()\n        print(f\"Question {i}\")\n        X_train = dataset\n        y_train = right_answers(i).sort_index()\n        gc.collect()\n        level_y_train.append(y_train)\n        estimators_xgb = [498, 448, 378, 364, 405, 495, 456, 249, 384, 405, 356, 262, 484, 381, 392, 248 ,248, 345]\n        param = {\n            'booster': 'gbtree',\n            'tree_method': 'hist',\n            'objective': 'binary:logistic',\n            'eval_metric':'logloss',\n            'learning_rate': 0.02,\n            'alpha': 8,\n            'max_depth': 4,\n            'subsample':0.8,\n            'colsample_bytree': 0.5,\n            'seed': 2023\n        }\n        param[\"n_estimators\"] = estimators_xgb[i-1]\n        bst = xgb.XGBClassifier(**param)\n        bst.fit(X_train.astype(\"float32\"), y_train[\"correct\"])\n        bst.save_model(f\"xgb_q{i}.json\")\n        train_pred = bst.predict_proba(X_train.astype(\"float32\"))[:,1]\n        dataset[f\"q{i}_ans\"] = train_pred\n        models.append(bst)\n        train_preds.append((train_pred > 0.63).astype(int))\n        print(time.time() - start)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-06-21T17:02:04.221163Z","iopub.execute_input":"2023-06-21T17:02:04.221606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del dataset\ngc.collect()\ntrain_preds = np.array(train_preds)\nlevel_y_train = np.array(level_y_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T23:27:27.238463Z","iopub.execute_input":"2023-06-20T23:27:27.238744Z","iopub.status.idle":"2023-06-20T23:27:27.366240Z","shell.execute_reply.started":"2023-06-20T23:27:27.238719Z","shell.execute_reply":"2023-06-20T23:27:27.364943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = pd.DataFrame({\n    \"f1_train\":[sklearn.metrics.f1_score(level_y_train[i],train_preds[i], average=\"macro\") for i in range(18)],\n    \"accuracy_train\": [sklearn.metrics.accuracy_score(level_y_train[i],train_preds[i]) for i in range(18)]\n})\naggr_metrics = {\n    \"f1_train\": sklearn.metrics.f1_score(level_y_train.reshape(-1),train_preds.reshape(-1), average=\"macro\"),\n}","metadata":{"execution":{"iopub.status.busy":"2023-06-20T23:27:27.368097Z","iopub.execute_input":"2023-06-20T23:27:27.368466Z","iopub.status.idle":"2023-06-20T23:27:27.842048Z","shell.execute_reply.started":"2023-06-20T23:27:27.368431Z","shell.execute_reply":"2023-06-20T23:27:27.841097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics","metadata":{"execution":{"iopub.status.busy":"2023-06-20T23:27:27.844958Z","iopub.execute_input":"2023-06-20T23:27:27.845405Z","iopub.status.idle":"2023-06-20T23:27:27.861448Z","shell.execute_reply.started":"2023-06-20T23:27:27.845370Z","shell.execute_reply":"2023-06-20T23:27:27.860150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aggr_metrics","metadata":{"execution":{"iopub.status.busy":"2023-06-20T23:27:27.863371Z","iopub.execute_input":"2023-06-20T23:27:27.864581Z","iopub.status.idle":"2023-06-20T23:27:27.874472Z","shell.execute_reply.started":"2023-06-20T23:27:27.864545Z","shell.execute_reply":"2023-06-20T23:27:27.873331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import jo_wilder_310\nenv = jo_wilder_310.make_env()\ntest_iterator = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2023-06-19T15:56:03.893206Z","iopub.status.idle":"2023-06-19T15:56:03.894051Z","shell.execute_reply.started":"2023-06-19T15:56:03.893811Z","shell.execute_reply":"2023-06-19T15:56:03.893834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start = time.time()\nlimits = {'0-4':(1,4), '5-12':(4,14), '13-22':(14,19)}\nfor (test, sample_submission) in test_iterator:\n    grp = test.level_group.values[0]\n    # FEATURE ENGINEER TEST DATA\n    df = preprocessing_2(test).drop(drop[grp] + [\"level_group\"], axis=1).fillna(-1)\n    \n    # INFER TEST DATA\n    a,b = limits[grp]\n    for t in range(a,b):\n        clf = models[t-1]\n        p = clf.predict_proba(df.astype('float32'))[:,1]\n        df[f\"q{t}_ans\"] = p\n        mask = sample_submission.session_id.str.contains(f'q{t}')\n        sample_submission.loc[mask,'correct'] = int( p > 0.63 )\n    \n    env.predict(sample_submission)\nprint(time.time() - start)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T08:48:08.167755Z","iopub.execute_input":"2023-06-18T08:48:08.168172Z","iopub.status.idle":"2023-06-18T08:48:10.461918Z","shell.execute_reply.started":"2023-06-18T08:48:08.168139Z","shell.execute_reply":"2023-06-18T08:48:10.460922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"submission.csv\").head()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T08:52:09.202336Z","iopub.execute_input":"2023-06-18T08:52:09.202864Z","iopub.status.idle":"2023-06-18T08:52:09.21781Z","shell.execute_reply.started":"2023-06-18T08:52:09.202824Z","shell.execute_reply":"2023-06-18T08:52:09.216569Z"},"trusted":true},"execution_count":null,"outputs":[]}]}