autogen/notebook/flaml_automl.ipynb

1164 lines
114 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"source": [
"Copyright (c) 2020-2021 Microsoft Corporation. All rights reserved. \n",
"\n",
"Licensed under the MIT License.\n",
"\n",
"# AutoML with FLAML Library\n",
"\n",
"\n",
"## 1. Introduction\n",
"\n",
"FLAML is a Python library (https://github.com/microsoft/FLAML) designed to automatically produce accurate machine learning models \n",
"with low computational cost. It is fast and cheap. The simple and lightweight design makes it easy \n",
"to use and extend, such as adding new learners. FLAML can \n",
"- serve as an economical AutoML engine,\n",
"- be used as a fast hyperparameter tuning tool, or \n",
"- be embedded in self-tuning software that requires low latency & resource in repetitive\n",
" tuning tasks.\n",
"\n",
"In this notebook, we use one real data example (binary classification) to showcase how to use FLAML library.\n",
"\n",
"FLAML requires `Python>=3.6`. To run this notebook example, please install flaml with the `notebook` option:\n",
"```bash\n",
"pip install flaml[notebook]\n",
"```"
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"!pip install flaml[notebook];"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"## 2. Classification Example\n",
"### Load data and preprocess\n",
"\n",
"Download [Airlines dataset](https://www.openml.org/d/1169) from OpenML. The task is to predict whether a given flight will be delayed, given the information of the scheduled departure."
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 1,
"source": [
"from flaml.data import load_openml_dataset\n",
"X_train, X_test, y_train, y_test = load_openml_dataset(dataset_id=1169, data_dir='./')"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"load dataset from ./openml_ds1169.pkl\n",
"Dataset name: airlines\n",
"X_train.shape: (404537, 7), y_train.shape: (404537,);\n",
"X_test.shape: (134846, 7), y_test.shape: (134846,)\n"
]
}
],
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
}
},
{
"cell_type": "markdown",
"source": [
"### Run FLAML\n",
"In the FLAML automl run configuration, users can specify the task type, time budget, error metric, learner list, whether to subsample, resampling strategy type, and so on. All these arguments have default values which will be used if users do not provide them. For example, the default ML learners of FLAML are `['lgbm', 'xgboost', 'catboost', 'rf', 'extra_tree', 'lrl1']`. "
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 2,
"source": [
"''' import AutoML class from flaml package '''\n",
"from flaml import AutoML\n",
"automl = AutoML()"
],
"outputs": [],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 3,
"source": [
"settings = {\n",
" \"time_budget\": 240, # total running time in seconds\n",
" \"metric\": 'accuracy', # can be: 'r2', 'rmse', 'mae', 'mse', 'accuracy', 'roc_auc', 'roc_auc_ovr',\n",
" # 'roc_auc_ovo', 'log_loss', 'mape', 'f1', 'ap', 'ndcg', 'micro_f1', 'macro_f1'\n",
" \"task\": 'classification', # task type \n",
" \"log_file_name\": 'airlines_experiment.log', # flaml log file\n",
" \"seed\": 7654321, # random seed\n",
"}"
],
"outputs": [],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 4,
"source": [
"'''The main flaml automl API'''\n",
"automl.fit(X_train=X_train, y_train=y_train, **settings)"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"[flaml.automl: 08-31 00:53:33] {1279} INFO - Evaluation method: holdout\n",
"[flaml.automl: 08-31 00:53:34] {1312} INFO - Minimizing error metric: 1-accuracy\n",
"[flaml.automl: 08-31 00:53:34] {1338} INFO - List of ML learners in AutoML Run: ['lgbm', 'rf', 'catboost', 'xgboost', 'extra_tree', 'lrl1']\n",
"[flaml.automl: 08-31 00:53:34] {1532} INFO - iteration 0, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:34] {1689} INFO - at 1.4s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-31 00:53:34] {1532} INFO - iteration 1, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:34] {1689} INFO - at 1.6s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-31 00:53:34] {1532} INFO - iteration 2, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:34] {1689} INFO - at 1.7s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-31 00:53:34] {1532} INFO - iteration 3, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:34] {1689} INFO - at 1.9s,\tbest lgbm's error=0.3661,\tbest lgbm's error=0.3661\n",
"[flaml.automl: 08-31 00:53:34] {1532} INFO - iteration 4, current learner xgboost\n",
"[flaml.automl: 08-31 00:53:35] {1689} INFO - at 2.1s,\tbest xgboost's error=0.3787,\tbest lgbm's error=0.3661\n",
"[flaml.automl: 08-31 00:53:35] {1532} INFO - iteration 5, current learner xgboost\n",
"[flaml.automl: 08-31 00:53:35] {1689} INFO - at 2.2s,\tbest xgboost's error=0.3769,\tbest lgbm's error=0.3661\n",
"[flaml.automl: 08-31 00:53:35] {1532} INFO - iteration 6, current learner extra_tree\n",
"[flaml.automl: 08-31 00:53:35] {1689} INFO - at 2.4s,\tbest extra_tree's error=0.3788,\tbest lgbm's error=0.3661\n",
"[flaml.automl: 08-31 00:53:35] {1532} INFO - iteration 7, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:35] {1689} INFO - at 2.5s,\tbest lgbm's error=0.3645,\tbest lgbm's error=0.3645\n",
"[flaml.automl: 08-31 00:53:35] {1532} INFO - iteration 8, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:35] {1689} INFO - at 2.7s,\tbest lgbm's error=0.3645,\tbest lgbm's error=0.3645\n",
"[flaml.automl: 08-31 00:53:35] {1532} INFO - iteration 9, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:35] {1689} INFO - at 2.8s,\tbest lgbm's error=0.3645,\tbest lgbm's error=0.3645\n",
"[flaml.automl: 08-31 00:53:35] {1532} INFO - iteration 10, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:35] {1689} INFO - at 3.0s,\tbest lgbm's error=0.3610,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:35] {1532} INFO - iteration 11, current learner extra_tree\n",
"[flaml.automl: 08-31 00:53:36] {1689} INFO - at 3.2s,\tbest extra_tree's error=0.3763,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:36] {1532} INFO - iteration 12, current learner rf\n",
"[flaml.automl: 08-31 00:53:36] {1689} INFO - at 3.4s,\tbest rf's error=0.3787,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:36] {1532} INFO - iteration 13, current learner rf\n",
"[flaml.automl: 08-31 00:53:36] {1689} INFO - at 3.5s,\tbest rf's error=0.3689,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:36] {1532} INFO - iteration 14, current learner rf\n",
"[flaml.automl: 08-31 00:53:36] {1689} INFO - at 3.7s,\tbest rf's error=0.3689,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:36] {1532} INFO - iteration 15, current learner rf\n",
"[flaml.automl: 08-31 00:53:36] {1689} INFO - at 4.0s,\tbest rf's error=0.3689,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:36] {1532} INFO - iteration 16, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:37] {1689} INFO - at 4.2s,\tbest lgbm's error=0.3610,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:37] {1532} INFO - iteration 17, current learner xgboost\n",
"[flaml.automl: 08-31 00:53:37] {1689} INFO - at 4.3s,\tbest xgboost's error=0.3765,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:37] {1532} INFO - iteration 18, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:37] {1689} INFO - at 4.9s,\tbest lgbm's error=0.3610,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:37] {1532} INFO - iteration 19, current learner rf\n",
"[flaml.automl: 08-31 00:53:38] {1689} INFO - at 5.1s,\tbest rf's error=0.3689,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:38] {1532} INFO - iteration 20, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:38] {1689} INFO - at 5.3s,\tbest lgbm's error=0.3610,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:38] {1532} INFO - iteration 21, current learner rf\n",
"[flaml.automl: 08-31 00:53:38] {1689} INFO - at 5.6s,\tbest rf's error=0.3689,\tbest lgbm's error=0.3610\n",
"[flaml.automl: 08-31 00:53:38] {1532} INFO - iteration 22, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:38] {1689} INFO - at 6.0s,\tbest lgbm's error=0.3604,\tbest lgbm's error=0.3604\n",
"[flaml.automl: 08-31 00:53:38] {1532} INFO - iteration 23, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:39] {1689} INFO - at 6.5s,\tbest lgbm's error=0.3545,\tbest lgbm's error=0.3545\n",
"[flaml.automl: 08-31 00:53:39] {1532} INFO - iteration 24, current learner rf\n",
"[flaml.automl: 08-31 00:53:39] {1689} INFO - at 6.8s,\tbest rf's error=0.3631,\tbest lgbm's error=0.3545\n",
"[flaml.automl: 08-31 00:53:39] {1532} INFO - iteration 25, current learner extra_tree\n",
"[flaml.automl: 08-31 00:53:39] {1689} INFO - at 7.0s,\tbest extra_tree's error=0.3763,\tbest lgbm's error=0.3545\n",
"[flaml.automl: 08-31 00:53:39] {1532} INFO - iteration 26, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:40] {1689} INFO - at 7.5s,\tbest lgbm's error=0.3523,\tbest lgbm's error=0.3523\n",
"[flaml.automl: 08-31 00:53:40] {1532} INFO - iteration 27, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:40] {1689} INFO - at 8.0s,\tbest lgbm's error=0.3523,\tbest lgbm's error=0.3523\n",
"[flaml.automl: 08-31 00:53:40] {1532} INFO - iteration 28, current learner rf\n",
"[flaml.automl: 08-31 00:53:41] {1689} INFO - at 8.5s,\tbest rf's error=0.3621,\tbest lgbm's error=0.3523\n",
"[flaml.automl: 08-31 00:53:41] {1532} INFO - iteration 29, current learner rf\n",
"[flaml.automl: 08-31 00:53:41] {1689} INFO - at 8.7s,\tbest rf's error=0.3621,\tbest lgbm's error=0.3523\n",
"[flaml.automl: 08-31 00:53:41] {1532} INFO - iteration 30, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:42] {1689} INFO - at 9.2s,\tbest lgbm's error=0.3523,\tbest lgbm's error=0.3523\n",
"[flaml.automl: 08-31 00:53:42] {1532} INFO - iteration 31, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:42] {1689} INFO - at 9.7s,\tbest lgbm's error=0.3523,\tbest lgbm's error=0.3523\n",
"[flaml.automl: 08-31 00:53:42] {1532} INFO - iteration 32, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:43] {1689} INFO - at 10.2s,\tbest lgbm's error=0.3523,\tbest lgbm's error=0.3523\n",
"[flaml.automl: 08-31 00:53:43] {1532} INFO - iteration 33, current learner extra_tree\n",
"[flaml.automl: 08-31 00:53:43] {1689} INFO - at 10.4s,\tbest extra_tree's error=0.3763,\tbest lgbm's error=0.3523\n",
"[flaml.automl: 08-31 00:53:43] {1532} INFO - iteration 34, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:46] {1689} INFO - at 13.3s,\tbest lgbm's error=0.3475,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:46] {1532} INFO - iteration 35, current learner catboost\n",
"[flaml.automl: 08-31 00:53:46] {1689} INFO - at 13.9s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:46] {1532} INFO - iteration 36, current learner catboost\n",
"[flaml.automl: 08-31 00:53:47] {1689} INFO - at 14.4s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:47] {1532} INFO - iteration 37, current learner xgboost\n",
"[flaml.automl: 08-31 00:53:47] {1689} INFO - at 14.5s,\tbest xgboost's error=0.3746,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:47] {1532} INFO - iteration 38, current learner catboost\n",
"[flaml.automl: 08-31 00:53:47] {1689} INFO - at 14.8s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:47] {1532} INFO - iteration 39, current learner catboost\n",
"[flaml.automl: 08-31 00:53:48] {1689} INFO - at 15.4s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:48] {1532} INFO - iteration 40, current learner catboost\n",
"[flaml.automl: 08-31 00:53:48] {1689} INFO - at 15.6s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:48] {1532} INFO - iteration 41, current learner catboost\n",
"[flaml.automl: 08-31 00:53:50] {1689} INFO - at 17.4s,\tbest catboost's error=0.3493,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:50] {1532} INFO - iteration 42, current learner xgboost\n",
"[flaml.automl: 08-31 00:53:50] {1689} INFO - at 17.6s,\tbest xgboost's error=0.3673,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:50] {1532} INFO - iteration 43, current learner lgbm\n",
"[flaml.automl: 08-31 00:53:52] {1689} INFO - at 19.8s,\tbest lgbm's error=0.3475,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:52] {1532} INFO - iteration 44, current learner xgboost\n",
"[flaml.automl: 08-31 00:53:52] {1689} INFO - at 19.9s,\tbest xgboost's error=0.3673,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:52] {1532} INFO - iteration 45, current learner extra_tree\n",
"[flaml.automl: 08-31 00:53:53] {1689} INFO - at 20.1s,\tbest extra_tree's error=0.3755,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:53] {1532} INFO - iteration 46, current learner xgboost\n",
"[flaml.automl: 08-31 00:53:53] {1689} INFO - at 20.3s,\tbest xgboost's error=0.3617,\tbest lgbm's error=0.3475\n",
"[flaml.automl: 08-31 00:53:53] {1532} INFO - iteration 47, current learner catboost\n",
"[flaml.automl: 08-31 00:53:55] {1689} INFO - at 22.6s,\tbest catboost's error=0.3469,\tbest catboost's error=0.3469\n",
"[flaml.automl: 08-31 00:53:55] {1532} INFO - iteration 48, current learner xgboost\n",
"[flaml.automl: 08-31 00:53:55] {1689} INFO - at 22.8s,\tbest xgboost's error=0.3617,\tbest catboost's error=0.3469\n",
"[flaml.automl: 08-31 00:53:55] {1532} INFO - iteration 49, current learner lgbm\n",
"[flaml.automl: 08-31 00:54:00] {1689} INFO - at 27.4s,\tbest lgbm's error=0.3455,\tbest lgbm's error=0.3455\n",
"[flaml.automl: 08-31 00:54:00] {1532} INFO - iteration 50, current learner catboost\n",
"[flaml.automl: 08-31 00:54:01] {1689} INFO - at 29.0s,\tbest catboost's error=0.3469,\tbest lgbm's error=0.3455\n",
"[flaml.automl: 08-31 00:54:01] {1532} INFO - iteration 51, current learner xgboost\n",
"[flaml.automl: 08-31 00:54:02] {1689} INFO - at 29.2s,\tbest xgboost's error=0.3617,\tbest lgbm's error=0.3455\n",
"[flaml.automl: 08-31 00:54:02] {1532} INFO - iteration 52, current learner catboost\n",
"[flaml.automl: 08-31 00:54:03] {1689} INFO - at 30.3s,\tbest catboost's error=0.3469,\tbest lgbm's error=0.3455\n",
"[flaml.automl: 08-31 00:54:03] {1532} INFO - iteration 53, current learner rf\n",
"[flaml.automl: 08-31 00:54:03] {1689} INFO - at 30.7s,\tbest rf's error=0.3621,\tbest lgbm's error=0.3455\n",
"[flaml.automl: 08-31 00:54:03] {1532} INFO - iteration 54, current learner lgbm\n",
"[flaml.automl: 08-31 00:54:10] {1689} INFO - at 37.2s,\tbest lgbm's error=0.3444,\tbest lgbm's error=0.3444\n",
"[flaml.automl: 08-31 00:54:10] {1532} INFO - iteration 55, current learner lgbm\n",
"[flaml.automl: 08-31 00:54:14] {1689} INFO - at 41.7s,\tbest lgbm's error=0.3444,\tbest lgbm's error=0.3444\n",
"[flaml.automl: 08-31 00:54:14] {1532} INFO - iteration 56, current learner xgboost\n",
"[flaml.automl: 08-31 00:54:14] {1689} INFO - at 41.9s,\tbest xgboost's error=0.3591,\tbest lgbm's error=0.3444\n",
"[flaml.automl: 08-31 00:54:14] {1532} INFO - iteration 57, current learner lgbm\n",
"[flaml.automl: 08-31 00:54:22] {1689} INFO - at 49.3s,\tbest lgbm's error=0.3444,\tbest lgbm's error=0.3444\n",
"[flaml.automl: 08-31 00:54:22] {1532} INFO - iteration 58, current learner rf\n",
"[flaml.automl: 08-31 00:54:22] {1689} INFO - at 49.8s,\tbest rf's error=0.3567,\tbest lgbm's error=0.3444\n",
"[flaml.automl: 08-31 00:54:22] {1532} INFO - iteration 59, current learner rf\n",
"[flaml.automl: 08-31 00:54:23] {1689} INFO - at 50.2s,\tbest rf's error=0.3567,\tbest lgbm's error=0.3444\n",
"[flaml.automl: 08-31 00:54:23] {1532} INFO - iteration 60, current learner lgbm\n",
"[flaml.automl: 08-31 00:54:28] {1689} INFO - at 55.4s,\tbest lgbm's error=0.3356,\tbest lgbm's error=0.3356\n",
"[flaml.automl: 08-31 00:54:28] {1532} INFO - iteration 61, current learner catboost\n",
"[flaml.automl: 08-31 00:54:32] {1689} INFO - at 59.4s,\tbest catboost's error=0.3469,\tbest lgbm's error=0.3356\n",
"[flaml.automl: 08-31 00:54:32] {1532} INFO - iteration 62, current learner lgbm\n",
"[flaml.automl: 08-31 00:54:37] {1689} INFO - at 64.7s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:54:37] {1532} INFO - iteration 63, current learner rf\n",
"[flaml.automl: 08-31 00:54:38] {1689} INFO - at 65.4s,\tbest rf's error=0.3565,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:54:38] {1532} INFO - iteration 64, current learner xgboost\n",
"[flaml.automl: 08-31 00:54:38] {1689} INFO - at 65.6s,\tbest xgboost's error=0.3591,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:54:38] {1532} INFO - iteration 65, current learner lgbm\n",
"[flaml.automl: 08-31 00:54:43] {1689} INFO - at 70.4s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:54:43] {1532} INFO - iteration 66, current learner lgbm\n",
"[flaml.automl: 08-31 00:54:54] {1689} INFO - at 81.3s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:54:54] {1532} INFO - iteration 67, current learner xgboost\n",
"[flaml.automl: 08-31 00:54:54] {1689} INFO - at 81.5s,\tbest xgboost's error=0.3591,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:54:54] {1532} INFO - iteration 68, current learner lgbm\n",
"[flaml.automl: 08-31 00:54:57] {1689} INFO - at 84.9s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:54:57] {1532} INFO - iteration 69, current learner lgbm\n",
"[flaml.automl: 08-31 00:55:01] {1689} INFO - at 88.3s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:01] {1532} INFO - iteration 70, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:01] {1689} INFO - at 88.5s,\tbest xgboost's error=0.3591,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:01] {1532} INFO - iteration 71, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:01] {1689} INFO - at 88.6s,\tbest xgboost's error=0.3591,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:01] {1532} INFO - iteration 72, current learner catboost\n",
"[flaml.automl: 08-31 00:55:04] {1689} INFO - at 91.8s,\tbest catboost's error=0.3469,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:04] {1532} INFO - iteration 73, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:05] {1689} INFO - at 92.1s,\tbest extra_tree's error=0.3755,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:05] {1532} INFO - iteration 74, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:05] {1689} INFO - at 92.3s,\tbest xgboost's error=0.3591,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:05] {1532} INFO - iteration 75, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:05] {1689} INFO - at 92.5s,\tbest extra_tree's error=0.3644,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:05] {1532} INFO - iteration 76, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:05] {1689} INFO - at 92.9s,\tbest xgboost's error=0.3574,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:05] {1532} INFO - iteration 77, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:06] {1689} INFO - at 93.3s,\tbest xgboost's error=0.3574,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:06] {1532} INFO - iteration 78, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:06] {1689} INFO - at 93.5s,\tbest extra_tree's error=0.3644,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:06] {1532} INFO - iteration 79, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:06] {1689} INFO - at 93.7s,\tbest extra_tree's error=0.3644,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:06] {1532} INFO - iteration 80, current learner rf\n",
"[flaml.automl: 08-31 00:55:07] {1689} INFO - at 94.3s,\tbest rf's error=0.3565,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:07] {1532} INFO - iteration 81, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:07] {1689} INFO - at 94.5s,\tbest extra_tree's error=0.3644,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:07] {1532} INFO - iteration 82, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:07] {1689} INFO - at 94.7s,\tbest extra_tree's error=0.3644,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:07] {1532} INFO - iteration 83, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:07] {1689} INFO - at 94.9s,\tbest extra_tree's error=0.3644,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:07] {1532} INFO - iteration 84, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:08] {1689} INFO - at 95.2s,\tbest extra_tree's error=0.3644,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:08] {1532} INFO - iteration 85, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:08] {1689} INFO - at 95.5s,\tbest extra_tree's error=0.3640,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:08] {1532} INFO - iteration 86, current learner rf\n",
"[flaml.automl: 08-31 00:55:09] {1689} INFO - at 96.5s,\tbest rf's error=0.3565,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:09] {1532} INFO - iteration 87, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:09] {1689} INFO - at 96.9s,\tbest xgboost's error=0.3534,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:09] {1532} INFO - iteration 88, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:10] {1689} INFO - at 97.2s,\tbest xgboost's error=0.3534,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:10] {1532} INFO - iteration 89, current learner lgbm\n",
"[flaml.automl: 08-31 00:55:19] {1689} INFO - at 106.4s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:19] {1532} INFO - iteration 90, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:20] {1689} INFO - at 107.3s,\tbest xgboost's error=0.3504,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:20] {1532} INFO - iteration 91, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:20] {1689} INFO - at 107.8s,\tbest xgboost's error=0.3504,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:20] {1532} INFO - iteration 92, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:20] {1689} INFO - at 108.0s,\tbest extra_tree's error=0.3624,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:20] {1532} INFO - iteration 93, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:21] {1689} INFO - at 108.3s,\tbest extra_tree's error=0.3624,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:21] {1532} INFO - iteration 94, current learner xgboost\n",
"[flaml.automl: 08-31 00:55:24] {1689} INFO - at 111.2s,\tbest xgboost's error=0.3504,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:24] {1532} INFO - iteration 95, current learner lgbm\n",
"[flaml.automl: 08-31 00:55:27] {1689} INFO - at 114.6s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:27] {1532} INFO - iteration 96, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:27] {1689} INFO - at 114.8s,\tbest extra_tree's error=0.3598,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:27] {1532} INFO - iteration 97, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:28] {1689} INFO - at 115.2s,\tbest extra_tree's error=0.3598,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:28] {1532} INFO - iteration 98, current learner extra_tree\n",
"[flaml.automl: 08-31 00:55:28] {1689} INFO - at 115.4s,\tbest extra_tree's error=0.3597,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:28] {1532} INFO - iteration 99, current learner lrl1\n",
"No low-cost partial config given to the search algorithm. For cost-frugal search, consider providing low-cost values for cost-related hps via 'low_cost_partial_config'.\n",
"/home/dmx/miniconda2/envs/blend/lib/python3.8/site-packages/sklearn/linear_model/_sag.py:328: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge\n",
" warnings.warn(\"The max_iter was reached which means \"\n",
"[flaml.automl: 08-31 00:55:28] {1689} INFO - at 115.8s,\tbest lrl1's error=0.4338,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:28] {1532} INFO - iteration 100, current learner lrl1\n",
"/home/dmx/miniconda2/envs/blend/lib/python3.8/site-packages/sklearn/linear_model/_sag.py:328: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge\n",
" warnings.warn(\"The max_iter was reached which means \"\n",
"[flaml.automl: 08-31 00:55:29] {1689} INFO - at 116.1s,\tbest lrl1's error=0.4338,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:29] {1532} INFO - iteration 101, current learner lrl1\n",
"/home/dmx/miniconda2/envs/blend/lib/python3.8/site-packages/sklearn/linear_model/_sag.py:328: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge\n",
" warnings.warn(\"The max_iter was reached which means \"\n",
"[flaml.automl: 08-31 00:55:29] {1689} INFO - at 116.4s,\tbest lrl1's error=0.4338,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:29] {1532} INFO - iteration 102, current learner lrl1\n",
"/home/dmx/miniconda2/envs/blend/lib/python3.8/site-packages/sklearn/linear_model/_sag.py:328: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge\n",
" warnings.warn(\"The max_iter was reached which means \"\n",
"[flaml.automl: 08-31 00:55:30] {1689} INFO - at 117.3s,\tbest lrl1's error=0.4334,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:30] {1532} INFO - iteration 103, current learner catboost\n",
"[flaml.automl: 08-31 00:55:42] {1689} INFO - at 130.0s,\tbest catboost's error=0.3385,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:42] {1532} INFO - iteration 104, current learner lgbm\n",
"[flaml.automl: 08-31 00:55:52] {1689} INFO - at 139.2s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:55:52] {1532} INFO - iteration 105, current learner catboost\n",
"[flaml.automl: 08-31 00:56:44] {1689} INFO - at 191.8s,\tbest catboost's error=0.3385,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:56:44] {1532} INFO - iteration 106, current learner lgbm\n",
"[flaml.automl: 08-31 00:56:49] {1689} INFO - at 196.9s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:56:49] {1532} INFO - iteration 107, current learner catboost\n",
"[flaml.automl: 08-31 00:56:56] {1689} INFO - at 203.1s,\tbest catboost's error=0.3378,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:56:56] {1532} INFO - iteration 108, current learner xgboost\n",
"[flaml.automl: 08-31 00:56:56] {1689} INFO - at 203.8s,\tbest xgboost's error=0.3504,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:56:56] {1532} INFO - iteration 109, current learner lgbm\n",
"[flaml.automl: 08-31 00:57:02] {1689} INFO - at 209.3s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:57:02] {1532} INFO - iteration 110, current learner extra_tree\n",
"[flaml.automl: 08-31 00:57:02] {1689} INFO - at 209.8s,\tbest extra_tree's error=0.3580,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:57:02] {1532} INFO - iteration 111, current learner extra_tree\n",
"[flaml.automl: 08-31 00:57:03] {1689} INFO - at 210.2s,\tbest extra_tree's error=0.3555,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:57:03] {1532} INFO - iteration 112, current learner lgbm\n",
"[flaml.automl: 08-31 00:57:21] {1689} INFO - at 228.3s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:57:21] {1532} INFO - iteration 113, current learner extra_tree\n",
"[flaml.automl: 08-31 00:57:21] {1689} INFO - at 228.6s,\tbest extra_tree's error=0.3555,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:57:21] {1532} INFO - iteration 114, current learner extra_tree\n",
"[flaml.automl: 08-31 00:57:21] {1689} INFO - at 229.0s,\tbest extra_tree's error=0.3555,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:57:21] {1532} INFO - iteration 115, current learner lrl1\n",
"/home/dmx/miniconda2/envs/blend/lib/python3.8/site-packages/sklearn/linear_model/_sag.py:328: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge\n",
" warnings.warn(\"The max_iter was reached which means \"\n",
"[flaml.automl: 08-31 00:57:22] {1689} INFO - at 229.9s,\tbest lrl1's error=0.4334,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:57:22] {1532} INFO - iteration 116, current learner lgbm\n",
"[flaml.automl: 08-31 00:57:25] {1689} INFO - at 232.4s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:57:25] {1532} INFO - iteration 117, current learner lgbm\n",
"[flaml.automl: 08-31 00:57:34] {1689} INFO - at 241.7s,\tbest lgbm's error=0.3300,\tbest lgbm's error=0.3300\n",
"[flaml.automl: 08-31 00:57:34] {1766} INFO - selected model: LGBMClassifier(colsample_bytree=0.3841266992710469,\n",
" learning_rate=0.04886499949999022, max_bin=512,\n",
" min_child_samples=4, n_estimators=220, num_leaves=270,\n",
" objective='binary', reg_alpha=0.0009765625,\n",
" reg_lambda=0.07539015928723636, verbose=-1)\n",
"[flaml.automl: 08-31 00:57:43] {1814} INFO - retrain lgbm for 9.1s\n",
"[flaml.automl: 08-31 00:57:43] {1817} INFO - retrained model: LGBMClassifier(colsample_bytree=0.3841266992710469,\n",
" learning_rate=0.04886499949999022, max_bin=512,\n",
" min_child_samples=4, n_estimators=220, num_leaves=270,\n",
" objective='binary', reg_alpha=0.0009765625,\n",
" reg_lambda=0.07539015928723636, verbose=-1)\n",
"[flaml.automl: 08-31 00:57:43] {1364} INFO - fit succeeded\n",
"[flaml.automl: 08-31 00:57:43] {1365} INFO - Time taken to find the best model: 241.71747207641602\n",
"[flaml.automl: 08-31 00:57:43] {1370} WARNING - Time taken to find the best model is 101% of the provided time budget and not all estimators' hyperparameter search converged. Consider increasing the time budget.\n"
]
}
],
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": [
"outputPrepend"
]
}
},
{
"cell_type": "markdown",
"source": [
"### Best model and metric"
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 5,
"source": [
"''' retrieve best config and best learner'''\n",
"print('Best ML leaner:', automl.best_estimator)\n",
"print('Best hyperparmeter config:', automl.best_config)\n",
"print('Best accuracy on validation data: {0:.4g}'.format(1-automl.best_loss))\n",
"print('Training duration of best run: {0:.4g} s'.format(automl.best_config_train_time))"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Best ML leaner: lgbm\n",
"Best hyperparmeter config: {'n_estimators': 220, 'num_leaves': 270, 'min_child_samples': 4, 'learning_rate': 0.04886499949999022, 'log_max_bin': 10, 'colsample_bytree': 0.3841266992710469, 'reg_alpha': 0.0009765625, 'reg_lambda': 0.07539015928723636, 'FLAML_sample_size': 364083}\n",
"Best accuracy on validation data: 0.67\n",
"Training duration of best run: 9.323 s\n"
]
}
],
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
}
},
{
"cell_type": "code",
"execution_count": 6,
"source": [
"automl.model.estimator"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"LGBMClassifier(colsample_bytree=0.3841266992710469,\n",
" learning_rate=0.04886499949999022, max_bin=512,\n",
" min_child_samples=4, n_estimators=220, num_leaves=270,\n",
" objective='binary', reg_alpha=0.0009765625,\n",
" reg_lambda=0.07539015928723636, verbose=-1)"
]
},
"metadata": {},
"execution_count": 6
}
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 7,
"source": [
"''' pickle and save the automl object '''\n",
"import pickle\n",
"with open('automl.pkl', 'wb') as f:\n",
" pickle.dump(automl, f, pickle.HIGHEST_PROTOCOL)"
],
"outputs": [],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 8,
"source": [
"''' compute predictions of testing dataset ''' \n",
"y_pred = automl.predict(X_test)\n",
"print('Predicted labels', y_pred)\n",
"print('True labels', y_test)\n",
"y_pred_proba = automl.predict_proba(X_test)[:,1]"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Predicted labels ['1' '0' '1' ... '1' '0' '0']\n",
"True labels 118331 0\n",
"328182 0\n",
"335454 0\n",
"520591 1\n",
"344651 0\n",
" ..\n",
"367080 0\n",
"203510 1\n",
"254894 0\n",
"296512 1\n",
"362444 0\n",
"Name: Delay, Length: 134846, dtype: category\n",
"Categories (2, object): ['0' < '1']\n"
]
}
],
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
}
},
{
"cell_type": "code",
"execution_count": 9,
"source": [
"''' compute different metric values on testing dataset'''\n",
"from flaml.ml import sklearn_metric_loss_score\n",
"print('accuracy', '=', 1 - sklearn_metric_loss_score('accuracy', y_pred, y_test))\n",
"print('roc_auc', '=', 1 - sklearn_metric_loss_score('roc_auc', y_pred_proba, y_test))\n",
"print('log_loss', '=', sklearn_metric_loss_score('log_loss', y_pred_proba, y_test))"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"accuracy = 0.6729231864497278\n",
"roc_auc = 0.7261961112785199\n",
"log_loss = 0.6033707263741326\n"
]
}
],
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
}
},
{
"cell_type": "markdown",
"source": [
"See Section 4 for an accuracy comparison with default LightGBM and XGBoost.\n",
"\n",
"### Log history"
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 10,
"source": [
"from flaml.data import get_output_from_log\n",
"time_history, best_valid_loss_history, valid_loss_history, config_history, metric_history = \\\n",
" get_output_from_log(filename=settings['log_file_name'], time_budget=240)\n",
"for config in config_history:\n",
" print(config)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'Current Learner': 'lgbm', 'Current Sample': 10000, 'Current Hyper-parameters': {'n_estimators': 4, 'num_leaves': 4, 'min_child_samples': 20, 'learning_rate': 0.09999999999999995, 'log_max_bin': 8, 'colsample_bytree': 1.0, 'reg_alpha': 0.0009765625, 'reg_lambda': 1.0, 'FLAML_sample_size': 10000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 4, 'num_leaves': 4, 'min_child_samples': 20, 'learning_rate': 0.09999999999999995, 'log_max_bin': 8, 'colsample_bytree': 1.0, 'reg_alpha': 0.0009765625, 'reg_lambda': 1.0, 'FLAML_sample_size': 10000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 10000, 'Current Hyper-parameters': {'n_estimators': 12, 'num_leaves': 4, 'min_child_samples': 15, 'learning_rate': 0.2712162364070373, 'log_max_bin': 10, 'colsample_bytree': 0.9285002286474459, 'reg_alpha': 0.002668211515123386, 'reg_lambda': 0.5215467339232843, 'FLAML_sample_size': 10000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 12, 'num_leaves': 4, 'min_child_samples': 15, 'learning_rate': 0.2712162364070373, 'log_max_bin': 10, 'colsample_bytree': 0.9285002286474459, 'reg_alpha': 0.002668211515123386, 'reg_lambda': 0.5215467339232843, 'FLAML_sample_size': 10000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 10000, 'Current Hyper-parameters': {'n_estimators': 14, 'num_leaves': 5, 'min_child_samples': 9, 'learning_rate': 0.2835381908967212, 'log_max_bin': 9, 'colsample_bytree': 0.8304072431299575, 'reg_alpha': 0.0014132988481787994, 'reg_lambda': 0.033183495034912504, 'FLAML_sample_size': 10000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 14, 'num_leaves': 5, 'min_child_samples': 9, 'learning_rate': 0.2835381908967212, 'log_max_bin': 9, 'colsample_bytree': 0.8304072431299575, 'reg_alpha': 0.0014132988481787994, 'reg_lambda': 0.033183495034912504, 'FLAML_sample_size': 10000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 10000, 'Current Hyper-parameters': {'n_estimators': 53, 'num_leaves': 4, 'min_child_samples': 7, 'learning_rate': 0.15662398373030859, 'log_max_bin': 10, 'colsample_bytree': 0.7610534336273627, 'reg_alpha': 0.0009765625, 'reg_lambda': 0.0064258982194552745, 'FLAML_sample_size': 10000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 53, 'num_leaves': 4, 'min_child_samples': 7, 'learning_rate': 0.15662398373030859, 'log_max_bin': 10, 'colsample_bytree': 0.7610534336273627, 'reg_alpha': 0.0009765625, 'reg_lambda': 0.0064258982194552745, 'FLAML_sample_size': 10000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 40000, 'Current Hyper-parameters': {'n_estimators': 53, 'num_leaves': 4, 'min_child_samples': 7, 'learning_rate': 0.15662398373030859, 'log_max_bin': 10, 'colsample_bytree': 0.7610534336273627, 'reg_alpha': 0.0009765625, 'reg_lambda': 0.0064258982194552745, 'FLAML_sample_size': 40000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 53, 'num_leaves': 4, 'min_child_samples': 7, 'learning_rate': 0.15662398373030859, 'log_max_bin': 10, 'colsample_bytree': 0.7610534336273627, 'reg_alpha': 0.0009765625, 'reg_lambda': 0.0064258982194552745, 'FLAML_sample_size': 40000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 40000, 'Current Hyper-parameters': {'n_estimators': 89, 'num_leaves': 4, 'min_child_samples': 6, 'learning_rate': 0.2915468353191124, 'log_max_bin': 10, 'colsample_bytree': 0.8291836310024803, 'reg_alpha': 0.0009765625, 'reg_lambda': 0.022917008702549507, 'FLAML_sample_size': 40000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 89, 'num_leaves': 4, 'min_child_samples': 6, 'learning_rate': 0.2915468353191124, 'log_max_bin': 10, 'colsample_bytree': 0.8291836310024803, 'reg_alpha': 0.0009765625, 'reg_lambda': 0.022917008702549507, 'FLAML_sample_size': 40000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 40000, 'Current Hyper-parameters': {'n_estimators': 53, 'num_leaves': 19, 'min_child_samples': 7, 'learning_rate': 0.15662398373030845, 'log_max_bin': 9, 'colsample_bytree': 0.7610534336273627, 'reg_alpha': 0.006958608037974516, 'reg_lambda': 0.0064258982194552745, 'FLAML_sample_size': 40000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 53, 'num_leaves': 19, 'min_child_samples': 7, 'learning_rate': 0.15662398373030845, 'log_max_bin': 9, 'colsample_bytree': 0.7610534336273627, 'reg_alpha': 0.006958608037974516, 'reg_lambda': 0.0064258982194552745, 'FLAML_sample_size': 40000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 364083, 'Current Hyper-parameters': {'n_estimators': 53, 'num_leaves': 19, 'min_child_samples': 7, 'learning_rate': 0.15662398373030845, 'log_max_bin': 9, 'colsample_bytree': 0.7610534336273627, 'reg_alpha': 0.006958608037974516, 'reg_lambda': 0.0064258982194552745, 'FLAML_sample_size': 364083}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 53, 'num_leaves': 19, 'min_child_samples': 7, 'learning_rate': 0.15662398373030845, 'log_max_bin': 9, 'colsample_bytree': 0.7610534336273627, 'reg_alpha': 0.006958608037974516, 'reg_lambda': 0.0064258982194552745, 'FLAML_sample_size': 364083}}\n",
"{'Current Learner': 'catboost', 'Current Sample': 40000, 'Current Hyper-parameters': {'early_stopping_rounds': 11, 'learning_rate': 0.0943289179113066, 'FLAML_sample_size': 40000}, 'Best Learner': 'catboost', 'Best Hyper-parameters': {'early_stopping_rounds': 11, 'learning_rate': 0.0943289179113066, 'FLAML_sample_size': 40000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 364083, 'Current Hyper-parameters': {'n_estimators': 112, 'num_leaves': 42, 'min_child_samples': 6, 'learning_rate': 0.05081630216512539, 'log_max_bin': 8, 'colsample_bytree': 0.6140029119098487, 'reg_alpha': 0.0043659867548350275, 'reg_lambda': 0.00988067243709054, 'FLAML_sample_size': 364083}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 112, 'num_leaves': 42, 'min_child_samples': 6, 'learning_rate': 0.05081630216512539, 'log_max_bin': 8, 'colsample_bytree': 0.6140029119098487, 'reg_alpha': 0.0043659867548350275, 'reg_lambda': 0.00988067243709054, 'FLAML_sample_size': 364083}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 364083, 'Current Hyper-parameters': {'n_estimators': 224, 'num_leaves': 19, 'min_child_samples': 3, 'learning_rate': 0.06277721620788371, 'log_max_bin': 9, 'colsample_bytree': 0.6641909243388362, 'reg_alpha': 0.02046640007359354, 'reg_lambda': 0.021186644668220945, 'FLAML_sample_size': 364083}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 224, 'num_leaves': 19, 'min_child_samples': 3, 'learning_rate': 0.06277721620788371, 'log_max_bin': 9, 'colsample_bytree': 0.6641909243388362, 'reg_alpha': 0.02046640007359354, 'reg_lambda': 0.021186644668220945, 'FLAML_sample_size': 364083}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 364083, 'Current Hyper-parameters': {'n_estimators': 159, 'num_leaves': 54, 'min_child_samples': 3, 'learning_rate': 0.15480526013767984, 'log_max_bin': 9, 'colsample_bytree': 0.4565737938156385, 'reg_alpha': 0.014844095616079196, 'reg_lambda': 0.01191495998733094, 'FLAML_sample_size': 364083}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 159, 'num_leaves': 54, 'min_child_samples': 3, 'learning_rate': 0.15480526013767984, 'log_max_bin': 9, 'colsample_bytree': 0.4565737938156385, 'reg_alpha': 0.014844095616079196, 'reg_lambda': 0.01191495998733094, 'FLAML_sample_size': 364083}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 364083, 'Current Hyper-parameters': {'n_estimators': 100, 'num_leaves': 310, 'min_child_samples': 2, 'learning_rate': 0.0958942820044505, 'log_max_bin': 10, 'colsample_bytree': 0.40118043723920377, 'reg_alpha': 0.006532533034382694, 'reg_lambda': 0.02014962736208268, 'FLAML_sample_size': 364083}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 100, 'num_leaves': 310, 'min_child_samples': 2, 'learning_rate': 0.0958942820044505, 'log_max_bin': 10, 'colsample_bytree': 0.40118043723920377, 'reg_alpha': 0.006532533034382694, 'reg_lambda': 0.02014962736208268, 'FLAML_sample_size': 364083}}\n"
]
}
],
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
}
},
{
"cell_type": "code",
"execution_count": 11,
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"plt.title('Learning Curve')\n",
"plt.xlabel('Wall Clock Time (s)')\n",
"plt.ylabel('Validation Accuracy')\n",
"plt.scatter(time_history, 1 - np.array(valid_loss_history))\n",
"plt.step(time_history, 1 - np.array(best_valid_loss_history), where='post')\n",
"plt.show()"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
],
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"277.314375pt\" version=\"1.1\" viewBox=\"0 0 392.14375 277.314375\" width=\"392.14375pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 277.314375 \nL 392.14375 277.314375 \nL 392.14375 0 \nL 0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 50.14375 239.758125 \nL 384.94375 239.758125 \nL 384.94375 22.318125 \nL 50.14375 22.318125 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"PathCollection_1\">\n <defs>\n <path d=\"M 0 3 \nC 0.795609 3 1.55874 2.683901 2.12132 2.12132 \nC 2.683901 1.55874 3 0.795609 3 0 \nC 3 -0.795609 2.683901 -1.55874 2.12132 -2.12132 \nC 1.55874 -2.683901 0.795609 -3 0 -3 \nC -0.795609 -3 -1.55874 -2.683901 -2.12132 -2.12132 \nC -2.683901 -1.55874 -3 -0.795609 -3 0 \nC -3 0.795609 -2.683901 1.55874 -2.12132 2.12132 \nC -1.55874 2.683901 -0.795609 3 0 3 \nz\n\" id=\"ma57fd23b24\" style=\"stroke:#1f77b4;\"/>\n </defs>\n <g clip-path=\"url(#p67affc388f)\">\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"65.361932\" xlink:href=\"#ma57fd23b24\" y=\"229.874489\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"67.475284\" xlink:href=\"#ma57fd23b24\" y=\"181.866789\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"70.521562\" xlink:href=\"#ma57fd23b24\" y=\"175.096473\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"72.871654\" xlink:href=\"#ma57fd23b24\" y=\"160.735195\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"87.311462\" xlink:href=\"#ma57fd23b24\" y=\"158.273262\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"89.835333\" xlink:href=\"#ma57fd23b24\" y=\"133.551349\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"94.247802\" xlink:href=\"#ma57fd23b24\" y=\"124.62684\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"122.20591\" xlink:href=\"#ma57fd23b24\" y=\"104.828793\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"166.896725\" xlink:href=\"#ma57fd23b24\" y=\"102.36686\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"190.136353\" xlink:href=\"#ma57fd23b24\" y=\"96.314608\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"237.166905\" xlink:href=\"#ma57fd23b24\" y=\"91.801063\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"324.748392\" xlink:href=\"#ma57fd23b24\" y=\"55.282386\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"369.725568\" xlink:href=\"#ma57fd23b24\" y=\"32.201761\"/>\n </g>\n </g>\n <g id=\"matplotlib.axis_1\">\n <g id=\"xtick_1\">\n <g id=\"line2d_1\">\n <defs>\n <path d=\"M 0 0 \nL 0 3.5 \n\" id=\"m7d47cee871\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n </defs>\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"58.415549\" xlink:href=\"#m7d47cee871\" y=\"239.758125\"/>\n </g>\n </g>\n <g id=\"text_1\">\n <!-- 0 -->\n <defs>\n <path d=\"M 31.78125 66.40625 \nQ 24.171875 66.40625 20.328125 58.90625 \nQ 16.5 51.421875 16.5 36.375 \nQ 16.5 21.390625 20.328125 13.890625 \nQ 24.171875 6.390625 31.78125 6.390625 \nQ 39.453125 6.390625 43.28125 13.890625 \nQ 47.125 21.390625 47.125 36.375 \nQ 47.125 51.421875 43.28125 58.90625 \nQ 39.453125 66.40625 31.78125 66.40625 \nz\nM 31.78125 74.21875 \nQ 44.046875 74.21875 50.515625 64.515625 \nQ 56.984375 54.828125 56.984375 36.375 \nQ 56.984375 17.96875 50.515625 8.265625 \nQ 44.046875 -1.421875 31.78125 -1.421875 \nQ 19.53125 -1.421875 13.0625 8.265625 \nQ 6.59375 17.96875 6.59375 36.375 \nQ 6.59375 54.828125 13.0625 64.515625 \nQ 19.53125 74.21875 31.78125 74.21875 \nz\n\" id=\"DejaVuSans-48\"/>\n </defs>\n
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEWCAYAAAB8LwAVAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8GearUAAAgAElEQVR4nO3de5hddX3v8fcnk5CMxTBgAk2GS8IhicYGE40o4gVoMZFHSUREsBfFFrReji01lNRKLRwFm2qPfUy1wYMUFUXSOEZNiRQQWm5JIJjL0GBMkMwETYCMIowkmXzPH2vtsLJZM7MSZs2ePfvzep79zF6/dftO2Ozv/H7rd1FEYGZmVm1ErQMwM7OhyQnCzMxyOUGYmVkuJwgzM8vlBGFmZrmcIMzMLJcThNkhkPQmSZtqHYdZmZwgrO5IelTSH9Qyhoj4r4iYVtb1Jc2RdJekpyXtlHSnpHPKup9ZHicIsxySmmp47/OAm4EbgGOBY4ArgHccwrUkyf+f2yHxB8eGDUkjJF0u6WeSnpT0HUlHZfbfLOkXkn6V/nX+ysy+6yV9WdIKSc8AZ6Q1lU9IWpeec5OkMenxp0vqyJzf67Hp/sskPS5pu6Q/kxSSTsr5HQR8AbgqIr4aEb+KiH0RcWdEXJwe82lJ38icMym93sh0+8eSPiPpbuBZYIGkNVX3+UtJy9P3oyX9o6THJP1S0lckNb/I/xw2DDhB2HDyMWA+8BZgIrALWJzZ/x/AFOBo4EHgm1Xnvxf4DPBS4L/TsvOBucBk4GTg/X3cP/dYSXOBS4E/AE4CTu/jGtOA44ClfRxTxB8Dl5D8Ll8Bpkmaktn/XuDG9P01wFRgZhpfK0mNxRqcE4QNJx8CPhkRHRHxHPBp4LzKX9YRcV1EPJ3Z9ypJR2TO/15E3J3+xf7btOyfI2J7RDwFfJ/kS7Q3vR17PvC1iNgYEc+m9+7Ny9Kfjxf9pXtxfXq/vRHxK+B7wIUAaaJ4ObA8rbFcAvxlRDwVEU8DnwUueJH3t2HACcKGkxOA70rqktQFPAz0AMdIapJ0Tdr89Gvg0fSccZnzt+Vc8xeZ988Ch/dx/96OnVh17bz7VDyZ/pzQxzFFVN/jRtIEQVJ7aEuT1XjgJcADmX+3W9Jya3BOEDacbAPeFhEtmdeYiOgk+VKcR9LMcwQwKT1HmfPLmtr4cZKHzRXH9XHsJpLf4119HPMMyZd6xe/mHFP9u9wKjJc0kyRRVJqXngC6gVdm/s2OiIi+EqE1CCcIq1ejJI3JvEaStLV/RtIJAJLGS5qXHv9S4DmSv9BfQtKMMli+A1wk6RWSXgJ8qrcDI5l//1LgU5IukjQ2ffj+RklL0sMeAt4s6fi0iWxhfwFExB6SnlGLgKNIEgYRsQ+4FvgnSUcDSGqVNOeQf1sbNpwgrF6tIPnLt/L6NPBFYDnwI0lPA/cBr0uPvwH4OdAJtKf7BkVE/Afwz8AdwObMvZ/r5filwHuADwDbgV8C/4fkOQIRcStwE7AOeAD4QcFQbiSpQd0cEXsz5X9diSttfvtPkofl1uDkBYPMBpekVwAbgNFVX9RmQ4prEGaDQNI70/EGRwKfA77v5GBDnROE2eD4ILAD+BlJz6o/r204Zv1zE5OZmeVyDcLMzHKNrHUAA2XcuHExadKkWodhZlZXHnjggSciIndg5LBJEJMmTWLNmjX9H2hmZvtJ+nlv+9zEZGZmuZwgzMwslxOEmZnlcoIwM7NcThBmZpar1AQhaa6kTZI2S7q8l2POl9QuaaOkG9OyMyQ9lHn9VtL8MmM1M6s3bWs7Oe2a25l8+Q857ZrbaVvbOaDXL62ba7ro+2LgLKADWC1peUS0Z46ZQjJV8WkRsasy3XBE3EG6Gle6pvBm4EdlxWpmVm/a1naycNl6uvf0ANDZ1c3CZesBmD+rdUDuUWYN4hRgc0RsiYjdwLdJFmzJuhhYHBG7ACJiR851zgP+I139yszMgEUrN+1PDhXde3pYtHLTgN2jzATRyoHLHnakZVlTgamS7pZ0X7q4e7ULgG/l3UDSJZLWSFqzc+fOAQnazKwebO/qPqjyQ1Hrh9QjgSnA6STLIF4rqaWyU9IEYAawMu/kiFgSEbMjYvb48V5C18wax8SW5oMqPxRlJohODlx799i0LKsDWB4ReyJiK/AIScKoOB/4brpcopmZpRbMmUbzqKYDyppHNbFgzsAtBlhmglgNTJE0WdJhJE1Fy6uOaSOpPSBpHEmT05bM/gvppXnJzKyRzZ/VytXnzuCwpuRrvLWlmavPnTFgD6ihxF5MEbFX0kdJmoeagOsiYqOkK4E1EbE83fdWSe0ki6gsiIgnASRNIqmB3FlWjGZm9Wz+rFa+teoxAG764KkDfv1SZ3ONiBUki8tny67IvA/g0vRVfe6jvPChtpmZDZJaP6Q2M7MhygnCzMxyOUGYmVkuJwgzM8vlBGFmZrmcIMzMLJcThJmZ5XKCMDOzXE4QZmaWywnCzMxyOUGYmVkuJwgzM8vlBGFmZrmcIMzMLFep032bmdWLtrWdLFq5ie1d3UxsaWbBnGkDuvhOPXKCMLOG17a2k4XL1tO9pweAzq5uFi5bD9DQScIJwswa3qKVm/Ynh4ruPT1ctnTd/hXbhqr2x3/N9AljS7m2n0GYWcPb3tWdW767Z98gR3Lwpk8Yy7yZ5dRyXIMws4Y3saWZzpwk0drSXMpaz/XCNQgza3gL5kyjeVTTAWXNo5pYMGdajSIaGlyDMLOGV3kQfdnSdezu2UerezEBThBmZkCSJCoPpBu5WSnLTUxmZpbLCcLMzHI5QZiZWS4nCDMzy+UEYWZmuZwgzMwslxOEmZnlcoIwM7NcThBmZpbLCcLMzHKVmiAkzZW0SdJmSZf3csz5ktolbZR0Y6b8eEk/kvRwun9SmbGamdmBSpuLSVITsBg4C+gAVktaHhHtmWOmAAuB0yJil6SjM5e4AfhMRNwq6XBg6E/MbmY2jJRZgzgF2BwRWyJiN/BtYF7VMRcDiyNiF0BE7ACQNB0YGRG3puW/iYhnS4zVzMyqlJkgWoFtme2OtCxrKjBV0t2S7pM0N1PeJWmZpLWSFqU1EjMzGyS1fkg9EpgCnA5cCFwrqSUtfxPwCeC1wInA+6tPlnSJpDWS1uzcuXOwYjYzawhlJohO4LjM9rFpWVYHsDwi9kTEVuARkoTRATyUNk/tBdqAV1ffICKWRMTsiJg9fvz4Un4JM7NGVWaCWA1MkTRZ0mHABcDyqmPaSGoPSBpH0rS0JT23RVLlW/9MoB0zMxs0pSWI9C//jwIrgYeB70TERklXSjonPWwl8KSkduAOYEFEPBkRPSTNS7dJWg8IuLasWM3M7IVKXXI0IlYAK6rKrsi8D+DS9FV97q3AyWXGZ2Zmvav1Q2ozMxuinCDMzCyXE4SZmeVygjAzs1z9JghJLxuMQMzMbGgpUoO4T9LNks6WpNIjMjOzIaFIgpgKLAH+GPippM9KmlpuWGZmVmv9JohI3BoRF5LMvvo+YJWkOyWdWnqEZmZWE/0OlEufQfwRSQ3il8DHSKbMmAncDEwuM0AzM6uNIiOp7wW+DsyPiI5M+RpJXyknLDMzq7UiCWJaOiXGC0TE5wY4HjPrRdvaThat3MT2rm4mtjSzYM405s+qXmLFbOAUeUj9o3SNBgAkHSlpZYkxmVmVtrWdLFy2ns6ubgLo7Opm4bL1tK2tnkHfbOAUqUGMj4iuykbO2tFmVrJFKzfRvafngLLuPT1ctnQd31r1WI2iGn7aH/810yeMrXUYQ0aRGkSPpOMrG5JOAHKbnMysHNu7unPLd/fsG+RIhrfpE8Yyb6ab7SqK1CA+Cfy3pDtJ1mV4E3BJqVGZlaRe2/EntjTTmZMkWluauemD7m1u5SgyDuIWkuU+bwK+DbwmIvwMwupOPbfjL5gzjeZRTQeUNY9qYsGcaTWKyBpB0QWDeoAdwBhguiQi4q7ywjIbePXejj+xZQxbdj5DkNQc6qX2Y/WryEC5PwM+DhwLPAS8nmRsxJnlhmY2sOq9HX/c4aMZd/ho5s1s5b2vO77
},
"metadata": {
"needs_background": "light"
}
}
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "markdown",
"source": [
"## 3. Comparison with alternatives\n"
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"### Default LightGBM"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 12,
"source": [
"from lightgbm import LGBMClassifier\n",
"lgbm = LGBMClassifier()"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 13,
"source": [
"lgbm.fit(X_train, y_train)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"LGBMClassifier()"
]
},
"metadata": {},
"execution_count": 13
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 14,
"source": [
"y_pred_lgbm = lgbm.predict(X_test)"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"### Default XGBoost"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 15,
"source": [
"from xgboost import XGBClassifier\n",
"xgb = XGBClassifier()\n",
"cat_columns = X_train.select_dtypes(include=['category']).columns\n",
"X = X_train.copy()\n",
"X[cat_columns] = X[cat_columns].apply(lambda x: x.cat.codes)\n"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 16,
"source": [
"xgb.fit(X, y_train)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n",
" colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,\n",
" importance_type='gain', interaction_constraints='',\n",
" learning_rate=0.300000012, max_delta_step=0, max_depth=6,\n",
" min_child_weight=1, missing=nan, monotone_constraints='()',\n",
" n_estimators=100, n_jobs=0, num_parallel_tree=1, random_state=0,\n",
" reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1,\n",
" tree_method='exact', validate_parameters=1, verbosity=None)"
]
},
"metadata": {},
"execution_count": 16
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 17,
"source": [
"X = X_test.copy()\n",
"X[cat_columns] = X[cat_columns].apply(lambda x: x.cat.codes)\n",
"y_pred_xgb = xgb.predict(X)"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 18,
"source": [
"print('default xgboost accuracy', '=', 1 - sklearn_metric_loss_score('accuracy', y_pred_xgb, y_test))\n",
"print('default lgbm accuracy', '=', 1 - sklearn_metric_loss_score('accuracy', y_pred_lgbm, y_test))\n",
"print('flaml accuracy', '=', 1 - sklearn_metric_loss_score('accuracy', y_pred, y_test))"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"default xgboost accuracy = 0.6676060098186078\n",
"default lgbm accuracy = 0.6602346380315323\n",
"flaml accuracy = 0.6729231864497278\n"
]
}
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"## 4. Customized Learner"
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "markdown",
"source": [
"Some experienced automl users may have a preferred model to tune or may already have a reasonably by-hand-tuned model before launching the automl experiment. They need to select optimal configurations for the customized model mixed with standard built-in learners. \n",
"\n",
"FLAML can easily incorporate customized/new learners (preferably with sklearn API) provided by users in a real-time manner, as demonstrated below."
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Example of Regularized Greedy Forest\n",
"\n",
"[Regularized Greedy Forest](https://arxiv.org/abs/1109.0887) (RGF) is a machine learning method currently not included in FLAML. The RGF has many tuning parameters, the most critical of which are: `[max_leaf, n_iter, n_tree_search, opt_interval, min_samples_leaf]`. To run a customized/new learner, the user needs to provide the following information:\n",
"* an implementation of the customized/new learner\n",
"* a list of hyperparameter names and types\n",
"* rough ranges of hyperparameters (i.e., upper/lower bounds)\n",
"* choose initial value corresponding to low cost for cost-related hyperparameters (e.g., initial value for max_leaf and n_iter should be small)\n",
"\n",
"In this example, the above information for RGF is wrapped in a python class called *MyRegularizedGreedyForest* that exposes the hyperparameters."
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 19,
"source": [
"''' SKLearnEstimator is the super class for a sklearn learner '''\n",
"from flaml.model import SKLearnEstimator\n",
"from flaml import tune\n",
"from rgf.sklearn import RGFClassifier, RGFRegressor\n",
"\n",
"\n",
"class MyRegularizedGreedyForest(SKLearnEstimator):\n",
"\n",
"\n",
" def __init__(self, task='binary:logistic', n_jobs=1, **params):\n",
" '''Constructor\n",
" \n",
" Args:\n",
" task: A string of the task type, one of\n",
" 'binary:logistic', 'multi:softmax', 'regression'\n",
" n_jobs: An integer of the number of parallel threads\n",
" params: A dictionary of the hyperparameter names and values\n",
" '''\n",
"\n",
" super().__init__(task, **params)\n",
"\n",
" '''task=regression for RGFRegressor; \n",
" binary:logistic and multiclass:softmax for RGFClassifier'''\n",
" if 'regression' in task:\n",
" self.estimator_class = RGFRegressor\n",
" else:\n",
" self.estimator_class = RGFClassifier\n",
"\n",
" # convert to int for integer hyperparameters\n",
" self.params = {\n",
" \"n_jobs\": n_jobs,\n",
" 'max_leaf': int(params['max_leaf']),\n",
" 'n_iter': int(params['n_iter']),\n",
" 'n_tree_search': int(params['n_tree_search']),\n",
" 'opt_interval': int(params['opt_interval']),\n",
" 'learning_rate': params['learning_rate'],\n",
" 'min_samples_leaf': int(params['min_samples_leaf'])\n",
" } \n",
"\n",
" @classmethod\n",
" def search_space(cls, data_size, task):\n",
" '''[required method] search space\n",
"\n",
" Returns:\n",
" A dictionary of the search space. \n",
" Each key is the name of a hyperparameter, and value is a dict with\n",
" its domain and init_value (optional), cat_hp_cost (optional) \n",
" e.g., \n",
" {'domain': tune.randint(lower=1, upper=10), 'init_value': 1}\n",
" '''\n",
" space = { \n",
" 'max_leaf': {'domain': tune.lograndint(lower=4, upper=data_size), 'init_value': 4, 'low_cost_init_value': 4},\n",
" 'n_iter': {'domain': tune.lograndint(lower=1, upper=data_size), 'init_value': 1, 'low_cost_init_value': 1},\n",
" 'n_tree_search': {'domain': tune.lograndint(lower=1, upper=32768), 'init_value': 1, 'low_cost_init_value': 1},\n",
" 'opt_interval': {'domain': tune.lograndint(lower=1, upper=10000), 'init_value': 100},\n",
" 'learning_rate': {'domain': tune.loguniform(lower=0.01, upper=20.0)},\n",
" 'min_samples_leaf': {'domain': tune.lograndint(lower=1, upper=20), 'init_value': 20},\n",
" }\n",
" return space\n",
"\n",
" @classmethod\n",
" def size(cls, config):\n",
" '''[optional method] memory size of the estimator in bytes\n",
" \n",
" Args:\n",
" config - the dict of the hyperparameter config\n",
"\n",
" Returns:\n",
" A float of the memory size required by the estimator to train the\n",
" given config\n",
" '''\n",
" max_leaves = int(round(config['max_leaf']))\n",
" n_estimators = int(round(config['n_iter']))\n",
" return (max_leaves * 3 + (max_leaves - 1) * 4 + 1.0) * n_estimators * 8\n",
"\n",
" @classmethod\n",
" def cost_relative2lgbm(cls):\n",
" '''[optional method] relative cost compared to lightgbm\n",
" '''\n",
" return 1.0\n"
],
"outputs": [],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Add Customized Learner and Run FLAML AutoML\n",
"\n",
"After adding RGF into the list of learners, we run automl by tuning hyperpameters of RGF as well as the default learners. "
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 20,
"source": [
"automl = AutoML()\n",
"automl.add_learner(learner_name='RGF', learner_class=MyRegularizedGreedyForest)"
],
"outputs": [],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 21,
"source": [
"settings = {\n",
" \"time_budget\": 10, # total running time in seconds\n",
" \"metric\": 'accuracy', \n",
" \"estimator_list\": ['RGF', 'lgbm', 'rf', 'xgboost'], # list of ML learners\n",
" \"task\": 'classification', # task type \n",
" \"log_file_name\": 'airlines_experiment_custom_learner.log', # flaml log file \n",
" \"log_training_metric\": True, # whether to log training metric\n",
"}\n",
"\n",
"automl.fit(X_train = X_train, y_train = y_train, **settings)"
],
"outputs": [],
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
}
},
{
"cell_type": "markdown",
"source": [
"## 5. Customized Metric\n",
"\n",
"It's also easy to customize the optimization metric. As an example, we demonstrate with a custom metric function which combines training loss and test loss as the final loss to minimize."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 22,
"source": [
"def custom_metric(X_test, y_test, estimator, labels, X_train, y_train,\n",
" weight_test=None, weight_train=None, config=None,\n",
" groups_test=None, groups_train=None):\n",
" from sklearn.metrics import log_loss\n",
" import time\n",
" start = time.time()\n",
" y_pred = estimator.predict_proba(X_test)\n",
" pred_time = (time.time() - start) / len(X_test)\n",
" test_loss = log_loss(y_test, y_pred, labels=labels,\n",
" sample_weight=weight_test)\n",
" y_pred = estimator.predict_proba(X_train)\n",
" train_loss = log_loss(y_train, y_pred, labels=labels,\n",
" sample_weight=weight_train)\n",
" alpha = 0.5\n",
" return test_loss * (1 + alpha) - alpha * train_loss, {\n",
" \"test_loss\": test_loss, \"train_loss\": train_loss, \"pred_time\": pred_time\n",
" }\n",
" # two elements are returned:\n",
" # the first element is the metric to minimize as a float number,\n",
" # the second element is a dictionary of the metrics to log"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"We can then pass this custom metric function to automl's `fit` method."
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 23,
"source": [
"automl = AutoML()\n",
"settings = {\n",
" \"time_budget\": 10, # total running time in seconds\n",
" \"metric\": custom_metric, # pass the custom metric funtion here\n",
" \"task\": 'classification', # task type\n",
" \"log_file_name\": 'airlines_experiment_custom_metric.log', # flaml log file\n",
"}\n",
"\n",
"automl.fit(X_train = X_train, y_train = y_train, **settings)"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"[flaml.automl: 08-31 00:58:32] {1279} INFO - Evaluation method: holdout\n",
"[flaml.automl: 08-31 00:58:32] {1312} INFO - Minimizing error metric: customized metric\n",
"[flaml.automl: 08-31 00:58:32] {1338} INFO - List of ML learners in AutoML Run: ['lgbm', 'rf', 'catboost', 'xgboost', 'extra_tree', 'lrl1']\n",
"[flaml.automl: 08-31 00:58:32] {1532} INFO - iteration 0, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:32] {1689} INFO - at 1.3s,\tbest lgbm's error=0.6646,\tbest lgbm's error=0.6646\n",
"[flaml.automl: 08-31 00:58:32] {1532} INFO - iteration 1, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:33] {1689} INFO - at 1.4s,\tbest lgbm's error=0.6646,\tbest lgbm's error=0.6646\n",
"[flaml.automl: 08-31 00:58:33] {1532} INFO - iteration 2, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:33] {1689} INFO - at 1.6s,\tbest lgbm's error=0.6493,\tbest lgbm's error=0.6493\n",
"[flaml.automl: 08-31 00:58:33] {1532} INFO - iteration 3, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:33] {1689} INFO - at 1.8s,\tbest xgboost's error=0.6672,\tbest lgbm's error=0.6493\n",
"[flaml.automl: 08-31 00:58:33] {1532} INFO - iteration 4, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:33] {1689} INFO - at 1.9s,\tbest lgbm's error=0.6419,\tbest lgbm's error=0.6419\n",
"[flaml.automl: 08-31 00:58:33] {1532} INFO - iteration 5, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:33] {1689} INFO - at 2.1s,\tbest lgbm's error=0.6419,\tbest lgbm's error=0.6419\n",
"[flaml.automl: 08-31 00:58:33] {1532} INFO - iteration 6, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:33] {1689} INFO - at 2.3s,\tbest lgbm's error=0.6419,\tbest lgbm's error=0.6419\n",
"[flaml.automl: 08-31 00:58:33] {1532} INFO - iteration 7, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:34] {1689} INFO - at 2.5s,\tbest lgbm's error=0.6375,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:34] {1532} INFO - iteration 8, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:34] {1689} INFO - at 2.6s,\tbest lgbm's error=0.6375,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:34] {1532} INFO - iteration 9, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:34] {1689} INFO - at 2.8s,\tbest xgboost's error=0.6672,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:34] {1532} INFO - iteration 10, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:34] {1689} INFO - at 2.9s,\tbest xgboost's error=0.6500,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:34] {1532} INFO - iteration 11, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:34] {1689} INFO - at 3.1s,\tbest extra_tree's error=0.6607,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:34] {1532} INFO - iteration 12, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:34] {1689} INFO - at 3.4s,\tbest extra_tree's error=0.6480,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:34] {1532} INFO - iteration 13, current learner rf\n",
"[flaml.automl: 08-31 00:58:35] {1689} INFO - at 3.6s,\tbest rf's error=0.6483,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:35] {1532} INFO - iteration 14, current learner rf\n",
"[flaml.automl: 08-31 00:58:35] {1689} INFO - at 3.8s,\tbest rf's error=0.6430,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:35] {1532} INFO - iteration 15, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:35] {1689} INFO - at 4.0s,\tbest extra_tree's error=0.6480,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:35] {1532} INFO - iteration 16, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:35] {1689} INFO - at 4.2s,\tbest lgbm's error=0.6375,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:35] {1532} INFO - iteration 17, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:36] {1689} INFO - at 4.4s,\tbest extra_tree's error=0.6480,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:36] {1532} INFO - iteration 18, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:36] {1689} INFO - at 4.5s,\tbest lgbm's error=0.6375,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:36] {1532} INFO - iteration 19, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:36] {1689} INFO - at 4.7s,\tbest xgboost's error=0.6413,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:36] {1532} INFO - iteration 20, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:36] {1689} INFO - at 4.9s,\tbest xgboost's error=0.6413,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:36] {1532} INFO - iteration 21, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:36] {1689} INFO - at 5.1s,\tbest xgboost's error=0.6413,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:36] {1532} INFO - iteration 22, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:36] {1689} INFO - at 5.2s,\tbest lgbm's error=0.6375,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:36] {1532} INFO - iteration 23, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:37] {1689} INFO - at 5.4s,\tbest lgbm's error=0.6375,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:37] {1532} INFO - iteration 24, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:37] {1689} INFO - at 5.6s,\tbest xgboost's error=0.6413,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:37] {1532} INFO - iteration 25, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:37] {1689} INFO - at 5.8s,\tbest extra_tree's error=0.6428,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:37] {1532} INFO - iteration 26, current learner rf\n",
"[flaml.automl: 08-31 00:58:37] {1689} INFO - at 6.1s,\tbest rf's error=0.6430,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:37] {1532} INFO - iteration 27, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:37] {1689} INFO - at 6.3s,\tbest xgboost's error=0.6413,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:37] {1532} INFO - iteration 28, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:38] {1689} INFO - at 6.5s,\tbest extra_tree's error=0.6428,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:38] {1532} INFO - iteration 29, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:38] {1689} INFO - at 6.8s,\tbest extra_tree's error=0.6428,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:38] {1532} INFO - iteration 30, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:38] {1689} INFO - at 7.1s,\tbest lgbm's error=0.6375,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:38] {1532} INFO - iteration 31, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:39] {1689} INFO - at 7.4s,\tbest lgbm's error=0.6375,\tbest lgbm's error=0.6375\n",
"[flaml.automl: 08-31 00:58:39] {1532} INFO - iteration 32, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:39] {1689} INFO - at 7.8s,\tbest lgbm's error=0.6366,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:39] {1532} INFO - iteration 33, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:39] {1689} INFO - at 7.9s,\tbest extra_tree's error=0.6428,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:39] {1532} INFO - iteration 34, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:39] {1689} INFO - at 8.3s,\tbest lgbm's error=0.6366,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:39] {1532} INFO - iteration 35, current learner rf\n",
"[flaml.automl: 08-31 00:58:40] {1689} INFO - at 8.4s,\tbest rf's error=0.6430,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:40] {1532} INFO - iteration 36, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:40] {1689} INFO - at 8.6s,\tbest extra_tree's error=0.6428,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:40] {1532} INFO - iteration 37, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:40] {1689} INFO - at 9.0s,\tbest xgboost's error=0.6393,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:40] {1532} INFO - iteration 38, current learner xgboost\n",
"[flaml.automl: 08-31 00:58:40] {1689} INFO - at 9.2s,\tbest xgboost's error=0.6393,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:40] {1532} INFO - iteration 39, current learner lgbm\n",
"[flaml.automl: 08-31 00:58:41] {1689} INFO - at 9.5s,\tbest lgbm's error=0.6366,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:41] {1532} INFO - iteration 40, current learner rf\n",
"[flaml.automl: 08-31 00:58:41] {1689} INFO - at 9.6s,\tbest rf's error=0.6430,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:41] {1532} INFO - iteration 41, current learner extra_tree\n",
"[flaml.automl: 08-31 00:58:41] {1689} INFO - at 9.8s,\tbest extra_tree's error=0.6428,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:41] {1532} INFO - iteration 42, current learner catboost\n",
"[flaml.automl: 08-31 00:58:41] {1689} INFO - at 10.0s,\tbest catboost's error=0.6846,\tbest lgbm's error=0.6366\n",
"[flaml.automl: 08-31 00:58:41] {1766} INFO - selected model: LGBMClassifier(colsample_bytree=0.9318698026248824,\n",
" learning_rate=0.3731008541260285, max_bin=256,\n",
" min_child_samples=18, n_estimators=4, num_leaves=19,\n",
" objective='binary', reg_alpha=0.04956056407320672,\n",
" reg_lambda=3.319594476328555, verbose=-1)\n",
"[flaml.automl: 08-31 00:58:41] {1820} INFO - not retraining because the time budget is too small.\n",
"[flaml.automl: 08-31 00:58:41] {1364} INFO - fit succeeded\n",
"[flaml.automl: 08-31 00:58:41] {1365} INFO - Time taken to find the best model: 7.795238971710205\n",
"[flaml.automl: 08-31 00:58:41] {1370} WARNING - Time taken to find the best model is 78% of the provided time budget and not all estimators' hyperparameter search converged. Consider increasing the time budget.\n"
]
}
],
"metadata": {}
}
],
"metadata": {
"interpreter": {
"hash": "0cfea3304185a9579d09e0953576b57c8581e46e6ebc6dfeb681bc5a511f7544"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.0 64-bit ('blend': conda)"
},
"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.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}