MoDeVa MoDeVa

User Guide

  • User Guide

API Reference

  • API Reference

Examples

  • Examples

Installation

  • Installation
Modeva
  • Random Search

Note

Go to the end to download the full example code.

Random Search

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

from modeva import DataSet
from modeva import TestSuite
from modeva.models import MoElasticNet
from modeva.models import ModelTuneRandomSearch

Load Dataset

ds = DataSet()
ds.load(name="BikeSharing")
ds.set_random_split()

ds.scale_numerical(features=("cnt",), method="log1p")
ds.preprocess()

Run random search

param_grid = {"alpha": [0.1, 1.0, 10],
              "l1_ratio": [(i + 1) * 0.1 for i in range(10)]}

model = MoElasticNet()
hpo = ModelTuneRandomSearch(dataset=ds, model=model)
result = hpo.run(param_distributions=param_grid,
                 n_iter=20,
                 metric="MSE",
                 cv=5)
result.table
alpha l1_ratio MSE MSE_rank Time
16 0.1 0.2 1.2240 1 0.0053
0 0.1 0.3 1.2558 2 0.0053
19 0.1 0.5 1.3430 3 0.0054
10 0.1 0.6 1.3890 4 0.0052
18 0.1 0.7 1.4156 5 0.0054
12 0.1 0.9 1.4405 6 0.0053
11 1.0 0.7 1.4520 7 0.0087
8 1.0 0.8 1.4528 8 0.0045
2 1.0 0.4 1.4531 9 0.0045
13 1.0 0.5 1.4539 10 0.0045
3 1.0 0.1 1.4540 11 0.0049
7 1.0 0.2 1.4568 12 0.0046
15 10.0 0.1 1.4994 13 0.0045
9 10.0 0.3 1.6682 14 0.0045
14 10.0 0.4 1.8006 15 0.0046
5 10.0 0.5 1.9698 16 0.0044
6 10.0 0.8 2.0789 17 0.0045
4 10.0 0.7 2.0789 17 0.0044
1 10.0 0.9 2.0789 17 0.0044
17 10.0 1.0 2.0789 17 0.0044


result.plot("parallel", figsize=(8, 6))


result.plot(("alpha", "MSE"))


result.plot(("l1_ratio", "MSE"))


Retrain model with best hyperparameter

model_tuned = MoElasticNet(**result.value["params"][0],
                           name="GLM-Tuned")
model_tuned.fit(ds.train_x, ds.train_y)
model_tuned
MoElasticNet(alpha=0.1, copy_X=True, fit_intercept=True,
             l1_ratio=0.30000000000000004, max_iter=1000, positive=False,
             precompute=False, random_state=None, selection='cyclic',
             tol=0.0001, warm_start=False)
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.
MoElasticNet(alpha=0.1, copy_X=True, fit_intercept=True,
             l1_ratio=0.30000000000000004, max_iter=1000, positive=False,
             precompute=False, random_state=None, selection='cyclic',
             tol=0.0001, warm_start=False)


Diagnose the tuned model

ts = TestSuite(ds, model_tuned)
result = ts.diagnose_accuracy_table()
result.table
MSE MAE R2
train 1.1690 0.8863 0.4175
test 1.1805 0.8876 0.4166
GAP 0.0115 0.0013 -0.0009


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

Download Jupyter notebook: plot_1_random.ipynb

Download Python source code: plot_1_random.py

Download zipped: plot_1_random.zip

Gallery generated by Sphinx-Gallery


© Copyright Modeva Dev Team.

Built with Sphinx using a theme provided by Read the Docs.