{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from autogluon.tabular import TabularDataset, TabularPredictor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**load data**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# train data\n",
    "train_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/train.csv')\n",
    "subsample_size = 100  # subsample subset of data for faster demo, try setting this to much larger values\n",
    "train_data = train_data.sample(n=subsample_size, random_state=0)\n",
    "# train_data.head()\n",
    "\n",
    "# test data\n",
    "test_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/test.csv')\n",
    "label = 'class'\n",
    "y_test = test_data[label]  # values to predict\n",
    "test_data_nolab = test_data.drop(columns=[label])  # delete label column to prove we're not cheating\n",
    "# test_data_nolab.head()\n",
    "# print(\"Summary of class variable: \\n\", train_data[label].describe())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fit models using interpretable preset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**fit using interpretable preset**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No path specified. Models will be saved in: \"AutogluonModels/ag-20211206_020056/\"\n",
      "Presets specified: ['interpretable']\n",
      "Beginning AutoGluon training ... Time limit = 8s\n",
      "AutoGluon will save models to \"AutogluonModels/ag-20211206_020056/\"\n",
      "AutoGluon Version:  0.3.2b20211202\n",
      "Python Version:     3.7.0\n",
      "Operating System:   Darwin\n",
      "Train Data Rows:    100\n",
      "Train Data Columns: 14\n",
      "Preprocessing data ...\n",
      "AutoGluon infers your prediction problem is: 'binary' (because only two unique label-values observed).\n",
      "\t2 unique label values:  [' >50K', ' <=50K']\n",
      "\tIf 'binary' is not the correct problem_type, please manually specify the problem_type argument in fit() (You may specify problem_type as one of: ['binary', 'multiclass', 'regression'])\n",
      "Selected class <--> label mapping:  class 1 =  >50K, class 0 =  <=50K\n",
      "\tNote: For your binary classification, AutoGluon arbitrarily selected which label-value represents positive ( >50K) vs negative ( <=50K) class.\n",
      "\tTo explicitly set the positive_class, either rename classes to 1 and 0, or specify positive_class in Predictor init.\n",
      "Using Feature Generators to preprocess the data ...\n",
      "Fitting AutoMLPipelineFeatureGenerator...\n",
      "\tAvailable Memory:                    7175.15 MB\n",
      "\tTrain Data (Original)  Memory Usage: 0.06 MB (0.0% of available memory)\n",
      "\tInferring data type of each feature based on column values. Set feature_metadata_in to manually specify special dtypes of the features.\n",
      "\tStage 1 Generators:\n",
      "\t\tFitting AsTypeFeatureGenerator...\n",
      "\t\t\tNote: Converting 1 features to boolean dtype as they only contain 2 unique values.\n",
      "\tStage 2 Generators:\n",
      "\t\tFitting FillNaFeatureGenerator...\n",
      "\tStage 3 Generators:\n",
      "\t\tFitting IdentityFeatureGenerator...\n",
      "\t\tFitting CategoryFeatureGenerator...\n",
      "\t\t\tFitting CategoryMemoryMinimizeFeatureGenerator...\n",
      "\tStage 4 Generators:\n",
      "\t\tFitting DropUniqueFeatureGenerator...\n",
      "\tTypes of features in original data (raw dtype, special dtypes):\n",
      "\t\t('int', [])    : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]\n",
      "\t\t('object', []) : 8 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]\n",
      "\tTypes of features in processed data (raw dtype, special dtypes):\n",
      "\t\t('category', [])  : 7 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]\n",
      "\t\t('int', [])       : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]\n",
      "\t\t('int', ['bool']) : 1 | ['sex']\n",
      "\t0.1s = Fit runtime\n",
      "\t14 features in original data used to generate 14 features in processed data.\n",
      "\tTrain Data (Processed) Memory Usage: 0.01 MB (0.0% of available memory)\n",
      "Data preprocessing and feature engineering runtime = 0.17s ...\n",
      "AutoGluon will gauge predictive performance using evaluation metric: 'accuracy'\n",
      "\tTo change this, specify the eval_metric argument of fit()\n",
      "Automatically generating train/validation split with holdout_frac=0.2, Train Rows: 80, Val Rows: 20\n",
      "Fitting 11 L1 models ...\n",
      "Fitting model: RuleFit ... Training model for up to 7.83s of the 7.83s of remaining time.\n",
      "\t0.75\t = Validation score   (accuracy)\n",
      "\t1.36s\t = Training   runtime\n",
      "\t0.03s\t = Validation runtime\n",
      "Fitting model: RuleFit_2 ... Training model for up to 6.43s of the 6.43s of remaining time.\n",
      "\t0.75\t = Validation score   (accuracy)\n",
      "\t0.49s\t = Training   runtime\n",
      "\t0.02s\t = Validation runtime\n",
      "Fitting model: RuleFit_3 ... Training model for up to 5.92s of the 5.92s of remaining time.\n",
      "\t0.75\t = Validation score   (accuracy)\n",
      "\t0.54s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: GreedyTree ... Training model for up to 5.36s of the 5.36s of remaining time.\n",
      "\t0.85\t = Validation score   (accuracy)\n",
      "\t0.02s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: OptimalRuleList ... Training model for up to 5.33s of the 5.33s of remaining time.\n",
      "\t0.75\t = Validation score   (accuracy)\n",
      "\t0.07s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: OptimalRuleList_2 ... Training model for up to 5.25s of the 5.25s of remaining time.\n",
      "\t0.75\t = Validation score   (accuracy)\n",
      "\t0.09s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: OptimalRuleList_3 ... Training model for up to 5.15s of the 5.15s of remaining time.\n",
      "\t0.75\t = Validation score   (accuracy)\n",
      "\t0.02s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: OptimalRuleList_4 ... Training model for up to 5.12s of the 5.11s of remaining time.\n",
      "\t0.75\t = Validation score   (accuracy)\n",
      "\t0.02s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: OptimalTree ... Training model for up to 5.08s of the 5.08s of remaining time.\n",
      "/Users/chandan/rules/imodels/imodels/tree/gosdt/pygosdt.py:127: UserWarning: Should install gosdt extension. On x86_64 linux or macOS: 'pip install gosdt'. On other platforms, see https://github.com/keyan3/GeneralizedOptimalSparseDecisionTrees. Defaulting to Non-optimal DecisionTreeClassifier.\n",
      "  \"Should install gosdt extension. On x86_64 linux or macOS: \"\n",
      "\t0.85\t = Validation score   (accuracy)\n",
      "\t0.03s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: BoostedRules ... Training model for up to 5.03s of the 5.03s of remaining time.\n",
      "\t0.75\t = Validation score   (accuracy)\n",
      "\t0.03s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: BoostedRules_2 ... Training model for up to 4.99s of the 4.99s of remaining time.\n",
      "\t0.75\t = Validation score   (accuracy)\n",
      "\t0.04s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: WeightedEnsemble_L2 ... Training model for up to 7.83s of the 4.8s of remaining time.\n",
      "\t0.85\t = Validation score   (accuracy)\n",
      "\t0.24s\t = Training   runtime\n",
      "\t0.0s\t = Validation runtime\n",
      "AutoGluon training complete, total runtime = 3.48s ...\n",
      "TabularPredictor saved. To load, use: predictor = TabularPredictor.load(\"AutogluonModels/ag-20211206_020056/\")\n"
     ]
    }
   ],
   "source": [
    "predictor = TabularPredictor(label=label).fit(train_data, presets='interpretable', time_limit=8, verbosity=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>model</th>\n",
       "      <th>score_val</th>\n",
       "      <th>pred_time_val</th>\n",
       "      <th>fit_time</th>\n",
       "      <th>pred_time_val_marginal</th>\n",
       "      <th>fit_time_marginal</th>\n",
       "      <th>stack_level</th>\n",
       "      <th>can_infer</th>\n",
       "      <th>fit_order</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>GreedyTree</td>\n",
       "      <td>0.85</td>\n",
       "      <td>0.009729</td>\n",
       "      <td>0.021507</td>\n",
       "      <td>0.009729</td>\n",
       "      <td>0.021507</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>0.85</td>\n",
       "      <td>0.010475</td>\n",
       "      <td>0.264943</td>\n",
       "      <td>0.000746</td>\n",
       "      <td>0.243436</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>OptimalTree</td>\n",
       "      <td>0.85</td>\n",
       "      <td>0.011716</td>\n",
       "      <td>0.026156</td>\n",
       "      <td>0.011716</td>\n",
       "      <td>0.026156</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>OptimalRuleList</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.006860</td>\n",
       "      <td>0.069624</td>\n",
       "      <td>0.006860</td>\n",
       "      <td>0.069624</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>OptimalRuleList_2</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.007151</td>\n",
       "      <td>0.086562</td>\n",
       "      <td>0.007151</td>\n",
       "      <td>0.086562</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>RuleFit_3</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.008418</td>\n",
       "      <td>0.544459</td>\n",
       "      <td>0.008418</td>\n",
       "      <td>0.544459</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>OptimalRuleList_3</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.008560</td>\n",
       "      <td>0.022690</td>\n",
       "      <td>0.008560</td>\n",
       "      <td>0.022690</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>OptimalRuleList_4</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.009097</td>\n",
       "      <td>0.024621</td>\n",
       "      <td>0.009097</td>\n",
       "      <td>0.024621</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>BoostedRules</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.010914</td>\n",
       "      <td>0.031247</td>\n",
       "      <td>0.010914</td>\n",
       "      <td>0.031247</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>BoostedRules_2</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.013277</td>\n",
       "      <td>0.035402</td>\n",
       "      <td>0.013277</td>\n",
       "      <td>0.035402</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>RuleFit_2</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.021511</td>\n",
       "      <td>0.486804</td>\n",
       "      <td>0.021511</td>\n",
       "      <td>0.486804</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>RuleFit</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.025171</td>\n",
       "      <td>1.364287</td>\n",
       "      <td>0.025171</td>\n",
       "      <td>1.364287</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  model  score_val  pred_time_val  fit_time  \\\n",
       "0            GreedyTree       0.85       0.009729  0.021507   \n",
       "1   WeightedEnsemble_L2       0.85       0.010475  0.264943   \n",
       "2           OptimalTree       0.85       0.011716  0.026156   \n",
       "3       OptimalRuleList       0.75       0.006860  0.069624   \n",
       "4     OptimalRuleList_2       0.75       0.007151  0.086562   \n",
       "5             RuleFit_3       0.75       0.008418  0.544459   \n",
       "6     OptimalRuleList_3       0.75       0.008560  0.022690   \n",
       "7     OptimalRuleList_4       0.75       0.009097  0.024621   \n",
       "8          BoostedRules       0.75       0.010914  0.031247   \n",
       "9        BoostedRules_2       0.75       0.013277  0.035402   \n",
       "10            RuleFit_2       0.75       0.021511  0.486804   \n",
       "11              RuleFit       0.75       0.025171  1.364287   \n",
       "\n",
       "    pred_time_val_marginal  fit_time_marginal  stack_level  can_infer  \\\n",
       "0                 0.009729           0.021507            1       True   \n",
       "1                 0.000746           0.243436            2       True   \n",
       "2                 0.011716           0.026156            1       True   \n",
       "3                 0.006860           0.069624            1       True   \n",
       "4                 0.007151           0.086562            1       True   \n",
       "5                 0.008418           0.544459            1       True   \n",
       "6                 0.008560           0.022690            1       True   \n",
       "7                 0.009097           0.024621            1       True   \n",
       "8                 0.010914           0.031247            1       True   \n",
       "9                 0.013277           0.035402            1       True   \n",
       "10                0.021511           0.486804            1       True   \n",
       "11                0.025171           1.364287            1       True   \n",
       "\n",
       "    fit_order  \n",
       "0           4  \n",
       "1          12  \n",
       "2           9  \n",
       "3           5  \n",
       "4           6  \n",
       "5           3  \n",
       "6           7  \n",
       "7           8  \n",
       "8          10  \n",
       "9          11  \n",
       "10          2  \n",
       "11          1  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictor.leaderboard(silent=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>model_types</th>\n",
       "      <th>model_performance</th>\n",
       "      <th>complexity</th>\n",
       "      <th>model_best</th>\n",
       "      <th>model_paths</th>\n",
       "      <th>model_fit_times</th>\n",
       "      <th>model_pred_times</th>\n",
       "      <th>num_bag_folds</th>\n",
       "      <th>max_stack_level</th>\n",
       "      <th>num_classes</th>\n",
       "      <th>model_hyperparams</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>GreedyTree</th>\n",
       "      <td>GreedyTreeModel</td>\n",
       "      <td>0.85</td>\n",
       "      <td>5.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Gree...</td>\n",
       "      <td>0.021507</td>\n",
       "      <td>0.009729</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OptimalTree</th>\n",
       "      <td>OptimalTreeModel</td>\n",
       "      <td>0.85</td>\n",
       "      <td>5.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Opti...</td>\n",
       "      <td>0.026156</td>\n",
       "      <td>0.011716</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RuleFit_3</th>\n",
       "      <td>RuleFitModel</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Rule...</td>\n",
       "      <td>0.544459</td>\n",
       "      <td>0.008418</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0, 'max_rules': 30}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OptimalRuleList_3</th>\n",
       "      <td>OptimalRuleListModel</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Opti...</td>\n",
       "      <td>0.022690</td>\n",
       "      <td>0.008560</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0, 'c': 0.1}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OptimalRuleList_4</th>\n",
       "      <td>OptimalRuleListModel</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Opti...</td>\n",
       "      <td>0.024621</td>\n",
       "      <td>0.009097</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0, 'c': 1.0}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OptimalRuleList</th>\n",
       "      <td>OptimalRuleListModel</td>\n",
       "      <td>0.75</td>\n",
       "      <td>3.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Opti...</td>\n",
       "      <td>0.069624</td>\n",
       "      <td>0.006860</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0, 'c': 0.001}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OptimalRuleList_2</th>\n",
       "      <td>OptimalRuleListModel</td>\n",
       "      <td>0.75</td>\n",
       "      <td>3.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Opti...</td>\n",
       "      <td>0.086562</td>\n",
       "      <td>0.007151</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0, 'c': 0.01}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BoostedRules</th>\n",
       "      <td>BoostedRulesModel</td>\n",
       "      <td>0.75</td>\n",
       "      <td>10.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Boos...</td>\n",
       "      <td>0.031247</td>\n",
       "      <td>0.010914</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0, 'n_estimators': 5}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RuleFit</th>\n",
       "      <td>RuleFitModel</td>\n",
       "      <td>0.75</td>\n",
       "      <td>16.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Rule...</td>\n",
       "      <td>1.364287</td>\n",
       "      <td>0.025171</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0, 'max_rules': 7}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RuleFit_2</th>\n",
       "      <td>RuleFitModel</td>\n",
       "      <td>0.75</td>\n",
       "      <td>16.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Rule...</td>\n",
       "      <td>0.486804</td>\n",
       "      <td>0.021511</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0, 'max_rules': 15}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BoostedRules_2</th>\n",
       "      <td>BoostedRulesModel</td>\n",
       "      <td>0.75</td>\n",
       "      <td>20.0</td>\n",
       "      <td>WeightedEnsemble_L2</td>\n",
       "      <td>AutogluonModels/ag-20211206_020056/models/Boos...</td>\n",
       "      <td>0.035402</td>\n",
       "      <td>0.013277</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>{'random_state': 0, 'n_estimators': 10}</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            model_types  model_performance  complexity  \\\n",
       "GreedyTree              GreedyTreeModel               0.85         5.0   \n",
       "OptimalTree            OptimalTreeModel               0.85         5.0   \n",
       "RuleFit_3                  RuleFitModel               0.75         0.0   \n",
       "OptimalRuleList_3  OptimalRuleListModel               0.75         1.0   \n",
       "OptimalRuleList_4  OptimalRuleListModel               0.75         1.0   \n",
       "OptimalRuleList    OptimalRuleListModel               0.75         3.0   \n",
       "OptimalRuleList_2  OptimalRuleListModel               0.75         3.0   \n",
       "BoostedRules          BoostedRulesModel               0.75        10.0   \n",
       "RuleFit                    RuleFitModel               0.75        16.0   \n",
       "RuleFit_2                  RuleFitModel               0.75        16.0   \n",
       "BoostedRules_2        BoostedRulesModel               0.75        20.0   \n",
       "\n",
       "                            model_best  \\\n",
       "GreedyTree         WeightedEnsemble_L2   \n",
       "OptimalTree        WeightedEnsemble_L2   \n",
       "RuleFit_3          WeightedEnsemble_L2   \n",
       "OptimalRuleList_3  WeightedEnsemble_L2   \n",
       "OptimalRuleList_4  WeightedEnsemble_L2   \n",
       "OptimalRuleList    WeightedEnsemble_L2   \n",
       "OptimalRuleList_2  WeightedEnsemble_L2   \n",
       "BoostedRules       WeightedEnsemble_L2   \n",
       "RuleFit            WeightedEnsemble_L2   \n",
       "RuleFit_2          WeightedEnsemble_L2   \n",
       "BoostedRules_2     WeightedEnsemble_L2   \n",
       "\n",
       "                                                         model_paths  \\\n",
       "GreedyTree         AutogluonModels/ag-20211206_020056/models/Gree...   \n",
       "OptimalTree        AutogluonModels/ag-20211206_020056/models/Opti...   \n",
       "RuleFit_3          AutogluonModels/ag-20211206_020056/models/Rule...   \n",
       "OptimalRuleList_3  AutogluonModels/ag-20211206_020056/models/Opti...   \n",
       "OptimalRuleList_4  AutogluonModels/ag-20211206_020056/models/Opti...   \n",
       "OptimalRuleList    AutogluonModels/ag-20211206_020056/models/Opti...   \n",
       "OptimalRuleList_2  AutogluonModels/ag-20211206_020056/models/Opti...   \n",
       "BoostedRules       AutogluonModels/ag-20211206_020056/models/Boos...   \n",
       "RuleFit            AutogluonModels/ag-20211206_020056/models/Rule...   \n",
       "RuleFit_2          AutogluonModels/ag-20211206_020056/models/Rule...   \n",
       "BoostedRules_2     AutogluonModels/ag-20211206_020056/models/Boos...   \n",
       "\n",
       "                   model_fit_times  model_pred_times  num_bag_folds  \\\n",
       "GreedyTree                0.021507          0.009729              0   \n",
       "OptimalTree               0.026156          0.011716              0   \n",
       "RuleFit_3                 0.544459          0.008418              0   \n",
       "OptimalRuleList_3         0.022690          0.008560              0   \n",
       "OptimalRuleList_4         0.024621          0.009097              0   \n",
       "OptimalRuleList           0.069624          0.006860              0   \n",
       "OptimalRuleList_2         0.086562          0.007151              0   \n",
       "BoostedRules              0.031247          0.010914              0   \n",
       "RuleFit                   1.364287          0.025171              0   \n",
       "RuleFit_2                 0.486804          0.021511              0   \n",
       "BoostedRules_2            0.035402          0.013277              0   \n",
       "\n",
       "                   max_stack_level  num_classes  \\\n",
       "GreedyTree                       2            2   \n",
       "OptimalTree                      2            2   \n",
       "RuleFit_3                        2            2   \n",
       "OptimalRuleList_3                2            2   \n",
       "OptimalRuleList_4                2            2   \n",
       "OptimalRuleList                  2            2   \n",
       "OptimalRuleList_2                2            2   \n",
       "BoostedRules                     2            2   \n",
       "RuleFit                          2            2   \n",
       "RuleFit_2                        2            2   \n",
       "BoostedRules_2                   2            2   \n",
       "\n",
       "                                         model_hyperparams  \n",
       "GreedyTree                             {'random_state': 0}  \n",
       "OptimalTree                            {'random_state': 0}  \n",
       "RuleFit_3             {'random_state': 0, 'max_rules': 30}  \n",
       "OptimalRuleList_3            {'random_state': 0, 'c': 0.1}  \n",
       "OptimalRuleList_4            {'random_state': 0, 'c': 1.0}  \n",
       "OptimalRuleList            {'random_state': 0, 'c': 0.001}  \n",
       "OptimalRuleList_2           {'random_state': 0, 'c': 0.01}  \n",
       "BoostedRules        {'random_state': 0, 'n_estimators': 5}  \n",
       "RuleFit                {'random_state': 0, 'max_rules': 7}  \n",
       "RuleFit_2             {'random_state': 0, 'max_rules': 15}  \n",
       "BoostedRules_2     {'random_state': 0, 'n_estimators': 10}  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictor.interpretable_models_summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GreedyTree:\n",
      "|--- relationship_0 <= 0.50\n",
      "|   |--- relationship_5 <= 0.50\n",
      "|   |   |--- relationship_2 <= 0.50\n",
      "|   |   |   |--- weights: [40.00, 0.00] class: 0\n",
      "|   |   |--- relationship_2 >  0.50\n",
      "|   |   |   |--- race_2 <= 0.50\n",
      "|   |   |   |   |--- weights: [0.00, 1.00] class: 1\n",
      "|   |   |   |--- race_2 >  0.50\n",
      "|   |   |   |   |--- weights: [1.00, 0.00] class: 0\n",
      "|   |--- relationship_5 >  0.50\n",
      "|   |   |--- weights: [4.00, 4.00] class: 0\n",
      "|--- relationship_0 >  0.50\n",
      "|   |--- race_2 <= 0.50\n",
      "|   |   |--- weights: [1.00, 0.00] class: 0\n",
      "|   |--- race_2 >  0.50\n",
      "|   |   |--- weights: [13.00, 16.00] class: 1\n",
      "\n"
     ]
    }
   ],
   "source": [
    "predictor.print_interpretable_rules() # automatically selects a model (here, RuleList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OptimalRuleList:\n",
      "\n",
      "   0.0 / 80 (positive class / total)\n",
      "\t↓ \n",
      "If race_0                              → 0.0 /    4 (0.0%)\n",
      "\t↓ \n",
      "   0.0 /    76\t \n",
      "\t↓ \n",
      "If race_0  & relationship_2 == 0       → 0.0 /    0 (0.0%)\n",
      "\t↓ \n",
      "   0.0 /    76\t \n",
      "\t↓ \n",
      "If relationship_0 == 0                 → 0.0 /   47 (0.0%)\n",
      "\t↓ \n",
      "   0.0 /    29\t \n",
      "\t↓ \n",
      "If relationship_0 == 0 & race_0 == 0   → 0.0 /    0 (0.0%)\n",
      "\t↓ \n",
      "   0.0 /    29\t \n",
      "\n",
      "BoostedRules:\n",
      "Rule → predicted probability (final prediction is weighted sum of all predictions)\n",
      "  If\u001b[96m relationship_0 <= 0.5\u001b[00m → 0.38 (weight: 0.58)\n",
      "  If\u001b[96m relationship_0 > 0.5\u001b[00m → 0.10 (weight: 0.72)\n",
      "  If\u001b[96m relationship_5 <= 0.5\u001b[00m → 0.74 (weight: 0.17)\n",
      "  If\u001b[96m relationship_5 > 0.5\u001b[00m → 0.03 (weight: 0.30)\n",
      "  If\u001b[96m relationship_1 <= 0.5\u001b[00m → 0.38 (weight: 0.34)\n",
      "\n",
      "RuleFit:\n",
      "                                                                      rule  coef\n",
      "                           relationship_0 <= 0.5 and relationship_2 <= 0.5 -0.00\n",
      "                           relationship_0 <= 0.5 and relationship_2 <= 0.5 -0.00\n",
      " relationship_0 <= 0.5 and relationship_2 <= 0.5 and relationship_5 <= 0.5 -0.47\n",
      " relationship_0 <= 0.5 and relationship_2 <= 0.5 and relationship_5 <= 0.5 -0.56\n",
      " relationship_0 <= 0.5 and relationship_2 <= 0.5 and relationship_5 <= 0.5 -0.73\n",
      "         relationship_0 <= 0.5 and relationship_5 <= 0.5 and race_0 <= 0.5 -0.89\n",
      "\n",
      "GreedyTree:\n",
      "|--- relationship_0 <= 0.50\n",
      "|   |--- relationship_5 <= 0.50\n",
      "|   |   |--- relationship_2 <= 0.50\n",
      "|   |   |   |--- weights: [40.00, 0.00] class: 0\n",
      "|   |   |--- relationship_2 >  0.50\n",
      "|   |   |   |--- race_2 <= 0.50\n",
      "|   |   |   |   |--- weights: [0.00, 1.00] class: 1\n",
      "|   |   |   |--- race_2 >  0.50\n",
      "|   |   |   |   |--- weights: [1.00, 0.00] class: 0\n",
      "|   |--- relationship_5 >  0.50\n",
      "|   |   |--- weights: [4.00, 4.00] class: 0\n",
      "|--- relationship_0 >  0.50\n",
      "|   |--- race_2 <= 0.50\n",
      "|   |   |--- weights: [1.00, 0.00] class: 0\n",
      "|   |--- race_2 >  0.50\n",
      "|   |   |--- weights: [13.00, 16.00] class: 1\n",
      "\n",
      "GreedyTree:\n",
      "|--- relationship_0 <= 0.50\n",
      "|   |--- relationship_5 <= 0.50\n",
      "|   |   |--- relationship_2 <= 0.50\n",
      "|   |   |   |--- weights: [40.00, 0.00] class: 0\n",
      "|   |   |--- relationship_2 >  0.50\n",
      "|   |   |   |--- race_2 <= 0.50\n",
      "|   |   |   |   |--- weights: [0.00, 1.00] class: 1\n",
      "|   |   |   |--- race_2 >  0.50\n",
      "|   |   |   |   |--- weights: [1.00, 0.00] class: 0\n",
      "|   |--- relationship_5 >  0.50\n",
      "|   |   |--- weights: [4.00, 4.00] class: 0\n",
      "|--- relationship_0 >  0.50\n",
      "|   |--- race_2 <= 0.50\n",
      "|   |   |--- weights: [1.00, 0.00] class: 0\n",
      "|   |--- race_2 >  0.50\n",
      "|   |   |--- weights: [13.00, 16.00] class: 1\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for model_name in ['OptimalRuleList', 'BoostedRules', 'RuleFit', 'GreedyTree', 'OptimalTree']:\n",
    "    predictor.print_interpretable_rules(model_name=model_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**fit custom model once (could also include other default models here)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No path specified. Models will be saved in: \"AutogluonModels/ag-20211206_020137/\"\n",
      "Beginning AutoGluon training ...\n",
      "AutoGluon will save models to \"AutogluonModels/ag-20211206_020137/\"\n",
      "AutoGluon Version:  0.3.2b20211202\n",
      "Python Version:     3.7.0\n",
      "Operating System:   Darwin\n",
      "Train Data Rows:    100\n",
      "Train Data Columns: 14\n",
      "Preprocessing data ...\n",
      "AutoGluon infers your prediction problem is: 'binary' (because only two unique label-values observed).\n",
      "\t2 unique label values:  [' >50K', ' <=50K']\n",
      "\tIf 'binary' is not the correct problem_type, please manually specify the problem_type argument in fit() (You may specify problem_type as one of: ['binary', 'multiclass', 'regression'])\n",
      "Selected class <--> label mapping:  class 1 =  >50K, class 0 =  <=50K\n",
      "\tNote: For your binary classification, AutoGluon arbitrarily selected which label-value represents positive ( >50K) vs negative ( <=50K) class.\n",
      "\tTo explicitly set the positive_class, either rename classes to 1 and 0, or specify positive_class in Predictor init.\n",
      "Using Feature Generators to preprocess the data ...\n",
      "Fitting AutoMLPipelineFeatureGenerator...\n",
      "\tAvailable Memory:                    7229.58 MB\n",
      "\tTrain Data (Original)  Memory Usage: 0.06 MB (0.0% of available memory)\n",
      "\tInferring data type of each feature based on column values. Set feature_metadata_in to manually specify special dtypes of the features.\n",
      "\tStage 1 Generators:\n",
      "\t\tFitting AsTypeFeatureGenerator...\n",
      "\t\t\tNote: Converting 1 features to boolean dtype as they only contain 2 unique values.\n",
      "\tStage 2 Generators:\n",
      "\t\tFitting FillNaFeatureGenerator...\n",
      "\tStage 3 Generators:\n",
      "\t\tFitting IdentityFeatureGenerator...\n",
      "\t\tFitting CategoryFeatureGenerator...\n",
      "\t\t\tFitting CategoryMemoryMinimizeFeatureGenerator...\n",
      "\tStage 4 Generators:\n",
      "\t\tFitting DropUniqueFeatureGenerator...\n",
      "\tTypes of features in original data (raw dtype, special dtypes):\n",
      "\t\t('int', [])    : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]\n",
      "\t\t('object', []) : 8 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]\n",
      "\tTypes of features in processed data (raw dtype, special dtypes):\n",
      "\t\t('category', [])  : 7 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]\n",
      "\t\t('int', [])       : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]\n",
      "\t\t('int', ['bool']) : 1 | ['sex']\n",
      "\t0.1s = Fit runtime\n",
      "\t14 features in original data used to generate 14 features in processed data.\n",
      "\tTrain Data (Processed) Memory Usage: 0.01 MB (0.0% of available memory)\n",
      "Data preprocessing and feature engineering runtime = 0.16s ...\n",
      "AutoGluon will gauge predictive performance using evaluation metric: 'accuracy'\n",
      "\tTo change this, specify the eval_metric argument of fit()\n",
      "Automatically generating train/validation split with holdout_frac=0.2, Train Rows: 80, Val Rows: 20\n",
      "Custom Model Type Detected: <class 'autogluon.tabular.models.imodels.imodels_models.OptimalTreeModel'>\n",
      "Fitting 1 L1 models ...\n",
      "Fitting model: OptimalTree ...\n",
      "/Users/chandan/rules/imodels/imodels/tree/gosdt/pygosdt.py:127: UserWarning: Should install gosdt extension. On x86_64 linux or macOS: 'pip install gosdt'. On other platforms, see https://github.com/keyan3/GeneralizedOptimalSparseDecisionTrees. Defaulting to Non-optimal DecisionTreeClassifier.\n",
      "  \"Should install gosdt extension. On x86_64 linux or macOS: \"\n",
      "\t0.85\t = Validation score   (accuracy)\n",
      "\t0.03s\t = Training   runtime\n",
      "\t0.01s\t = Validation runtime\n",
      "Fitting model: WeightedEnsemble_L2 ...\n",
      "\t0.85\t = Validation score   (accuracy)\n",
      "\t0.0s\t = Training   runtime\n",
      "\t0.0s\t = Validation runtime\n",
      "AutoGluon training complete, total runtime = 0.28s ...\n",
      "TabularPredictor saved. To load, use: predictor = TabularPredictor.load(\"AutogluonModels/ag-20211206_020137/\")\n"
     ]
    }
   ],
   "source": [
    "from autogluon.tabular.models import RuleFitModel, GreedyTreeModel, OptimalRuleListModel, \\\n",
    "  BayesianRuleSetModel, OptimalTreeModel, BoostedRulesModel\n",
    "\n",
    "# hyperparams = get_hyperparameter_config('default') # get all default hyperparams for various models\n",
    "custom_hyperparameters = {\n",
    "#     RuleFitModel: [{}, {'max_rules': 4}], # empty brackets are dfeault hyperparams\n",
    "#     GreedyTreeModel: [{}],\n",
    "    OptimalTreeModel: [{}],\n",
    "#     BoostedRulesModel: [{}],\n",
    "#     OptimalRuleListModel: [{}]\n",
    "}  # Train 3 CustomRandomForestModel with different hyperparameters\n",
    "predictor = TabularPredictor(label=label).fit(train_data, hyperparameters=custom_hyperparameters, verbosity=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# tune custom models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No path specified. Models will be saved in: \"AutogluonModels/ag-20211206_020147/\"\n",
      "Warning: hyperparameter tuning is currently experimental and may cause the process to hang.\n",
      "Beginning AutoGluon training ... Time limit = 60s\n",
      "AutoGluon will save models to \"AutogluonModels/ag-20211206_020147/\"\n",
      "AutoGluon Version:  0.3.2b20211202\n",
      "Python Version:     3.7.0\n",
      "Operating System:   Darwin\n",
      "Train Data Rows:    100\n",
      "Train Data Columns: 14\n",
      "Preprocessing data ...\n",
      "AutoGluon infers your prediction problem is: 'binary' (because only two unique label-values observed).\n",
      "\t2 unique label values:  [' >50K', ' <=50K']\n",
      "\tIf 'binary' is not the correct problem_type, please manually specify the problem_type argument in fit() (You may specify problem_type as one of: ['binary', 'multiclass', 'regression'])\n",
      "Selected class <--> label mapping:  class 1 =  >50K, class 0 =  <=50K\n",
      "\tNote: For your binary classification, AutoGluon arbitrarily selected which label-value represents positive ( >50K) vs negative ( <=50K) class.\n",
      "\tTo explicitly set the positive_class, either rename classes to 1 and 0, or specify positive_class in Predictor init.\n",
      "Using Feature Generators to preprocess the data ...\n",
      "Fitting AutoMLPipelineFeatureGenerator...\n",
      "\tAvailable Memory:                    7242.29 MB\n",
      "\tTrain Data (Original)  Memory Usage: 0.06 MB (0.0% of available memory)\n",
      "\tInferring data type of each feature based on column values. Set feature_metadata_in to manually specify special dtypes of the features.\n",
      "\tStage 1 Generators:\n",
      "\t\tFitting AsTypeFeatureGenerator...\n",
      "\t\t\tNote: Converting 1 features to boolean dtype as they only contain 2 unique values.\n",
      "\tStage 2 Generators:\n",
      "\t\tFitting FillNaFeatureGenerator...\n",
      "\tStage 3 Generators:\n",
      "\t\tFitting IdentityFeatureGenerator...\n",
      "\t\tFitting CategoryFeatureGenerator...\n",
      "\t\t\tFitting CategoryMemoryMinimizeFeatureGenerator...\n",
      "\tStage 4 Generators:\n",
      "\t\tFitting DropUniqueFeatureGenerator...\n",
      "\tTypes of features in original data (raw dtype, special dtypes):\n",
      "\t\t('int', [])    : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]\n",
      "\t\t('object', []) : 8 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]\n",
      "\tTypes of features in processed data (raw dtype, special dtypes):\n",
      "\t\t('category', [])  : 7 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]\n",
      "\t\t('int', [])       : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]\n",
      "\t\t('int', ['bool']) : 1 | ['sex']\n",
      "\t0.1s = Fit runtime\n",
      "\t14 features in original data used to generate 14 features in processed data.\n",
      "\tTrain Data (Processed) Memory Usage: 0.01 MB (0.0% of available memory)\n",
      "Data preprocessing and feature engineering runtime = 0.15s ...\n",
      "AutoGluon will gauge predictive performance using evaluation metric: 'accuracy'\n",
      "\tTo change this, specify the eval_metric argument of fit()\n",
      "Automatically generating train/validation split with holdout_frac=0.2, Train Rows: 80, Val Rows: 20\n",
      "Custom Model Type Detected: <class 'autogluon.tabular.models.imodels.imodels_models.RuleFitModel'>\n",
      "Custom Model Type Detected: <class 'autogluon.tabular.models.imodels.imodels_models.GreedyTreeModel'>\n",
      "Fitting 2 L1 models ...\n",
      "Hyperparameter tuning model: RuleFit ...\n",
      "/Users/chandan/venvs/autogluon/lib/python3.7/site-packages/sklearn/svm/_base.py:1208: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.\n",
      "  ConvergenceWarning,\n"
     ]
    }
   ],
   "source": [
    "from autogluon.core.space import Categorical, Int, Real\n",
    "from autogluon.tabular.configs.hyperparameter_configs import get_hyperparameter_config\n",
    "\n",
    "custom_hyperparameters_hpo = {\n",
    "    RuleFitModel: {\n",
    "        'tree_size': Int(lower=5, upper=30),\n",
    "        'max_rules': Int(lower=2, upper=15),\n",
    "    },\n",
    "    GreedyTreeModel: {\n",
    "        'max_leaf_nodes': Int(lower=2, upper=30),\n",
    "        'ccp_alpha': Real(lower=0, upper=5),\n",
    "    }\n",
    "}\n",
    "# Hyperparameter tune CustomRandomForestModel for 20 seconds\n",
    "predictor = TabularPredictor(label=label).fit(train_data,\n",
    "                                              hyperparameters=custom_hyperparameters_hpo,\n",
    "                                              hyperparameter_tune_kwargs='auto',  # enables HPO\n",
    "                                              time_limit=60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "leaderboard = predictor.leaderboard(silent=True)\n",
    "leaderboard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# test individual model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Separate features and labels\n",
    "X = train_data.drop(columns=[label])\n",
    "y = train_data[label]\n",
    "X_test = test_data.drop(columns=[label])\n",
    "y_test = test_data[label]\n",
    "\n",
    "# basic model fitting\n",
    "save_path = 'AutogluonModels'  # specifies folder to store trained models\n",
    "predictor = TabularPredictor(label=label, path=save_path, verbosity=0).fit(train_data)\n",
    "\n",
    "# basic model evaluation\n",
    "predictor = TabularPredictor.load(save_path)  # unnecessary, just demonstrates how to load previously-trained predictor from file\n",
    "y_pred = predictor.predict(test_data_nolab)\n",
    "print(\"Predictions:  \\n\", y_pred)\n",
    "perf = predictor.evaluate_predictions(y_true=y_test, y_pred=y_pred, auxiliary_metrics=True)\n",
    "\n",
    "predictor.leaderboard(test_data, silent=True)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "formats": "ipynb,../../tests/notebooks//py:percent"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
