{"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":"markdown","source":"# Table of Contents\n* [Import first slice of Training Data](#import)\n* [Admin Variables](#admin)\n* [Categorical Features](#categorical)\n* [Ball Position](#ball_pos)\n* [Ball Velocity](#ball_vel)\n* [Players](#players)\n* [Boost](#boost)\n* [Feature Engineering](#feature_eng)\n* [Targets](#targets)\n* [Drill down to individual Game](#game)\n* [Model for Target 1](#model1)\n* [Model for Target 2](#model2)\n* [Predict on Test Set](#test)","metadata":{}},{"cell_type":"code","source":"# packages\n\n# standard\nimport pandas as pd\nimport numpy as np\nimport time\n\n# plots\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport seaborn as sns\n\n# machine learning tools\nimport h2o\nfrom h2o.estimators import H2OGradientBoostingEstimator\n\n# other stuff\nimport gc # garbage collection","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-04T09:59:19.856661Z","iopub.execute_input":"2022-10-04T09:59:19.857535Z","iopub.status.idle":"2022-10-04T09:59:22.570095Z","shell.execute_reply.started":"2022-10-04T09:59:19.857436Z","shell.execute_reply":"2022-10-04T09:59:22.568928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# notebook options\npd.set_option('display.max_columns', 500)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T09:59:22.572053Z","iopub.execute_input":"2022-10-04T09:59:22.572706Z","iopub.status.idle":"2022-10-04T09:59:22.579451Z","shell.execute_reply.started":"2022-10-04T09:59:22.572671Z","shell.execute_reply":"2022-10-04T09:59:22.576116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# file overview\n!ls -l '../input/tabular-playground-series-oct-2022/'","metadata":{"execution":{"iopub.status.busy":"2022-10-04T09:59:22.581757Z","iopub.execute_input":"2022-10-04T09:59:22.582442Z","iopub.status.idle":"2022-10-04T09:59:23.756478Z","shell.execute_reply.started":"2022-10-04T09:59:22.582392Z","shell.execute_reply":"2022-10-04T09:59:23.755174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='import'></a>\n# Import first slice of Training Data","metadata":{}},{"cell_type":"code","source":"# get data types first (as described in the documentation)\ndtypes_df = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_dtypes.csv')\ndtypes = {k: v for (k, v) in zip(dtypes_df.column, dtypes_df.dtype)}\ndtypes","metadata":{"execution":{"iopub.status.busy":"2022-10-04T09:59:24.742177Z","iopub.execute_input":"2022-10-04T09:59:24.742616Z","iopub.status.idle":"2022-10-04T09:59:24.774663Z","shell.execute_reply.started":"2022-10-04T09:59:24.742577Z","shell.execute_reply":"2022-10-04T09:59:24.773876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load FIRST training set according to given data types\nt1 = time.time()\ndf = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_0.csv', dtype=dtypes)\nt2 = time.time()\nprint('Elapsed time [s]:', np.round(t2-t1,4))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T09:59:27.213172Z","iopub.execute_input":"2022-10-04T09:59:27.213583Z","iopub.status.idle":"2022-10-04T10:00:05.730931Z","shell.execute_reply.started":"2022-10-04T09:59:27.213551Z","shell.execute_reply":"2022-10-04T10:00:05.729552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# first glance\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:00:05.732793Z","iopub.execute_input":"2022-10-04T10:00:05.733117Z","iopub.status.idle":"2022-10-04T10:00:05.798392Z","shell.execute_reply.started":"2022-10-04T10:00:05.733089Z","shell.execute_reply":"2022-10-04T10:00:05.797201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# structure of training data\ndf.info(show_counts=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:00:56.143313Z","iopub.execute_input":"2022-10-04T10:00:56.148735Z","iopub.status.idle":"2022-10-04T10:00:57.362885Z","shell.execute_reply.started":"2022-10-04T10:00:56.148495Z","shell.execute_reply":"2022-10-04T10:00:57.361429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='admin'></a>\n# Admin Variables","metadata":{}},{"cell_type":"code","source":"# frequencies + plot\nfor f in ['game_num','event_id']:\n    print(f + ':')\n    print(df[f].value_counts())\n    plt.figure(figsize=(12,4))\n    df[f].value_counts()[0:20].plot(kind='bar')\n    plt.title(f + ' - Top 20')\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:00.306292Z","iopub.execute_input":"2022-10-04T10:01:00.307085Z","iopub.status.idle":"2022-10-04T10:01:01.066313Z","shell.execute_reply.started":"2022-10-04T10:01:00.307047Z","shell.execute_reply":"2022-10-04T10:01:01.064919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='categorical'></a>\n# Categorical Features","metadata":{}},{"cell_type":"code","source":"# frequencies + plot\nfor f in ['player_scoring_next','team_scoring_next']:\n    print(f + ':')\n    print(df[f].value_counts())\n    plt.figure(figsize=(12,4))\n    df[f].value_counts()[0:20].plot(kind='bar')\n    plt.title(f + ' - Top 20')\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:03.535187Z","iopub.execute_input":"2022-10-04T10:01:03.535651Z","iopub.status.idle":"2022-10-04T10:01:04.424739Z","shell.execute_reply.started":"2022-10-04T10:01:03.535614Z","shell.execute_reply":"2022-10-04T10:01:04.423571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Please note that none of these two features is available in test set!","metadata":{}},{"cell_type":"markdown","source":"<a id='ball_pos'></a>\n# Ball Position","metadata":{}},{"cell_type":"code","source":"ball_pos = ['ball_pos_x','ball_pos_y','ball_pos_z']\ndf[ball_pos].describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:07.660121Z","iopub.execute_input":"2022-10-04T10:01:07.660568Z","iopub.status.idle":"2022-10-04T10:01:07.893997Z","shell.execute_reply.started":"2022-10-04T10:01:07.660528Z","shell.execute_reply":"2022-10-04T10:01:07.893058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scatter plots\nsns.pairplot(df[ball_pos], kind='scatter',             \n             plot_kws={'s' : 1,\n                       'alpha' : 0.01})\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:10.204579Z","iopub.execute_input":"2022-10-04T10:01:10.205380Z","iopub.status.idle":"2022-10-04T10:01:19.393037Z","shell.execute_reply.started":"2022-10-04T10:01:10.205331Z","shell.execute_reply":"2022-10-04T10:01:19.391898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# correlation ball position\ncorr_pearson = df[ball_pos].corr(method='pearson')\ncorr_spearman = df[ball_pos].corr(method='spearman')\n\nplt.figure(figsize=(12,4))\nax1 = plt.subplot(1,2,1)\nsns.heatmap(corr_pearson, annot=True, cmap='RdYlGn', vmin=-1, vmax=+1)\nplt.title('Ball Position - Pearson Corr.')\n\nax2 = plt.subplot(1,2,2, sharex=ax1)\nsns.heatmap(corr_spearman, annot=True, cmap='RdYlGn', vmin=-1, vmax=+1)\nplt.title('Ball Position - Spearman Corr.')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:23.619218Z","iopub.execute_input":"2022-10-04T10:01:23.620037Z","iopub.status.idle":"2022-10-04T10:01:25.808000Z","shell.execute_reply.started":"2022-10-04T10:01:23.619998Z","shell.execute_reply":"2022-10-04T10:01:25.806648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='ball_vel'></a>\n# Ball Velocity","metadata":{}},{"cell_type":"code","source":"ball_vel = ['ball_vel_x','ball_vel_y','ball_vel_z']\ndf[ball_vel].describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:28.003766Z","iopub.execute_input":"2022-10-04T10:01:28.004190Z","iopub.status.idle":"2022-10-04T10:01:28.228999Z","shell.execute_reply.started":"2022-10-04T10:01:28.004149Z","shell.execute_reply":"2022-10-04T10:01:28.227688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scatter plots\nsns.pairplot(df[ball_vel], kind='scatter',             \n             plot_kws={'s' : 1,\n                       'alpha' : 0.01})\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:30.630096Z","iopub.execute_input":"2022-10-04T10:01:30.630891Z","iopub.status.idle":"2022-10-04T10:01:41.381412Z","shell.execute_reply.started":"2022-10-04T10:01:30.630839Z","shell.execute_reply":"2022-10-04T10:01:41.380273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# correlation ball velocity\ncorr_pearson = df[ball_vel].corr(method='pearson')\ncorr_spearman = df[ball_vel].corr(method='spearman')\n\nplt.figure(figsize=(12,4))\nax1 = plt.subplot(1,2,1)\nsns.heatmap(corr_pearson, annot=True, cmap='RdYlGn', vmin=-1, vmax=+1)\nplt.title('Ball Velocity - Pearson Corr.')\nax2 = plt.subplot(1,2,2, sharex=ax1)\nsns.heatmap(corr_spearman, annot=True, cmap='RdYlGn', vmin=-1, vmax=+1)\nplt.title('Ball Velocity - Spearman Corr.')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:44.542388Z","iopub.execute_input":"2022-10-04T10:01:44.542798Z","iopub.status.idle":"2022-10-04T10:01:46.604792Z","shell.execute_reply.started":"2022-10-04T10:01:44.542766Z","shell.execute_reply":"2022-10-04T10:01:46.603458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='players'></a>\n# Players","metadata":{}},{"cell_type":"code","source":"players = ['p0_pos_x', 'p0_pos_y', 'p0_pos_z', 'p0_vel_x', 'p0_vel_y', 'p0_vel_z', 'p0_boost',\n           'p1_pos_x', 'p1_pos_y', 'p1_pos_z', 'p1_vel_x', 'p1_vel_y', 'p1_vel_z', 'p1_boost',\n           'p2_pos_x', 'p2_pos_y', 'p2_pos_z', 'p2_vel_x', 'p2_vel_y', 'p2_vel_z', 'p2_boost',\n           'p3_pos_x', 'p3_pos_y', 'p3_pos_z', 'p3_vel_x', 'p3_vel_y', 'p3_vel_z', 'p3_boost',\n           'p4_pos_x', 'p4_pos_y', 'p4_pos_z', 'p4_vel_x', 'p4_vel_y', 'p4_vel_z', 'p4_boost',\n           'p5_pos_x', 'p5_pos_y', 'p5_pos_z', 'p5_vel_x', 'p5_vel_y', 'p5_vel_z', 'p5_boost']\ndf[players].describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:47.166401Z","iopub.execute_input":"2022-10-04T10:01:47.167121Z","iopub.status.idle":"2022-10-04T10:01:52.313947Z","shell.execute_reply.started":"2022-10-04T10:01:47.167084Z","shell.execute_reply":"2022-10-04T10:01:52.312481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# correlation of players' data\ncorr_pearson = df[players].corr(method='pearson')\n\nplt.figure(figsize=(12,10))\nsns.heatmap(corr_pearson, annot=False, cmap='RdYlGn',\n            vmin=-1, vmax=+1)\nplt.title('Player Data - Pearson Corr.')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:01:52.316238Z","iopub.execute_input":"2022-10-04T10:01:52.316611Z","iopub.status.idle":"2022-10-04T10:02:04.204668Z","shell.execute_reply.started":"2022-10-04T10:01:52.316569Z","shell.execute_reply":"2022-10-04T10:02:04.203459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='boost'></a>\n# Boost","metadata":{}},{"cell_type":"code","source":"boost = ['boost0_timer', 'boost1_timer', 'boost2_timer',\n         'boost3_timer', 'boost4_timer', 'boost5_timer']\ndf[boost].describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:02:04.206567Z","iopub.execute_input":"2022-10-04T10:02:04.206966Z","iopub.status.idle":"2022-10-04T10:02:05.709860Z","shell.execute_reply.started":"2022-10-04T10:02:04.206929Z","shell.execute_reply":"2022-10-04T10:02:05.708996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show individual distributions \nfor f in boost:\n    df[f].plot(kind='hist')\n    plt.title(f)\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:02:05.711709Z","iopub.execute_input":"2022-10-04T10:02:05.712880Z","iopub.status.idle":"2022-10-04T10:02:10.818558Z","shell.execute_reply.started":"2022-10-04T10:02:05.712827Z","shell.execute_reply":"2022-10-04T10:02:10.817293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scatter plots\nsns.pairplot(df[boost], kind='scatter',             \n             plot_kws={'s' : 1,\n                       'alpha' : 0.01})\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:02:10.820243Z","iopub.execute_input":"2022-10-04T10:02:10.821422Z","iopub.status.idle":"2022-10-04T10:02:55.816249Z","shell.execute_reply.started":"2022-10-04T10:02:10.821384Z","shell.execute_reply":"2022-10-04T10:02:55.814957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='feature_eng'></a>\n# Feature Engineering","metadata":{}},{"cell_type":"code","source":"# add absolute speeds of ball and players\ndf['ball_speed'] = np.sqrt(df.ball_vel_x**2 + df.ball_vel_y**2 + df.ball_vel_z**2)\ndf['p0_speed'] = np.sqrt(df.p0_vel_x**2 + df.p0_vel_y**2 + df.p0_vel_z**2)\ndf['p1_speed'] = np.sqrt(df.p1_vel_x**2 + df.p1_vel_y**2 + df.p1_vel_z**2)\ndf['p2_speed'] = np.sqrt(df.p2_vel_x**2 + df.p2_vel_y**2 + df.p2_vel_z**2)\ndf['p3_speed'] = np.sqrt(df.p3_vel_x**2 + df.p3_vel_y**2 + df.p3_vel_z**2)\ndf['p4_speed'] = np.sqrt(df.p4_vel_x**2 + df.p4_vel_y**2 + df.p4_vel_z**2)\ndf['p5_speed'] = np.sqrt(df.p5_vel_x**2 + df.p5_vel_y**2 + df.p5_vel_z**2)\n\nspeeds = ['ball_speed','p0_speed','p1_speed','p2_speed',\n          'p3_speed','p4_speed','p5_speed']","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:12:12.142596Z","iopub.execute_input":"2022-10-04T10:12:12.143031Z","iopub.status.idle":"2022-10-04T10:12:12.571852Z","shell.execute_reply.started":"2022-10-04T10:12:12.142994Z","shell.execute_reply":"2022-10-04T10:12:12.570784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show basic stats for speed\ndf[speeds].describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:13:15.845847Z","iopub.execute_input":"2022-10-04T10:13:15.847083Z","iopub.status.idle":"2022-10-04T10:13:16.474127Z","shell.execute_reply.started":"2022-10-04T10:13:15.847037Z","shell.execute_reply":"2022-10-04T10:13:16.472791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scatter plots\nsns.pairplot(df[speeds], kind='scatter',             \n             plot_kws={'s' : 1,\n                       'alpha' : 0.01})\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:13:36.956059Z","iopub.execute_input":"2022-10-04T10:13:36.956496Z","iopub.status.idle":"2022-10-04T10:14:29.761524Z","shell.execute_reply.started":"2022-10-04T10:13:36.956463Z","shell.execute_reply":"2022-10-04T10:14:29.760190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# correlation of speeds\ncorr_pearson = df[speeds].corr(method='pearson')\n\nplt.figure(figsize=(6,5))\nsns.heatmap(corr_pearson, annot=True, cmap='RdYlGn',\n            vmin=-1, vmax=+1)\nplt.title('Speeds - Pearson Corr.')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:17:41.560795Z","iopub.execute_input":"2022-10-04T10:17:41.561271Z","iopub.status.idle":"2022-10-04T10:17:42.561384Z","shell.execute_reply.started":"2022-10-04T10:17:41.561230Z","shell.execute_reply":"2022-10-04T10:17:42.560089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='targets'></a>\n# Targets","metadata":{}},{"cell_type":"code","source":"targets = ['team_A_scoring_within_10sec','team_B_scoring_within_10sec']","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:17:49.129108Z","iopub.execute_input":"2022-10-04T10:17:49.130514Z","iopub.status.idle":"2022-10-04T10:17:49.136315Z","shell.execute_reply.started":"2022-10-04T10:17:49.130453Z","shell.execute_reply":"2022-10-04T10:17:49.134866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot targets\nfor t in targets:\n    print(df[t].value_counts())\n    df[t].value_counts().plot(kind='bar')\n    plt.title('Target '+t)\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:17:50.530427Z","iopub.execute_input":"2022-10-04T10:17:50.531471Z","iopub.status.idle":"2022-10-04T10:17:51.003472Z","shell.execute_reply.started":"2022-10-04T10:17:50.531421Z","shell.execute_reply":"2022-10-04T10:17:51.002260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Bivariate view:","metadata":{}},{"cell_type":"code","source":"# dependency structure\nctab_target = pd.crosstab(df[targets[0]], df[targets[1]])\nctab_target","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:17:53.446147Z","iopub.execute_input":"2022-10-04T10:17:53.446552Z","iopub.status.idle":"2022-10-04T10:17:53.654556Z","shell.execute_reply.started":"2022-10-04T10:17:53.446521Z","shell.execute_reply":"2022-10-04T10:17:53.653280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# normalized version\nctab_target / (ctab_target.sum().sum())","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:17:55.271635Z","iopub.execute_input":"2022-10-04T10:17:55.272080Z","iopub.status.idle":"2022-10-04T10:17:55.286466Z","shell.execute_reply.started":"2022-10-04T10:17:55.272044Z","shell.execute_reply":"2022-10-04T10:17:55.284938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### => We see that there are no common occurrences of \"1\" implying a negative correlation of the targets.","metadata":{"execution":{"iopub.status.busy":"2022-10-01T11:01:19.597906Z","iopub.execute_input":"2022-10-01T11:01:19.598444Z","iopub.status.idle":"2022-10-01T11:01:19.604208Z","shell.execute_reply.started":"2022-10-01T11:01:19.598402Z","shell.execute_reply":"2022-10-01T11:01:19.602813Z"}}},{"cell_type":"markdown","source":"### Targets vs Ball Position:","metadata":{}},{"cell_type":"code","source":"for f in ball_pos:\n    plt.figure(figsize=(14,4))\n    ax1 = plt.subplot(1,2,1)\n    sns.violinplot(data=df, x='team_A_scoring_within_10sec', y=f)\n    plt.grid()\n    plt.title('Team A Scoring vs ' + f)\n    ax2 = plt.subplot(1,2,2, sharex=ax1)\n    sns.violinplot(data=df, x='team_B_scoring_within_10sec', y=f)\n    plt.grid()\n    plt.title('Team B Scoring vs ' + f)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:17:58.086940Z","iopub.execute_input":"2022-10-04T10:17:58.087416Z","iopub.status.idle":"2022-10-04T10:18:26.824942Z","shell.execute_reply.started":"2022-10-04T10:17:58.087376Z","shell.execute_reply":"2022-10-04T10:18:26.823592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Targets vs Ball Velocity:","metadata":{}},{"cell_type":"code","source":"for f in ball_vel:\n    plt.figure(figsize=(14,4))\n    ax1 = plt.subplot(1,2,1)\n    sns.violinplot(data=df, x='team_A_scoring_within_10sec', y=f)\n    plt.grid()\n    plt.title('Team A Scoring vs ' + f)\n    ax2 = plt.subplot(1,2,2, sharex=ax1)\n    sns.violinplot(data=df, x='team_B_scoring_within_10sec', y=f)\n    plt.grid()\n    plt.title('Team B Scoring vs ' + f)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:18:26.827418Z","iopub.execute_input":"2022-10-04T10:18:26.828219Z","iopub.status.idle":"2022-10-04T10:18:57.693332Z","shell.execute_reply.started":"2022-10-04T10:18:26.828168Z","shell.execute_reply":"2022-10-04T10:18:57.692091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='game'></a>\n# Drill down to individual Game","metadata":{}},{"cell_type":"code","source":"# filter a specific game\nmy_game = 2\ndf_game = df[df.game_num==my_game].reset_index(drop=True)\ndf_game","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:18:57.695731Z","iopub.execute_input":"2022-10-04T10:18:57.696215Z","iopub.status.idle":"2022-10-04T10:18:57.785126Z","shell.execute_reply.started":"2022-10-04T10:18:57.696177Z","shell.execute_reply":"2022-10-04T10:18:57.783888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot event time\nplt.figure(figsize=(14,4))\nplt.plot(df_game.event_time)\nplt.title('Event Time')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:18:57.786791Z","iopub.execute_input":"2022-10-04T10:18:57.788176Z","iopub.status.idle":"2022-10-04T10:18:57.950511Z","shell.execute_reply.started":"2022-10-04T10:18:57.788118Z","shell.execute_reply":"2022-10-04T10:18:57.949231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot target development vs time\ndf_game[targets].plot(figsize=(14,4))\nplt.title('Targets vs Time Steps')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:19:01.946662Z","iopub.execute_input":"2022-10-04T10:19:01.947076Z","iopub.status.idle":"2022-10-04T10:19:02.169008Z","shell.execute_reply.started":"2022-10-04T10:19:01.947043Z","shell.execute_reply":"2022-10-04T10:19:02.168107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot ball position vs time\ndf_game[ball_pos].plot(figsize=(14,5))\nplt.title('Ball Position in specific game')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:19:04.075580Z","iopub.execute_input":"2022-10-04T10:19:04.076468Z","iopub.status.idle":"2022-10-04T10:19:04.344909Z","shell.execute_reply.started":"2022-10-04T10:19:04.076413Z","shell.execute_reply":"2022-10-04T10:19:04.343412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot ball velocity vs time\ndf_game[ball_vel].plot(figsize=(14,5))\nplt.title('Ball Velocity in specific game')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:19:07.260898Z","iopub.execute_input":"2022-10-04T10:19:07.261356Z","iopub.status.idle":"2022-10-04T10:19:07.539582Z","shell.execute_reply.started":"2022-10-04T10:19:07.261316Z","shell.execute_reply":"2022-10-04T10:19:07.538377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot boosts vs time\ndf_game[boost].plot(figsize=(14,4))\nplt.title('Boost Timer in specific game')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:19:11.306159Z","iopub.execute_input":"2022-10-04T10:19:11.306636Z","iopub.status.idle":"2022-10-04T10:19:11.652895Z","shell.execute_reply.started":"2022-10-04T10:19:11.306592Z","shell.execute_reply":"2022-10-04T10:19:11.651440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot (absolute) speeds vs time\ndf_game[speeds].plot(figsize=(14,6))\nplt.title('Speeds in specific game')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:19:46.129497Z","iopub.execute_input":"2022-10-04T10:19:46.129943Z","iopub.status.idle":"2022-10-04T10:19:46.626155Z","shell.execute_reply.started":"2022-10-04T10:19:46.129908Z","shell.execute_reply":"2022-10-04T10:19:46.624885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# interactive 3d scatter plot\nfig = px.scatter_3d(data_frame=df_game,\n                    x=ball_pos[0], y=ball_pos[1], z=ball_pos[2],\n                    color=df_game.index,\n                    opacity=0.25)\nfig.update_layout(title='Ball Position in specific Game')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:19:54.846429Z","iopub.execute_input":"2022-10-04T10:19:54.846877Z","iopub.status.idle":"2022-10-04T10:19:56.073826Z","shell.execute_reply.started":"2022-10-04T10:19:54.846840Z","shell.execute_reply":"2022-10-04T10:19:56.072521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# interactive 3d scatter plot\nfig = px.scatter_3d(data_frame=df_game,\n                    x=ball_pos[0], y=ball_pos[1], z=ball_pos[2],\n                    color=df_game.event_id.astype('category'),\n                    opacity=0.25)\nfig.update_layout(title='Ball Position in specific Game - Grouped by Event')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:20:02.476084Z","iopub.execute_input":"2022-10-04T10:20:02.477552Z","iopub.status.idle":"2022-10-04T10:20:02.585812Z","shell.execute_reply.started":"2022-10-04T10:20:02.477495Z","shell.execute_reply":"2022-10-04T10:20:02.584555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Double-click on legend to filter/select events.","metadata":{}},{"cell_type":"code","source":"# interactive 3d scatter plot\nfig = px.scatter_3d(data_frame=df_game,\n                    x=ball_vel[0], y=ball_vel[1], z=ball_vel[2],\n                    color=df_game.index,\n                    opacity=0.25)\nfig.update_layout(title='Ball Velocity in specific Game')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:20:09.127551Z","iopub.execute_input":"2022-10-04T10:20:09.128025Z","iopub.status.idle":"2022-10-04T10:20:09.197411Z","shell.execute_reply.started":"2022-10-04T10:20:09.127987Z","shell.execute_reply":"2022-10-04T10:20:09.196185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='model1'></a>\n# Model - Target 1","metadata":{}},{"cell_type":"code","source":"# start H2O\nh2o.init(max_mem_size='12G', nthreads=4) # Use maximum of 13 GB RAM and 4 cores","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-10-04T10:22:43.967500Z","iopub.execute_input":"2022-10-04T10:22:43.968001Z","iopub.status.idle":"2022-10-04T10:22:52.865377Z","shell.execute_reply.started":"2022-10-04T10:22:43.967966Z","shell.execute_reply":"2022-10-04T10:22:52.863858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# select features\nfeatures = ball_pos + ball_vel + players + boost + speeds\nprint(features)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:22:57.172685Z","iopub.execute_input":"2022-10-04T10:22:57.173104Z","iopub.status.idle":"2022-10-04T10:22:57.180705Z","shell.execute_reply.started":"2022-10-04T10:22:57.173072Z","shell.execute_reply":"2022-10-04T10:22:57.179209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# upload training data in H2O environment\nt1 = time.time()\ntrain_hex = h2o.H2OFrame(df)\nt2 = time.time()\nprint('Elapsed time [s]:', np.round(t2-t1,4))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:22:59.056078Z","iopub.execute_input":"2022-10-04T10:22:59.056522Z","iopub.status.idle":"2022-10-04T10:28:02.922065Z","shell.execute_reply.started":"2022-10-04T10:22:59.056491Z","shell.execute_reply":"2022-10-04T10:28:02.920623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# memory management => remove original training data\ndel df\ngc.collect();","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:28:04.894325Z","iopub.execute_input":"2022-10-04T10:28:04.894740Z","iopub.status.idle":"2022-10-04T10:28:05.263830Z","shell.execute_reply.started":"2022-10-04T10:28:04.894705Z","shell.execute_reply":"2022-10-04T10:28:05.262415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define targets and convert them to categorical => binary classification problem\ntarget_1 = 'team_A_scoring_within_10sec'\ntarget_2 = 'team_B_scoring_within_10sec'\ntrain_hex[target_1] = train_hex[target_1].asfactor()\ntrain_hex[target_2] = train_hex[target_2].asfactor()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:28:12.790757Z","iopub.execute_input":"2022-10-04T10:28:12.791227Z","iopub.status.idle":"2022-10-04T10:28:12.799272Z","shell.execute_reply.started":"2022-10-04T10:28:12.791188Z","shell.execute_reply":"2022-10-04T10:28:12.797822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit Gradient Boosting model\nn_1 = 150\nn_cv = 5\nfit_1 = H2OGradientBoostingEstimator(ntrees=n_1,\n                                     max_depth=6,\n                                     min_rows=10,\n                                     learn_rate=0.1, # default: 0.1\n                                     sample_rate=0.5,\n                                     col_sample_rate=0.2,\n                                     nfolds=n_cv,\n                                     score_each_iteration=True,\n                                     stopping_metric='logloss',\n                                     stopping_rounds=5,\n                                     stopping_tolerance=0.0001,\n                                     seed=999)\n# train model\nt1 = time.time()\nfit_1.train(x=features,\n            y=target_1,\n            training_frame=train_hex)\nt2 = time.time()\nprint('Elapsed time [s]: ', np.round(t2-t1,2))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:28:23.357359Z","iopub.execute_input":"2022-10-04T10:28:23.357767Z","iopub.status.idle":"2022-10-04T10:33:08.680811Z","shell.execute_reply.started":"2022-10-04T10:28:23.357736Z","shell.execute_reply":"2022-10-04T10:33:08.679359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show cross validation metrics\nfit_1.cross_validation_metrics_summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:37:14.776547Z","iopub.execute_input":"2022-10-04T10:37:14.777039Z","iopub.status.idle":"2022-10-04T10:37:14.806792Z","shell.execute_reply.started":"2022-10-04T10:37:14.777000Z","shell.execute_reply":"2022-10-04T10:37:14.805609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show scoring history - training vs cross validations\nfor i in range(n_cv):\n    cv_model_temp = fit_1.cross_validation_models()[i]\n    df_cv_score_history = cv_model_temp.score_history()\n    my_title = 'Target 1 - CV ' + str(1+i) + ' - Scoring History [log_loss]'\n    plt.scatter(df_cv_score_history.number_of_trees,\n                y=df_cv_score_history.training_logloss, \n                c='blue', label='training')\n    plt.scatter(df_cv_score_history.number_of_trees,\n                y=df_cv_score_history.validation_logloss, \n                c='darkorange', label='validation')\n    plt.title(my_title)\n    plt.xlabel('Number of Trees')\n    plt.ylabel('log_loss')\n    plt.ylim(0.00,0.25)\n    plt.legend()\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:37:24.611189Z","iopub.execute_input":"2022-10-04T10:37:24.611622Z","iopub.status.idle":"2022-10-04T10:37:27.191723Z","shell.execute_reply.started":"2022-10-04T10:37:24.611591Z","shell.execute_reply":"2022-10-04T10:37:27.190662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# variable importance\nfit_1.varimp_plot(len(features))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:37:29.440730Z","iopub.execute_input":"2022-10-04T10:37:29.441196Z","iopub.status.idle":"2022-10-04T10:37:30.336426Z","shell.execute_reply.started":"2022-10-04T10:37:29.441124Z","shell.execute_reply":"2022-10-04T10:37:30.335072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict on train set (extract probabilities only)\npred_train_1 = fit_1.predict(train_hex)['p1']\npred_train_1 = pred_train_1.as_data_frame().p1\n\n# plot train set predictions (probabilities)\nplt.figure(figsize=(8,4))\nplt.hist(pred_train_1, bins=100)\nplt.title('Predictions on Train Set - Target 1 / GBM')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:40:27.456965Z","iopub.execute_input":"2022-10-04T10:40:27.457543Z","iopub.status.idle":"2022-10-04T10:40:33.471242Z","shell.execute_reply.started":"2022-10-04T10:40:27.457502Z","shell.execute_reply":"2022-10-04T10:40:33.469953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check calibration\nn_actual_1 = train_hex[target_1].ascharacter().asnumeric().sum()\nn_pred_1 = sum(pred_train_1)\n\nprint('Actual Frequency    :', n_actual_1)\nprint('Predicted Frequency :', n_pred_1)\nprint('Calibration Ratio   :', n_pred_1 / n_actual_1)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:40:36.055419Z","iopub.execute_input":"2022-10-04T10:40:36.055893Z","iopub.status.idle":"2022-10-04T10:40:37.463372Z","shell.execute_reply.started":"2022-10-04T10:40:36.055852Z","shell.execute_reply":"2022-10-04T10:40:37.462093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='model2'></a>\n# Model - Target 2","metadata":{}},{"cell_type":"code","source":"# fit Gradient Boosting model\nn_2 = 150\nn_cv = 5\nfit_2 = H2OGradientBoostingEstimator(ntrees=n_2,\n                                     max_depth=6,\n                                     min_rows=10,\n                                     learn_rate=0.1, # default: 0.1\n                                     sample_rate=0.5,\n                                     col_sample_rate=0.2,\n                                     nfolds=n_cv,\n                                     score_each_iteration=True,\n                                     stopping_metric='logloss',\n                                     stopping_rounds=5,\n                                     stopping_tolerance=0.0001,\n                                     seed=999)\n# train model\nt1 = time.time()\nfit_2.train(x=features,\n            y=target_2,\n            training_frame=train_hex)\nt2 = time.time()\nprint('Elapsed time [s]: ', np.round(t2-t1,2))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:40:52.538531Z","iopub.execute_input":"2022-10-04T10:40:52.539704Z","iopub.status.idle":"2022-10-04T10:45:10.803534Z","shell.execute_reply.started":"2022-10-04T10:40:52.539659Z","shell.execute_reply":"2022-10-04T10:45:10.802068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show cross validation metrics\nfit_2.cross_validation_metrics_summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:45:57.751086Z","iopub.execute_input":"2022-10-04T10:45:57.751629Z","iopub.status.idle":"2022-10-04T10:45:57.776201Z","shell.execute_reply.started":"2022-10-04T10:45:57.751578Z","shell.execute_reply":"2022-10-04T10:45:57.775309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show scoring history - training vs cross validations\nfor i in range(n_cv):\n    cv_model_temp = fit_2.cross_validation_models()[i]\n    df_cv_score_history = cv_model_temp.score_history()\n    my_title = 'Target 2 - CV ' + str(1+i) + ' - Scoring History [log_loss]'\n    plt.scatter(df_cv_score_history.number_of_trees,\n                y=df_cv_score_history.training_logloss, \n                c='blue', label='training')\n    plt.scatter(df_cv_score_history.number_of_trees,\n                y=df_cv_score_history.validation_logloss, \n                c='darkorange', label='validation')\n    plt.title(my_title)\n    plt.xlabel('Number of Trees')\n    plt.ylabel('log_loss')\n    plt.ylim(0.00,0.25)\n    plt.legend()\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:46:05.034181Z","iopub.execute_input":"2022-10-04T10:46:05.035250Z","iopub.status.idle":"2022-10-04T10:46:07.366626Z","shell.execute_reply.started":"2022-10-04T10:46:05.035202Z","shell.execute_reply":"2022-10-04T10:46:07.365249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# variable importance\nfit_2.varimp_plot(len(features))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:46:09.364631Z","iopub.execute_input":"2022-10-04T10:46:09.365468Z","iopub.status.idle":"2022-10-04T10:46:10.299934Z","shell.execute_reply.started":"2022-10-04T10:46:09.365423Z","shell.execute_reply":"2022-10-04T10:46:10.298672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict on train set (extract probabilities only)\npred_train_2 = fit_2.predict(train_hex)['p1']\npred_train_2 = pred_train_2.as_data_frame().p1\n\n# plot train set predictions (probabilities)\nplt.figure(figsize=(8,4))\nplt.hist(pred_train_2, bins=100)\nplt.title('Predictions on Train Set - Target 2 / GBM')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:46:15.744836Z","iopub.execute_input":"2022-10-04T10:46:15.745747Z","iopub.status.idle":"2022-10-04T10:46:20.923813Z","shell.execute_reply.started":"2022-10-04T10:46:15.745711Z","shell.execute_reply":"2022-10-04T10:46:20.922332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check calibration\nn_actual_2 = train_hex[target_2].ascharacter().asnumeric().sum()\nn_pred_2 = sum(pred_train_2)\n\nprint('Actual Frequency    :', n_actual_2)\nprint('Predicted Frequency :', n_pred_2)\nprint('Calibration Ratio   :', n_pred_2 / n_actual_2)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:46:20.927641Z","iopub.execute_input":"2022-10-04T10:46:20.928817Z","iopub.status.idle":"2022-10-04T10:46:21.615782Z","shell.execute_reply.started":"2022-10-04T10:46:20.928763Z","shell.execute_reply":"2022-10-04T10:46:21.614424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='test'></a>\n# Predict on Test Set","metadata":{}},{"cell_type":"code","source":"# get data types first (as described in the documentation)\ndtypes_df_test = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/test_dtypes.csv')\ndtypes_test = {k: v for (k, v) in zip(dtypes_df_test.column, dtypes_df_test.dtype)}","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:46:24.861945Z","iopub.execute_input":"2022-10-04T10:46:24.862390Z","iopub.status.idle":"2022-10-04T10:46:24.882150Z","shell.execute_reply.started":"2022-10-04T10:46:24.862352Z","shell.execute_reply":"2022-10-04T10:46:24.881238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load test set according to given data types\nt1 = time.time()\ndf_test = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/test.csv', dtype=dtypes_test)\nt2 = time.time()\nprint('Elapsed time [s]:', np.round(t2-t1,4))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:46:27.273817Z","iopub.execute_input":"2022-10-04T10:46:27.274622Z","iopub.status.idle":"2022-10-04T10:46:38.266492Z","shell.execute_reply.started":"2022-10-04T10:46:27.274580Z","shell.execute_reply":"2022-10-04T10:46:38.265184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# structure of training data\ndf_test.info(show_counts=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:46:38.268222Z","iopub.execute_input":"2022-10-04T10:46:38.268588Z","iopub.status.idle":"2022-10-04T10:46:38.391609Z","shell.execute_reply.started":"2022-10-04T10:46:38.268555Z","shell.execute_reply":"2022-10-04T10:46:38.390314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# repeat feature engineering steps\ndf_test['ball_speed'] = np.sqrt(df_test.ball_vel_x**2 + df_test.ball_vel_y**2 + df_test.ball_vel_z**2)\ndf_test['p0_speed'] = np.sqrt(df_test.p0_vel_x**2 + df_test.p0_vel_y**2 + df_test.p0_vel_z**2)\ndf_test['p1_speed'] = np.sqrt(df_test.p1_vel_x**2 + df_test.p1_vel_y**2 + df_test.p1_vel_z**2)\ndf_test['p2_speed'] = np.sqrt(df_test.p2_vel_x**2 + df_test.p2_vel_y**2 + df_test.p2_vel_z**2)\ndf_test['p3_speed'] = np.sqrt(df_test.p3_vel_x**2 + df_test.p3_vel_y**2 + df_test.p3_vel_z**2)\ndf_test['p4_speed'] = np.sqrt(df_test.p4_vel_x**2 + df_test.p4_vel_y**2 + df_test.p4_vel_z**2)\ndf_test['p5_speed'] = np.sqrt(df_test.p5_vel_x**2 + df_test.p5_vel_y**2 + df_test.p5_vel_z**2)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:48:15.272872Z","iopub.execute_input":"2022-10-04T10:48:15.273409Z","iopub.status.idle":"2022-10-04T10:48:15.318994Z","shell.execute_reply.started":"2022-10-04T10:48:15.273370Z","shell.execute_reply":"2022-10-04T10:48:15.317812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# upload test data in H2O environment\nt1 = time.time()\ntest_hex = h2o.H2OFrame(df_test)\nt2 = time.time()\nprint('Elapsed time [s]:', np.round(t2-t1,4))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:48:17.411008Z","iopub.execute_input":"2022-10-04T10:48:17.411469Z","iopub.status.idle":"2022-10-04T10:49:36.645913Z","shell.execute_reply.started":"2022-10-04T10:48:17.411429Z","shell.execute_reply":"2022-10-04T10:49:36.644551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# memory management => remove original test data\ndel df_test\ngc.collect();","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:49:44.931984Z","iopub.execute_input":"2022-10-04T10:49:44.933196Z","iopub.status.idle":"2022-10-04T10:49:45.291054Z","shell.execute_reply.started":"2022-10-04T10:49:44.933122Z","shell.execute_reply":"2022-10-04T10:49:45.289551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict on test set (extract probabilities only)\npred_test_1 = fit_1.predict(test_hex)['p1']\npred_test_1 = pred_test_1.as_data_frame().p1\n\n# plot train set predictions (probabilities)\nplt.figure(figsize=(8,4))\nplt.hist(pred_test_1, bins=100)\nplt.title('Predictions on Test Set - Target 1 / GBM')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:49:46.571533Z","iopub.execute_input":"2022-10-04T10:49:46.571952Z","iopub.status.idle":"2022-10-04T10:49:49.171686Z","shell.execute_reply.started":"2022-10-04T10:49:46.571918Z","shell.execute_reply":"2022-10-04T10:49:49.170359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict on test set (extract probabilities only)\npred_test_2 = fit_2.predict(test_hex)['p1']\npred_test_2 = pred_test_2.as_data_frame().p1\n\n# plot train set predictions (probabilities)\nplt.figure(figsize=(8,4))\nplt.hist(pred_test_2, bins=100)\nplt.title('Predictions on Test Set - Target 2 / GBM')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:49:51.690843Z","iopub.execute_input":"2022-10-04T10:49:51.691268Z","iopub.status.idle":"2022-10-04T10:49:53.603458Z","shell.execute_reply.started":"2022-10-04T10:49:51.691234Z","shell.execute_reply":"2022-10-04T10:49:53.602297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare submission\ndf_sub = pd.read_csv('../input/tabular-playground-series-oct-2022/sample_submission.csv')\ndf_sub[target_1] = pred_test_1\ndf_sub[target_2] = pred_test_2\ndf_sub","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:49:54.414289Z","iopub.execute_input":"2022-10-04T10:49:54.414691Z","iopub.status.idle":"2022-10-04T10:49:54.641168Z","shell.execute_reply.started":"2022-10-04T10:49:54.414659Z","shell.execute_reply":"2022-10-04T10:49:54.639955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# bivariate plot of target predictions\nsns.jointplot(data=df_sub, x=target_1, y=target_2,\n              joint_kws={'s' : 1,\n                        'alpha' : 0.1})\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:49:57.079971Z","iopub.execute_input":"2022-10-04T10:49:57.080392Z","iopub.status.idle":"2022-10-04T10:50:02.535687Z","shell.execute_reply.started":"2022-10-04T10:49:57.080359Z","shell.execute_reply":"2022-10-04T10:50:02.534445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# correlation of target predictions\ndf_sub[[target_1,target_2]].corr()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:50:02.537786Z","iopub.execute_input":"2022-10-04T10:50:02.538115Z","iopub.status.idle":"2022-10-04T10:50:02.574167Z","shell.execute_reply.started":"2022-10-04T10:50:02.538086Z","shell.execute_reply":"2022-10-04T10:50:02.573104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save to file\ndf_sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T10:50:02.575537Z","iopub.execute_input":"2022-10-04T10:50:02.575907Z","iopub.status.idle":"2022-10-04T10:50:05.029653Z","shell.execute_reply.started":"2022-10-04T10:50:02.575873Z","shell.execute_reply":"2022-10-04T10:50:05.028531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Open issues:\n\n* Models are not yet really tuned. \n* We have just used the first part of the training set.\n* Feature engineering could certainly be improved.\n* Can we include the dependence structure of the two targets into the modeling?","metadata":{}}]}