{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":81933,"databundleVersionId":9643020,"sourceType":"competition"}],"dockerImageVersionId":30369,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## 📋Table of Contents\n* [Import and First Glance](#import)\n* [Distributions of Features](#eda)\n* [Correlation of Features](#corr)\n* [Target vs Features](#target_features)\n* [Train Set-only Features](#eda_train)\n* [Time Series Data](#time_series)\n* [Model](#model)","metadata":{}},{"cell_type":"code","source":"# standard\nimport numpy as np\nimport pandas as pd\nimport time\n\n# plots\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly\nimport plotly.express as px\n\n# statistics\nfrom scipy import stats\nfrom sklearn.metrics import cohen_kappa_score\n\n# H2O\nimport h2o\nfrom h2o.estimators import H2OGradientBoostingEstimator","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:49.465879Z","iopub.execute_input":"2025-02-28T09:59:49.466369Z","iopub.status.idle":"2025-02-28T09:59:51.578720Z","shell.execute_reply.started":"2025-02-28T09:59:49.466240Z","shell.execute_reply":"2025-02-28T09:59:51.577742Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# configs\npd.set_option('display.max_columns', None) # we want to display all columns in this notebook\npd.set_option('display.max_rows', 100) # increase rows to be displayed\npd.set_option('display.max_colwidth', None) # show full cell contents\n\n# random seed\nmy_random_seed = 111\n\n# aesthetics\ndefault_color_1 = 'darkblue'\ndefault_color_2 = 'darkgreen'\ndefault_color_3 = 'darkred'","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:51.580943Z","iopub.execute_input":"2025-02-28T09:59:51.581308Z","iopub.status.idle":"2025-02-28T09:59:51.587828Z","shell.execute_reply.started":"2025-02-28T09:59:51.581275Z","shell.execute_reply":"2025-02-28T09:59:51.586603Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='import'></a>\n# Import and First Glance","metadata":{}},{"cell_type":"code","source":"# load data\ndf_train = pd.read_csv('../input/child-mind-institute-problematic-internet-use/train.csv')\ndf_test = pd.read_csv('../input/child-mind-institute-problematic-internet-use/test.csv')\ndf_sub = pd.read_csv('../input/child-mind-institute-problematic-internet-use/sample_submission.csv')\ndf_dict = pd.read_csv('../input/child-mind-institute-problematic-internet-use/data_dictionary.csv')","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:51.589105Z","iopub.execute_input":"2025-02-28T09:59:51.589410Z","iopub.status.idle":"2025-02-28T09:59:51.697704Z","shell.execute_reply.started":"2025-02-28T09:59:51.589384Z","shell.execute_reply":"2025-02-28T09:59:51.696419Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# show data dictionary\ndf_dict","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:51.699714Z","iopub.execute_input":"2025-02-28T09:59:51.700209Z","iopub.status.idle":"2025-02-28T09:59:51.739635Z","shell.execute_reply.started":"2025-02-28T09:59:51.700163Z","shell.execute_reply":"2025-02-28T09:59:51.738561Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# preview training data\ndf_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:51.741340Z","iopub.execute_input":"2025-02-28T09:59:51.741648Z","iopub.status.idle":"2025-02-28T09:59:51.838401Z","shell.execute_reply.started":"2025-02-28T09:59:51.741621Z","shell.execute_reply":"2025-02-28T09:59:51.837288Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# structure of data - train\ndf_train.info()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:51.839726Z","iopub.execute_input":"2025-02-28T09:59:51.840061Z","iopub.status.idle":"2025-02-28T09:59:51.872001Z","shell.execute_reply.started":"2025-02-28T09:59:51.840031Z","shell.execute_reply":"2025-02-28T09:59:51.870717Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 💡 We have a lot of missing values, even for the target!","metadata":{}},{"cell_type":"code","source":"# remove training data w/o target\ndf_train = df_train.dropna(subset=['sii'])","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:51.876394Z","iopub.execute_input":"2025-02-28T09:59:51.876721Z","iopub.status.idle":"2025-02-28T09:59:51.888139Z","shell.execute_reply.started":"2025-02-28T09:59:51.876693Z","shell.execute_reply":"2025-02-28T09:59:51.886994Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# structure of data - test\ndf_test.info()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:51.889877Z","iopub.execute_input":"2025-02-28T09:59:51.890217Z","iopub.status.idle":"2025-02-28T09:59:51.912335Z","shell.execute_reply.started":"2025-02-28T09:59:51.890187Z","shell.execute_reply":"2025-02-28T09:59:51.911084Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 💡 Test set does not contain the PCIAT-PCIAT... features!","metadata":{}},{"cell_type":"markdown","source":"<a id='eda'></a>\n# Distributions of Features","metadata":{}},{"cell_type":"code","source":"# basic stats - train\ndf_train.describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:51.913692Z","iopub.execute_input":"2025-02-28T09:59:51.914052Z","iopub.status.idle":"2025-02-28T09:59:52.168518Z","shell.execute_reply.started":"2025-02-28T09:59:51.914021Z","shell.execute_reply":"2025-02-28T09:59:52.167113Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# basic stats - test\ndf_test.describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:52.170055Z","iopub.execute_input":"2025-02-28T09:59:52.170485Z","iopub.status.idle":"2025-02-28T09:59:52.357536Z","shell.execute_reply.started":"2025-02-28T09:59:52.170445Z","shell.execute_reply":"2025-02-28T09:59:52.356262Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define features and target\n\n# categorical\nfeatures_cat_train_test = ['Basic_Demos-Enroll_Season', 'Basic_Demos-Sex', 'CGAS-Season', \n                           'Physical-Season', 'Fitness_Endurance-Season', 'FGC-Season',\n                           'BIA-Season', 'PAQ_A-Season', 'PAQ_C-Season', \n                           'SDS-Season', 'PreInt_EduHx-Season',]\n\n# numerical\nfeatures_num_train_test = ['Basic_Demos-Age', 'CGAS-CGAS_Score', 'Physical-BMI',\n                           'Physical-Height', 'Physical-Weight', 'Physical-Waist_Circumference',\n                           'Physical-Diastolic_BP', 'Physical-HeartRate', 'Physical-Systolic_BP',\n                           'Fitness_Endurance-Max_Stage', 'Fitness_Endurance-Time_Mins', \n                           'Fitness_Endurance-Time_Sec',\n                           'FGC-FGC_CU', 'FGC-FGC_CU_Zone', 'FGC-FGC_GSND',\n                           'FGC-FGC_GSND_Zone', 'FGC-FGC_GSD', 'FGC-FGC_GSD_Zone', 'FGC-FGC_PU',\n                           'FGC-FGC_PU_Zone', 'FGC-FGC_SRL', 'FGC-FGC_SRL_Zone', 'FGC-FGC_SRR',\n                           'FGC-FGC_SRR_Zone', 'FGC-FGC_TL', 'FGC-FGC_TL_Zone', \n                           'BIA-BIA_Activity_Level_num', 'BIA-BIA_BMC', 'BIA-BIA_BMI',\n                           'BIA-BIA_BMR', 'BIA-BIA_DEE', 'BIA-BIA_ECW', 'BIA-BIA_FFM',\n                           'BIA-BIA_FFMI', 'BIA-BIA_FMI', 'BIA-BIA_Fat', 'BIA-BIA_Frame_num',\n                           'BIA-BIA_ICW', 'BIA-BIA_LDM', 'BIA-BIA_LST', 'BIA-BIA_SMM',\n                           'BIA-BIA_TBW',  'PAQ_A-PAQ_A_Total', \n                           'PAQ_C-PAQ_C_Total', 'SDS-SDS_Total_Raw', 'SDS-SDS_Total_T', \n                           'PreInt_EduHx-computerinternet_hoursday']\n\nfeatures_num_train_only = ['PCIAT-PCIAT_01', 'PCIAT-PCIAT_02',\n                           'PCIAT-PCIAT_03', 'PCIAT-PCIAT_04', 'PCIAT-PCIAT_05', 'PCIAT-PCIAT_06',\n                           'PCIAT-PCIAT_07', 'PCIAT-PCIAT_08', 'PCIAT-PCIAT_09', 'PCIAT-PCIAT_10',\n                           'PCIAT-PCIAT_11', 'PCIAT-PCIAT_12', 'PCIAT-PCIAT_13', 'PCIAT-PCIAT_14',\n                           'PCIAT-PCIAT_15', 'PCIAT-PCIAT_16', 'PCIAT-PCIAT_17', 'PCIAT-PCIAT_18',\n                           'PCIAT-PCIAT_19', 'PCIAT-PCIAT_20', 'PCIAT-PCIAT_Total']\n\nfeatures_cat_train_only = ['PCIAT-Season']\n\n# target\ntarget = 'sii'","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:52.359466Z","iopub.execute_input":"2025-02-28T09:59:52.359926Z","iopub.status.idle":"2025-02-28T09:59:52.372362Z","shell.execute_reply.started":"2025-02-28T09:59:52.359884Z","shell.execute_reply":"2025-02-28T09:59:52.371118Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot histograms (train and test)\nfor f in features_num_train_test:\n    plt.figure(figsize=(14,3))\n    ax1 = plt.subplot(1,2,1)\n    df_train[f].plot(kind='hist', bins=15, color=default_color_1)\n    plt.title(f + ' - Train')\n    plt.grid()\n    ax2 = plt.subplot(1,2,2, sharex=ax1)\n    df_test[f].plot(kind='hist', bins=15, color=default_color_2)\n    plt.title(f + ' - Test')\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T09:59:52.373949Z","iopub.execute_input":"2025-02-28T09:59:52.374382Z","iopub.status.idle":"2025-02-28T10:00:09.105846Z","shell.execute_reply.started":"2025-02-28T09:59:52.374342Z","shell.execute_reply":"2025-02-28T10:00:09.104750Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# boxplots (train and test)\nfor f in features_num_train_test:\n    plt.figure(figsize=(14,1))\n    ax1 = plt.subplot(1,2,1)\n    df_temp = df_train[f].dropna() # boxplot does not like missings...\n    plt.boxplot(df_temp, vert=False)\n    plt.title(f + ' - Train')\n    plt.grid()\n    ax2 = plt.subplot(1,2,2, sharex=ax1)\n    df_temp = df_test[f].dropna()\n    plt.boxplot(df_temp, vert=False)\n    plt.title(f + ' - Test')\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:09.107306Z","iopub.execute_input":"2025-02-28T10:00:09.107702Z","iopub.status.idle":"2025-02-28T10:00:19.951393Z","shell.execute_reply.started":"2025-02-28T10:00:09.107670Z","shell.execute_reply":"2025-02-28T10:00:19.950299Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot categorical feature distributions (train and test)\nfor f in features_cat_train_test:\n    plt.figure(figsize=(15,3))\n    ax1 = plt.subplot(1,2,1)\n    df_train[f].value_counts().sort_index().plot(kind='bar', color=default_color_1)\n    plt.title(f + ' - Train')\n    plt.grid()\n    ax2 = plt.subplot(1,2,2)\n    df_test[f].value_counts().sort_index().plot(kind='bar', color=default_color_2)\n    plt.title(f + ' - Test')\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:03:20.301209Z","iopub.execute_input":"2025-02-28T10:03:20.301641Z","iopub.status.idle":"2025-02-28T10:03:23.096376Z","shell.execute_reply.started":"2025-02-28T10:03:20.301606Z","shell.execute_reply":"2025-02-28T10:03:23.095248Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='corr'></a>\n# Correlation of Features","metadata":{}},{"cell_type":"code","source":"# calc correlation matrices\ncorr_pearson = df_train[features_num_train_test].corr(method='pearson')\ncorr_spearman = df_train[features_num_train_test].corr(method='spearman')","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:23.001740Z","iopub.execute_input":"2025-02-28T10:00:23.002117Z","iopub.status.idle":"2025-02-28T10:00:23.352177Z","shell.execute_reply.started":"2025-02-28T10:00:23.002084Z","shell.execute_reply":"2025-02-28T10:00:23.350730Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# and plot them\nplt.figure(figsize=(16,14))\nsns.heatmap(corr_pearson, annot=False, cmap='RdYlGn',\n            fmt='.2f', linecolor='black', linewidths=0.5,\n            vmin=-1, vmax=+1)\nplt.title('Pearson Correlation')\n\nplt.figure(figsize=(16,14))\nsns.heatmap(corr_spearman, annot=False, cmap='RdYlGn', \n            fmt='.2f', linecolor='black', linewidths=0.5,\n            vmin=-1, vmax=+1)\nplt.title('Spearman Correlation')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:23.353496Z","iopub.execute_input":"2025-02-28T10:00:23.353842Z","iopub.status.idle":"2025-02-28T10:00:25.885252Z","shell.execute_reply.started":"2025-02-28T10:00:23.353792Z","shell.execute_reply":"2025-02-28T10:00:25.884065Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='target_features'></a>\n# Target vs Features","metadata":{}},{"cell_type":"code","source":"# plot target distribution\ndf_train[target].value_counts().sort_index().plot(kind='bar', color=default_color_3)\nplt.title('Target')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:25.886766Z","iopub.execute_input":"2025-02-28T10:00:25.887248Z","iopub.status.idle":"2025-02-28T10:00:26.028047Z","shell.execute_reply.started":"2025-02-28T10:00:25.887205Z","shell.execute_reply":"2025-02-28T10:00:26.027000Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# target vs numerical features\nfor f in features_num_train_test:\n    plt.figure(figsize=(8,4))\n    sns.violinplot(data=df_train, x=target, y=f)\n    plt.title('Target vs ' + f)\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:26.030385Z","iopub.execute_input":"2025-02-28T10:00:26.030864Z","iopub.status.idle":"2025-02-28T10:00:36.532763Z","shell.execute_reply.started":"2025-02-28T10:00:26.030801Z","shell.execute_reply":"2025-02-28T10:00:36.531678Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# impact of categorical features - normalized cross tables\nfor f in features_cat_train_test:\n    if f != 'hospital_number': # skip hospital_number due to too many levels\n        ctab = pd.crosstab(df_train[target], df_train[f])\n        ctab_norm = ctab / ctab.sum()\n        plt.figure(figsize=(12,2))\n        g = sns.heatmap(ctab_norm, annot=True,\n                        fmt='.2%', linecolor='black',\n                        linewidths=1, cmap='Greens', \n                        vmin=0, vmax=+1)\n        plt.title(f + ' vs target - train')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:36.534394Z","iopub.execute_input":"2025-02-28T10:00:36.534872Z","iopub.status.idle":"2025-02-28T10:00:39.295685Z","shell.execute_reply.started":"2025-02-28T10:00:36.534803Z","shell.execute_reply":"2025-02-28T10:00:39.294588Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='eda_train'></a>\n# Train Set-only Features","metadata":{"execution":{"iopub.status.busy":"2024-09-20T06:55:51.102915Z","iopub.execute_input":"2024-09-20T06:55:51.103580Z","iopub.status.idle":"2024-09-20T06:55:51.111259Z","shell.execute_reply.started":"2024-09-20T06:55:51.103538Z","shell.execute_reply":"2024-09-20T06:55:51.109248Z"}}},{"cell_type":"code","source":"# plot histograms\nfor f in features_num_train_only:\n    plt.figure(figsize=(8,3))\n    df_train[f].plot(kind='hist', bins=15, color=default_color_1)\n    plt.title(f + ' - Train only')\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:39.297134Z","iopub.execute_input":"2025-02-28T10:00:39.297436Z","iopub.status.idle":"2025-02-28T10:00:43.551008Z","shell.execute_reply.started":"2025-02-28T10:00:39.297410Z","shell.execute_reply":"2025-02-28T10:00:43.549793Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot categorical feature distributions\nfor f in features_cat_train_only:\n    plt.figure(figsize=(8,3))\n    df_train[f].value_counts().sort_index().plot(kind='bar', color=default_color_1)\n    plt.title(f + ' - Train only')\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:43.552462Z","iopub.execute_input":"2025-02-28T10:00:43.552878Z","iopub.status.idle":"2025-02-28T10:00:43.721756Z","shell.execute_reply.started":"2025-02-28T10:00:43.552844Z","shell.execute_reply":"2025-02-28T10:00:43.720586Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# calc correlation matrices\ncorr_pearson_train_only = df_train[features_num_train_only].corr(method='pearson')\ncorr_spearman_train_only = df_train[features_num_train_only].corr(method='spearman')","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:43.728317Z","iopub.execute_input":"2025-02-28T10:00:43.728673Z","iopub.status.idle":"2025-02-28T10:00:43.837764Z","shell.execute_reply.started":"2025-02-28T10:00:43.728644Z","shell.execute_reply":"2025-02-28T10:00:43.836755Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# and plot them\nplt.figure(figsize=(14,10))\nsns.heatmap(corr_pearson_train_only, annot=True, cmap='RdYlGn',\n            fmt='.2f', linecolor='black', linewidths=0.5,\n            vmin=-1, vmax=+1)\nplt.title('Pearson Correlation')\n\nplt.figure(figsize=(14,10))\nsns.heatmap(corr_spearman_train_only, annot=True, cmap='RdYlGn', \n            fmt='.2f', linecolor='black', linewidths=0.5,\n            vmin=-1, vmax=+1)\nplt.title('Spearman Correlation')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:43.839073Z","iopub.execute_input":"2025-02-28T10:00:43.839415Z","iopub.status.idle":"2025-02-28T10:00:48.078725Z","shell.execute_reply.started":"2025-02-28T10:00:43.839383Z","shell.execute_reply":"2025-02-28T10:00:48.077403Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='time_series'></a>\n# Time Series Data","metadata":{}},{"cell_type":"code","source":"# load example file\nmy_id = 'id=00115b9f'\nfull_path = '../input/child-mind-institute-problematic-internet-use/series_train.parquet/' + my_id + '/part-0.parquet'\ndf_ts_example = pd.read_parquet(full_path)","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:48.080449Z","iopub.execute_input":"2025-02-28T10:00:48.080910Z","iopub.status.idle":"2025-02-28T10:00:48.232360Z","shell.execute_reply.started":"2025-02-28T10:00:48.080868Z","shell.execute_reply":"2025-02-28T10:00:48.231126Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# preview\ndf_ts_example","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:48.233962Z","iopub.execute_input":"2025-02-28T10:00:48.234337Z","iopub.status.idle":"2025-02-28T10:00:48.262071Z","shell.execute_reply.started":"2025-02-28T10:00:48.234301Z","shell.execute_reply":"2025-02-28T10:00:48.260892Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# basic stats\ndf_ts_example.describe()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:48.263475Z","iopub.execute_input":"2025-02-28T10:00:48.263844Z","iopub.status.idle":"2025-02-28T10:00:48.329751Z","shell.execute_reply.started":"2025-02-28T10:00:48.263787Z","shell.execute_reply":"2025-02-28T10:00:48.328611Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# derived features\ndf_ts_example['normXYZ'] = np.sqrt(df_ts_example.X*df_ts_example.X + df_ts_example.Y*df_ts_example.Y + df_ts_example.Z*df_ts_example.Z)\n\nplt.figure(figsize=(8,3))\nplt.hist(df_ts_example.normXYZ, bins=50, color=default_color_1)\nplt.title('normXYZ- histogram')\nplt.grid()\nplt.show()\n\nplt.figure(figsize=(8,3))\nplt.boxplot(df_ts_example.normXYZ, vert=False)\nplt.title('normXYZ - boxplot')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:48.330991Z","iopub.execute_input":"2025-02-28T10:00:48.331302Z","iopub.status.idle":"2025-02-28T10:00:48.741896Z","shell.execute_reply.started":"2025-02-28T10:00:48.331275Z","shell.execute_reply":"2025-02-28T10:00:48.740769Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 💡Most accelaration vectors are close to the unit sphere, but there are also a lot of \"outliers\".","metadata":{}},{"cell_type":"code","source":"# features of time series data\nfeatures_ts = ['X', 'Y', 'Z', 'enmo', 'anglez', 'non-wear_flag', \n               'light', 'battery_voltage', 'time_of_day',\n               'weekday', 'quarter', 'relative_date_PCIAT',\n               'normXYZ']","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:48.743264Z","iopub.execute_input":"2025-02-28T10:00:48.743573Z","iopub.status.idle":"2025-02-28T10:00:48.749144Z","shell.execute_reply.started":"2025-02-28T10:00:48.743546Z","shell.execute_reply":"2025-02-28T10:00:48.747871Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot time series\nfor f in features_ts:\n    plt.figure(figsize=(16,6))\n    plt.scatter(df_ts_example.step, df_ts_example[f], color=default_color_1,\n                s=1, alpha=0.5)\n    my_title = 'Time Series for ' + my_id + ' - ' + f\n    plt.title(my_title)\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:48.750600Z","iopub.execute_input":"2025-02-28T10:00:48.751370Z","iopub.status.idle":"2025-02-28T10:00:51.599020Z","shell.execute_reply.started":"2025-02-28T10:00:48.751326Z","shell.execute_reply":"2025-02-28T10:00:51.597860Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# prepare plotly\nplotly.offline.init_notebook_mode(connected = True)\n\n# interactive scatter plot\nfeature_x = 'X'\nfeature_y = 'Y'\nfeature_z = 'Z'\nfig = px.scatter_3d(df_ts_example, x=feature_x, y=feature_y, z=feature_z,                    \n                    hover_data=['step', 'normXYZ'],\n                    size='enmo',\n                    opacity=0.5)\nfig.update_layout(title='Scatter Plot')\nfig.show(renderer='iframe')","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:51.600392Z","iopub.execute_input":"2025-02-28T10:00:51.600736Z","iopub.status.idle":"2025-02-28T10:00:52.715110Z","shell.execute_reply.started":"2025-02-28T10:00:51.600705Z","shell.execute_reply":"2025-02-28T10:00:52.714017Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Note: If plot is not visible open/run this notebook in edit mode or download the file 3d_plot.html created in the next cell.","metadata":{}},{"cell_type":"code","source":"# export plot as HTML\nplotly.offline.plot(fig, filename='3d_plot.html');","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:00:52.716571Z","iopub.execute_input":"2025-02-28T10:00:52.717094Z","iopub.status.idle":"2025-02-28T10:00:52.828315Z","shell.execute_reply.started":"2025-02-28T10:00:52.717048Z","shell.execute_reply":"2025-02-28T10:00:52.826896Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='model'></a>\n# Model","metadata":{}},{"cell_type":"code","source":"# start H2O\nh2o.init(max_mem_size='12G', nthreads=4)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2025-02-28T10:00:52.829878Z","iopub.execute_input":"2025-02-28T10:00:52.831004Z","iopub.status.idle":"2025-02-28T10:01:00.227013Z","shell.execute_reply.started":"2025-02-28T10:00:52.830959Z","shell.execute_reply":"2025-02-28T10:01:00.225588Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# prepare data for upload in H2O environment\n\n# use only common columns\nselect_cols = features_num_train_test + features_cat_train_test\ny = df_train[target]\ndf_train = df_train[select_cols]\ndf_test = df_test[select_cols]\n\ndf_train['fold'] = 'train'\ndf_test['fold'] = 'test'\n\ndf_train[target] = y\ndf_test[target] = 0\n\n# combine train & test in one data frame\ndf = pd.concat([df_train, df_test])","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:00.228605Z","iopub.execute_input":"2025-02-28T10:01:00.229003Z","iopub.status.idle":"2025-02-28T10:01:00.247607Z","shell.execute_reply.started":"2025-02-28T10:01:00.228963Z","shell.execute_reply":"2025-02-28T10:01:00.246430Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# upload\ndf_hex = h2o.H2OFrame(df)\n# and split again\ntrain_hex = df_hex[df_hex['fold']=='train']\ntest_hex = df_hex[df_hex['fold']=='test']","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:00.250612Z","iopub.execute_input":"2025-02-28T10:01:00.250992Z","iopub.status.idle":"2025-02-28T10:01:02.208986Z","shell.execute_reply.started":"2025-02-28T10:01:00.250960Z","shell.execute_reply":"2025-02-28T10:01:02.207911Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# force categorical target\ntrain_hex[target] = train_hex[target].asfactor()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:02.210264Z","iopub.execute_input":"2025-02-28T10:01:02.210687Z","iopub.status.idle":"2025-02-28T10:01:02.555534Z","shell.execute_reply.started":"2025-02-28T10:01:02.210644Z","shell.execute_reply":"2025-02-28T10:01:02.554302Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define model\ngbm_model = H2OGradientBoostingEstimator(distribution = 'multinomial',\n                                         nfolds = 10,\n                                         ntrees = 50,\n                                         learn_rate = 0.1,\n                                         max_depth = 5,\n                                         col_sample_rate = 0.3,\n                                         # stopping_rounds = 10,\n                                         # stopping_tolerance = 0.0001,\n                                         # stopping_metric = 'mean_per_class_error',\n                                         # balance_classes = True,\n                                         score_each_iteration = False,                                          \n                                         seed=my_random_seed)\n\n# and train model\nfeatures = features_num_train_test + features_cat_train_test\ngbm_model.train(features, target, training_frame = train_hex);","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:02.557058Z","iopub.execute_input":"2025-02-28T10:01:02.557522Z","iopub.status.idle":"2025-02-28T10:01:27.415621Z","shell.execute_reply.started":"2025-02-28T10:01:02.557477Z","shell.execute_reply":"2025-02-28T10:01:27.414053Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# short summary of model\ngbm_model.show_summary()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:27.417460Z","iopub.execute_input":"2025-02-28T10:01:27.417956Z","iopub.status.idle":"2025-02-28T10:01:27.427156Z","shell.execute_reply.started":"2025-02-28T10:01:27.417909Z","shell.execute_reply":"2025-02-28T10:01:27.426045Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# show cross validation results\ngbm_model.cross_validation_metrics_summary().as_data_frame()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:27.428379Z","iopub.execute_input":"2025-02-28T10:01:27.428802Z","iopub.status.idle":"2025-02-28T10:01:27.468201Z","shell.execute_reply.started":"2025-02-28T10:01:27.428760Z","shell.execute_reply":"2025-02-28T10:01:27.467179Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# variable importance\ngbm_model.varimp_plot(30);","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:27.470441Z","iopub.execute_input":"2025-02-28T10:01:27.470922Z","iopub.status.idle":"2025-02-28T10:01:27.928862Z","shell.execute_reply.started":"2025-02-28T10:01:27.470874Z","shell.execute_reply":"2025-02-28T10:01:27.927608Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Evaluate on training set","metadata":{}},{"cell_type":"code","source":"# predict on training data\npred_train = gbm_model.predict(train_hex)\npred_train = pred_train.as_data_frame();\npred_train.head()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:27.930535Z","iopub.execute_input":"2025-02-28T10:01:27.931001Z","iopub.status.idle":"2025-02-28T10:01:28.261263Z","shell.execute_reply.started":"2025-02-28T10:01:27.930957Z","shell.execute_reply":"2025-02-28T10:01:28.260263Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# summary of predictions\nprint(pred_train.predict.value_counts())\npred_train.predict.value_counts().plot(kind='bar', color=default_color_3)\nplt.title('Predictions - Train')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:28.262444Z","iopub.execute_input":"2025-02-28T10:01:28.262868Z","iopub.status.idle":"2025-02-28T10:01:28.476343Z","shell.execute_reply.started":"2025-02-28T10:01:28.262806Z","shell.execute_reply":"2025-02-28T10:01:28.475258Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# confusion matrix\nconf_train = pd.crosstab(pred_train.predict, df_train[target])\nsns.heatmap(conf_train, annot=True, cmap='Reds', \n            fmt='.0f', linecolor='black', linewidths=0.5)\nplt.title('Confusion Matrix - Training')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:28.477657Z","iopub.execute_input":"2025-02-28T10:01:28.478006Z","iopub.status.idle":"2025-02-28T10:01:28.750844Z","shell.execute_reply.started":"2025-02-28T10:01:28.477973Z","shell.execute_reply":"2025-02-28T10:01:28.749301Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot predicted probabilities\ng = sns.pairplot(pred_train, hue='predict')\ng.fig.suptitle('Predicted Probabilities - Train', y=1.05)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:02:50.365653Z","iopub.execute_input":"2025-02-28T10:02:50.366140Z","iopub.status.idle":"2025-02-28T10:02:56.399406Z","shell.execute_reply.started":"2025-02-28T10:02:50.366102Z","shell.execute_reply":"2025-02-28T10:02:56.398338Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Evaluate on test set","metadata":{}},{"cell_type":"code","source":"# predict on test data\npred_test = gbm_model.predict(test_hex)\npred_test = pred_test.as_data_frame();\npred_test.head()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:35.002130Z","iopub.execute_input":"2025-02-28T10:01:35.002453Z","iopub.status.idle":"2025-02-28T10:01:35.382221Z","shell.execute_reply.started":"2025-02-28T10:01:35.002424Z","shell.execute_reply":"2025-02-28T10:01:35.380991Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# summary of predictions\nprint(pred_test.predict.value_counts())\npred_test.predict.value_counts().plot(kind='bar', color=default_color_3)\nplt.title('Predictions - Test')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:35.383724Z","iopub.execute_input":"2025-02-28T10:01:35.384109Z","iopub.status.idle":"2025-02-28T10:01:35.559747Z","shell.execute_reply.started":"2025-02-28T10:01:35.384076Z","shell.execute_reply":"2025-02-28T10:01:35.558482Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# submission\ndf_sub[target] = pred_test.predict\ndf_sub.head(10)","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:35.561237Z","iopub.execute_input":"2025-02-28T10:01:35.561659Z","iopub.status.idle":"2025-02-28T10:01:35.576139Z","shell.execute_reply.started":"2025-02-28T10:01:35.561617Z","shell.execute_reply":"2025-02-28T10:01:35.574893Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# save file\ndf_sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2025-02-28T10:01:35.577380Z","iopub.execute_input":"2025-02-28T10:01:35.577847Z","iopub.status.idle":"2025-02-28T10:01:35.586517Z","shell.execute_reply.started":"2025-02-28T10:01:35.577785Z","shell.execute_reply":"2025-02-28T10:01:35.585420Z"},"trusted":true},"outputs":[],"execution_count":null}]}