{"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 matplotlib.pyplot as plt\nimport gc\nimport pickle\nimport joblib\nimport polars as pl\nfrom sklearn.model_selection import KFold, GroupKFold\nfrom xgboost import XGBClassifier\nfrom catboost import CatBoostClassifier, Pool\nimport lightgbm as lgb\nfrom sklearn.metrics import f1_score\nfrom tqdm.notebook import tqdm\nfrom collections import defaultdict\nimport warnings\nfrom itertools import combinations\n\nwarnings.filterwarnings('ignore')\npd.set_option(\"display.max_columns\", None)\npd.set_option(\"display.max_rows\", 800)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-23T05:20:43.875670Z","iopub.execute_input":"2023-06-23T05:20:43.876083Z","iopub.status.idle":"2023-06-23T05:20:47.062609Z","shell.execute_reply.started":"2023-06-23T05:20:43.876046Z","shell.execute_reply":"2023-06-23T05:20:47.061376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def trans_str(x):\n    if x in ['Ugh. Fine.','Okay. Thanks anyway.','Ugh. Fine.','Okay. Thanks anyway.']:\n        y = 'Ugh. Fine.'\n    elif x in ['Wha... but...','What?!']:\n        y = 'Wha... but...'\n    elif x in ['And you are?','Who are you?']:\n        y = 'Who are you?'\n    elif x in ['Just this old slip from 1916.','Yes! This old slip from 1916.','Yes! This cool old slip from 1916.']:\n        y = 'Just this old slip from 1916.'\n    elif x in ['Run along to the university.','Head over to the university.']:\n        y = 'Head over to the university.'\n    elif x in ['Um, are you okay?','Are you okay?']:\n        y = 'Um, are you okay?'\n    elif x in ['Not really...','Sure, Gramps!','YES!!!']:\n        y = 'YES!!!'\n    elif x in ['Yippee!','Sounds good, Mrs. M.']:\n        y = 'Yippee!'\n    elif x in ['Then who took Teddy???','Then who took Teddy?']:\n        y = 'Then who took Teddy?'\n    elif x in ['Meetings are BORING!','Can I come, Gramps?','Ugh. Meetings are so boring.']:\n        y = 'Meetings are BORING!'\n    elif x in ['I need to find Wells!!!','Can you help me? I need to find Wells!']:\n        y = 'I need to find Wells!!!'\n    elif x in ['Grab your notebook and come upstairs!','Sure thing, Jo. Grab your notebook and come upstairs!']:\n        y = 'Grab your notebook and come upstairs!'\n    elif x in ['Could be. But we need evidence.','Then we need evidence.']:\n        y = 'Could be. But we need evidence.'\n    elif x in ['Huh?','Hi! How can I help you?']:\n        y = 'Hi! How can I help you?'\n    elif x in [\"Fine, fine. Let's see...\",\"Hmm. Let's see...\"]:\n        y = \"Hmm. Let's see...\"\n    elif x in [\"He's a badger!\",\"Don't worry, he won't! (And he's a badger, by the way.)\"]:\n        y = \"He's a badger!\"\n    elif x in [\"I can't calm down. This is important!\",\"Please? This is really important.\"]:\n        y = \"I can't calm down. This is important!\"\n    elif x in [\"Why don't you go play with your grampa?\",\"Why don't you go catch up with your grampa?\"]:\n        y = \"Why don't you go play with your grampa?\"\n    elif x in [\"Gah. I can't believe this.\",\"I can't believe this.\"]:\n        y = \"I can't believe this.\"\n    elif x in [\"Ooh, thanks!\",\"Thanks.\"]:\n        y = \"Ooh, thanks!\"\n    elif x in [\"Whatever.\",\"I can't believe this.\"]:\n        y = \"Whatever.\"\n    elif x in [\"Are you telling me there's a LIVE badger in MY museum?\",\"Are you telling me there's a live badger in my museum?\"]:\n        y = \"Are you telling me there's a LIVE badger in MY museum?\"\n    elif x in [\"Ha! You're funny.\",\"Hi! How can I help you?\"]:\n        y = \"Ha! You're funny.\"\n    elif x in [\"WELLS WAS WRONG AND GRAMPS WAS RIGHT!!!\",\"Wells was wrong!\"]:\n        y = \"Wells was wrong!\"\n    elif x in [\"Watch it, Jo.\",\"Um.... yeah. Sorry, Mrs. M.\"]:\n        y = \"Watch it, Jo.\"\n    elif x in [\"Hold your horses, Jo.\",\"Slow down, Jo.\"]:\n        y = \"Hold your horses, Jo.\"\n    elif x in [\"Well? What are you still doing here?\",\"What are you still doing here, Jolie?\"]:\n        y = \"What are you still doing here, Jolie?\"\n    elif x in [\"Not sure. Do I look like a deer expert to you?\",\"Not sure.\"]:\n        y = \"Not sure.\"\n    elif x in [\"I'll stay here with the sunset.\",\"I'll wait here.\"]:\n        y = \"I'll wait here.\"\n    elif x in [\"And Gramps only drinks Holdgers!\",\"Gramps only drinks Holdgers.\"]:\n        y = \"And Gramps only drinks Holdgers!\"\n    elif x in [\"Hopefully you can rustle up some clues!\",\"Hopefully you can find some clues!\"]:\n        y = \"Hopefully you can find some clues!\"\n    elif x in [\"Yes!!! I'm saved!\",\"Yes!!!\"]:\n        y = \"Yes!!! I'm saved!\"\n    elif x in [\"It's for the flag display!\",\"We're just looking for photos for the flag display.\"]:\n        y = \"It's for the flag display!\"\n    elif x in [\"I think I can help with your animal problem.\",\"I think I might be able to help you.\"]:\n        y = \"I think I can help with your animal problem.\"\n    elif x in [\"And we still need to figure out that flag!\",\"We still need to figure out that flag. Do you know anyone who could help?\"]:\n        y = \"And we still need to figure out that flag!\"\n    elif x in [\"A vexy-wha?\",\"What's a vexillophile?\"]:\n        y = \"What's a vexillophile?\"\n    elif x in [\"I'll hurry back and then we can go exploring!\",\"I get to go to Gramps's meeting!\"]:\n        y = \"I get to go to Gramps's meeting!\"\n    elif x in [\"What if he wants to have Teddy stuffed?!\",\"HE WANTS TO KILL TEDDY!\"]:\n        y = \"HE WANTS TO KILL TEDDY!\"\n    elif x in [\"No thanks. I don't need help from kids.\",\"Ha! I don't need your help.\"]:\n        y = \"Ha! I don't need your help.\"\n    elif x in [\"Teddy would never leave without his scarf.\",\"Teddy would NEVER leave without his scarf!\"]:\n        y = \"Teddy would NEVER leave without his scarf!\"\n    elif x in [\"Please let me know if you do.\",\"Please let me know if you do. It's important!\"]:\n        y = \"Please let me know if you do. It's important!\"\n    elif x in [\"But what if Wells kidnapped Teddy?\",\"BUT WELLS STOLE TEDDY!\"]:\n        y = \"But what if Wells kidnapped Teddy?\"\n    elif x in [\"Now if only I could read this thing.\",\"Now if only I could read this thing. Blasted tiny letters...\"]:\n        y = \"Now if only I could read this thing.\"\n    elif x in [\"Ordinary people standing up for what they believe in!\",\"Ordinary people standing up for what they believe in.\"]:\n        y = \"Ordinary people standing up for what they believe in!\"\n    elif x in [\"Okay. I'll find Teddy!\",\"Don't worry, Gramps. I'll find Teddy!\"]:\n        y = \"Don't worry, Gramps. I'll find Teddy!\"\n    elif x in [\"Gadzooks! Poor critter.\",\"Poor badger.\"]:\n        y = \"Gadzooks! Poor critter.\"\n    elif x in [\"Can you help me find Wells?\",\"I need to find Wells right away! Do you know where he is?\"]:\n        y = \"Can you help me find Wells?\"\n    elif x in [\"I need to find the owner of this slip.\",\"Can you help me? I need to find the owner of this slip.\"]:\n        y = \"I need to find the owner of this slip.\"\n    elif x in [\"It's not a jersey, Mrs. M!\",\"IT'S NOT A JERSEY!\"]:\n        y = \"It's not a jersey, Mrs. M!\"\n    elif x in [\"We're looking for some photos.\",\"Actually, we're just here for some photos.\"]:\n        y = \"We're looking for some photos.\"\n    elif x in [\"I have to head over there and check out the animals.\",\"Ugh. I have to head over there and check out the animals.\"]:\n        y = \"I have to head over there and check out the animals.\"\n    elif x in [\"Now I Just need to find all the cleaners from way back in 1916.\",\"Yikes... this could take a while.\"]:\n        y = \"Now I Just need to find all the cleaners from way back in 1916.\"\n    elif x in [\"Ugh... I think that lynx is looking at me funny.\",\"Ugh...\"]:\n        y = \"Ugh... I think that lynx is looking at me funny.\"\n    elif x in [\"You're right, Gramps. Let's investigate!\",\"Fine. Let's investigate!\"]:\n        y = \"You're right, Gramps. Let's investigate!\"\n    elif x in [\"Come on, kid. Let's go.\",\"Come on, kid. You're slowing me down.\"]:\n        y = \"Come on, kid. Let's go.\"\n    elif x in [\"Our shirt was around WAYYY before the women's basketball team!\",\"Our shirt was around way before the women's basketball team!\"]:\n        y = \"Our shirt was around WAYYY before the women's basketball team!\"\n    elif x in [\"Oh, trust me. He'll make time.\",\"Okay. Thanks!\"]:\n        y = \"Oh, trust me. He'll make time.\"\n    elif x in [\"Teddy! I'm glad to see you.\",\"Teddy! I'm sure glad to see you.\"]:\n        y = \"Teddy! I'm glad to see you.\"\n    elif x in [\"Sorry, I'm too busy for kids right now.\",\"I don't have time for kids.\"]:\n        y = \"Sorry, I'm too busy for kids right now.\"\n    elif x in [\"Aha! Good catch, Jo.\",\"Good catch!\"]:\n        y = \"Aha! Good catch, Jo.\"\n    elif x in [\"He’s always trying to get you in trouble, and he doesn't like animals!\",\"He's wrong about old shirts and his name rhymes with \\\"smells\\\"...\"]:\n        y = \"He’s always trying to get you in trouble, and he doesn't like animals!\"\n    elif x in [\"See you later, Teddy.\",\"Hang tight, Teddy.\"]:\n        y = \"Hang tight, Teddy.\"\n    elif x in [\"Great. Just great. Could this day get any worse?!\",\"Oh no. If I don't impress the boss soon, I'm gonna get fired!\"]:\n        y = \"Great. Just great. Could this day get any worse?!\"\n    elif x in [\"Are you sure? I know where you can find a real, live badger for the exhibit!\",\"Fine. Then I guess you don't want a real, live badger for the exhibit.\"]:\n        y = \"Are you sure? I know where you can find a real, live badger for the exhibit!\"\n    elif x in [\"It must've been Wells.\",\"I have an idea.\"]:\n        y = \"It must've been Wells.\"\n    elif x in [\"Alright, everybody hold up-\",\"Wait a minute.\"]:\n        y = \"Alright, everybody hold up-\"\n    elif x in [\"You look like you're on a mission.\",\"How can I help you?\"]:\n        y = \"You look like you're on a mission.\"\n    elif x in [\"Calm down, kid. I haven't seen him.\",\"I haven't seen him.\"]:\n        y = \"Calm down, kid. I haven't seen him.\"\n    elif x in [\"A real, live ferret!\",\"Yes!!!\"]:\n        y = \"A real, live ferret!\"\n    elif x in [\"Ooh, I like clues!\",\"Sure!\",\"Do I have to?\"]:\n        y = \"Ooh, I like clues!\"\n    elif x in [\"Okay. Thanks anyway.\",\"Yeah. Thanks anyway.\"]:\n        y = \"Okay. Thanks anyway.\"\n    elif x in [\"The boss is gonna love it!!!\",\"The boss is gonna love it!\"]:\n        y = \"The boss is gonna love it!!!\"\n    elif x in [\"I'll ride with you!\",\"Can I ride with you?\"]:\n        y = \"I'll ride with you!\"\n    elif x in [\"Hot Dog! I knew it!\",\"I knew it!\"]:\n        y = \"Hot Dog! I knew it!\"\n    elif x in [\"I found a coffee cup from Bean Town!\",\"I found a coffee cup from Bean Town.\"]:\n        y = \"I found a coffee cup from Bean Town!\"\n    else:\n        y = x\n    \n    return y","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:20:47.065003Z","iopub.execute_input":"2023-06-23T05:20:47.065376Z","iopub.status.idle":"2023-06-23T05:20:47.108997Z","shell.execute_reply.started":"2023-06-23T05:20:47.065321Z","shell.execute_reply":"2023-06-23T05:20:47.107775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CATS = ['event_name', 'name','fqid', 'room_fqid', 'text_fqid']\n\nevent_name_feature = ['cutscene_click', 'person_click', 'navigate_click',\n       'observation_click', 'notification_click', 'object_click',\n       'object_hover', 'map_hover', 'map_click', 'checkpoint',\n       'notebook_click']\n\nname_feature = ['basic', 'undefined', 'close', 'open', 'prev', 'next']\n\nNUMS = [ \n        'page', \n        'room_coor_x', \n        'room_coor_y', \n        'screen_coor_x', \n        'screen_coor_y', \n        'hover_duration', \n        'elapsed_time_diff']\n\npage = [0, 1, 2, 3, 4, 5, 6]","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:20:47.110730Z","iopub.execute_input":"2023-06-23T05:20:47.111614Z","iopub.status.idle":"2023-06-23T05:20:47.119260Z","shell.execute_reply.started":"2023-06-23T05:20:47.111552Z","shell.execute_reply":"2023-06-23T05:20:47.118133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_dict = pickle.load(open('/kaggle/input/game-play-dart-files/unique_dict_dart_70203_th_635.pkl', 'rb'))\n\n# fqid的枚举项\nfqid_feature1 = unique_dict['fqid_feature1']\nfqid_feature2 = unique_dict['fqid_feature2']\nfqid_feature3 = unique_dict['fqid_feature3']\n# room_fqid的枚举项\nroom_fqid_feature1 = unique_dict['room_fqid_feature1']\nroom_fqid_feature2 = unique_dict['room_fqid_feature2']\nroom_fqid_feature3 = unique_dict['room_fqid_feature3']\n# text_fqid的枚举项\ntext_fqid_feature1 = unique_dict['text_fqid_feature1']\ntext_fqid_feature2 = unique_dict['text_fqid_feature2']\ntext_fqid_feature3 = unique_dict['text_fqid_feature3']\n# level的枚举项\nlevel_feature1 = unique_dict['level_feature1']\nlevel_feature2 = unique_dict['level_feature2']\nlevel_feature3 = unique_dict['level_feature3']\n# text的枚举项\ntext_feature1 = unique_dict['text_feature1']\ntext_feature2 = unique_dict['text_feature2']\ntext_feature3 = unique_dict['text_feature3']\n\n# object_click行为下，room_fqid的枚举项\nobject_click_feature1 = unique_dict['object_click_feature1']\nobject_click_feature2 = unique_dict['object_click_feature2']\nobject_click_feature3 = unique_dict['object_click_feature3']\n# navigate_click行为下，room_fqid的枚举项\nnavigate_click_none_feature1 = unique_dict['navigate_click_none_feature1']\nnavigate_click_none_feature2 = unique_dict['navigate_click_none_feature2']\nnavigate_click_none_feature3 = unique_dict['navigate_click_none_feature3']\n\nnavigate_click_notnone_feature1 = unique_dict['navigate_click_notnone_feature1']\nnavigate_click_notnone_feature2 = unique_dict['navigate_click_notnone_feature2']\nnavigate_click_notnone_feature3 = unique_dict['navigate_click_notnone_feature3']\n# person_click行为下，fqid的枚举项\nperson_click_feature1 = unique_dict['person_click_feature1']\nperson_click_feature2 = unique_dict['person_click_feature2']\nperson_click_feature3 = unique_dict['person_click_feature3']\n# navigate_click行为下，level的枚举项\n# object_hover行为下，room_fqid的枚举项\nobject_hover_feature1 = unique_dict['object_hover_feature1']\nobject_hover_feature2 = unique_dict['object_hover_feature2']\nobject_hover_feature3 = unique_dict['object_hover_feature3']","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:20:47.122270Z","iopub.execute_input":"2023-06-23T05:20:47.122788Z","iopub.status.idle":"2023-06-23T05:20:47.147116Z","shell.execute_reply.started":"2023-06-23T05:20:47.122749Z","shell.execute_reply":"2023-06-23T05:20:47.145802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_engineer(x, grp, feature_suffix):\n\n    if grp=='0-4':\n        fqid_feature = fqid_feature1\n        room_fqid_feature = room_fqid_feature1\n        text_fqid_feature = text_fqid_feature1\n        level_feature = level_feature1\n        text_feature = text_feature1\n        object_click_feature = object_click_feature1\n        navigate_click_none_feature = navigate_click_none_feature1\n        navigate_click_notnone_feature = navigate_click_notnone_feature1\n        person_click_feature = person_click_feature1\n        object_hover_feature = object_hover_feature1\n    elif grp=='5-12':\n        fqid_feature = fqid_feature2\n        room_fqid_feature = room_fqid_feature2\n        text_fqid_feature = text_fqid_feature2\n        level_feature = level_feature2\n        text_feature = text_feature2\n        object_click_feature = object_click_feature2\n        navigate_click_none_feature = navigate_click_none_feature2\n        navigate_click_notnone_feature = navigate_click_notnone_feature2\n        person_click_feature = person_click_feature2\n        object_hover_feature = object_hover_feature2\n    elif grp=='13-22':\n        fqid_feature = fqid_feature3\n        room_fqid_feature = room_fqid_feature3\n        text_fqid_feature = text_fqid_feature3\n        level_feature = level_feature3\n        text_feature = text_feature3\n        object_click_feature = object_click_feature3\n        navigate_click_none_feature = navigate_click_none_feature3\n        navigate_click_notnone_feature = navigate_click_notnone_feature3\n        person_click_feature = person_click_feature3\n        object_hover_feature = object_hover_feature3\n        \n    aggs = [\n        #pl.col(\"origin\").median().alias(f\"origin\"),\n        pl.col(\"index\").count().alias(f\"session_number_{feature_suffix}\"),\n        \n        *[pl.col(c).drop_nulls().n_unique().alias(f\"{c}_unique_{feature_suffix}\") for c in CATS],\n        *[pl.col(c).mean().alias(f\"{c}_mean_{feature_suffix}\") for c in NUMS],\n        *[pl.col(c).std().alias(f\"{c}_std_{feature_suffix}\") for c in NUMS],\n        *[pl.col(c).min().alias(f\"{c}_min_{feature_suffix}\") for c in NUMS],\n        *[pl.col(c).max().alias(f\"{c}_max_{feature_suffix}\") for c in NUMS],\n        \n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"event_name\")==c).max().alias(f\"{c}_ET_max_{feature_suffix}\") for c in event_name_feature],\n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"event_name\")==c).count().alias(f\"{c}_ET_count_{feature_suffix}\") for c in event_name_feature],\n        *[((pl.col('event_name') == c) * pl.col('elapsed_time_diff')).sum().alias(f'{c}_ET_sum_{feature_suffix}').cast(pl.Float32) / 1000 for c in event_name_feature],\n        \n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"name\")==c).max().alias(f\"{c}_ET_max_{feature_suffix}\") for c in name_feature],\n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"name\")==c).count().alias(f\"{c}_ET_count_{feature_suffix}\") for c in name_feature],\n        *[((pl.col('name') == c) * pl.col('elapsed_time_diff')).sum().alias(f'{c}_ET_sum_{feature_suffix}').cast(pl.Float32) / 1000 for c in name_feature],\n        \n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"fqid\")==c).max().alias(f\"{c}_ET_max_{feature_suffix}\") for c in fqid_feature],\n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"fqid\")==c).count().alias(f\"{c}_ET_count_{feature_suffix}\") for c in fqid_feature],\n        *[((pl.col('fqid') == c) * pl.col('elapsed_time_diff')).sum().alias(f'{c}_ET_sum_{feature_suffix}').cast(pl.Float32) / 1000 for c in fqid_feature],\n        \n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"room_fqid\")==c).max().alias(f\"{c}_ET_max_{feature_suffix}\") for c in room_fqid_feature],\n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"room_fqid\")==c).count().alias(f\"{c}_ET_count_{feature_suffix}\") for c in room_fqid_feature],\n        *[((pl.col('room_fqid') == c) * pl.col('elapsed_time_diff')).sum().alias(f'{c}_ET_sum_{feature_suffix}').cast(pl.Float32) / 1000 for c in room_fqid_feature],\n        \n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"text_fqid\")==c).max().alias(f\"{c}_ET_max_{feature_suffix}\") for c in text_fqid_feature],\n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"text_fqid\")==c).count().alias(f\"{c}_ET_count_{feature_suffix}\") for c in text_fqid_feature],\n        *[((pl.col('text_fqid') == c) * pl.col('elapsed_time_diff')).sum().alias(f'{c}_ET_sum_{feature_suffix}').cast(pl.Float32) / 1000 for c in text_fqid_feature],\n        \n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"text\")==c).max().alias(f\"{c}_ET_max_{feature_suffix}\") for c in text_feature],\n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"text\")==c).count().alias(f\"{c}_ET_count_{feature_suffix}\") for c in text_feature],\n        *[((pl.col('text') == c) * pl.col('elapsed_time_diff')).sum().alias(f'{c}_ET_sum_{feature_suffix}').cast(pl.Float32) / 1000 for c in text_feature],\n        \n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"level\")==c).max().alias(f\"level{c}_ET_max_{feature_suffix}\") for c in level_feature],\n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"level\")==c).count().alias(f\"level{c}_ET_count_{feature_suffix}\") for c in level_feature],\n        \n        *[((pl.col('page') == c) * pl.col('elapsed_time_diff')).sum().alias(f'page{c}_ET_sum_{feature_suffix}').cast(pl.Float32) / 1000 for c in page],\n        ###############\n        *[pl.col(\"elapsed_time_diff\").filter(pl.col(\"page\")==c).count().alias(f\"page{c}_ET_count_{feature_suffix}\") for c in page],\n        \n        pl.col(\"room_fqid_is_change\").sum().alias(f\"room_change_count_{feature_suffix}\"),\n        pl.col(\"room_fqid_is_change\").mean().alias(f\"room_change_mean_{feature_suffix}\"),\n        *[((pl.col('level') == c) * pl.col('room_fqid_is_change')).sum().alias(f\"room_change_level{c}_count_{feature_suffix}\") for c in level_feature],\n        *[pl.col(\"room_fqid_is_change\").filter(pl.col(\"level\")==c).mean().alias(f\"room_change_level{c}_mean_{feature_suffix}\") for c in level_feature],\n        \n        #############################################\n        pl.col(\"text_fqid_is_change\").sum().alias(f\"text_fqid_change_count_{feature_suffix}\"),\n        pl.col(\"text_fqid_is_change\").mean().alias(f\"text_fqid_change_mean_{feature_suffix}\"),\n        *[((pl.col('level') == c) * pl.col('text_fqid_is_change')).sum().alias(f\"text_fqid_change_level{c}_count_{feature_suffix}\") for c in level_feature],\n        *[pl.col(\"text_fqid_is_change\").filter(pl.col(\"level\")==c).mean().alias(f\"text_fqid_change_level{c}_mean_{feature_suffix}\") for c in level_feature],\n        \n        pl.col(\"fqid_is_change\").sum().alias(f\"fqid_change_count_{feature_suffix}\"),\n        pl.col(\"fqid_is_change\").mean().alias(f\"fqid_change_mean_{feature_suffix}\"),\n        *[((pl.col('level') == c) * pl.col('fqid_is_change')).sum().alias(f\"fqid_change_level{c}_count_{feature_suffix}\") for c in level_feature],\n        *[pl.col(\"fqid_is_change\").filter(pl.col(\"level\")==c).mean().alias(f\"fqid_change_level{c}_mean_{feature_suffix}\") for c in level_feature],\n        \n        *[pl.col(\"hover_duration\").filter((pl.col(\"fqid\")==c)&(pl.col(\"event_name\")=='object_hover')).mean().alias(f\"{c}_object_hover_{feature_suffix}\") for c in object_hover_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"room_fqid\")==c)&(pl.col(\"event_name\")=='object_click')).count().alias(f\"{c}_OC_count_{feature_suffix}\") for c in object_click_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"room_fqid\")==c)&(pl.col(\"event_name\")=='object_click')).sum().alias(f\"{c}_OC_sum_{feature_suffix}\") for c in object_click_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"level\")==c)&(pl.col(\"event_name\")=='object_click')).count().alias(f\"level{c}_OC_count_{feature_suffix}\") for c in level_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"level\")==c)&(pl.col(\"event_name\")=='object_click')).sum().alias(f\"level{c}_OC_sum_{feature_suffix}\") for c in level_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"room_fqid\")==c)&(pl.col(\"event_name\")=='navigate_click')&(pl.col(\"fqid\")=='fqid_None')).count().alias(f\"{c}_NC_none_count_{feature_suffix}\") for c in navigate_click_none_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"room_fqid\")==c)&(pl.col(\"event_name\")=='navigate_click')&(pl.col(\"fqid\")=='fqid_None')).sum().alias(f\"{c}_NC_none_sum_{feature_suffix}\").cast(pl.Float32) / 1000 for c in navigate_click_none_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"level\")==c)&(pl.col(\"event_name\")=='navigate_click')&(pl.col(\"fqid\")=='fqid_None')).count().alias(f\"level{c}_NC_none_count_{feature_suffix}\") for c in level_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"level\")==c)&(pl.col(\"event_name\")=='navigate_click')&(pl.col(\"fqid\")=='fqid_None')).sum().alias(f\"level{c}_NC_none_sum_{feature_suffix}\").cast(pl.Float32) / 1000 for c in level_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"room_fqid\")==c)&(pl.col(\"event_name\")=='navigate_click')&(pl.col(\"fqid\")!='fqid_None')).count().alias(f\"{c}_NC_notnone_count_{feature_suffix}\") for c in navigate_click_notnone_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"room_fqid\")==c)&(pl.col(\"event_name\")=='navigate_click')&(pl.col(\"fqid\")!='fqid_None')).sum().alias(f\"{c}_NC_notnone_sum_{feature_suffix}\") for c in navigate_click_notnone_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"level\")==c)&(pl.col(\"event_name\")=='navigate_click')&(pl.col(\"fqid\")!='fqid_None')).count().alias(f\"level{c}_NC_notnone_count_{feature_suffix}\") for c in level_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"level\")==c)&(pl.col(\"event_name\")=='navigate_click')&(pl.col(\"fqid\")!='fqid_None')).sum().alias(f\"level{c}_NC_notnone_sum_{feature_suffix}\") for c in level_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"fqid\")==c)&(pl.col(\"event_name\")=='person_click')).count().alias(f\"{c}_PC_count_{feature_suffix}\") for c in person_click_feature],\n        *[pl.col(\"elapsed_time_diff\").filter((pl.col(\"fqid\")==c)&(pl.col(\"event_name\")=='person_click')).sum().alias(f\"{c}_PC_sum_{feature_suffix}\") for c in person_click_feature],\n        *[pl.col(\"hover_duration\").filter((pl.col(\"room_fqid\")==c)&(pl.col(\"event_name\")=='object_hover')).count().alias(f\"{c}_OH_count_{feature_suffix}\") for c in object_hover_feature],\n        *[pl.col(\"hover_duration\").filter((pl.col(\"room_fqid\")==c)&(pl.col(\"event_name\")=='object_hover')).sum().alias(f\"{c}_OH_sum_{feature_suffix}\") for c in object_hover_feature],\n        \n    ]\n    \n    df = x.groupby([\"session_id\"], maintain_order=True).agg(aggs).sort(\"session_id\")\n    \n    df = df.with_columns([\n        df['session_id'].cast(str).apply(lambda x: str(x)[:2]+'-'+str(x)[2:4]).alias('date'),\n        df['session_id'].cast(str).apply(lambda x: str(x)[:2]).cast(pl.Int32).alias('year'),\n        (df['session_id'].cast(str).apply(lambda x: str(x)[2:4]).cast(pl.Int32)+1).alias('month'),\n        (df['session_id'].cast(str).apply(lambda x: str(x)[4:6]).cast(pl.Int32)+1).alias('weekday'),\n        (df['session_id'].cast(str).apply(lambda x: str(x)[6:8]).cast(pl.Int32)+1).alias('hour'),\n    ])\n\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:20:47.148948Z","iopub.execute_input":"2023-06-23T05:20:47.149303Z","iopub.status.idle":"2023-06-23T05:20:47.228173Z","shell.execute_reply.started":"2023-06-23T05:20:47.149268Z","shell.execute_reply":"2023-06-23T05:20:47.227132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dart_features = pickle.load(open('/kaggle/input/game-play-dart-files/used_features_dart_70203_th_635.pkl', 'rb'))\nbest_threshold = 0.630\nfeature_transforms = pickle.load(open('/kaggle/input/game-play-dart-files/feature_transfrom_70203_th_635.pkl', 'rb'))\n\nFEATURES1_transform = feature_transforms['0-4']\nFEATURES2_transform = feature_transforms['5-12']\nFEATURES3_transform = feature_transforms['13-22']\n\nn_folds = 5\ndisplay(best_threshold)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:20:47.229902Z","iopub.execute_input":"2023-06-23T05:20:47.230239Z","iopub.status.idle":"2023-06-23T05:20:47.259034Z","shell.execute_reply.started":"2023-06-23T05:20:47.230205Z","shell.execute_reply":"2023-06-23T05:20:47.258057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dart_models = {}\nfor grp in ['0-4','5-12','13-22']:\n    for i in range(n_folds):\n        dart_models[f\"{grp}_fold_{i}\"] = joblib.load(f\"/kaggle/input/game-play-dart-models/dart_70203/grp{grp}/dart_fold{i}_seed42.pkl\")","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:20:51.632585Z","iopub.execute_input":"2023-06-23T05:20:51.633564Z","iopub.status.idle":"2023-06-23T05:21:03.447853Z","shell.execute_reply.started":"2023-06-23T05:20:51.633519Z","shell.execute_reply":"2023-06-23T05:21:03.446857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import jo_wilder\nenv = jo_wilder.make_env()\njo_wilder.make_env.func_dict['__called__'] = False\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:21:26.382030Z","iopub.execute_input":"2023-06-23T05:21:26.382466Z","iopub.status.idle":"2023-06-23T05:21:26.409758Z","shell.execute_reply.started":"2023-06-23T05:21:26.382425Z","shell.execute_reply":"2023-06-23T05:21:26.408652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nhistory = {}\nlevel_predict_result = {}\n\nfor (test, sample_submission) in iter_test:\n    grp = test.level_group.values[0]\n    session_id = test.session_id.values[0]\n\n    columns = [\n        pl.col(\"page\").cast(pl.Float32),\n        (\n            (pl.col(\"elapsed_time\") - pl.col(\"elapsed_time\").shift(1))\n             .fill_null(0)\n             .clip(0, 1e9)\n             .over([\"session_id\", \"level_group\"])\n             .alias(\"elapsed_time_diff\")\n        ),\n        (\n            (pl.col(\"room_fqid\") != pl.col(\"room_fqid\").shift(1).fill_null(strategy=\"backward\"))\n             .cast(pl.Int8)\n             .over([\"session_id\", \"level_group\",\"level\"])\n             .alias(\"room_fqid_is_change\")\n        ),\n        (\n            (pl.col(\"fqid\") != pl.col(\"fqid\").shift(1).fill_null(strategy=\"backward\"))\n             .cast(pl.Int8)\n             .over([\"session_id\", \"level_group\",\"level\"])\n             .alias(\"fqid_is_change\")\n        ),\n        (\n            (pl.col(\"text_fqid\") != pl.col(\"text_fqid\").shift(1).fill_null(strategy=\"backward\"))\n             .cast(pl.Int8)\n             .over([\"session_id\", \"level_group\",\"level\"])\n             .alias(\"text_fqid_is_change\")\n        ),\n        pl.col(\"fqid\").fill_null(\"fqid_None\"),\n        pl.col(\"text_fqid\").fill_null(\"text_fqid_None\"),\n        pl.col(\"text\").apply(trans_str),\n        #pl.lit(0).alias(\"origin\")\n    ]\n\n    test = (pl.from_pandas(test)\n          .drop([\"fullscreen\", \"hq\", \"music\"])\n          .sort(by=\"elapsed_time\")\n          .with_columns(columns))\n\n    # INFER TEST DATA       \n    if grp=='0-4':\n        history[session_id] = test\n        test_o = feature_engineer(test, grp=grp, feature_suffix='L1').to_pandas()\n        test_o = test_o.rename(columns=FEATURES1_transform)\n        \n    elif grp=='5-12':\n        test_o = pl.concat([history[session_id], test], how=\"vertical\")\n        history[session_id] = test_o\n        test_o = feature_engineer(test_o, grp=grp, feature_suffix='L2').to_pandas()\n        test_o = test_o.rename(columns=FEATURES2_transform)\n        \n        test_o['level_1_meta'] = level_predict_result[f\"{session_id}_L1\"]\n\n    elif grp=='13-22':\n        test_o = pl.concat([history[session_id], test], how=\"vertical\")\n        del history[session_id]\n        gc.collect()\n        test_o = feature_engineer(test_o, grp=grp, feature_suffix='L3').to_pandas()\n        test_o = test_o.rename(columns=FEATURES3_transform)\n        \n        test_o['level_1_meta'] = level_predict_result[f\"{session_id}_L1\"]\n        test_o['level_2_meta'] = level_predict_result[f\"{session_id}_L2\"]\n        #######\n        test_o['level_12_meta_per'] = test_o['level_2_meta'] / test_o['level_1_meta']\n        \n    sample_submission['q'] = sample_submission['session_id'].apply(lambda x: int(x.split('_')[-1][1:]))\n    sample_submission['session_name'] = sample_submission['session_id'].apply(lambda x: int(x.split('_')[0]))\n    \n    test_o = test_o.rename(columns={'session_id': 'session_name'})\n    sample_submission = pd.merge(sample_submission, test_o, how='left', on='session_name')\n    sample_submission['correct'] = 0\n    \n    dart_f = dart_features[grp]\n        \n    for i in range(n_folds):\n        dart_m = dart_models[f'{grp}_fold_{i}']\n        sample_submission['correct'] += dart_m.predict(sample_submission[dart_f].astype('float32').values) / n_folds\n        \n    if grp=='0-4':\n        level_predict_result[f\"{session_id}_L1\"] = sample_submission.loc[sample_submission.q.isin([2,3]), 'correct'].mean()\n    elif grp=='5-12':\n        level_predict_result[f\"{session_id}_L2\"] = sample_submission['correct'].mean()\n        \n    ################################################\n    sample_submission = sample_submission.sort_values(by='q')\n    #print(f\"session_id: {session_id} grp {grp} has done.\")\n\n    sample_submission['correct'] = (sample_submission['correct'] > best_threshold).astype(int)\n    env.predict(sample_submission[['session_id','correct']])","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:21:35.075788Z","iopub.execute_input":"2023-06-23T05:21:35.076177Z","iopub.status.idle":"2023-06-23T05:21:38.110467Z","shell.execute_reply.started":"2023-06-23T05:21:35.076141Z","shell.execute_reply":"2023-06-23T05:21:38.107990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('submission.csv')\ndisplay(sub)\ndisplay(sub['correct'].mean())","metadata":{"execution":{"iopub.status.busy":"2023-06-23T05:21:48.173866Z","iopub.execute_input":"2023-06-23T05:21:48.174269Z","iopub.status.idle":"2023-06-23T05:21:48.203465Z","shell.execute_reply.started":"2023-06-23T05:21:48.174232Z","shell.execute_reply":"2023-06-23T05:21:48.202576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}