{"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)\nfrom sklearn.model_selection import StratifiedKFold\nimport tensorflow as tf\nimport tensorflow_decision_forests as tfdf\n\nprint(\"Tensorflow version:\", tf.__version__)\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-08-02T07:03:39.090251Z","iopub.execute_input":"2023-08-02T07:03:39.090755Z","iopub.status.idle":"2023-08-02T07:03:43.109567Z","shell.execute_reply.started":"2023-08-02T07:03:39.090725Z","shell.execute_reply":"2023-08-02T07:03:43.108725Z"},"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    '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\nevent_types = ['navigate_click','person_click','cutscene_click','object_click',\n          'map_hover','notification_click','map_click','observation_click',\n          'checkpoint']\n\ndef create_features(train):\n    extra_features = []\n\n    # Make columns for following numerical values, getting their mean for a session_id and level_group\n    # for num in ['Time Spent Per Action', 'level','page','room_coor_x', 'room_coor_y', 'screen_coor_x', 'screen_coor_y', 'hover_duration']:\n    #     tmp = train.groupby(['session_id', 'level_group'])[num].agg('std')\n    #     tmp.name = tmp.name + '_std'\n    #     extra_features.append(tmp)\n    \n    for num in ['Time Spent Per Action', 'room_coor_x', 'room_coor_y', 'screen_coor_x', 'screen_coor_y', 'hover_duration']:\n        tmp = train.groupby(['session_id', 'level_group'])[num].agg('mean')\n        tmp.name = tmp.name + '_mean'\n        extra_features.append(tmp)\n    \n    # Make columns for event types, getting their count for a session_id and level_group\n    for event in event_types:\n        tmp = train.groupby(['session_id', 'level_group'])['event_name'].apply(lambda x: (x == event).sum())\n        tmp.name = event + '_sum'\n        extra_features.append(tmp)\n\n    # Make columns for following numerical values, getting their mean for a session_id and level_group\n    for num in ['event_name', 'name','fqid', 'room_fqid', 'text_fqid']:\n        tmp = train.groupby(['session_id', 'level_group'])[num].agg('nunique')\n        tmp.name = tmp.name + '_nunique'\n        extra_features.append(tmp)\n        \n    # Make columns for each event type elapsed time. getting their mean for a session_id and level_group\n    tmp = train.groupby(['session_id', 'level_group']).apply(lambda x: x[[\"level_group\", \"elapsed_time\"]][\"elapsed_time\"].max())\n    tmp.name = \"elapsed_time_per_level_group\"\n    extra_features.append(tmp)\n\n    train = pd.concat(extra_features, axis=1)\n    train = train.fillna(-1)\n    train = train.reset_index()\n    train = train.set_index('session_id')\n\n    return train\n\ndef outlier_capping(x, upper, lower):\n    if x > upper:\n        return upper\n    if x < lower:\n        return lower\n    else:\n        return x\n    \ndef pre_processing(train):\n    # fill empty numerical values with mean value, and add a dummy column indicating it was NaN\n    for column in ['elapsed_time','level','page','room_coor_x', 'room_coor_y', 'screen_coor_x', 'screen_coor_y', 'hover_duration']:\n        train[column].fillna(train[column].mean(), inplace=True)\n\n    train = train.sort_values(by=['session_id', 'elapsed_time'])\n    train['Time Spent Per Action'] = train.groupby('session_id')['elapsed_time'].diff().fillna(0)    \n    outliers = ['Time Spent Per Action', 'hover_duration']\n    for outlier in outliers:\n        q1, q3 = np.percentile(train[outlier], [25, 75])\n        iqr = q3 - q1\n        upper_boundary = q3 + iqr * 1.5\n        lower_boundary = q1 - iqr * 1.5\n        train[outlier] = train[outlier].apply(lambda x: outlier_capping(x, upper_boundary, lower_boundary))\n    \n    return train\n","metadata":{"execution":{"iopub.status.busy":"2023-08-02T07:03:43.110910Z","iopub.execute_input":"2023-08-02T07:03:43.111795Z","iopub.status.idle":"2023-08-02T07:03:43.131656Z","shell.execute_reply.started":"2023-08-02T07:03:43.111762Z","shell.execute_reply":"2023-08-02T07:03:43.130277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = {}\n\nfor dirname, _ , filenames in os.walk(\"/kaggle/input/d/danielyu01/nn-models/\"):\n    for filename in filenames:\n        if filename.endswith(\".keras\"):\n            models[filename.split('.')[0]] = tf.keras.models.load_model(os.path.join(dirname, filename), compile=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T07:03:43.134151Z","iopub.execute_input":"2023-08-02T07:03:43.134526Z","iopub.status.idle":"2023-08-02T07:03:44.891507Z","shell.execute_reply.started":"2023-08-02T07:03:43.134495Z","shell.execute_reply":"2023-08-02T07:03:44.890640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import jo_wilder_310 as jo_wilder\nenv = jo_wilder.make_env()\niter_test = env.iter_test()\n\nlimits = {'0-4':(1,4), '5-12':(4,14), '13-22':(14,19)}\n\nfor (test, sample_submission) in iter_test:\n    test_df = pre_processing(test)        \n    test_df = create_features(test_df)\n    grp = test_df.level_group.values[0]\n    a,b = limits[grp]\n    for t in range(a,b):\n        model = models[f'nn_{t}']\n        test_ds = test_df.loc[:, test_df.columns != 'level_group']\n#         predictions = model.predict(np.reshape(np.array([1.46586071e+03, 3.94491070e+00, -6.46254507e+01, 4.34962687e+02, 3.74597015e+02, 2.00680000e+03,\n#                                               6.20000000e+01, 2.10000000e+01, 2.70000000e+01, 9.00000000e+00, 2.00000000e+00, 5.00000000e+00,\n#                                               3.00000000e+00, 3.00000000e+00, 1.00000000e+00, 1.10000000e+01, 4.00000000e+00, 2.40000000e+01,\n#                                               6.00000000e+00, 1.30000000e+01, 2.67350000e+05]), (1,-1 )))\n            \n        mask = sample_submission.session_id.str.contains(f'q{t}')\n        try:\n            predictions = model.predict(test_ds)\n            sample_submission.loc[mask,'correct'] = int(predictions[0][0] > 0.63)\n        except Exception:\n            sample_submission.loc[mask,'correct'] = 1\n#         sample_submission.loc[mask,'correct'] = int(predictions[0][0] > 0.63)\n    \n    env.predict(sample_submission)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T07:03:44.896266Z","iopub.execute_input":"2023-08-02T07:03:44.896865Z","iopub.status.idle":"2023-08-02T07:03:50.249407Z","shell.execute_reply.started":"2023-08-02T07:03:44.896825Z","shell.execute_reply":"2023-08-02T07:03:50.248339Z"},"trusted":true},"execution_count":null,"outputs":[]}]}