{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T14:16:24.184190Z","iopub.execute_input":"2024-12-07T14:16:24.185230Z","iopub.status.idle":"2024-12-07T14:16:26.309760Z","shell.execute_reply.started":"2024-12-07T14:16:24.185103Z","shell.execute_reply":"2024-12-07T14:16:26.308599Z"}},"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":"2024-12-07T14:16:26.312154Z","iopub.execute_input":"2024-12-07T14:16:26.312491Z","iopub.status.idle":"2024-12-07T14:16:26.319005Z","shell.execute_reply.started":"2024-12-07T14:16:26.312462Z","shell.execute_reply":"2024-12-07T14:16:26.317673Z"},"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":"2024-12-07T14:16:26.320439Z","iopub.execute_input":"2024-12-07T14:16:26.320774Z","iopub.status.idle":"2024-12-07T14:16:26.429370Z","shell.execute_reply.started":"2024-12-07T14:16:26.320744Z","shell.execute_reply":"2024-12-07T14:16:26.428150Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# show data dictionary\ndf_dict","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:16:26.430803Z","iopub.execute_input":"2024-12-07T14:16:26.431250Z","iopub.status.idle":"2024-12-07T14:16:26.464126Z","shell.execute_reply.started":"2024-12-07T14:16:26.431208Z","shell.execute_reply":"2024-12-07T14:16:26.463084Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# preview training data\ndf_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:16:26.468146Z","iopub.execute_input":"2024-12-07T14:16:26.469018Z","iopub.status.idle":"2024-12-07T14:16:26.563928Z","shell.execute_reply.started":"2024-12-07T14:16:26.468964Z","shell.execute_reply":"2024-12-07T14:16:26.562917Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# structure of data - train\ndf_train.info()","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:16:26.565338Z","iopub.execute_input":"2024-12-07T14:16:26.565743Z","iopub.status.idle":"2024-12-07T14:16:26.599166Z","shell.execute_reply.started":"2024-12-07T14:16:26.565702Z","shell.execute_reply":"2024-12-07T14:16:26.598038Z"},"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":"2024-12-07T14:16:26.600474Z","iopub.execute_input":"2024-12-07T14:16:26.600813Z","iopub.status.idle":"2024-12-07T14:16:26.611133Z","shell.execute_reply.started":"2024-12-07T14:16:26.600783Z","shell.execute_reply":"2024-12-07T14:16:26.609897Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# structure of data - test\ndf_test.info()","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:16:26.612480Z","iopub.execute_input":"2024-12-07T14:16:26.612793Z","iopub.status.idle":"2024-12-07T14:16:26.632405Z","shell.execute_reply.started":"2024-12-07T14:16:26.612765Z","shell.execute_reply":"2024-12-07T14:16:26.631276Z"},"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":"2024-12-07T14:16:26.633992Z","iopub.execute_input":"2024-12-07T14:16:26.634411Z","iopub.status.idle":"2024-12-07T14:16:26.886887Z","shell.execute_reply.started":"2024-12-07T14:16:26.634371Z","shell.execute_reply":"2024-12-07T14:16:26.885548Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# basic stats - test\ndf_test.describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:16:26.888603Z","iopub.execute_input":"2024-12-07T14:16:26.889151Z","iopub.status.idle":"2024-12-07T14:16:27.069719Z","shell.execute_reply.started":"2024-12-07T14:16:26.889100Z","shell.execute_reply":"2024-12-07T14:16:27.068463Z"},"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":"2024-12-07T14:16:27.071276Z","iopub.execute_input":"2024-12-07T14:16:27.071615Z","iopub.status.idle":"2024-12-07T14:16:27.080615Z","shell.execute_reply.started":"2024-12-07T14:16:27.071583Z","shell.execute_reply":"2024-12-07T14:16:27.079306Z"},"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":"2024-12-07T14:16:27.082251Z","iopub.execute_input":"2024-12-07T14:16:27.082694Z","iopub.status.idle":"2024-12-07T14:16:43.363557Z","shell.execute_reply.started":"2024-12-07T14:16:27.082649Z","shell.execute_reply":"2024-12-07T14:16:43.362164Z"},"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":"2024-12-07T14:16:43.365472Z","iopub.execute_input":"2024-12-07T14:16:43.366068Z","iopub.status.idle":"2024-12-07T14:16:54.386986Z","shell.execute_reply.started":"2024-12-07T14:16:43.366022Z","shell.execute_reply":"2024-12-07T14:16:54.385945Z"},"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, sharex=ax1)\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":"2024-12-07T14:16:54.393681Z","iopub.execute_input":"2024-12-07T14:16:54.394103Z","iopub.status.idle":"2024-12-07T14:16:57.467015Z","shell.execute_reply.started":"2024-12-07T14:16:54.394072Z","shell.execute_reply":"2024-12-07T14:16:57.465944Z"},"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":"2024-12-07T14:16:57.468326Z","iopub.execute_input":"2024-12-07T14:16:57.468641Z","iopub.status.idle":"2024-12-07T14:16:57.809875Z","shell.execute_reply.started":"2024-12-07T14:16:57.468612Z","shell.execute_reply":"2024-12-07T14:16:57.808649Z"},"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":"2024-12-07T14:16:57.811456Z","iopub.execute_input":"2024-12-07T14:16:57.811928Z","iopub.status.idle":"2024-12-07T14:17:00.344722Z","shell.execute_reply.started":"2024-12-07T14:16:57.811870Z","shell.execute_reply":"2024-12-07T14:17:00.343578Z"},"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":"2024-12-07T14:17:00.346451Z","iopub.execute_input":"2024-12-07T14:17:00.346770Z","iopub.status.idle":"2024-12-07T14:17:00.526001Z","shell.execute_reply.started":"2024-12-07T14:17:00.346741Z","shell.execute_reply":"2024-12-07T14:17:00.524921Z"},"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":"2024-12-07T14:17:00.527271Z","iopub.execute_input":"2024-12-07T14:17:00.527572Z","iopub.status.idle":"2024-12-07T14:17:11.367917Z","shell.execute_reply.started":"2024-12-07T14:17:00.527544Z","shell.execute_reply":"2024-12-07T14:17:11.366762Z"},"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":"2024-12-07T14:17:11.369369Z","iopub.execute_input":"2024-12-07T14:17:11.369697Z","iopub.status.idle":"2024-12-07T14:17:14.349570Z","shell.execute_reply.started":"2024-12-07T14:17:11.369667Z","shell.execute_reply":"2024-12-07T14:17:14.348383Z"},"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.10358Z","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":"2024-12-07T14:17:14.351207Z","iopub.execute_input":"2024-12-07T14:17:14.351640Z","iopub.status.idle":"2024-12-07T14:17:18.902719Z","shell.execute_reply.started":"2024-12-07T14:17:14.351597Z","shell.execute_reply":"2024-12-07T14:17:18.901554Z"},"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":"2024-12-07T14:17:18.904170Z","iopub.execute_input":"2024-12-07T14:17:18.904488Z","iopub.status.idle":"2024-12-07T14:17:19.090874Z","shell.execute_reply.started":"2024-12-07T14:17:18.904458Z","shell.execute_reply":"2024-12-07T14:17:19.089593Z"},"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":"2024-12-07T14:17:19.092191Z","iopub.execute_input":"2024-12-07T14:17:19.092490Z","iopub.status.idle":"2024-12-07T14:17:19.203292Z","shell.execute_reply.started":"2024-12-07T14:17:19.092462Z","shell.execute_reply":"2024-12-07T14:17:19.202053Z"},"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":"2024-12-07T14:17:19.204682Z","iopub.execute_input":"2024-12-07T14:17:19.205047Z","iopub.status.idle":"2024-12-07T14:17:23.223002Z","shell.execute_reply.started":"2024-12-07T14:17:19.205015Z","shell.execute_reply":"2024-12-07T14:17:23.221759Z"},"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":"2024-12-07T14:17:23.224611Z","iopub.execute_input":"2024-12-07T14:17:23.225015Z","iopub.status.idle":"2024-12-07T14:17:23.357730Z","shell.execute_reply.started":"2024-12-07T14:17:23.224978Z","shell.execute_reply":"2024-12-07T14:17:23.355937Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# preview\ndf_ts_example","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:17:23.360056Z","iopub.execute_input":"2024-12-07T14:17:23.360496Z","iopub.status.idle":"2024-12-07T14:17:23.392336Z","shell.execute_reply.started":"2024-12-07T14:17:23.360454Z","shell.execute_reply":"2024-12-07T14:17:23.391100Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# basic stats\ndf_ts_example.describe()","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:17:23.394030Z","iopub.execute_input":"2024-12-07T14:17:23.394496Z","iopub.status.idle":"2024-12-07T14:17:23.462917Z","shell.execute_reply.started":"2024-12-07T14:17:23.394449Z","shell.execute_reply":"2024-12-07T14:17:23.461726Z"},"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":"2024-12-07T14:17:23.464238Z","iopub.execute_input":"2024-12-07T14:17:23.464524Z","iopub.status.idle":"2024-12-07T14:17:23.904494Z","shell.execute_reply.started":"2024-12-07T14:17:23.464498Z","shell.execute_reply":"2024-12-07T14:17:23.903430Z"},"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":"2024-12-07T14:17:23.906107Z","iopub.execute_input":"2024-12-07T14:17:23.906537Z","iopub.status.idle":"2024-12-07T14:17:23.913085Z","shell.execute_reply.started":"2024-12-07T14:17:23.906494Z","shell.execute_reply":"2024-12-07T14:17:23.911917Z"},"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":"2024-12-07T14:17:23.914482Z","iopub.execute_input":"2024-12-07T14:17:23.914760Z","iopub.status.idle":"2024-12-07T14:17:26.859384Z","shell.execute_reply.started":"2024-12-07T14:17:23.914735Z","shell.execute_reply":"2024-12-07T14:17:26.858223Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:17:26.860772Z","iopub.execute_input":"2024-12-07T14:17:26.861099Z","iopub.status.idle":"2024-12-07T14:17:28.448872Z","shell.execute_reply.started":"2024-12-07T14:17:26.861072Z","shell.execute_reply":"2024-12-07T14:17:28.446985Z"},"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":"2024-12-07T14:17:28.450967Z","iopub.execute_input":"2024-12-07T14:17:28.451303Z","iopub.status.idle":"2024-12-07T14:17:28.593593Z","shell.execute_reply.started":"2024-12-07T14:17:28.451275Z","shell.execute_reply":"2024-12-07T14:17:28.592423Z"},"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":"2024-12-07T14:17:28.594750Z","iopub.execute_input":"2024-12-07T14:17:28.595257Z","iopub.status.idle":"2024-12-07T14:17:36.216292Z","shell.execute_reply.started":"2024-12-07T14:17:28.595223Z","shell.execute_reply":"2024-12-07T14:17:36.215040Z"},"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":"2024-12-07T14:17:36.218047Z","iopub.execute_input":"2024-12-07T14:17:36.219046Z","iopub.status.idle":"2024-12-07T14:17:36.245074Z","shell.execute_reply.started":"2024-12-07T14:17:36.218992Z","shell.execute_reply":"2024-12-07T14:17:36.243969Z"},"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":"2024-12-07T14:17:36.247055Z","iopub.execute_input":"2024-12-07T14:17:36.247477Z","iopub.status.idle":"2024-12-07T14:17:37.992155Z","shell.execute_reply.started":"2024-12-07T14:17:36.247437Z","shell.execute_reply":"2024-12-07T14:17:37.989277Z"},"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":"2024-12-07T14:17:37.997538Z","iopub.execute_input":"2024-12-07T14:17:37.997975Z","iopub.status.idle":"2024-12-07T14:17:38.355664Z","shell.execute_reply.started":"2024-12-07T14:17:37.997933Z","shell.execute_reply":"2024-12-07T14:17:38.354210Z"},"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":"2024-12-07T14:17:38.357132Z","iopub.execute_input":"2024-12-07T14:17:38.360233Z","iopub.status.idle":"2024-12-07T14:17:59.600307Z","shell.execute_reply.started":"2024-12-07T14:17:38.360179Z","shell.execute_reply":"2024-12-07T14:17:59.598915Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# short summary of model\ngbm_model.show_summary()","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:17:59.602145Z","iopub.execute_input":"2024-12-07T14:17:59.602559Z","iopub.status.idle":"2024-12-07T14:17:59.612803Z","shell.execute_reply.started":"2024-12-07T14:17:59.602520Z","shell.execute_reply":"2024-12-07T14:17:59.611599Z"},"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":"2024-12-07T14:17:59.614091Z","iopub.execute_input":"2024-12-07T14:17:59.614485Z","iopub.status.idle":"2024-12-07T14:17:59.654524Z","shell.execute_reply.started":"2024-12-07T14:17:59.614446Z","shell.execute_reply":"2024-12-07T14:17:59.653454Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# variable importance\ngbm_model.varimp_plot(30);","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:17:59.655764Z","iopub.execute_input":"2024-12-07T14:17:59.656186Z","iopub.status.idle":"2024-12-07T14:18:00.160673Z","shell.execute_reply.started":"2024-12-07T14:17:59.656146Z","shell.execute_reply":"2024-12-07T14:18:00.159615Z"},"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":"2024-12-07T14:18:00.161955Z","iopub.execute_input":"2024-12-07T14:18:00.162350Z","iopub.status.idle":"2024-12-07T14:18:00.707724Z","shell.execute_reply.started":"2024-12-07T14:18:00.162311Z","shell.execute_reply":"2024-12-07T14:18:00.706753Z"},"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":"2024-12-07T14:18:00.716805Z","iopub.execute_input":"2024-12-07T14:18:00.717265Z","iopub.status.idle":"2024-12-07T14:18:00.920439Z","shell.execute_reply.started":"2024-12-07T14:18:00.717225Z","shell.execute_reply":"2024-12-07T14:18:00.919274Z"},"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":"2024-12-07T14:18:00.921908Z","iopub.execute_input":"2024-12-07T14:18:00.922719Z","iopub.status.idle":"2024-12-07T14:18:01.223934Z","shell.execute_reply.started":"2024-12-07T14:18:00.922676Z","shell.execute_reply":"2024-12-07T14:18:01.222750Z"},"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')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:18:01.225293Z","iopub.execute_input":"2024-12-07T14:18:01.225715Z","iopub.status.idle":"2024-12-07T14:18:07.202670Z","shell.execute_reply.started":"2024-12-07T14:18:01.225663Z","shell.execute_reply":"2024-12-07T14:18:07.201465Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Evaluate on test set","metadata":{}},{"cell_type":"code","source":"# predict on training data\npred_test = gbm_model.predict(test_hex)\npred_test = pred_test.as_data_frame();\npred_test.head()","metadata":{"execution":{"iopub.status.busy":"2024-12-07T14:18:07.204425Z","iopub.execute_input":"2024-12-07T14:18:07.204881Z","iopub.status.idle":"2024-12-07T14:18:07.579384Z","shell.execute_reply.started":"2024-12-07T14:18:07.204814Z","shell.execute_reply":"2024-12-07T14:18:07.578396Z"},"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":"2024-12-07T14:18:07.580836Z","iopub.execute_input":"2024-12-07T14:18:07.581282Z","iopub.status.idle":"2024-12-07T14:18:07.774840Z","shell.execute_reply.started":"2024-12-07T14:18:07.581240Z","shell.execute_reply":"2024-12-07T14:18:07.773754Z"},"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":"2024-12-07T14:18:07.776239Z","iopub.execute_input":"2024-12-07T14:18:07.776542Z","iopub.status.idle":"2024-12-07T14:18:07.787199Z","shell.execute_reply.started":"2024-12-07T14:18:07.776513Z","shell.execute_reply":"2024-12-07T14:18:07.786105Z"},"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":"2024-12-07T14:18:07.788729Z","iopub.execute_input":"2024-12-07T14:18:07.789706Z","iopub.status.idle":"2024-12-07T14:18:07.799406Z","shell.execute_reply.started":"2024-12-07T14:18:07.789633Z","shell.execute_reply":"2024-12-07T14:18:07.798195Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Work in progress...","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-09-28T18:56:17.135691Z","iopub.execute_input":"2023-09-28T18:56:17.136136Z","iopub.status.idle":"2023-09-28T18:56:17.204689Z","shell.execute_reply.started":"2023-09-28T18:56:17.136101Z","shell.execute_reply":"2023-09-28T18:56:17.20336Z"}}}]}