autogen/notebook/flaml_automl.ipynb

1015 lines
100 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install flaml[notebook];"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"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"
]
}
],
"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='./')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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']`. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"''' import AutoML class from flaml package '''\n",
"from flaml import AutoML\n",
"automl = AutoML()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"settings = {\n",
" \"time_budget\": 300, # total running time in seconds\n",
" \"metric\": 'accuracy', # primary metrics can be chosen from: ['accuracy','roc_auc','f1','log_loss','mae','mse','r2']\n",
" \"task\": 'classification', # task type \n",
" \"log_file_name\": 'airlines_experiment.log', # flaml log file\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": [
"outputPrepend"
]
},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"ror=0.3777\n",
"[flaml.automl: 04-07 09:18:58] {993} INFO - iteration 2, current learner lgbm\n",
"[flaml.automl: 04-07 09:18:58] {1141} INFO - at 0.9s,\tbest lgbm's error=0.3672,\tbest lgbm's error=0.3672\n",
"[flaml.automl: 04-07 09:18:58] {993} INFO - iteration 3, current learner lgbm\n",
"[flaml.automl: 04-07 09:18:58] {1141} INFO - at 1.0s,\tbest lgbm's error=0.3672,\tbest lgbm's error=0.3672\n",
"[flaml.automl: 04-07 09:18:58] {993} INFO - iteration 4, current learner lgbm\n",
"[flaml.automl: 04-07 09:18:58] {1141} INFO - at 1.1s,\tbest lgbm's error=0.3648,\tbest lgbm's error=0.3648\n",
"[flaml.automl: 04-07 09:18:58] {993} INFO - iteration 5, current learner lgbm\n",
"[flaml.automl: 04-07 09:18:59] {1141} INFO - at 1.3s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:18:59] {993} INFO - iteration 6, current learner lgbm\n",
"[flaml.automl: 04-07 09:18:59] {1141} INFO - at 1.4s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:18:59] {993} INFO - iteration 7, current learner lgbm\n",
"[flaml.automl: 04-07 09:18:59] {1141} INFO - at 1.5s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:18:59] {993} INFO - iteration 8, current learner lgbm\n",
"[flaml.automl: 04-07 09:18:59] {1141} INFO - at 1.6s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:18:59] {993} INFO - iteration 9, current learner lgbm\n",
"[flaml.automl: 04-07 09:18:59] {1141} INFO - at 1.8s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:18:59] {993} INFO - iteration 10, current learner xgboost\n",
"[flaml.automl: 04-07 09:18:59] {1141} INFO - at 1.8s,\tbest xgboost's error=0.3787,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:18:59] {993} INFO - iteration 11, current learner xgboost\n",
"[flaml.automl: 04-07 09:18:59] {1141} INFO - at 1.9s,\tbest xgboost's error=0.3768,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:18:59] {993} INFO - iteration 12, current learner extra_tree\n",
"[flaml.automl: 04-07 09:18:59] {1141} INFO - at 2.1s,\tbest extra_tree's error=0.3985,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:18:59] {993} INFO - iteration 13, current learner extra_tree\n",
"[flaml.automl: 04-07 09:19:00] {1141} INFO - at 2.2s,\tbest extra_tree's error=0.3985,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:19:00] {993} INFO - iteration 14, current learner extra_tree\n",
"[flaml.automl: 04-07 09:19:00] {1141} INFO - at 2.4s,\tbest extra_tree's error=0.3971,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:19:00] {993} INFO - iteration 15, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:00] {1141} INFO - at 2.5s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:19:00] {993} INFO - iteration 16, current learner rf\n",
"[flaml.automl: 04-07 09:19:00] {1141} INFO - at 2.7s,\tbest rf's error=0.4017,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:19:00] {993} INFO - iteration 17, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:00] {1141} INFO - at 2.8s,\tbest xgboost's error=0.3746,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:19:00] {993} INFO - iteration 18, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:00] {1141} INFO - at 3.0s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:19:00] {993} INFO - iteration 19, current learner rf\n",
"[flaml.automl: 04-07 09:19:01] {1141} INFO - at 3.2s,\tbest rf's error=0.4017,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:19:01] {993} INFO - iteration 20, current learner rf\n",
"[flaml.automl: 04-07 09:19:01] {1141} INFO - at 3.4s,\tbest rf's error=0.3993,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:19:01] {993} INFO - iteration 21, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:01] {1141} INFO - at 3.5s,\tbest xgboost's error=0.3746,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:19:01] {993} INFO - iteration 22, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:01] {1141} INFO - at 3.8s,\tbest lgbm's error=0.3555,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:19:01] {993} INFO - iteration 23, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:01] {1141} INFO - at 4.0s,\tbest lgbm's error=0.3555,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:19:01] {993} INFO - iteration 24, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:02] {1141} INFO - at 4.2s,\tbest xgboost's error=0.3627,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:19:02] {993} INFO - iteration 25, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:02] {1141} INFO - at 4.4s,\tbest xgboost's error=0.3627,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:19:02] {993} INFO - iteration 26, current learner rf\n",
"[flaml.automl: 04-07 09:19:02] {1141} INFO - at 4.9s,\tbest rf's error=0.3993,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:19:02] {993} INFO - iteration 27, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:02] {1141} INFO - at 5.1s,\tbest xgboost's error=0.3627,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:19:02] {993} INFO - iteration 28, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:03] {1141} INFO - at 5.3s,\tbest xgboost's error=0.3627,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:19:03] {993} INFO - iteration 29, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:04] {1141} INFO - at 6.2s,\tbest xgboost's error=0.3559,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:19:04] {993} INFO - iteration 30, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:04] {1141} INFO - at 6.8s,\tbest lgbm's error=0.3555,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:19:04] {993} INFO - iteration 31, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:05] {1141} INFO - at 8.1s,\tbest lgbm's error=0.3524,\tbest lgbm's error=0.3524\n",
"[flaml.automl: 04-07 09:19:05] {993} INFO - iteration 32, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:06] {1141} INFO - at 8.9s,\tbest lgbm's error=0.3524,\tbest lgbm's error=0.3524\n",
"[flaml.automl: 04-07 09:19:06] {993} INFO - iteration 33, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:07] {1141} INFO - at 9.6s,\tbest xgboost's error=0.3559,\tbest lgbm's error=0.3524\n",
"[flaml.automl: 04-07 09:19:07] {993} INFO - iteration 34, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:13] {1141} INFO - at 15.8s,\tbest lgbm's error=0.3498,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:19:13] {993} INFO - iteration 35, current learner catboost\n",
"[flaml.automl: 04-07 09:19:16] {1141} INFO - at 18.5s,\tbest catboost's error=0.3644,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:19:16] {993} INFO - iteration 36, current learner catboost\n",
"[flaml.automl: 04-07 09:19:17] {1141} INFO - at 19.3s,\tbest catboost's error=0.3644,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:19:17] {993} INFO - iteration 37, current learner extra_tree\n",
"[flaml.automl: 04-07 09:19:17] {1141} INFO - at 19.7s,\tbest extra_tree's error=0.3971,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:19:17] {993} INFO - iteration 38, current learner catboost\n",
"[flaml.automl: 04-07 09:19:18] {1141} INFO - at 20.6s,\tbest catboost's error=0.3608,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:19:18] {993} INFO - iteration 39, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:18] {1141} INFO - at 21.0s,\tbest xgboost's error=0.3559,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:19:18] {993} INFO - iteration 40, current learner rf\n",
"[flaml.automl: 04-07 09:19:19] {1141} INFO - at 21.7s,\tbest rf's error=0.3936,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:19:19] {993} INFO - iteration 41, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:20] {1141} INFO - at 22.4s,\tbest xgboost's error=0.3559,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:19:20] {993} INFO - iteration 42, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:21] {1141} INFO - at 23.4s,\tbest xgboost's error=0.3559,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:19:21] {993} INFO - iteration 43, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:25] {1141} INFO - at 27.3s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:25] {993} INFO - iteration 44, current learner catboost\n",
"[flaml.automl: 04-07 09:19:25] {1141} INFO - at 27.8s,\tbest catboost's error=0.3608,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:25] {993} INFO - iteration 45, current learner extra_tree\n",
"[flaml.automl: 04-07 09:19:26] {1141} INFO - at 28.1s,\tbest extra_tree's error=0.3923,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:26] {993} INFO - iteration 46, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:26] {1141} INFO - at 28.6s,\tbest xgboost's error=0.3559,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:26] {993} INFO - iteration 47, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:35] {1141} INFO - at 37.6s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:35] {993} INFO - iteration 48, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:35] {1141} INFO - at 38.1s,\tbest xgboost's error=0.3559,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:35] {993} INFO - iteration 49, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:36] {1141} INFO - at 38.6s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:36] {993} INFO - iteration 50, current learner catboost\n",
"[flaml.automl: 04-07 09:19:36] {1141} INFO - at 38.9s,\tbest catboost's error=0.3608,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:36] {993} INFO - iteration 51, current learner extra_tree\n",
"[flaml.automl: 04-07 09:19:36] {1141} INFO - at 39.1s,\tbest extra_tree's error=0.3895,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:36] {993} INFO - iteration 52, current learner rf\n",
"[flaml.automl: 04-07 09:19:37] {1141} INFO - at 39.4s,\tbest rf's error=0.3904,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:37] {993} INFO - iteration 53, current learner rf\n",
"[flaml.automl: 04-07 09:19:37] {1141} INFO - at 39.8s,\tbest rf's error=0.3857,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:37] {993} INFO - iteration 54, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:42] {1141} INFO - at 44.5s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:42] {993} INFO - iteration 55, current learner lgbm\n",
"[flaml.automl: 04-07 09:19:43] {1141} INFO - at 45.1s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:43] {993} INFO - iteration 56, current learner xgboost\n",
"[flaml.automl: 04-07 09:19:46] {1141} INFO - at 48.4s,\tbest xgboost's error=0.3553,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:46] {993} INFO - iteration 57, current learner rf\n",
"[flaml.automl: 04-07 09:19:46] {1141} INFO - at 48.7s,\tbest rf's error=0.3857,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:46] {993} INFO - iteration 58, current learner rf\n",
"[flaml.automl: 04-07 09:19:48] {1141} INFO - at 50.4s,\tbest rf's error=0.3808,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:48] {993} INFO - iteration 59, current learner catboost\n",
"[flaml.automl: 04-07 09:19:49] {1141} INFO - at 51.5s,\tbest catboost's error=0.3608,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:19:49] {993} INFO - iteration 60, current learner lgbm\n",
"[flaml.automl: 04-07 09:20:10] {1141} INFO - at 72.6s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:20:10] {993} INFO - iteration 61, current learner extra_tree\n",
"[flaml.automl: 04-07 09:20:10] {1141} INFO - at 72.9s,\tbest extra_tree's error=0.3877,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:20:10] {993} INFO - iteration 62, current learner lgbm\n",
"[flaml.automl: 04-07 09:20:23] {1141} INFO - at 85.6s,\tbest lgbm's error=0.3428,\tbest lgbm's error=0.3428\n",
"[flaml.automl: 04-07 09:20:23] {993} INFO - iteration 63, current learner xgboost\n",
"[flaml.automl: 04-07 09:20:25] {1141} INFO - at 87.8s,\tbest xgboost's error=0.3553,\tbest lgbm's error=0.3428\n",
"[flaml.automl: 04-07 09:20:25] {993} INFO - iteration 64, current learner extra_tree\n",
"[flaml.automl: 04-07 09:20:25] {1141} INFO - at 88.1s,\tbest extra_tree's error=0.3877,\tbest lgbm's error=0.3428\n",
"[flaml.automl: 04-07 09:20:25] {993} INFO - iteration 65, current learner lgbm\n",
"[flaml.automl: 04-07 09:20:49] {1141} INFO - at 111.4s,\tbest lgbm's error=0.3379,\tbest lgbm's error=0.3379\n",
"[flaml.automl: 04-07 09:20:49] {993} INFO - iteration 66, current learner rf\n",
"[flaml.automl: 04-07 09:20:50] {1141} INFO - at 113.1s,\tbest rf's error=0.3808,\tbest lgbm's error=0.3379\n",
"[flaml.automl: 04-07 09:20:50] {993} INFO - iteration 67, current learner catboost\n",
"[flaml.automl: 04-07 09:20:51] {1141} INFO - at 113.4s,\tbest catboost's error=0.3608,\tbest lgbm's error=0.3379\n",
"[flaml.automl: 04-07 09:20:51] {993} INFO - iteration 68, current learner rf\n",
"[flaml.automl: 04-07 09:20:51] {1141} INFO - at 114.0s,\tbest rf's error=0.3808,\tbest lgbm's error=0.3379\n",
"[flaml.automl: 04-07 09:20:51] {993} INFO - iteration 69, current learner lgbm\n",
"[flaml.automl: 04-07 09:21:01] {1141} INFO - at 123.4s,\tbest lgbm's error=0.3361,\tbest lgbm's error=0.3361\n",
"[flaml.automl: 04-07 09:21:01] {993} INFO - iteration 70, current learner lgbm\n",
"[flaml.automl: 04-07 09:21:25] {1141} INFO - at 148.1s,\tbest lgbm's error=0.3361,\tbest lgbm's error=0.3361\n",
"[flaml.automl: 04-07 09:21:25] {993} INFO - iteration 71, current learner catboost\n",
"[flaml.automl: 04-07 09:21:27] {1141} INFO - at 149.3s,\tbest catboost's error=0.3530,\tbest lgbm's error=0.3361\n",
"[flaml.automl: 04-07 09:21:27] {993} INFO - iteration 72, current learner catboost\n",
"[flaml.automl: 04-07 09:21:28] {1141} INFO - at 150.5s,\tbest catboost's error=0.3530,\tbest lgbm's error=0.3361\n",
"[flaml.automl: 04-07 09:21:28] {993} INFO - iteration 73, current learner extra_tree\n",
"[flaml.automl: 04-07 09:21:29] {1141} INFO - at 152.1s,\tbest extra_tree's error=0.3827,\tbest lgbm's error=0.3361\n",
"[flaml.automl: 04-07 09:21:29] {993} INFO - iteration 74, current learner catboost\n",
"[flaml.automl: 04-07 09:21:32] {1141} INFO - at 154.2s,\tbest catboost's error=0.3530,\tbest lgbm's error=0.3361\n",
"[flaml.automl: 04-07 09:21:32] {993} INFO - iteration 75, current learner extra_tree\n",
"[flaml.automl: 04-07 09:21:34] {1141} INFO - at 156.4s,\tbest extra_tree's error=0.3827,\tbest lgbm's error=0.3361\n",
"[flaml.automl: 04-07 09:21:34] {993} INFO - iteration 76, current learner lgbm\n",
"[flaml.automl: 04-07 09:21:49] {1141} INFO - at 171.2s,\tbest lgbm's error=0.3297,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:21:49] {993} INFO - iteration 77, current learner lrl1\n",
"No init config given to FLOW2. Using random initial config.For cost-frugal search, consider providing init values for cost-related hps via 'init_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: 04-07 09:21:49] {1141} INFO - at 171.5s,\tbest lrl1's error=0.4337,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:21:49] {993} INFO - iteration 78, 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: 04-07 09:21:49] {1141} INFO - at 171.8s,\tbest lrl1's error=0.4337,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:21:49] {993} INFO - iteration 79, 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: 04-07 09:21:50] {1141} INFO - at 172.2s,\tbest lrl1's error=0.4337,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:21:50] {993} INFO - iteration 80, 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: 04-07 09:21:50] {1141} INFO - at 172.5s,\tbest lrl1's error=0.4337,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:21:50] {993} INFO - iteration 81, 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: 04-07 09:21:50] {1141} INFO - at 172.8s,\tbest lrl1's error=0.4337,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:21:50] {993} INFO - iteration 82, 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: 04-07 09:21:51] {1141} INFO - at 174.0s,\tbest lrl1's error=0.4334,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:21:51] {993} INFO - iteration 83, current learner rf\n",
"[flaml.automl: 04-07 09:21:55] {1141} INFO - at 177.7s,\tbest rf's error=0.3808,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:21:55] {993} INFO - iteration 84, current learner catboost\n",
"[flaml.automl: 04-07 09:21:56] {1141} INFO - at 178.6s,\tbest catboost's error=0.3530,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:21:56] {993} INFO - iteration 85, current learner catboost\n",
"[flaml.automl: 04-07 09:22:06] {1141} INFO - at 188.6s,\tbest catboost's error=0.3481,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:22:06] {993} INFO - iteration 86, current learner extra_tree\n",
"[flaml.automl: 04-07 09:22:06] {1141} INFO - at 189.0s,\tbest extra_tree's error=0.3827,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:22:06] {993} INFO - iteration 87, current learner catboost\n",
"[flaml.automl: 04-07 09:22:12] {1141} INFO - at 195.1s,\tbest catboost's error=0.3481,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:22:12] {993} INFO - iteration 88, current learner catboost\n",
"[flaml.automl: 04-07 09:22:34] {1141} INFO - at 216.6s,\tbest catboost's error=0.3481,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:22:34] {993} INFO - iteration 89, current learner lgbm\n",
"[flaml.automl: 04-07 09:22:43] {1141} INFO - at 225.8s,\tbest lgbm's error=0.3297,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:22:43] {993} INFO - iteration 90, current learner lgbm\n",
"[flaml.automl: 04-07 09:22:57] {1141} INFO - at 239.3s,\tbest lgbm's error=0.3297,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:22:57] {993} INFO - iteration 91, current learner extra_tree\n",
"[flaml.automl: 04-07 09:22:59] {1141} INFO - at 242.0s,\tbest extra_tree's error=0.3827,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:22:59] {993} INFO - iteration 92, current learner lgbm\n",
"[flaml.automl: 04-07 09:23:47] {1141} INFO - at 289.9s,\tbest lgbm's error=0.3297,\tbest lgbm's error=0.3297\n",
"[flaml.automl: 04-07 09:23:47] {993} INFO - iteration 93, current learner extra_tree\n",
"[flaml.automl: 04-07 09:23:47] {1149} INFO - no enough budget for learner extra_tree\n",
"[flaml.automl: 04-07 09:23:47] {993} INFO - iteration 94, current learner catboost\n",
"[flaml.automl: 04-07 09:23:47] {1149} INFO - no enough budget for learner catboost\n",
"[flaml.automl: 04-07 09:23:47] {993} INFO - iteration 95, current learner rf\n",
"[flaml.automl: 04-07 09:23:47] {1149} INFO - no enough budget for learner rf\n",
"[flaml.automl: 04-07 09:23:47] {993} INFO - iteration 96, current learner xgboost\n",
"[flaml.automl: 04-07 09:23:47] {1149} INFO - no enough budget for learner xgboost\n",
"[flaml.automl: 04-07 09:23:47] {993} INFO - iteration 97, current learner lrl1\n",
"[flaml.automl: 04-07 09:23:47] {1149} INFO - no enough budget for learner lrl1\n",
"[flaml.automl: 04-07 09:23:47] {1187} INFO - selected model: LGBMClassifier(learning_rate=0.0710308200604776, max_bin=1023,\n",
" min_child_samples=55, n_estimators=66, num_leaves=1256,\n",
" objective='binary', reg_alpha=0.02231327328083961,\n",
" reg_lambda=1.4895868449253324, subsample=0.7242742367442448)\n",
"[flaml.automl: 04-07 09:23:47] {944} INFO - fit succeeded\n"
]
}
],
"source": [
"'''The main flaml automl API'''\n",
"automl.fit(X_train=X_train, y_train=y_train, **settings)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Best model and metric"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Best ML leaner: lgbm\nBest hyperparmeter config: {'n_estimators': 66.0, 'num_leaves': 1256.0, 'min_child_samples': 55.0, 'learning_rate': 0.0710308200604776, 'subsample': 0.7242742367442448, 'log_max_bin': 10.0, 'colsample_bytree': 1.0, 'reg_alpha': 0.02231327328083961, 'reg_lambda': 1.4895868449253324, 'FLAML_sample_size': 364083}\nBest accuracy on validation data: 0.6703\nTraining duration of best run: 14.85 s\n"
]
}
],
"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))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"LGBMClassifier(learning_rate=0.0710308200604776, max_bin=1023,\n",
" min_child_samples=55, n_estimators=66, num_leaves=1256,\n",
" objective='binary', reg_alpha=0.02231327328083961,\n",
" reg_lambda=1.4895868449253324, subsample=0.7242742367442448)"
]
},
"metadata": {},
"execution_count": 6
}
],
"source": [
"automl.model"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"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)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Predicted labels [1 0 1 ... 1 0 0]\nTrue labels [0 0 0 ... 0 1 0]\n"
]
}
],
"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]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"accuracy = 0.6704166234074426\nroc_auc = 0.7235915818210762\nlog_loss = 0.604223973210284\nf1 = 0.587509165328606\n"
]
}
],
"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))\n",
"print('f1', '=', 1 - sklearn_metric_loss_score('f1', y_pred, y_test))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"See Section 4 for an accuracy comparison with default LightGBM and XGBoost.\n",
"\n",
"### Log history"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"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.1, '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.1, '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': 4.0, 'num_leaves': 4.0, 'min_child_samples': 25.0, 'learning_rate': 1.0, 'subsample': 0.8513627344387318, 'log_max_bin': 10.0, 'colsample_bytree': 0.9684145930669938, 'reg_alpha': 0.001831177697321707, 'reg_lambda': 0.2790165919053839, 'FLAML_sample_size': 10000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 4.0, 'num_leaves': 4.0, 'min_child_samples': 25.0, 'learning_rate': 1.0, 'subsample': 0.8513627344387318, 'log_max_bin': 10.0, 'colsample_bytree': 0.9684145930669938, 'reg_alpha': 0.001831177697321707, 'reg_lambda': 0.2790165919053839, 'FLAML_sample_size': 10000}}\n{'Current Learner': 'lgbm', 'Current Sample': 10000, 'Current Hyper-parameters': {'n_estimators': 23.0, 'num_leaves': 4.0, 'min_child_samples': 48.0, 'learning_rate': 1.0, 'subsample': 0.9814787163243813, 'log_max_bin': 10.0, 'colsample_bytree': 0.9534346594834143, 'reg_alpha': 0.002208534076096185, 'reg_lambda': 0.5460627024738886, 'FLAML_sample_size': 10000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 23.0, 'num_leaves': 4.0, 'min_child_samples': 48.0, 'learning_rate': 1.0, 'subsample': 0.9814787163243813, 'log_max_bin': 10.0, 'colsample_bytree': 0.9534346594834143, 'reg_alpha': 0.002208534076096185, 'reg_lambda': 0.5460627024738886, 'FLAML_sample_size': 10000}}\n{'Current Learner': 'lgbm', 'Current Sample': 10000, 'Current Hyper-parameters': {'n_estimators': 11.0, 'num_leaves': 17.0, 'min_child_samples': 42.0, 'learning_rate': 0.4743416464891248, 'subsample': 0.9233328006239466, 'log_max_bin': 10.0, 'colsample_bytree': 1.0, 'reg_alpha': 0.034996420228767956, 'reg_lambda': 0.6169079461473814, 'FLAML_sample_size': 10000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 11.0, 'num_leaves': 17.0, 'min_child_samples': 42.0, 'learning_rate': 0.4743416464891248, 'subsample': 0.9233328006239466, 'log_max_bin': 10.0, 'colsample_bytree': 1.0, 'reg_alpha': 0.034996420228767956, 'reg_lambda': 0.6169079461473814, 'FLAML_sample_size': 10000}}\n{'Current Learner': 'lgbm', 'Current Sample': 40000, 'Current Hyper-parameters': {'n_estimators': 11.0, 'num_leaves': 17.0, 'min_child_samples': 42.0, 'learning_rate': 0.4743416464891248, 'subsample': 0.9233328006239466, 'log_max_bin': 10.0, 'colsample_bytree': 1.0, 'reg_alpha': 0.034996420228767956, 'reg_lambda': 0.6169079461473814, 'FLAML_sample_size': 40000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 11.0, 'num_leaves': 17.0, 'min_child_samples': 42.0, 'learning_rate': 0.4743416464891248, 'subsample': 0.9233328006239466, 'log_max_bin': 10.0, 'colsample_bytree': 1.0, 'reg_alpha': 0.034996420228767956, 'reg_lambda': 0.6169079461473814, 'FLAML_sample_size': 40000}}\n{'Current Learner': 'lgbm', 'Current Sample': 40000, 'Current Hyper-parameters': {'n_estimators': 33.0, 'num_leaves': 29.0, 'min_child_samples': 47.0, 'learning_rate': 0.15519742191308053, 'subsample': 1.0, 'log_max_bin': 9.0, 'colsample_bytree': 1.0, 'reg_alpha': 0.07062243857330504, 'reg_lambda': 0.5761644116593855, 'FLAML_sample_size': 40000}, 'Best Learner': 'lgbm', 'Best Hyper-parameters': {'n_estimators': 33.0, 'num_leaves': 29.0, 'min_child_samples': 47.0, 'learning_rate': 0.15519742191308053, 'subsample': 1.0, 'log_max_bin': 9.0, 'colsample_bytree': 1.0, 'reg_alpha': 0.07062243857330504, 'reg_lambda': 0.5761644116593855, 'FLAML_sample_size': 40000}}\n{'Current Learner': 'lgbm', 'Current Sample': 40000, 'Curren
]
}
],
"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=60)\n",
"\n",
"for config in config_history:\n",
" print(config)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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 398.50625 277.314375\" width=\"398.50625pt\" 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 398.50625 277.314375 \nL 398.50625 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 56.50625 239.758125 \nL 391.30625 239.758125 \nL 391.30625 22.318125 \nL 56.50625 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=\"m7e58859600\" style=\"stroke:#1f77b4;\"/>\n </defs>\n <g clip-path=\"url(#p6d8aa524ec)\">\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"71.724432\" xlink:href=\"#m7e58859600\" y=\"229.874489\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"73.275419\" xlink:href=\"#m7e58859600\" y=\"158.577146\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"75.659326\" xlink:href=\"#m7e58859600\" y=\"141.850992\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"77.443453\" xlink:href=\"#m7e58859600\" y=\"135.937705\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"106.907798\" xlink:href=\"#m7e58859600\" y=\"78.494349\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"155.489784\" xlink:href=\"#m7e58859600\" y=\"57.375468\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"244.30482\" xlink:href=\"#m7e58859600\" y=\"39.635608\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"376.088068\" xlink:href=\"#m7e58859600\" 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=\"m5d3026066d\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n </defs>\n <g>\n <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"62.894968\" xlink:href=\"#m5d3026066d\" 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(59.713718 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=\"120.324246\" xlink:href=\"#m5d3026066d\" y=\"239.758125\"/>\n </g>\n </g>\n <g id=\"text_2\">\n <!-- 5 -->\n <defs>\n <path d=\"M 10.796875 72.90625 \nL 49.515625 72.90625 \nL 49.515625 64.59375 \nL 19.828125 64.59375 \nL 19.828125 46.734375 \nQ 21.96875 47.46875 24.1
"image/png": "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
},
"metadata": {
"needs_background": "light"
}
}
],
"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()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 3. Customized Learner"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"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.qloguniform(lower=4, upper=data_size, q=1), 'init_value': 4, 'low_cost_init_value': 4},\n",
" 'n_iter': {'domain': tune.qloguniform(lower=1, upper=data_size, q=1), 'init_value': 1, 'low_cost_init_value': 1},\n",
" 'n_tree_search': {'domain': tune.qloguniform(lower=1, upper=32768, q=1), 'init_value': 1, 'low_cost_init_value': 1},\n",
" 'opt_interval': {'domain': tune.qloguniform(lower=1, upper=10000, q=1), 'init_value': 100},\n",
" 'learning_rate': {'domain': tune.loguniform(lower=0.01, upper=20.0)},\n",
" 'min_samples_leaf': {'domain': tune.qloguniform(lower=1, upper=20, q=1), '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"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"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. "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"automl = AutoML()\n",
"automl.add_learner(learner_name='RGF', learner_class=MyRegularizedGreedyForest)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"[flaml.automl: 04-07 09:24:01] {890} INFO - Evaluation method: holdout\n",
"[flaml.automl: 04-07 09:24:01] {596} INFO - Using StratifiedKFold\n",
"[flaml.automl: 04-07 09:24:01] {911} INFO - Minimizing error metric: 1-accuracy\n",
"[flaml.automl: 04-07 09:24:01] {929} INFO - List of ML learners in AutoML Run: ['RGF', 'lgbm', 'rf', 'xgboost']\n",
"[flaml.automl: 04-07 09:24:01] {993} INFO - iteration 0, current learner RGF\n",
"/home/dmx/miniconda2/envs/blend/lib/python3.8/site-packages/rgf/utils.py:225: UserWarning: Cannot find FastRGF executable files. FastRGF estimators will be unavailable for usage.\n",
" warnings.warn(\"Cannot find FastRGF executable files. \"\n",
"[flaml.automl: 04-07 09:24:05] {1141} INFO - at 4.2s,\tbest RGF's error=0.3787,\tbest RGF's error=0.3787\n",
"[flaml.automl: 04-07 09:24:05] {993} INFO - iteration 1, current learner RGF\n",
"[flaml.automl: 04-07 09:24:06] {1141} INFO - at 5.8s,\tbest RGF's error=0.3787,\tbest RGF's error=0.3787\n",
"[flaml.automl: 04-07 09:24:06] {993} INFO - iteration 2, current learner RGF\n",
"[flaml.automl: 04-07 09:24:08] {1141} INFO - at 7.7s,\tbest RGF's error=0.3787,\tbest RGF's error=0.3787\n",
"[flaml.automl: 04-07 09:24:08] {993} INFO - iteration 3, current learner RGF\n",
"[flaml.automl: 04-07 09:24:10] {1141} INFO - at 9.5s,\tbest RGF's error=0.3706,\tbest RGF's error=0.3706\n",
"[flaml.automl: 04-07 09:24:10] {993} INFO - iteration 4, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:10] {1141} INFO - at 9.6s,\tbest lgbm's error=0.3777,\tbest RGF's error=0.3706\n",
"[flaml.automl: 04-07 09:24:10] {993} INFO - iteration 5, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:10] {1141} INFO - at 9.7s,\tbest lgbm's error=0.3777,\tbest RGF's error=0.3706\n",
"[flaml.automl: 04-07 09:24:10] {993} INFO - iteration 6, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:10] {1141} INFO - at 9.7s,\tbest lgbm's error=0.3672,\tbest lgbm's error=0.3672\n",
"[flaml.automl: 04-07 09:24:10] {993} INFO - iteration 7, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:10] {1141} INFO - at 9.8s,\tbest lgbm's error=0.3672,\tbest lgbm's error=0.3672\n",
"[flaml.automl: 04-07 09:24:10] {993} INFO - iteration 8, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:10] {1141} INFO - at 9.9s,\tbest lgbm's error=0.3648,\tbest lgbm's error=0.3648\n",
"[flaml.automl: 04-07 09:24:10] {993} INFO - iteration 9, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:11] {1141} INFO - at 10.1s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:24:11] {993} INFO - iteration 10, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:11] {1141} INFO - at 10.1s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:24:11] {993} INFO - iteration 11, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:11] {1141} INFO - at 10.3s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:24:11] {993} INFO - iteration 12, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:11] {1141} INFO - at 10.4s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:24:11] {993} INFO - iteration 13, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:11] {1141} INFO - at 10.5s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:24:11] {993} INFO - iteration 14, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:11] {1141} INFO - at 10.6s,\tbest lgbm's error=0.3639,\tbest lgbm's error=0.3639\n",
"[flaml.automl: 04-07 09:24:11] {993} INFO - iteration 15, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:11] {1141} INFO - at 10.9s,\tbest lgbm's error=0.3555,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:24:11] {993} INFO - iteration 16, current learner RGF\n",
"[flaml.automl: 04-07 09:24:13] {1141} INFO - at 12.5s,\tbest RGF's error=0.3706,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:24:13] {993} INFO - iteration 17, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:13] {1141} INFO - at 12.8s,\tbest lgbm's error=0.3555,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:24:13] {993} INFO - iteration 18, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:13] {1141} INFO - at 13.0s,\tbest lgbm's error=0.3555,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:24:13] {993} INFO - iteration 19, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:14] {1141} INFO - at 13.3s,\tbest lgbm's error=0.3555,\tbest lgbm's error=0.3555\n",
"[flaml.automl: 04-07 09:24:14] {993} INFO - iteration 20, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:14] {1141} INFO - at 13.8s,\tbest lgbm's error=0.3524,\tbest lgbm's error=0.3524\n",
"[flaml.automl: 04-07 09:24:14] {993} INFO - iteration 21, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:15] {1141} INFO - at 14.2s,\tbest lgbm's error=0.3524,\tbest lgbm's error=0.3524\n",
"[flaml.automl: 04-07 09:24:15] {993} INFO - iteration 22, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:17] {1141} INFO - at 17.0s,\tbest lgbm's error=0.3498,\tbest lgbm's error=0.3498\n",
"[flaml.automl: 04-07 09:24:17] {993} INFO - iteration 23, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:20] {1141} INFO - at 20.0s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:20] {993} INFO - iteration 24, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:22] {1141} INFO - at 22.0s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:22] {993} INFO - iteration 25, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:23] {1141} INFO - at 22.6s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:23] {993} INFO - iteration 26, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:27] {1141} INFO - at 26.6s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:27] {993} INFO - iteration 27, current learner xgboost\n",
"[flaml.automl: 04-07 09:24:27] {1141} INFO - at 26.6s,\tbest xgboost's error=0.3787,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:27] {993} INFO - iteration 28, current learner xgboost\n",
"[flaml.automl: 04-07 09:24:27] {1141} INFO - at 26.7s,\tbest xgboost's error=0.3768,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:27] {993} INFO - iteration 29, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:28] {1141} INFO - at 27.6s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:28] {993} INFO - iteration 30, current learner RGF\n",
"[flaml.automl: 04-07 09:24:30] {1141} INFO - at 29.1s,\tbest RGF's error=0.3706,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:30] {993} INFO - iteration 31, current learner lgbm\n",
"[flaml.automl: 04-07 09:24:37] {1141} INFO - at 36.5s,\tbest lgbm's error=0.3487,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:37] {993} INFO - iteration 32, current learner xgboost\n",
"[flaml.automl: 04-07 09:24:37] {1141} INFO - at 36.6s,\tbest xgboost's error=0.3746,\tbest lgbm's error=0.3487\n",
"[flaml.automl: 04-07 09:24:37] {993} INFO - iteration 33, current learner lgbm\n",
"[flaml.automl: 04-07 09:25:04] {1141} INFO - at 63.6s,\tbest lgbm's error=0.3428,\tbest lgbm's error=0.3428\n",
"[flaml.automl: 04-07 09:25:04] {1187} INFO - selected model: LGBMClassifier(colsample_bytree=0.8589079860800738,\n",
" learning_rate=0.04261799404250151, max_bin=1023,\n",
" min_child_samples=99, n_estimators=318, num_leaves=27,\n",
" objective='binary', reg_alpha=0.054177182376811454,\n",
" reg_lambda=3.7064664547599495, subsample=0.9982731696185565)\n",
"[flaml.automl: 04-07 09:25:04] {944} INFO - fit succeeded\n"
]
}
],
"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)"
]
},
{
"source": [
"## 4. Comparison with alternatives\n",
"\n",
"### FLAML's accuracy"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"flaml accuracy = 0.6704166234074426\n"
]
}
],
"source": [
"print('flaml accuracy', '=', 1 - sklearn_metric_loss_score('accuracy', y_pred, y_test))"
]
},
{
"source": [
"### Default LightGBM"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"from lightgbm import LGBMClassifier\n",
"lgbm = LGBMClassifier()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"LGBMClassifier()"
]
},
"metadata": {},
"execution_count": 17
}
],
"source": [
"lgbm.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"default lgbm accuracy = 0.6602346380315323\n"
]
}
],
"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))"
]
},
{
"source": [
"### Default XGBoost"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"from xgboost import XGBClassifier\n",
"xgb = XGBClassifier()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"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": 20
}
],
"source": [
"xgb.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"default xgboost accuracy = 0.6676060098186078\n"
]
}
],
"source": [
"y_pred = xgb.predict(X_test)\n",
"from flaml.ml import sklearn_metric_loss_score\n",
"print('default xgboost accuracy', '=', 1 - sklearn_metric_loss_score('accuracy', y_pred, y_test))"
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.0 64-bit ('blend': conda)",
"metadata": {
"interpreter": {
"hash": "0cfea3304185a9579d09e0953576b57c8581e46e6ebc6dfeb681bc5a511f7544"
}
}
},
"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
}