{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-08T16:56:29.306870Z","iopub.execute_input":"2022-08-08T16:56:29.307453Z","iopub.status.idle":"2022-08-08T16:56:29.343904Z","shell.execute_reply.started":"2022-08-08T16:56:29.307345Z","shell.execute_reply":"2022-08-08T16:56:29.342639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <font color= 'blueviolet'> SECTION 1: Basic EDA </font>","metadata":{}},{"cell_type":"markdown","source":"## Load Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as mpatches\nimport seaborn as sns\nfrom sklearn.metrics import accuracy_score, f1_score, recall_score, precision_score, precision_recall_curve, confusion_matrix, classification_report\nfrom sklearn.model_selection import train_test_split, StratifiedKFold, cross_val_score\nfrom functools import partial\nimport pickle\nimport h2o\nfrom h2o.automl import H2OAutoML\n\npd.set_option('display.max_column', None)\npd.set_option('display.max_row', None)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:29.345659Z","iopub.execute_input":"2022-08-08T16:56:29.346092Z","iopub.status.idle":"2022-08-08T16:56:31.069894Z","shell.execute_reply.started":"2022-08-08T16:56:29.346056Z","shell.execute_reply":"2022-08-08T16:56:31.067081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load train and test Datasets","metadata":{"execution":{"iopub.status.busy":"2022-08-03T11:58:12.214590Z","iopub.execute_input":"2022-08-03T11:58:12.215595Z","iopub.status.idle":"2022-08-03T11:58:12.220864Z","shell.execute_reply.started":"2022-08-03T11:58:12.215552Z","shell.execute_reply":"2022-08-03T11:58:12.219666Z"}}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/predict-potential-spammers-on-fiverr/train.csv')\nsubmission_df = pd.read_csv('/kaggle/input/predict-potential-spammers-on-fiverr/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:31.071875Z","iopub.execute_input":"2022-08-08T16:56:31.072422Z","iopub.status.idle":"2022-08-08T16:56:33.804881Z","shell.execute_reply.started":"2022-08-08T16:56:31.072358Z","shell.execute_reply":"2022-08-08T16:56:33.803895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:33.807685Z","iopub.execute_input":"2022-08-08T16:56:33.808505Z","iopub.status.idle":"2022-08-08T16:56:33.855627Z","shell.execute_reply.started":"2022-08-08T16:56:33.808456Z","shell.execute_reply":"2022-08-08T16:56:33.854090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:33.857154Z","iopub.execute_input":"2022-08-08T16:56:33.857667Z","iopub.status.idle":"2022-08-08T16:56:33.868222Z","shell.execute_reply.started":"2022-08-08T16:56:33.857615Z","shell.execute_reply":"2022-08-08T16:56:33.866956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# missing values in all columns\ntrain.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:33.869559Z","iopub.execute_input":"2022-08-08T16:56:33.870029Z","iopub.status.idle":"2022-08-08T16:56:33.938048Z","shell.execute_reply.started":"2022-08-08T16:56:33.869980Z","shell.execute_reply":"2022-08-08T16:56:33.937063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Target Variable Percentages","metadata":{}},{"cell_type":"code","source":"train.label.value_counts(normalize=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:33.941519Z","iopub.execute_input":"2022-08-08T16:56:33.942378Z","iopub.status.idle":"2022-08-08T16:56:33.964993Z","shell.execute_reply.started":"2022-08-08T16:56:33.942339Z","shell.execute_reply":"2022-08-08T16:56:33.963739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Columns","metadata":{}},{"cell_type":"code","source":"feature_cols = [col for col in train if col.startswith('X')]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:33.967129Z","iopub.execute_input":"2022-08-08T16:56:33.968067Z","iopub.status.idle":"2022-08-08T16:56:33.975597Z","shell.execute_reply.started":"2022-08-08T16:56:33.968018Z","shell.execute_reply":"2022-08-08T16:56:33.974235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(feature_cols)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:33.980762Z","iopub.execute_input":"2022-08-08T16:56:33.981528Z","iopub.status.idle":"2022-08-08T16:56:33.989311Z","shell.execute_reply.started":"2022-08-08T16:56:33.981478Z","shell.execute_reply":"2022-08-08T16:56:33.988334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Remove Constant Columns (Columns with no Variation)","metadata":{}},{"cell_type":"code","source":"no_variation_cols = list()\nfor col in feature_cols:\n    if train[col].nunique() == 1:\n        no_variation_cols.append(col)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:33.990659Z","iopub.execute_input":"2022-08-08T16:56:33.991217Z","iopub.status.idle":"2022-08-08T16:56:34.155738Z","shell.execute_reply.started":"2022-08-08T16:56:33.991176Z","shell.execute_reply":"2022-08-08T16:56:34.154748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(no_variation_cols)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.156985Z","iopub.execute_input":"2022-08-08T16:56:34.157540Z","iopub.status.idle":"2022-08-08T16:56:34.162607Z","shell.execute_reply.started":"2022-08-08T16:56:34.157506Z","shell.execute_reply":"2022-08-08T16:56:34.161505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(columns = no_variation_cols, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.164161Z","iopub.execute_input":"2022-08-08T16:56:34.165023Z","iopub.status.idle":"2022-08-08T16:56:34.230073Z","shell.execute_reply.started":"2022-08-08T16:56:34.164987Z","shell.execute_reply":"2022-08-08T16:56:34.228779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.231927Z","iopub.execute_input":"2022-08-08T16:56:34.232469Z","iopub.status.idle":"2022-08-08T16:56:34.241119Z","shell.execute_reply.started":"2022-08-08T16:56:34.232418Z","shell.execute_reply":"2022-08-08T16:56:34.239685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Binary Columns","metadata":{}},{"cell_type":"code","source":"# remove no variation columns from feature columns list\nfeature_cols = [i for i in feature_cols + no_variation_cols if i not in feature_cols or i not in no_variation_cols]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.243145Z","iopub.execute_input":"2022-08-08T16:56:34.244232Z","iopub.status.idle":"2022-08-08T16:56:34.250219Z","shell.execute_reply.started":"2022-08-08T16:56:34.244186Z","shell.execute_reply":"2022-08-08T16:56:34.249172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binary_col = list()\nfor col in feature_cols:\n    if train[col].nunique() == 2:\n        binary_col.append(col)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.251506Z","iopub.execute_input":"2022-08-08T16:56:34.252010Z","iopub.status.idle":"2022-08-08T16:56:34.390266Z","shell.execute_reply.started":"2022-08-08T16:56:34.251980Z","shell.execute_reply":"2022-08-08T16:56:34.389296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(binary_col)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.391589Z","iopub.execute_input":"2022-08-08T16:56:34.392116Z","iopub.status.idle":"2022-08-08T16:56:34.397223Z","shell.execute_reply.started":"2022-08-08T16:56:34.392082Z","shell.execute_reply":"2022-08-08T16:56:34.396415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Stacked Bar chart of Binary Variables","metadata":{}},{"cell_type":"code","source":"# Percentage of 1s in each column\nones = train[binary_col].astype(bool).sum(axis=0).reset_index(name = 'Percentage')\nones.rename(columns = {'index':'Column'}, inplace = True)\n\nones['Percentage'] = ones['Percentage']/len(train)*100\n\nones","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.398702Z","iopub.execute_input":"2022-08-08T16:56:34.399774Z","iopub.status.idle":"2022-08-08T16:56:34.430632Z","shell.execute_reply.started":"2022-08-08T16:56:34.399721Z","shell.execute_reply":"2022-08-08T16:56:34.429769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# complete column\ntotal = ones.copy()\n\ntotal.drop(columns = 'Percentage', inplace = True)\ntotal['Percentage'] = 100\n\ntotal","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.432207Z","iopub.execute_input":"2022-08-08T16:56:34.432576Z","iopub.status.idle":"2022-08-08T16:56:34.445323Z","shell.execute_reply.started":"2022-08-08T16:56:34.432543Z","shell.execute_reply":"2022-08-08T16:56:34.444070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create stacked bar chart\n\n# set the figure size\nplt.figure(figsize=(12, 8))\n\nbar1 = sns.barplot(x=\"Column\",  y=\"Percentage\", data=total, color='yellowgreen')\nbar2 = sns.barplot(x=\"Column\", y=\"Percentage\", data=ones, color='blueviolet')\n\n# add legend\ntop_bar = mpatches.Patch(color='yellowgreen', label='0')\nbottom_bar = mpatches.Patch(color='blueviolet', label='1')\nplt.legend(handles=[top_bar, bottom_bar])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.446816Z","iopub.execute_input":"2022-08-08T16:56:34.447956Z","iopub.status.idle":"2022-08-08T16:56:34.815530Z","shell.execute_reply.started":"2022-08-08T16:56:34.447913Z","shell.execute_reply":"2022-08-08T16:56:34.813944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Histograms of Non Binary Variables Seperated by Label","metadata":{}},{"cell_type":"code","source":"non_binary_cols = [i for i in feature_cols + binary_col if i not in feature_cols or i not in binary_col]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.816951Z","iopub.execute_input":"2022-08-08T16:56:34.817960Z","iopub.status.idle":"2022-08-08T16:56:34.824775Z","shell.execute_reply.started":"2022-08-08T16:56:34.817915Z","shell.execute_reply":"2022-08-08T16:56:34.823210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# select only non binary columns\nprint(non_binary_cols)\n\nprint(f'Number of Non-Binary Columns : {len(non_binary_cols)}')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.826515Z","iopub.execute_input":"2022-08-08T16:56:34.826945Z","iopub.status.idle":"2022-08-08T16:56:34.835629Z","shell.execute_reply.started":"2022-08-08T16:56:34.826891Z","shell.execute_reply":"2022-08-08T16:56:34.834581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Boxplots of Non Binary Variables Seperated by Label","metadata":{}},{"cell_type":"code","source":"nrows = 10\nncols = 4\nfig, axes = plt.subplots(nrows, ncols, figsize=(18, 35))\nplt.subplots_adjust(hspace = 0.3)\n\nfor idx, x in enumerate(non_binary_cols):\n    \n    sns.boxplot(x = 'label' , y = x, data=train,\n                palette=['yellowgreen', 'blueviolet'],\n                 ax = axes[np.floor_divide(idx, ncols), idx % ncols])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:34.837158Z","iopub.execute_input":"2022-08-08T16:56:34.837812Z","iopub.status.idle":"2022-08-08T16:56:42.262067Z","shell.execute_reply.started":"2022-08-08T16:56:34.837775Z","shell.execute_reply":"2022-08-08T16:56:42.260645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Violin of Non Binary Variables Seperated by Label","metadata":{}},{"cell_type":"code","source":"nrows = 10\nncols = 4\nfig, axes = plt.subplots(nrows, ncols, figsize=(18, 35))\nplt.subplots_adjust(hspace = 0.3)\n\nfor idx, x in enumerate(non_binary_cols):\n    \n    sns.violinplot(x = 'label' , y = x, data=train,\n                palette=['yellowgreen', 'blueviolet'],\n                 ax = axes[np.floor_divide(idx, ncols), idx % ncols])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:56:42.263611Z","iopub.execute_input":"2022-08-08T16:56:42.264002Z","iopub.status.idle":"2022-08-08T16:57:23.765167Z","shell.execute_reply.started":"2022-08-08T16:56:42.263967Z","shell.execute_reply":"2022-08-08T16:57:23.763898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Correlation Plot","metadata":{}},{"cell_type":"code","source":"# set the figure size\nplt.figure(figsize=(15, 10))\n\ncorr = train.corr()\nsns.heatmap(corr, \n            xticklabels=corr.columns.values,\n            yticklabels=corr.columns.values)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:23.771673Z","iopub.execute_input":"2022-08-08T16:57:23.772168Z","iopub.status.idle":"2022-08-08T16:57:27.380561Z","shell.execute_reply.started":"2022-08-08T16:57:23.772133Z","shell.execute_reply":"2022-08-08T16:57:27.379491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.381955Z","iopub.execute_input":"2022-08-08T16:57:27.382309Z","iopub.status.idle":"2022-08-08T16:57:27.389211Z","shell.execute_reply.started":"2022-08-08T16:57:27.382277Z","shell.execute_reply":"2022-08-08T16:57:27.388132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Handle missing values","metadata":{}},{"cell_type":"code","source":"# count of missing values in the feature X13\ntrain.X13.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.390708Z","iopub.execute_input":"2022-08-08T16:57:27.391029Z","iopub.status.idle":"2022-08-08T16:57:27.405946Z","shell.execute_reply.started":"2022-08-08T16:57:27.391001Z","shell.execute_reply":"2022-08-08T16:57:27.404592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train['X13'].isna()]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.407237Z","iopub.execute_input":"2022-08-08T16:57:27.407578Z","iopub.status.idle":"2022-08-08T16:57:27.438525Z","shell.execute_reply.started":"2022-08-08T16:57:27.407549Z","shell.execute_reply":"2022-08-08T16:57:27.437485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.X13.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.439846Z","iopub.execute_input":"2022-08-08T16:57:27.440155Z","iopub.status.idle":"2022-08-08T16:57:27.453214Z","shell.execute_reply.started":"2022-08-08T16:57:27.440127Z","shell.execute_reply":"2022-08-08T16:57:27.452108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.crosstab(train.X13, train.label)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.454936Z","iopub.execute_input":"2022-08-08T16:57:27.455865Z","iopub.status.idle":"2022-08-08T16:57:27.523782Z","shell.execute_reply.started":"2022-08-08T16:57:27.455818Z","shell.execute_reply":"2022-08-08T16:57:27.522691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#median imputation\ntrain['X13'].fillna(value=round(train['X13'].median()), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.525095Z","iopub.execute_input":"2022-08-08T16:57:27.525453Z","iopub.status.idle":"2022-08-08T16:57:27.538105Z","shell.execute_reply.started":"2022-08-08T16:57:27.525421Z","shell.execute_reply":"2022-08-08T16:57:27.537277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.X13.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.540105Z","iopub.execute_input":"2022-08-08T16:57:27.540558Z","iopub.status.idle":"2022-08-08T16:57:27.548466Z","shell.execute_reply.started":"2022-08-08T16:57:27.540523Z","shell.execute_reply":"2022-08-08T16:57:27.547414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Perform Similar Manipulations on Test Set","metadata":{}},{"cell_type":"code","source":"# Drop constant columns\nsubmission_df.drop(columns = no_variation_cols, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.549854Z","iopub.execute_input":"2022-08-08T16:57:27.550756Z","iopub.status.idle":"2022-08-08T16:57:27.560051Z","shell.execute_reply.started":"2022-08-08T16:57:27.550723Z","shell.execute_reply":"2022-08-08T16:57:27.559210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, submission_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.561911Z","iopub.execute_input":"2022-08-08T16:57:27.562742Z","iopub.status.idle":"2022-08-08T16:57:27.579050Z","shell.execute_reply.started":"2022-08-08T16:57:27.562697Z","shell.execute_reply":"2022-08-08T16:57:27.578080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.580490Z","iopub.execute_input":"2022-08-08T16:57:27.580850Z","iopub.status.idle":"2022-08-08T16:57:27.598392Z","shell.execute_reply.started":"2022-08-08T16:57:27.580818Z","shell.execute_reply":"2022-08-08T16:57:27.597327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <font color= 'blueviolet'> Section 2: H20 - AutoML </font>","metadata":{}},{"cell_type":"markdown","source":"## Set up H20","metadata":{}},{"cell_type":"code","source":"# initiate h20\nh2o.init()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:27.599729Z","iopub.execute_input":"2022-08-08T16:57:27.601042Z","iopub.status.idle":"2022-08-08T16:57:35.984131Z","shell.execute_reply.started":"2022-08-08T16:57:27.601009Z","shell.execute_reply":"2022-08-08T16:57:35.982580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h2o.cluster_info()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:35.989871Z","iopub.execute_input":"2022-08-08T16:57:35.992673Z","iopub.status.idle":"2022-08-08T16:57:36.008661Z","shell.execute_reply.started":"2022-08-08T16:57:35.992616Z","shell.execute_reply":"2022-08-08T16:57:36.007276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h2o.ls","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:36.010406Z","iopub.execute_input":"2022-08-08T16:57:36.011163Z","iopub.status.idle":"2022-08-08T16:57:36.155156Z","shell.execute_reply.started":"2022-08-08T16:57:36.011119Z","shell.execute_reply":"2022-08-08T16:57:36.153915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert to H20 dataframe","metadata":{}},{"cell_type":"code","source":"df = h2o.H2OFrame(train)\nsubmission_set = h2o.H2OFrame(submission_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:36.159397Z","iopub.execute_input":"2022-08-08T16:57:36.160726Z","iopub.status.idle":"2022-08-08T16:57:56.679875Z","shell.execute_reply.started":"2022-08-08T16:57:36.160678Z","shell.execute_reply":"2022-08-08T16:57:56.678641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:56.681863Z","iopub.execute_input":"2022-08-08T16:57:56.682604Z","iopub.status.idle":"2022-08-08T16:57:56.858663Z","shell.execute_reply.started":"2022-08-08T16:57:56.682567Z","shell.execute_reply":"2022-08-08T16:57:56.857303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:56.861039Z","iopub.execute_input":"2022-08-08T16:57:56.861928Z","iopub.status.idle":"2022-08-08T16:57:56.872387Z","shell.execute_reply.started":"2022-08-08T16:57:56.861881Z","shell.execute_reply":"2022-08-08T16:57:56.871271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop user id\ndf = df.drop('user_id')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:56.874504Z","iopub.execute_input":"2022-08-08T16:57:56.875390Z","iopub.status.idle":"2022-08-08T16:57:56.882915Z","shell.execute_reply.started":"2022-08-08T16:57:56.875333Z","shell.execute_reply":"2022-08-08T16:57:56.881596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split data into Train and Test, and Set up Response and Predictor Variables","metadata":{}},{"cell_type":"code","source":"# Split dataset giving the training dataset 75% of the data\ntrain, test = df.split_frame(ratios = [.80], seed = 69)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:56.886707Z","iopub.execute_input":"2022-08-08T16:57:56.887988Z","iopub.status.idle":"2022-08-08T16:57:58.432134Z","shell.execute_reply.started":"2022-08-08T16:57:56.887930Z","shell.execute_reply":"2022-08-08T16:57:58.429623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:58.434986Z","iopub.execute_input":"2022-08-08T16:57:58.436103Z","iopub.status.idle":"2022-08-08T16:57:58.447691Z","shell.execute_reply.started":"2022-08-08T16:57:58.436050Z","shell.execute_reply":"2022-08-08T16:57:58.446337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Identify predictors and response\nx = train.columns\ny = \"label\"\nx.remove(y)\n\n# For binary classification, response should be a factor\ntrain[y] = train[y].asfactor()\ntest[y] = test[y].asfactor()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:58.449983Z","iopub.execute_input":"2022-08-08T16:57:58.450521Z","iopub.status.idle":"2022-08-08T16:57:58.458998Z","shell.execute_reply.started":"2022-08-08T16:57:58.450476Z","shell.execute_reply":"2022-08-08T16:57:58.457771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set up AutoML and train","metadata":{}},{"cell_type":"code","source":"aml = H2OAutoML(max_models=50, seed=69, nfolds = 5)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:58.460852Z","iopub.execute_input":"2022-08-08T16:57:58.461734Z","iopub.status.idle":"2022-08-08T16:57:58.517419Z","shell.execute_reply.started":"2022-08-08T16:57:58.461700Z","shell.execute_reply":"2022-08-08T16:57:58.516360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aml.train(x=x, y=y, training_frame=train)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T16:57:58.520117Z","iopub.execute_input":"2022-08-08T16:57:58.520872Z","iopub.status.idle":"2022-08-08T18:19:43.620653Z","shell.execute_reply.started":"2022-08-08T16:57:58.520830Z","shell.execute_reply":"2022-08-08T18:19:43.618313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Leaderboard","metadata":{}},{"cell_type":"code","source":"lb = aml.leaderboard\n# Print all rows instead of default (10 rows)\nlb.head(rows=lb.nrows)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.622295Z","iopub.status.idle":"2022-08-08T18:19:43.623397Z","shell.execute_reply.started":"2022-08-08T18:19:43.623069Z","shell.execute_reply":"2022-08-08T18:19:43.623099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = aml.get_best_model()\nprint(best_model)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.625635Z","iopub.status.idle":"2022-08-08T18:19:43.626984Z","shell.execute_reply.started":"2022-08-08T18:19:43.626694Z","shell.execute_reply":"2022-08-08T18:19:43.626726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save best model path\nmodel_path = h2o.save_model(model=best_model,path='h20_model', force=True)\nprint(model_path)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.628300Z","iopub.status.idle":"2022-08-08T18:19:43.629434Z","shell.execute_reply.started":"2022-08-08T18:19:43.629150Z","shell.execute_reply":"2022-08-08T18:19:43.629178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make Predictions on Test Set","metadata":{}},{"cell_type":"code","source":"preds = aml.leader.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.631077Z","iopub.status.idle":"2022-08-08T18:19:43.632383Z","shell.execute_reply.started":"2022-08-08T18:19:43.632090Z","shell.execute_reply":"2022-08-08T18:19:43.632113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.633503Z","iopub.status.idle":"2022-08-08T18:19:43.634600Z","shell.execute_reply.started":"2022-08-08T18:19:43.634398Z","shell.execute_reply":"2022-08-08T18:19:43.634419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert predictions to pandas dataframe\npreds = preds.as_data_frame()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.635900Z","iopub.status.idle":"2022-08-08T18:19:43.636577Z","shell.execute_reply.started":"2022-08-08T18:19:43.636365Z","shell.execute_reply":"2022-08-08T18:19:43.636386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Description of predicted variables\npreds.describe().transpose()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.637495Z","iopub.status.idle":"2022-08-08T18:19:43.638240Z","shell.execute_reply.started":"2022-08-08T18:19:43.638050Z","shell.execute_reply":"2022-08-08T18:19:43.638069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.predict.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.640338Z","iopub.status.idle":"2022-08-08T18:19:43.640746Z","shell.execute_reply.started":"2022-08-08T18:19:43.640529Z","shell.execute_reply":"2022-08-08T18:19:43.640547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.642577Z","iopub.status.idle":"2022-08-08T18:19:43.642962Z","shell.execute_reply.started":"2022-08-08T18:19:43.642770Z","shell.execute_reply":"2022-08-08T18:19:43.642787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.644765Z","iopub.status.idle":"2022-08-08T18:19:43.645149Z","shell.execute_reply.started":"2022-08-08T18:19:43.644950Z","shell.execute_reply":"2022-08-08T18:19:43.644967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert test to pandas dataframe\ntest = test.as_data_frame()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.646316Z","iopub.status.idle":"2022-08-08T18:19:43.646679Z","shell.execute_reply.started":"2022-08-08T18:19:43.646496Z","shell.execute_reply":"2022-08-08T18:19:43.646513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Performance","metadata":{}},{"cell_type":"code","source":"print(confusion_matrix(test['label'], preds['predict']))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.647616Z","iopub.status.idle":"2022-08-08T18:19:43.647987Z","shell.execute_reply.started":"2022-08-08T18:19:43.647798Z","shell.execute_reply":"2022-08-08T18:19:43.647815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(test['label'], preds['predict']))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.648864Z","iopub.status.idle":"2022-08-08T18:19:43.649220Z","shell.execute_reply.started":"2022-08-08T18:19:43.649038Z","shell.execute_reply":"2022-08-08T18:19:43.649060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict on Test Set used for Submission","metadata":{}},{"cell_type":"code","source":"submission_set.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.650595Z","iopub.status.idle":"2022-08-08T18:19:43.650981Z","shell.execute_reply.started":"2022-08-08T18:19:43.650792Z","shell.execute_reply":"2022-08-08T18:19:43.650810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_set.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.652476Z","iopub.status.idle":"2022-08-08T18:19:43.652842Z","shell.execute_reply.started":"2022-08-08T18:19:43.652661Z","shell.execute_reply":"2022-08-08T18:19:43.652678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load Previously Saved Model\nloaded_model = h2o.load_model(path=model_path)\nprint(loaded_model)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.654219Z","iopub.status.idle":"2022-08-08T18:19:43.654715Z","shell.execute_reply.started":"2022-08-08T18:19:43.654517Z","shell.execute_reply":"2022-08-08T18:19:43.654537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make Predictions\nsubmission_preds = loaded_model.predict(submission_set.drop('user_id'))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.656356Z","iopub.status.idle":"2022-08-08T18:19:43.656734Z","shell.execute_reply.started":"2022-08-08T18:19:43.656548Z","shell.execute_reply":"2022-08-08T18:19:43.656566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_preds.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.658527Z","iopub.status.idle":"2022-08-08T18:19:43.658917Z","shell.execute_reply.started":"2022-08-08T18:19:43.658725Z","shell.execute_reply":"2022-08-08T18:19:43.658743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_preds.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.661495Z","iopub.status.idle":"2022-08-08T18:19:43.662225Z","shell.execute_reply.started":"2022-08-08T18:19:43.661866Z","shell.execute_reply":"2022-08-08T18:19:43.661898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_set['prediction'] = submission_preds['predict']","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.664676Z","iopub.status.idle":"2022-08-08T18:19:43.665367Z","shell.execute_reply.started":"2022-08-08T18:19:43.665002Z","shell.execute_reply":"2022-08-08T18:19:43.665042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_set = submission_set[['user_id', 'prediction']]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.667145Z","iopub.status.idle":"2022-08-08T18:19:43.667794Z","shell.execute_reply.started":"2022-08-08T18:19:43.667483Z","shell.execute_reply":"2022-08-08T18:19:43.667512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert to Pnadas dataframe\nsubmission_set = submission_set.as_data_frame()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.670166Z","iopub.status.idle":"2022-08-08T18:19:43.671077Z","shell.execute_reply.started":"2022-08-08T18:19:43.670736Z","shell.execute_reply":"2022-08-08T18:19:43.670768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_set.prediction.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.673100Z","iopub.status.idle":"2022-08-08T18:19:43.673759Z","shell.execute_reply.started":"2022-08-08T18:19:43.673433Z","shell.execute_reply":"2022-08-08T18:19:43.673474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save to CSV","metadata":{}},{"cell_type":"code","source":"submission_set.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T18:19:43.675208Z","iopub.status.idle":"2022-08-08T18:19:43.675835Z","shell.execute_reply.started":"2022-08-08T18:19:43.675524Z","shell.execute_reply":"2022-08-08T18:19:43.675553Z"},"trusted":true},"execution_count":null,"outputs":[]}]}