.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/linear_model/plot_nnls.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 or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_linear_model_plot_nnls.py: ========================== Non-negative least squares ========================== In this example, we fit a linear model with positive constraints on the regression coefficients and compare the estimated coefficients to a classic linear regression. .. GENERATED FROM PYTHON SOURCE LINES 11-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.metrics import r2_score .. GENERATED FROM PYTHON SOURCE LINES 18-19 Generate some random data .. GENERATED FROM PYTHON SOURCE LINES 19-31 .. code-block:: Python np.random.seed(42) n_samples, n_features = 200, 50 X = np.random.randn(n_samples, n_features) true_coef = 3 * np.random.randn(n_features) # Threshold coefficients to render them non-negative true_coef[true_coef < 0] = 0 y = np.dot(X, true_coef) # Add some noise y += 5 * np.random.normal(size=(n_samples,)) .. GENERATED FROM PYTHON SOURCE LINES 32-33 Split the data in train set and test set .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: Python from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Fit the Non-Negative least squares. .. GENERATED FROM PYTHON SOURCE LINES 39-46 .. code-block:: Python from sklearn.linear_model import LinearRegression reg_nnls = LinearRegression(positive=True) y_pred_nnls = reg_nnls.fit(X_train, y_train).predict(X_test) r2_score_nnls = r2_score(y_test, y_pred_nnls) print("NNLS R2 score", r2_score_nnls) .. rst-class:: sphx-glr-script-out .. code-block:: none NNLS R2 score 0.8225220806196526 .. GENERATED FROM PYTHON SOURCE LINES 47-48 Fit an OLS. .. GENERATED FROM PYTHON SOURCE LINES 48-54 .. code-block:: Python reg_ols = LinearRegression() y_pred_ols = reg_ols.fit(X_train, y_train).predict(X_test) r2_score_ols = r2_score(y_test, y_pred_ols) print("OLS R2 score", r2_score_ols) .. rst-class:: sphx-glr-script-out .. code-block:: none OLS R2 score 0.7436926291700354 .. GENERATED FROM PYTHON SOURCE LINES 55-59 Comparing the regression coefficients between OLS and NNLS, we can observe they are highly correlated (the dashed line is the identity relation), but the non-negative constraint shrinks some to 0. The Non-Negative Least squares inherently yield sparse results. .. GENERATED FROM PYTHON SOURCE LINES 59-70 .. code-block:: Python fig, ax = plt.subplots() ax.plot(reg_ols.coef_, reg_nnls.coef_, linewidth=0, marker=".") low_x, high_x = ax.get_xlim() low_y, high_y = ax.get_ylim() low = max(low_x, low_y) high = min(high_x, high_y) ax.plot([low, high], [low, high], ls="--", c=".3", alpha=0.5) ax.set_xlabel("OLS regression coefficients", fontweight="bold") ax.set_ylabel("NNLS regression coefficients", fontweight="bold") .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_nnls_001.png :alt: plot nnls :srcset: /auto_examples/linear_model/images/sphx_glr_plot_nnls_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(55.847222222222214, 0.5, 'NNLS regression coefficients') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.077 seconds) .. _sphx_glr_download_auto_examples_linear_model_plot_nnls.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/linear_model/plot_nnls.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/?path=auto_examples/linear_model/plot_nnls.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_nnls.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_nnls.py ` .. include:: plot_nnls.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_