autogen/notebook/flaml_automl.ipynb

1043 lines
103 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\": 300, # total running time in seconds\n",
" \"metric\": 'accuracy', # primary metrics can be chosen from: ['accuracy','roc_auc','roc_auc_ovr','roc_auc_ovo','f1','log_loss','mae','mse','r2']\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-13 17:55:42] {1121} INFO - Evaluation method: holdout\n",
"[flaml.automl: 08-13 17:55:42] {618} INFO - Using StratifiedKFold\n",
"[flaml.automl: 08-13 17:55:42] {1142} INFO - Minimizing error metric: 1-accuracy\n",
"[flaml.automl: 08-13 17:55:42] {1162} INFO - List of ML learners in AutoML Run: ['lgbm', 'rf', 'catboost', 'xgboost', 'extra_tree', 'lrl1']\n",
"[flaml.automl: 08-13 17:55:42] {1252} INFO - iteration 0, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:43] {1405} INFO - at 1.9s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 17:55:43] {1252} INFO - iteration 1, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:43] {1405} INFO - at 1.9s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 17:55:43] {1252} INFO - iteration 2, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:43] {1405} INFO - at 2.0s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 17:55:43] {1252} INFO - iteration 3, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:43] {1405} INFO - at 2.1s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 17:55:43] {1252} INFO - iteration 4, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:43] {1405} INFO - at 2.2s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 17:55:43] {1252} INFO - iteration 5, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:43] {1405} INFO - at 2.2s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 17:55:43] {1252} INFO - iteration 6, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:44] {1405} INFO - at 2.3s,\tbest lgbm's error=0.3742,\tbest lgbm's error=0.3742\n",
"[flaml.automl: 08-13 17:55:44] {1252} INFO - iteration 7, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:44] {1405} INFO - at 2.4s,\tbest lgbm's error=0.3613,\tbest lgbm's error=0.3613\n",
"[flaml.automl: 08-13 17:55:44] {1252} INFO - iteration 8, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:44] {1405} INFO - at 2.5s,\tbest lgbm's error=0.3613,\tbest lgbm's error=0.3613\n",
"[flaml.automl: 08-13 17:55:44] {1252} INFO - iteration 9, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:44] {1405} INFO - at 2.7s,\tbest lgbm's error=0.3613,\tbest lgbm's error=0.3613\n",
"[flaml.automl: 08-13 17:55:44] {1252} INFO - iteration 10, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:44] {1405} INFO - at 2.8s,\tbest lgbm's error=0.3613,\tbest lgbm's error=0.3613\n",
"[flaml.automl: 08-13 17:55:44] {1252} INFO - iteration 11, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:44] {1405} INFO - at 2.9s,\tbest lgbm's error=0.3613,\tbest lgbm's error=0.3613\n",
"[flaml.automl: 08-13 17:55:44] {1252} INFO - iteration 12, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:44] {1405} INFO - at 3.1s,\tbest lgbm's error=0.3573,\tbest lgbm's error=0.3573\n",
"[flaml.automl: 08-13 17:55:44] {1252} INFO - iteration 13, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:44] {1405} INFO - at 3.2s,\tbest lgbm's error=0.3573,\tbest lgbm's error=0.3573\n",
"[flaml.automl: 08-13 17:55:44] {1252} INFO - iteration 14, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:45] {1405} INFO - at 3.5s,\tbest lgbm's error=0.3570,\tbest lgbm's error=0.3570\n",
"[flaml.automl: 08-13 17:55:45] {1252} INFO - iteration 15, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:45] {1405} INFO - at 3.8s,\tbest lgbm's error=0.3570,\tbest lgbm's error=0.3570\n",
"[flaml.automl: 08-13 17:55:45] {1252} INFO - iteration 16, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:45] {1405} INFO - at 4.1s,\tbest lgbm's error=0.3510,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:45] {1252} INFO - iteration 17, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:46] {1405} INFO - at 4.3s,\tbest lgbm's error=0.3510,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:46] {1252} INFO - iteration 18, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:46] {1405} INFO - at 5.0s,\tbest lgbm's error=0.3510,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:46] {1252} INFO - iteration 19, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:46] {1405} INFO - at 5.2s,\tbest lgbm's error=0.3510,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:46] {1252} INFO - iteration 20, current learner xgboost\n",
"[flaml.automl: 08-13 17:55:47] {1405} INFO - at 5.4s,\tbest xgboost's error=0.3787,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:47] {1252} INFO - iteration 21, current learner xgboost\n",
"[flaml.automl: 08-13 17:55:47] {1405} INFO - at 5.5s,\tbest xgboost's error=0.3769,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:47] {1252} INFO - iteration 22, current learner xgboost\n",
"[flaml.automl: 08-13 17:55:47] {1405} INFO - at 5.7s,\tbest xgboost's error=0.3769,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:47] {1252} INFO - iteration 23, current learner xgboost\n",
"[flaml.automl: 08-13 17:55:47] {1405} INFO - at 5.8s,\tbest xgboost's error=0.3769,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:47] {1252} INFO - iteration 24, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:48] {1405} INFO - at 6.6s,\tbest lgbm's error=0.3510,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:48] {1252} INFO - iteration 25, current learner extra_tree\n",
"[flaml.automl: 08-13 17:55:48] {1405} INFO - at 6.8s,\tbest extra_tree's error=0.3773,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:48] {1252} INFO - iteration 26, current learner extra_tree\n",
"[flaml.automl: 08-13 17:55:48] {1405} INFO - at 7.0s,\tbest extra_tree's error=0.3760,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:48] {1252} INFO - iteration 27, current learner rf\n",
"[flaml.automl: 08-13 17:55:48] {1405} INFO - at 7.2s,\tbest rf's error=0.3787,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:48] {1252} INFO - iteration 28, current learner rf\n",
"[flaml.automl: 08-13 17:55:49] {1405} INFO - at 7.4s,\tbest rf's error=0.3709,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:49] {1252} INFO - iteration 29, current learner xgboost\n",
"[flaml.automl: 08-13 17:55:49] {1405} INFO - at 7.5s,\tbest xgboost's error=0.3765,\tbest lgbm's error=0.3510\n",
"[flaml.automl: 08-13 17:55:49] {1252} INFO - iteration 30, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:51] {1405} INFO - at 9.5s,\tbest lgbm's error=0.3465,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:55:51] {1252} INFO - iteration 31, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:53] {1405} INFO - at 11.4s,\tbest lgbm's error=0.3465,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:55:53] {1252} INFO - iteration 32, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:55] {1405} INFO - at 13.8s,\tbest lgbm's error=0.3465,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:55:55] {1252} INFO - iteration 33, current learner rf\n",
"[flaml.automl: 08-13 17:55:55] {1405} INFO - at 14.1s,\tbest rf's error=0.3709,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:55:55] {1252} INFO - iteration 34, current learner lgbm\n",
"[flaml.automl: 08-13 17:55:57] {1405} INFO - at 15.4s,\tbest lgbm's error=0.3465,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:55:57] {1252} INFO - iteration 35, current learner rf\n",
"[flaml.automl: 08-13 17:55:57] {1405} INFO - at 15.7s,\tbest rf's error=0.3709,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:55:57] {1252} INFO - iteration 36, current learner extra_tree\n",
"[flaml.automl: 08-13 17:55:57] {1405} INFO - at 15.8s,\tbest extra_tree's error=0.3760,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:55:57] {1252} INFO - iteration 37, current learner rf\n",
"[flaml.automl: 08-13 17:55:57] {1405} INFO - at 16.0s,\tbest rf's error=0.3654,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:55:57] {1252} INFO - iteration 38, current learner rf\n",
"[flaml.automl: 08-13 17:55:57] {1405} INFO - at 16.3s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:55:57] {1252} INFO - iteration 39, current learner lgbm\n",
"[flaml.automl: 08-13 17:56:02] {1405} INFO - at 20.5s,\tbest lgbm's error=0.3465,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:02] {1252} INFO - iteration 40, current learner rf\n",
"[flaml.automl: 08-13 17:56:02] {1405} INFO - at 20.8s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:02] {1252} INFO - iteration 41, current learner extra_tree\n",
"[flaml.automl: 08-13 17:56:02] {1405} INFO - at 21.0s,\tbest extra_tree's error=0.3760,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:02] {1252} INFO - iteration 42, current learner lgbm\n",
"[flaml.automl: 08-13 17:56:03] {1405} INFO - at 21.9s,\tbest lgbm's error=0.3465,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:03] {1252} INFO - iteration 43, current learner rf\n",
"[flaml.automl: 08-13 17:56:04] {1405} INFO - at 22.3s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:04] {1252} INFO - iteration 44, current learner catboost\n",
"[flaml.automl: 08-13 17:56:05] {1405} INFO - at 23.6s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:05] {1252} INFO - iteration 45, current learner extra_tree\n",
"[flaml.automl: 08-13 17:56:05] {1405} INFO - at 23.7s,\tbest extra_tree's error=0.3709,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:05] {1252} INFO - iteration 46, current learner catboost\n",
"[flaml.automl: 08-13 17:56:06] {1405} INFO - at 24.5s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:06] {1252} INFO - iteration 47, current learner catboost\n",
"[flaml.automl: 08-13 17:56:06] {1405} INFO - at 24.9s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:06] {1252} INFO - iteration 48, current learner catboost\n",
"[flaml.automl: 08-13 17:56:07] {1405} INFO - at 25.6s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3465\n",
"[flaml.automl: 08-13 17:56:07] {1252} INFO - iteration 49, current learner lgbm\n",
"[flaml.automl: 08-13 17:56:13] {1405} INFO - at 31.4s,\tbest lgbm's error=0.3335,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:13] {1252} INFO - iteration 50, current learner catboost\n",
"[flaml.automl: 08-13 17:56:13] {1405} INFO - at 31.8s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:13] {1252} INFO - iteration 51, current learner extra_tree\n",
"[flaml.automl: 08-13 17:56:13] {1405} INFO - at 32.1s,\tbest extra_tree's error=0.3709,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:13] {1252} INFO - iteration 52, current learner catboost\n",
"[flaml.automl: 08-13 17:56:14] {1405} INFO - at 32.6s,\tbest catboost's error=0.3602,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:14] {1252} INFO - iteration 53, current learner catboost\n",
"[flaml.automl: 08-13 17:56:18] {1405} INFO - at 36.3s,\tbest catboost's error=0.3493,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:18] {1252} INFO - iteration 54, current learner lgbm\n",
"[flaml.automl: 08-13 17:56:22] {1405} INFO - at 40.9s,\tbest lgbm's error=0.3335,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:22] {1252} INFO - iteration 55, current learner lgbm\n",
"[flaml.automl: 08-13 17:56:31] {1405} INFO - at 50.0s,\tbest lgbm's error=0.3335,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:31] {1252} INFO - iteration 56, current learner extra_tree\n",
"[flaml.automl: 08-13 17:56:31] {1405} INFO - at 50.2s,\tbest extra_tree's error=0.3664,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:31] {1252} INFO - iteration 57, current learner rf\n",
"[flaml.automl: 08-13 17:56:32] {1405} INFO - at 50.4s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:32] {1252} INFO - iteration 58, current learner rf\n",
"[flaml.automl: 08-13 17:56:32] {1405} INFO - at 50.7s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:32] {1252} INFO - iteration 59, current learner xgboost\n",
"[flaml.automl: 08-13 17:56:32] {1405} INFO - at 50.8s,\tbest xgboost's error=0.3765,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:32] {1252} INFO - iteration 60, current learner lgbm\n",
"[flaml.automl: 08-13 17:56:34] {1405} INFO - at 52.6s,\tbest lgbm's error=0.3335,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:34] {1252} INFO - iteration 61, current learner extra_tree\n",
"[flaml.automl: 08-13 17:56:34] {1405} INFO - at 52.9s,\tbest extra_tree's error=0.3643,\tbest lgbm's error=0.3335\n",
"[flaml.automl: 08-13 17:56:34] {1252} INFO - iteration 62, current learner lgbm\n",
"[flaml.automl: 08-13 17:57:04] {1405} INFO - at 82.4s,\tbest lgbm's error=0.3277,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:57:04] {1252} INFO - iteration 63, current learner extra_tree\n",
"[flaml.automl: 08-13 17:57:04] {1405} INFO - at 82.6s,\tbest extra_tree's error=0.3643,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:57:04] {1252} INFO - iteration 64, current learner extra_tree\n",
"[flaml.automl: 08-13 17:57:04] {1405} INFO - at 82.9s,\tbest extra_tree's error=0.3643,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:57:04] {1252} INFO - iteration 65, current learner lgbm\n",
"[flaml.automl: 08-13 17:57:35] {1405} INFO - at 113.3s,\tbest lgbm's error=0.3277,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:57:35] {1252} INFO - iteration 66, current learner catboost\n",
"[flaml.automl: 08-13 17:57:36] {1405} INFO - at 114.6s,\tbest catboost's error=0.3493,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:57:36] {1252} INFO - iteration 67, current learner extra_tree\n",
"[flaml.automl: 08-13 17:57:36] {1405} INFO - at 114.9s,\tbest extra_tree's error=0.3643,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:57:36] {1252} INFO - iteration 68, current learner catboost\n",
"[flaml.automl: 08-13 17:57:38] {1405} INFO - at 117.1s,\tbest catboost's error=0.3493,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:57:38] {1252} INFO - iteration 69, current learner lgbm\n",
"[flaml.automl: 08-13 17:58:18] {1405} INFO - at 156.9s,\tbest lgbm's error=0.3277,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:18] {1252} INFO - iteration 70, current learner catboost\n",
"[flaml.automl: 08-13 17:58:20] {1405} INFO - at 159.2s,\tbest catboost's error=0.3493,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:20] {1252} INFO - iteration 71, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:21] {1405} INFO - at 159.6s,\tbest xgboost's error=0.3667,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:21] {1252} INFO - iteration 72, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:21] {1405} INFO - at 159.8s,\tbest xgboost's error=0.3667,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:21] {1252} INFO - iteration 73, current learner extra_tree\n",
"[flaml.automl: 08-13 17:58:21] {1405} INFO - at 160.1s,\tbest extra_tree's error=0.3643,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:21] {1252} INFO - iteration 74, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:22] {1405} INFO - at 160.4s,\tbest xgboost's error=0.3667,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:22] {1252} INFO - iteration 75, current learner extra_tree\n",
"[flaml.automl: 08-13 17:58:22] {1405} INFO - at 160.8s,\tbest extra_tree's error=0.3636,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:22] {1252} INFO - iteration 76, current learner rf\n",
"[flaml.automl: 08-13 17:58:23] {1405} INFO - at 161.5s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:23] {1252} INFO - iteration 77, current learner rf\n",
"[flaml.automl: 08-13 17:58:23] {1405} INFO - at 162.0s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:23] {1252} INFO - iteration 78, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:24] {1405} INFO - at 162.5s,\tbest xgboost's error=0.3625,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:24] {1252} INFO - iteration 79, current learner catboost\n",
"[flaml.automl: 08-13 17:58:26] {1405} INFO - at 164.4s,\tbest catboost's error=0.3493,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:26] {1252} INFO - iteration 80, current learner rf\n",
"[flaml.automl: 08-13 17:58:26] {1405} INFO - at 165.2s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:26] {1252} INFO - iteration 81, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:27] {1405} INFO - at 165.5s,\tbest xgboost's error=0.3625,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:27] {1252} INFO - iteration 82, current learner catboost\n",
"[flaml.automl: 08-13 17:58:28] {1405} INFO - at 166.6s,\tbest catboost's error=0.3493,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:28] {1252} INFO - iteration 83, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:28] {1405} INFO - at 166.9s,\tbest xgboost's error=0.3625,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:28] {1252} INFO - iteration 84, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:28] {1405} INFO - at 167.3s,\tbest xgboost's error=0.3611,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:28] {1252} INFO - iteration 85, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:29] {1405} INFO - at 167.6s,\tbest xgboost's error=0.3611,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:29] {1252} INFO - iteration 86, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:29] {1405} INFO - at 167.9s,\tbest xgboost's error=0.3611,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:29] {1252} INFO - iteration 87, current learner xgboost\n",
"[flaml.automl: 08-13 17:58:30] {1405} INFO - at 168.4s,\tbest xgboost's error=0.3611,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:30] {1252} INFO - iteration 88, current learner rf\n",
"[flaml.automl: 08-13 17:58:30] {1405} INFO - at 168.9s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:30] {1252} INFO - iteration 89, current learner rf\n",
"[flaml.automl: 08-13 17:58:31] {1405} INFO - at 169.7s,\tbest rf's error=0.3634,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:31] {1252} INFO - iteration 90, current learner extra_tree\n",
"[flaml.automl: 08-13 17:58:31] {1405} INFO - at 170.0s,\tbest extra_tree's error=0.3636,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:31] {1252} INFO - iteration 91, current learner catboost\n",
"[flaml.automl: 08-13 17:58:33] {1405} INFO - at 171.9s,\tbest catboost's error=0.3493,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:33] {1252} INFO - iteration 92, current learner extra_tree\n",
"[flaml.automl: 08-13 17:58:33] {1405} INFO - at 172.3s,\tbest extra_tree's error=0.3636,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:33] {1252} INFO - iteration 93, current learner catboost\n",
"[flaml.automl: 08-13 17:58:35] {1405} INFO - at 174.2s,\tbest catboost's error=0.3493,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:58:35] {1252} INFO - iteration 94, current learner catboost\n",
"[flaml.automl: 08-13 17:59:07] {1405} INFO - at 205.6s,\tbest catboost's error=0.3372,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:59:07] {1252} INFO - iteration 95, current learner lgbm\n",
"[flaml.automl: 08-13 17:59:15] {1405} INFO - at 213.3s,\tbest lgbm's error=0.3277,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:59:15] {1252} INFO - iteration 96, current learner extra_tree\n",
"[flaml.automl: 08-13 17:59:15] {1405} INFO - at 213.9s,\tbest extra_tree's error=0.3636,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 17:59:15] {1252} INFO - iteration 97, current learner catboost\n",
"[flaml.automl: 08-13 18:00:34] {1405} INFO - at 293.1s,\tbest catboost's error=0.3372,\tbest lgbm's error=0.3277\n",
"[flaml.automl: 08-13 18:00:34] {1252} INFO - iteration 98, current learner xgboost\n",
"[flaml.automl: 08-13 18:00:34] {1422} INFO - no enough budget for learner xgboost\n",
"[flaml.automl: 08-13 18:00:34] {1252} 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",
"[flaml.automl: 08-13 18:00:34] {1422} INFO - no enough budget for learner lrl1\n",
"[flaml.automl: 08-13 18:00:34] {1252} INFO - iteration 100, current learner rf\n",
"[flaml.automl: 08-13 18:00:34] {1422} INFO - no enough budget for learner rf\n",
"[flaml.automl: 08-13 18:00:34] {1252} INFO - iteration 101, current learner extra_tree\n",
"[flaml.automl: 08-13 18:00:34] {1422} INFO - no enough budget for learner extra_tree\n",
"[flaml.automl: 08-13 18:00:34] {1461} INFO - selected model: LGBMClassifier(colsample_bytree=0.8371742593966071,\n",
" learning_rate=0.10600185051767465, max_bin=128,\n",
" min_child_samples=44, n_estimators=2687, num_leaves=21,\n",
" objective='binary', reg_alpha=0.0027205185280732864,\n",
" reg_lambda=2.0031491728266557, verbose=-1)\n",
"[flaml.automl: 08-13 18:00:34] {1184} INFO - fit succeeded\n",
"[flaml.automl: 08-13 18:00:34] {1185} INFO - Time taken to find the best model: 82.42118620872498\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': 2687, 'num_leaves': 21, 'min_child_samples': 44, 'learning_rate': 0.10600185051767465, 'subsample': 1.0, 'log_max_bin': 8, 'colsample_bytree': 0.8371742593966071, 'reg_alpha': 0.0027205185280732864, 'reg_lambda': 2.0031491728266557, 'FLAML_sample_size': 364083}\n",
"Best accuracy on validation data: 0.6723\n",
"Training duration of best run: 29.51 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.8371742593966071,\n",
" learning_rate=0.10600185051767465, max_bin=128,\n",
" min_child_samples=44, n_estimators=2687, num_leaves=21,\n",
" objective='binary', reg_alpha=0.0027205185280732864,\n",
" reg_lambda=2.0031491728266557, 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.6721815997508269\n",
"roc_auc = 0.7257907244977934\n",
"log_loss = 0.6031168823610242\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, train_loss_history = \\\n",
" get_output_from_log(filename=settings['log_file_name'], time_budget=300)\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, 'subsample': 1.0, '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, 'subsample': 1.0, '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': 30, 'learning_rate': 0.18850082505120694, 'subsample': 1.0, 'log_max_bin': 10, 'colsample_bytree': 1.0, 'reg_alpha': 0.0009765625, 'reg_lambda': 0.43802345595978204, 'FLAML_sample_size': 10000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 12, 'num_leaves': 4, 'min_child_samples': 30, 'learning_rate': 0.18850082505120694, 'subsample': 1.0, 'log_max_bin': 10, 'colsample_bytree': 1.0, 'reg_alpha': 0.0009765625, 'reg_lambda': 0.43802345595978204, 'FLAML_sample_size': 10000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 10000, 'Current Hyper-parameters': {'n_estimators': 23, 'num_leaves': 4, 'min_child_samples': 39, 'learning_rate': 0.4839966785164543, 'subsample': 1.0, 'log_max_bin': 9, 'colsample_bytree': 0.8499027725496043, 'reg_alpha': 0.0015851927568202393, 'reg_lambda': 1.9570976003429281, 'FLAML_sample_size': 10000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 23, 'num_leaves': 4, 'min_child_samples': 39, 'learning_rate': 0.4839966785164543, 'subsample': 1.0, 'log_max_bin': 9, 'colsample_bytree': 0.8499027725496043, 'reg_alpha': 0.0015851927568202393, 'reg_lambda': 1.9570976003429281, 'FLAML_sample_size': 10000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 40000, 'Current Hyper-parameters': {'n_estimators': 23, 'num_leaves': 4, 'min_child_samples': 39, 'learning_rate': 0.4839966785164543, 'subsample': 1.0, 'log_max_bin': 9, 'colsample_bytree': 0.8499027725496043, 'reg_alpha': 0.0015851927568202393, 'reg_lambda': 1.9570976003429281, 'FLAML_sample_size': 40000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 23, 'num_leaves': 4, 'min_child_samples': 39, 'learning_rate': 0.4839966785164543, 'subsample': 1.0, 'log_max_bin': 9, 'colsample_bytree': 0.8499027725496043, 'reg_alpha': 0.0015851927568202393, 'reg_lambda': 1.9570976003429281, 'FLAML_sample_size': 40000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 40000, 'Current Hyper-parameters': {'n_estimators': 74, 'num_leaves': 4, 'min_child_samples': 41, 'learning_rate': 0.17743258768982648, 'subsample': 1.0, 'log_max_bin': 7, 'colsample_bytree': 0.804982542436943, 'reg_alpha': 0.0009765625, 'reg_lambda': 3.547311998768578, 'FLAML_sample_size': 40000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 74, 'num_leaves': 4, 'min_child_samples': 41, 'learning_rate': 0.17743258768982648, 'subsample': 1.0, 'log_max_bin': 7, 'colsample_bytree': 0.804982542436943, 'reg_alpha': 0.0009765625, 'reg_lambda': 3.547311998768578, 'FLAML_sample_size': 40000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 40000, 'Current Hyper-parameters': {'n_estimators': 135, 'num_leaves': 7, 'min_child_samples': 21, 'learning_rate': 0.29597808772418305, 'subsample': 1.0, 'log_max_bin': 6, 'colsample_bytree': 0.7208090706891741, 'reg_alpha': 0.0017607866203119683, 'reg_lambda': 1.8488863473486183, 'FLAML_sample_size': 40000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 135, 'num_leaves': 7, 'min_child_samples': 21, 'learning_rate': 0.29597808772418305, 'subsample': 1.0, 'log_max_bin': 6, 'colsample_bytree': 0.7208090706891741, 'reg_alpha': 0.0017607866203119683, 'reg_lambda': 1.8488863473486183, 'FLAML_sample_size': 40000}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 364083, 'Current Hyper-parameters': {'n_estimators': 135, 'num_leaves': 7, 'min_child_samples': 21, 'learning_rate': 0.29597808772418305, 'subsample': 1.0, 'log_max_bin': 6, 'colsample_bytree': 0.7208090706891741, 'reg_alpha': 0.0017607866203119683, 'reg_lambda': 1.8488863473486183, 'FLAML_sample_size': 364083}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 135, 'num_leaves': 7, 'min_child_samples': 21, 'learning_rate': 0.29597808772418305, 'subsample': 1.0, 'log_max_bin': 6, 'colsample_bytree': 0.7208090706891741, 'reg_alpha': 0.0017607866203119683, 'reg_lambda': 1.8488863473486183, 'FLAML_sample_size': 364083}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 364083, 'Current Hyper-parameters': {'n_estimators': 529, 'num_leaves': 20, 'min_child_samples': 35, 'learning_rate': 0.22270723105961662, 'subsample': 1.0, 'log_max_bin': 7, 'colsample_bytree': 0.7169048987045028, 'reg_alpha': 0.01040607895773376, 'reg_lambda': 4.44145781234249, 'FLAML_sample_size': 364083}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 529, 'num_leaves': 20, 'min_child_samples': 35, 'learning_rate': 0.22270723105961662, 'subsample': 1.0, 'log_max_bin': 7, 'colsample_bytree': 0.7169048987045028, 'reg_alpha': 0.01040607895773376, 'reg_lambda': 4.44145781234249, 'FLAML_sample_size': 364083}}\n",
"{'Current Learner': 'lgbm', 'Current Sample': 364083, 'Current Hyper-parameters': {'n_estimators': 2687, 'num_leaves': 21, 'min_child_samples': 44, 'learning_rate': 0.10600185051767465, 'subsample': 1.0, 'log_max_bin': 8, 'colsample_bytree': 0.8371742593966071, 'reg_alpha': 0.0027205185280732864, 'reg_lambda': 2.0031491728266557, 'FLAML_sample_size': 364083}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 2687, 'num_leaves': 21, 'min_child_samples': 44, 'learning_rate': 0.10600185051767465, 'subsample': 1.0, 'log_max_bin': 8, 'colsample_bytree': 0.8371742593966071, 'reg_alpha': 0.0027205185280732864, 'reg_lambda': 2.0031491728266557, '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=\"md49891ee92\" style=\"stroke:#1f77b4;\"/>\n </defs>\n <g clip-path=\"url(#pe8209fddcf)\">\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"65.361932\" xlink:href=\"#md49891ee92\" y=\"229.874489\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"67.186194\" xlink:href=\"#md49891ee92\" y=\"215.99929\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"67.578385\" xlink:href=\"#md49891ee92\" y=\"165.188702\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"69.943861\" xlink:href=\"#md49891ee92\" y=\"149.35925\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"71.571288\" xlink:href=\"#md49891ee92\" y=\"147.991273\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"73.931949\" xlink:href=\"#md49891ee92\" y=\"124.44252\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"94.249032\" xlink:href=\"#md49891ee92\" y=\"106.463389\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"176.858812\" xlink:href=\"#md49891ee92\" y=\"55.261951\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"369.725568\" xlink:href=\"#md49891ee92\" 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=\"me003466906\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n </defs>\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"58.362073\" xlink:href=\"#me003466906\" 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 <g transform=\"translate(55.180823 254.356562)scale(0.1 -0.1)\">\n <use xlink:href=\"#DejaVuSans-48\"/>\n </g>\n </g>\n </g>\n <g id=\"xtick_2\">\n <g id=\"line2d_2\">\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"96.139192\" xlink:href=\"#me003466906\" y=\"239.758125\"/>\n </g>\n </g>\n <g id=\"text_2\">\n <!-- 10 -->\n <defs>\n <path d=\"M 12.40625 8.296875 \nL 28.51
"image/png": "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
},
"metadata": {
"needs_background": "light"
}
}
],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "markdown",
"source": [
"## 3. 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": 12,
"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": 13,
"source": [
"automl = AutoML()\n",
"automl.add_learner(learner_name='RGF', learner_class=MyRegularizedGreedyForest)"
],
"outputs": [],
"metadata": {
"slideshow": {
"slide_type": "slide"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"source": [
"settings = {\n",
" \"time_budget\": 60, # 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.log', # flaml log file \n",
" \"log_training_metric\": True, # whether to log training metric\n",
"}\n",
"\n",
"'''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-13 18:00:59] {1121} INFO - Evaluation method: holdout\n",
"[flaml.automl: 08-13 18:01:00] {618} INFO - Using StratifiedKFold\n",
"[flaml.automl: 08-13 18:01:00] {1142} INFO - Minimizing error metric: 1-accuracy\n",
"[flaml.automl: 08-13 18:01:00] {1162} INFO - List of ML learners in AutoML Run: ['RGF', 'lgbm', 'rf', 'xgboost']\n",
"[flaml.automl: 08-13 18:01:00] {1252} INFO - iteration 0, current learner RGF\n",
"/home/dmx/miniconda2/envs/test/lib/python3.8/site-packages/rgf/utils.py:224: UserWarning: Cannot find FastRGF executable files. FastRGF estimators will be unavailable for usage.\n",
" warnings.warn(\"Cannot find FastRGF executable files. \"\n",
"[flaml.automl: 08-13 18:01:06] {1405} INFO - at 8.1s,\tbest RGF's error=0.3787,\tbest RGF's error=0.3787\n",
"[flaml.automl: 08-13 18:01:06] {1252} INFO - iteration 1, current learner RGF\n",
"[flaml.automl: 08-13 18:01:10] {1405} INFO - at 11.4s,\tbest RGF's error=0.3787,\tbest RGF's error=0.3787\n",
"[flaml.automl: 08-13 18:01:10] {1252} INFO - iteration 2, current learner RGF\n",
"[flaml.automl: 08-13 18:01:14] {1405} INFO - at 15.8s,\tbest RGF's error=0.3787,\tbest RGF's error=0.3787\n",
"[flaml.automl: 08-13 18:01:14] {1252} INFO - iteration 3, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:15] {1405} INFO - at 16.2s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 18:01:15] {1252} INFO - iteration 4, current learner RGF\n",
"[flaml.automl: 08-13 18:01:18] {1405} INFO - at 19.8s,\tbest RGF's error=0.3787,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 18:01:18] {1252} INFO - iteration 5, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:18] {1405} INFO - at 20.1s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 18:01:18] {1252} INFO - iteration 6, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:19] {1405} INFO - at 21.2s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 18:01:19] {1252} INFO - iteration 7, current learner RGF\n",
"[flaml.automl: 08-13 18:01:23] {1405} INFO - at 24.6s,\tbest RGF's error=0.3787,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 18:01:23] {1252} INFO - iteration 8, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:24] {1405} INFO - at 25.2s,\tbest lgbm's error=0.3777,\tbest lgbm's error=0.3777\n",
"[flaml.automl: 08-13 18:01:24] {1252} INFO - iteration 9, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:24] {1405} INFO - at 25.4s,\tbest lgbm's error=0.3765,\tbest lgbm's error=0.3765\n",
"[flaml.automl: 08-13 18:01:24] {1252} INFO - iteration 10, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:24] {1405} INFO - at 25.6s,\tbest lgbm's error=0.3765,\tbest lgbm's error=0.3765\n",
"[flaml.automl: 08-13 18:01:24] {1252} INFO - iteration 11, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:24] {1405} INFO - at 25.9s,\tbest lgbm's error=0.3752,\tbest lgbm's error=0.3752\n",
"[flaml.automl: 08-13 18:01:24] {1252} INFO - iteration 12, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:24] {1405} INFO - at 26.1s,\tbest lgbm's error=0.3573,\tbest lgbm's error=0.3573\n",
"[flaml.automl: 08-13 18:01:24] {1252} INFO - iteration 13, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:25] {1405} INFO - at 27.0s,\tbest lgbm's error=0.3573,\tbest lgbm's error=0.3573\n",
"[flaml.automl: 08-13 18:01:25] {1252} INFO - iteration 14, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:26] {1405} INFO - at 27.3s,\tbest lgbm's error=0.3525,\tbest lgbm's error=0.3525\n",
"[flaml.automl: 08-13 18:01:26] {1252} INFO - iteration 15, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:27] {1405} INFO - at 28.5s,\tbest lgbm's error=0.3525,\tbest lgbm's error=0.3525\n",
"[flaml.automl: 08-13 18:01:27] {1252} INFO - iteration 16, current learner RGF\n",
"[flaml.automl: 08-13 18:01:33] {1405} INFO - at 34.6s,\tbest RGF's error=0.3684,\tbest lgbm's error=0.3525\n",
"[flaml.automl: 08-13 18:01:33] {1252} INFO - iteration 17, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:33] {1405} INFO - at 35.0s,\tbest lgbm's error=0.3525,\tbest lgbm's error=0.3525\n",
"[flaml.automl: 08-13 18:01:33] {1252} INFO - iteration 18, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:39] {1405} INFO - at 41.2s,\tbest lgbm's error=0.3501,\tbest lgbm's error=0.3501\n",
"[flaml.automl: 08-13 18:01:39] {1252} INFO - iteration 19, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:43] {1405} INFO - at 45.2s,\tbest lgbm's error=0.3501,\tbest lgbm's error=0.3501\n",
"[flaml.automl: 08-13 18:01:43] {1252} INFO - iteration 20, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:47] {1405} INFO - at 48.9s,\tbest lgbm's error=0.3501,\tbest lgbm's error=0.3501\n",
"[flaml.automl: 08-13 18:01:50] {1437} INFO - retrain lgbm for 2.3s\n",
"[flaml.automl: 08-13 18:01:50] {1252} INFO - iteration 21, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:51] {1405} INFO - at 53.2s,\tbest lgbm's error=0.3501,\tbest lgbm's error=0.3501\n",
"[flaml.automl: 08-13 18:01:51] {1252} INFO - iteration 22, current learner lgbm\n",
"[flaml.automl: 08-13 18:01:55] {1405} INFO - at 56.8s,\tbest lgbm's error=0.3501,\tbest lgbm's error=0.3501\n",
"[flaml.automl: 08-13 18:01:58] {1437} INFO - retrain lgbm for 2.4s\n",
"[flaml.automl: 08-13 18:01:58] {1252} INFO - iteration 23, current learner xgboost\n",
"[flaml.automl: 08-13 18:01:58] {1405} INFO - at 59.4s,\tbest xgboost's error=0.3787,\tbest lgbm's error=0.3501\n",
"[flaml.automl: 08-13 18:01:58] {1437} INFO - retrain xgboost for 0.7s\n",
"[flaml.automl: 08-13 18:01:58] {1461} INFO - selected model: LGBMClassifier(colsample_bytree=0.7967145599266738,\n",
" learning_rate=0.589471433950518, max_bin=256,\n",
" min_child_samples=44, n_estimators=22, num_leaves=12,\n",
" objective='binary', reg_alpha=0.040774029561503077,\n",
" reg_lambda=9.878828628614547, verbose=-1)\n",
"[flaml.automl: 08-13 18:01:58] {1184} INFO - fit succeeded\n",
"[flaml.automl: 08-13 18:01:58] {1185} INFO - Time taken to find the best model: 41.16750192642212\n"
]
}
],
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
}
},
{
"cell_type": "markdown",
"source": [
"## 4. Comparison with alternatives\n",
"\n",
"### FLAML's accuracy"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 15,
"source": [
"print('flaml accuracy', '=', 1 - sklearn_metric_loss_score('accuracy', y_pred, y_test))"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"flaml accuracy = 0.6721815997508269\n"
]
}
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"### Default LightGBM"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 16,
"source": [
"from lightgbm import LGBMClassifier\n",
"lgbm = LGBMClassifier()"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 17,
"source": [
"lgbm.fit(X_train, y_train)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"LGBMClassifier()"
]
},
"metadata": {},
"execution_count": 17
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 18,
"source": [
"y_pred = lgbm.predict(X_test)\n",
"from flaml.ml import sklearn_metric_loss_score\n",
"print('default lgbm accuracy', '=', 1 - sklearn_metric_loss_score('accuracy', y_pred, y_test))"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"default lgbm accuracy = 0.6602346380315323\n"
]
}
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"### Default XGBoost"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 19,
"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": 20,
"source": [
"xgb.fit(X, y_train)"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/home/dmx/miniconda2/envs/test/lib/python3.8/site-packages/xgboost/sklearn.py:1146: UserWarning: The use of label encoder in XGBClassifier is deprecated and will be removed in a future release. To remove this warning, do the following: 1) Pass option use_label_encoder=False when constructing XGBClassifier object; and 2) Encode your labels (y) as integers starting with 0, i.e. 0, 1, 2, ..., [num_class - 1].\n",
" warnings.warn(label_encoder_deprecation_msg, UserWarning)\n"
]
},
{
"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=8, 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": 20
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 21,
"source": [
"X = X_test.copy()\n",
"X[cat_columns] = X[cat_columns].apply(lambda x: x.cat.codes)\n",
"y_pred = xgb.predict(X)\n",
"from flaml.ml import sklearn_metric_loss_score\n",
"print('default xgboost accuracy', '=', 1 - sklearn_metric_loss_score('accuracy', y_pred, y_test))"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"default xgboost accuracy = 0.6676060098186078\n"
]
}
],
"metadata": {}
}
],
"metadata": {
"interpreter": {
"hash": "ea9f131eb1b7663628f6445553ba215a834e2f0b4d18774746f0f47938ce4671"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.0 64-bit ('test': 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
}