Open In Colab

Calibrating Regressor Prediction Interval

This example requires full licence, and the program will break if you use the trial licence.

Installation

# To install the required package, use the following command:
# !pip install modeva

Authentication

# To get authentication, use the following command: (To get full access please replace the token to your own token)
# from modeva.utils.authenticate import authenticate
# authenticate(auth_code='eaaa4301-b140-484c-8e93-f9f633c8bacb')

Import required modules

import numpy as np
import mocharts as mc
from IPython.display import HTML
from modeva import DataSet
from modeva.models import MoXGBRegressor

Build a model

ds = DataSet()
ds.load(name="BikeSharing")
ds.set_random_split()
ds.scale_numerical(features=("cnt",), method="log1p")
ds.preprocess()

model = MoXGBRegressor(max_depth=2)
model.fit(ds.train_x, ds.train_y)
MoXGBRegressor(base_score=None, booster=None, callbacks=None,
               colsample_bylevel=None, colsample_bynode=None,
               colsample_bytree=None, device=None, early_stopping_rounds=None,
               enable_categorical=False, eval_metric=None, feature_types=None,
               gamma=None, grow_policy=None, importance_type=None,
               interaction_constraints=None, learning_rate=None, max_bin=None,
               max_cat_threshold=None, max_cat_to_onehot=None,
               max_delta_step=None, max_depth=2, max_leaves=None,
               min_child_weight=None, missing=nan, monotone_constraints=None,
               multi_strategy=None, n_estimators=None, n_jobs=None,
               num_parallel_tree=None, objective='reg:squarederror', ...)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


Calibrate the model

model.calibrate_interval(X=ds.test_x, y=ds.test_y, alpha=0.1, max_depth=5)

Get prediction interval

print(model.predict_interval(ds.test_x[:5]))
[[2.48640678 4.60513461]
 [3.47997744 5.00162004]
 [3.80710694 5.3893073 ]
 [3.73282696 5.26802368]
 [3.33365986 5.01830676]]

Visualize prediction interval

p = model.predict(ds.test_x)
pi = model.predict_interval(ds.test_x)
idx = np.argsort(ds.test_y.ravel())

options = mc.lineplot(np.hstack([np.arange(pi.shape[0]),
                                 np.arange(pi.shape[0]),
                                 np.arange(pi.shape[0])]),
                      np.hstack([pi[idx, 0],
                                 pi[idx, 1],
                                 ds.test_y[idx].ravel()]),
                      label=np.hstack([["low"] * pi.shape[0],
                                       ["up"] * pi.shape[0],
                                       ["actual"] * pi.shape[0]]))

options.set_xaxis(axis_name="samples")
options.set_yaxis(axis_name="prediction")
options.set_legend()
options.figsize = {'width': 500, 'height': 400}
htmlstr = mc.mocharts_plot(options.render(), return_html=True, silent=True)
HTML(htmlstr)


Rest calibration when needed

model.reset_calibrate_interval()

Total running time of the script: (0 minutes 1.028 seconds)

Gallery generated by Sphinx-Gallery