.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_linear_models.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_linear_models.py: Linear Models Optimization =========================== Hyperparameter optimization for Ridge, Lasso, and ElasticNet regression. .. GENERATED FROM PYTHON SOURCE LINES 6-16 .. code-block:: default from sklearn.datasets import load_diabetes from sklearn.linear_model import Ridge, Lasso, ElasticNet from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, r2_score import numpy as np import plotly from mloptimizer.interfaces import HyperparameterSpaceBuilder, GeneticSearch from mloptimizer.application.reporting.plots import plotly_search_space, plotly_logbook .. GENERATED FROM PYTHON SOURCE LINES 17-18 Load and prepare the dataset .. GENERATED FROM PYTHON SOURCE LINES 18-24 .. code-block:: default print("Loading Diabetes dataset...") data = load_diabetes() X, y = data.data, data.target print(f"Dataset shape: {X.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Loading Diabetes dataset... Dataset shape: (442, 10) .. GENERATED FROM PYTHON SOURCE LINES 25-26 Split the data .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: default X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) .. GENERATED FROM PYTHON SOURCE LINES 31-32 Optimize Ridge regression .. GENERATED FROM PYTHON SOURCE LINES 32-52 .. code-block:: default print("\n=== Ridge Regression ===") hyperparam_space = HyperparameterSpaceBuilder.get_default_space( estimator_class=Ridge ) opt = GeneticSearch( estimator_class=Ridge, hyperparam_space=hyperparam_space, generations=5, population_size=8, seed=42, use_mlflow=False, use_parallel=False ) opt.fit(X_train, y_train) y_pred = opt.best_estimator_.predict(X_test) print(f"Ridge - Best params: {opt.best_params_}") print(f"Ridge - Test R2: {r2_score(y_test, y_pred):.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none === Ridge Regression === /home/docs/checkouts/readthedocs.org/user_builds/mloptimizer/checkouts/master/examples/plot_linear_models.py:37: UserWarning: Expected mutations per offspring is very low (0.32). With mutpb=0.8, indpb=0.2, and 2 hyperparameters, the population will converge prematurely. Recommended: mutpb >= 0.8, indpb >= 0.2 (gives ~0.3 mutations/offspring). opt = GeneticSearch( /home/docs/checkouts/readthedocs.org/user_builds/mloptimizer/checkouts/master/examples/plot_linear_models.py:37: UserWarning: Some hyperparameters have very small integer ranges (< 10 distinct values): 'solver' (6 values: 0 to 5). Small ranges limit search granularity. Consider increasing the range or scale for float types. opt = GeneticSearch( Genetic execution: 0%| | 0/6 [00:00= 0.8, indpb >= 0.2 (gives ~0.5 mutations/offspring). opt = GeneticSearch( /home/docs/checkouts/readthedocs.org/user_builds/mloptimizer/checkouts/master/examples/plot_linear_models.py:71: UserWarning: Some hyperparameters have very small integer ranges (< 10 distinct values): 'selection' (2 values: 0 to 1). Small ranges limit search granularity. Consider increasing the range or scale for float types. opt = GeneticSearch( Genetic execution: 0%| | 0/6 [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_linear_models.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_