<jupyter_start><jupyter_text>Import<jupyter_code># Basic
import pandas as pd
import numpy as np

import warnings

warnings.simplefilter('ignore')

# ML Toolkit
from robusta.optimizer import GridSearchCV, RandomSearchCV, OptunaCV
from robusta.crossval import crossval_predict

from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.datasets import make_regression

# Model
from lightgbm import LGBMRegressor

# Plot
from jupyterthemes import jtplot
import matplotlib

jtplot.style('gruvboxd')
matplotlib.use('nbagg')<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>X, y = make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=666)

X = pd.DataFrame(X)
y = pd.Series(y)

X.rename(columns=lambda x: 'x{}'.format(x), inplace=True)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
X_train<jupyter_output><empty_output><jupyter_text>Task<jupyter_code>get_score = lambda y_true, y_pred: -mean_squared_error(y_true, y_pred)
scoring = 'neg_mean_squared_error'

cv = 5<jupyter_output><empty_output><jupyter_text>Baseline<jupyter_code>model = LGBMRegressor()
_, y_pred = crossval_predict(model, cv, X_train, y_train, X_new=X_test,
                             scoring=scoring, verbose=2, n_jobs=None)

get_score(y_test, y_pred)<jupyter_output>[06:39:30]  LGBMRegressor

[06:39:30]  FOLD  0:   -1849.9203
[06:39:30]  FOLD  1:   -985.8744
[06:39:30]  FOLD  2:   -1560.7978
[06:39:31]  FOLD  3:   -1821.7524
[06:39:31]  FOLD  4:   -1409.5014

[06:39:31]  AVERAGE:   -1525.5693 ± 315.8833<jupyter_text>Optimizer  Grid Search<jupyter_code>model = LGBMRegressor()

# https://lightgbm.readthedocs.io/en/latest/Parameters.html
param_space = {
    'learning_rate': 0.2,
    'max_depth': (4, 12, 2),
    
    'bagging_fraction': (0.2, 0.8, 0.2),
    'feature_fraction': (0.2, 0.8, 0.2),
}

optimizer = GridSearchCV(model, cv, scoring, param_space=param_space, verbose=2)
optimizer.fit(X_train, y_train)

model = optimizer.best_estimator_
_, y_pred = crossval_predict(model, cv, X_train, y_train, X_new=X_test,
                             scoring=scoring, verbose=2, n_jobs=None)

get_score(y_test, y_pred)
%matplotlib notebook

optimizer.plot(color='g')<jupyter_output><empty_output><jupyter_text>Random Search<jupyter_code>model = LGBMRegressor()

# https://lightgbm.readthedocs.io/en/latest/Parameters.html
param_space = {
    'max_depth': (3, 12, 1),
    'num_leaves': {15, 31, 63, 127, 255, 511, 1023, 2047, 4095},

    'bagging_fraction': (0.1, 0.9, 0.05),
    'feature_fraction': (0.1, 0.9, 0.05),
}

optimizer = RandomSearchCV(model, cv, scoring, param_space=param_space, max_iter=50)
optimizer.fit(X_train, y_train)

model = optimizer.best_estimator_
_, y_pred = crossval_predict(model, cv, X_train, y_train, X_new=X_test,
                             scoring=scoring, verbose=2, n_jobs=None)

get_score(y_test, y_pred)
%matplotlib notebook

optimizer.plot(color='r')<jupyter_output><empty_output><jupyter_text>Optuna<jupyter_code>model = LGBMRegressor()

# https://lightgbm.readthedocs.io/en/latest/Parameters.html
param_space = {
    'max_depth': (3, 12, 1),
    'num_leaves': {15, 31, 63, 127, 255, 511, 1023, 2047, 4095},

    'bagging_fraction': (0.1, 0.9, 0.05),
    'feature_fraction': (0.1, 0.9, 0.05),
}

optimizer = OptunaCV(model, cv, scoring, param_space=param_space, max_iter=50)
optimizer.fit(X_train, y_train)

model = optimizer.best_estimator_
_, y_pred = crossval_predict(model, cv, X_train, y_train, X_new=X_test,
                             scoring=scoring, verbose=2, n_jobs=None)

get_score(y_test, y_pred)
%matplotlib notebook

optimizer.plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13302875571083042, 'fraction_numerical': 0.06261757592045149, 'mean_word_length': 5.141914191419142, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '615968', 'license': 'MIT', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1513, 'n_words': 262, 'path': 'examples/optimizer-plot.ipynb', 'repo_name': 'uberkinder/Robusta-AutoML', 'size': '3721'}