.. 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 Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_linear_model_plot_nnls.py: ========================== 非负最小二乘法 ========================== 在这个示例中,我们拟合了一个对回归系数施加正约束的线性模型,并将估计的系数与经典线性回归进行比较。 .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.metrics import r2_score .. GENERATED FROM PYTHON SOURCE LINES 16-17 生成一些随机数据 .. GENERATED FROM PYTHON SOURCE LINES 17-30 .. 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) # 将阈值系数调整为非负值 true_coef[true_coef < 0] = 0 y = np.dot(X, true_coef) # 添加一些噪音 y += 5 * np.random.normal(size=(n_samples,)) .. GENERATED FROM PYTHON SOURCE LINES 31-32 将数据分为训练集和测试集 .. GENERATED FROM PYTHON SOURCE LINES 32-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 拟合非负最小二乘法。 .. GENERATED FROM PYTHON SOURCE LINES 39-47 .. 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.8225220806196525 .. GENERATED FROM PYTHON SOURCE LINES 48-49 Fit an OLS. .. GENERATED FROM PYTHON SOURCE LINES 49-55 .. 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.7436926291700346 .. GENERATED FROM PYTHON SOURCE LINES 56-57 比较OLS和NNLS的回归系数,我们可以观察到它们高度相关(虚线是恒等关系),但非负约束将一些系数缩减为0。非负最小二乘法本质上会产生稀疏结果。 .. GENERATED FROM PYTHON SOURCE LINES 57-69 .. 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.031 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:: 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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_nnls.zip ` .. include:: plot_nnls.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_