非负最小二乘法#

在本例中,我们拟合了一个回归系数具有正约束的线性模型,并将估计的系数与经典线性回归进行了比较。

import matplotlib.pyplot as plt
import numpy as np

from sklearn.metrics import r2_score

生成一些随机数据

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,))

将数据拆分为训练集和测试集

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)

拟合非负最小二乘法。

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)
NNLS R2 score 0.8225220806196525

拟合一个普通最小二乘法。

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)
OLS R2 score 0.7436926291700353

比较普通最小二乘法和非负最小二乘法之间的回归系数,我们可以观察到它们高度相关(虚线是恒等关系),但非负约束将一些系数缩减为 0。非负最小二乘法本质上会产生稀疏结果。

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")
plot nnls
Text(55.847222222222214, 0.5, 'NNLS regression coefficients')

脚本总运行时间:(0 分钟 0.074 秒)

相关示例

逻辑函数

逻辑函数

线性回归示例

线性回归示例

机器学习推断因果关系的失败

机器学习推断因果关系的失败

基于 L1 的稀疏信号模型

基于 L1 的稀疏信号模型

由 Sphinx-Gallery 生成的图库