Note
Go to the end to download the full example code. or to run this example in your browser via JupyterLite or Binder
Monotonic Constraints#
This example illustrates the effect of monotonic constraints on a gradient boosting estimator.
We build an artificial dataset where the target value is in general positively correlated with the first feature (with some random and non-random variations), and in general negatively correlated with the second feature.
By imposing a monotonic increase or a monotonic decrease constraint, respectively, on the features during the learning process, the estimator is able to properly follow the general trend instead of being subject to the variations.
This example was inspired by the XGBoost documentation.
# Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause
importmatplotlib.pyplotasplt importnumpyasnp fromsklearn.ensembleimport HistGradientBoostingRegressor fromsklearn.inspectionimport PartialDependenceDisplay rng = np.random.RandomState (0) n_samples = 1000 f_0 = rng.rand(n_samples) f_1 = rng.rand(n_samples) X = np.c_ [f_0, f_1] noise = rng.normal(loc=0.0, scale=0.01, size=n_samples) # y is positively correlated with f_0, and negatively correlated with f_1 y = 5 * f_0 + np.sin (10 * np.pi * f_0) - 5 * f_1 - np.cos (10 * np.pi * f_1) + noise
Fit a first model on this dataset without any constraints.
gbdt_no_cst = HistGradientBoostingRegressor () gbdt_no_cst.fit(X, y)
HistGradientBoostingRegressor()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.