Skip to main content
Ctrl+K

Modeva-AI

  • Installation
  • User Guide
  • API Reference
  • Gallery
  • Changelog
  • GitHub
  • PyPI
  • Installation
  • User Guide
  • API Reference
  • Gallery
  • Changelog
  • GitHub
  • PyPI

Section Navigation

  • Get Started
  • Dataset
  • Model Development
    • ModelZoo
    • Built-in Interpretable Models
    • External Models
    • Model Calibration
    • Hyperparameter Tuning
      • Grid Search
      • Random Search
      • Particle Swarm Optimization Search
      • Tuning with optuna (Experimental)
  • Model Validation
  • Utilities
  • Low Code
  • Gallery of Modeva Examples
  • Model Development
  • Hyperparameter Tuning
  • 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 1.8218
0 0.1 0.3 1.2558 2 1.7433
19 0.1 0.5 1.3430 3 2.6010
10 0.1 0.6 1.3890 4 2.0390
18 0.1 0.7 1.4156 5 2.3220
12 0.1 0.9 1.4405 6 2.2322
11 1.0 0.7 1.4520 7 0.0972
8 1.0 0.8 1.4528 8 0.1730
2 1.0 0.4 1.4531 9 0.1622
13 1.0 0.5 1.4539 10 0.4610
3 1.0 0.1 1.4540 11 0.5850
7 1.0 0.2 1.4568 12 0.2376
15 10 0.1 1.4994 13 0.6641
9 10 0.3 1.6682 14 0.2405
14 10 0.4 1.8006 15 0.1642
5 10 0.5 1.9698 16 0.1542
6 10 0.8 2.0789 17 0.0918
4 10 0.7 2.0789 17 0.0582
1 10 0.9 2.0789 17 0.0543
17 10 1.0 2.0789 17 0.6085


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: (1 minutes 24.578 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

previous

Grid Search

next

Particle Swarm Optimization Search

On this page
  • Run random search
  • Retrain model with best hyperparameter
  • Diagnose the tuned model

© Copyright 2024-2025, Modeva Team.

Built with the PyData Sphinx Theme 0.16.0.