多项式回归

有 a和b 两个特征,那么它的 2 次多项式的次数为 1, a, b, a^{2} , ab, b^{2} 

PolynomialFeatures 这个类有 3 个参数

degree:控制多项式的次数;

interaction_only:默认为 False,如果指定为 True,那么就不会有特征⾃⼰和⾃⼰结合的项,组合的特征中没有 a 和 b ;

include_bias:默认为 True 。如果为 True 的话,那么结果中就会有 0 次幂项,即全为 1 这⼀列。

from sklearn.preprocessing import PolynomialFeatures
import numpy as np
from sympy import false

X = np.arange(6).reshape(3, 2)
print(X)
poly = PolynomialFeatures(degree=2)
res = poly.fit_transform(X)
print(res)

结果

[[0 1] 
 [2 3] 
 [4 5]]

[[ 1.  0.  1.  0.  0.  1.] 
 [ 1.  2.  3.  4.  6.  9.] 
 [ 1.  4.  5. 16. 20. 25.]]

 

from sklearn.preprocessing import PolynomialFeatures
import numpy as np
from sympy import false
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error



data = np.array([[ -2.95507616, 10.94533252],
[ -0.44226119, 2.96705822],
[ -2.13294087, 6.57336839],
[ 1.84990823, 5.44244467],
[ 0.35139795, 2.83533936],
[ -1.77443098, 5.6800407 ],
[ -1.8657203 , 6.34470814],
[ 1.61526823, 4.77833358],
[ -2.38043687, 8.51887713],
[ -1.40513866, 4.18262786]])
X = data[:, 0].reshape(-1, 1) # 将array转换成矩阵 reshape(-1,1)转换成1列
y = data[:,1].reshape(-1, 1)

poly_features = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly_features.fit_transform(X)
print(X_poly)

lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)
print(lin_reg.intercept_, lin_reg.coef_)
# [ 2.60996757] [[-0.12759678 0.9144504 ]]
X_plot = np.linspace(-3, 3, 1000).reshape(-1, 1)
X_plot_poly = poly_features.fit_transform(X_plot)
y_plot = lin_reg.predict(X_plot_poly)
plt.plot(X_plot, y_plot, 'red')
plt.plot(X, y, 'b.')
plt.show()

y_pre = lin_reg.predict(X_poly)
mean_squared_error(y, y_pre)

 

 结果:[2.60996757] [[-0.12759678  0.9144504 ]]

方程为为h = −0.13x + 0.91x^{2}+ 2.61

 引⼊⾼阶项x^{2},训练误差明显下降,那么训练误差是否还有进⼀步下降的空间呢?

from sklearn.preprocessing import PolynomialFeatures
import numpy as np
from sympy import false
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error

data = np.array([[ -2.95507616, 10.94533252],
[ -0.44226119, 2.96705822],
[ -2.13294087, 6.57336839],
[ 1.84990823, 5.44244467],
[ 0.35139795, 2.83533936],
[ -1.77443098, 5.6800407 ],
[ -1.8657203 , 6.34470814],
[ 1.61526823, 4.77833358],
[ -2.38043687, 8.51887713],
[ -1.40513866, 4.18262786]])
X = data[:, 0].reshape(-1, 1) # 将array转换成矩阵 reshape(-1,1)转换成1列
y = data[:,1].reshape(-1, 1)
def try_degree(degree, X, y):
    poly_features_d = PolynomialFeatures(degree=degree, include_bias=False)
    X_poly_d = poly_features_d.fit_transform(X)
    lin_reg_d = LinearRegression()
    lin_reg_d.fit(X_poly_d, y)
    return {'X_poly': X_poly_d, 'intercept': lin_reg_d.intercept_, 'coef': lin_reg_d.coef_}
degree2loss_paras = []
for i in range(2, 20):
    paras = try_degree(i, X, y)
# ⾃⼰实现预测值的求解
h = np.dot(paras['X_poly'], paras['coef'].T) + paras['intercept']
_loss = mean_squared_error(h, y)
degree2loss_paras.append({'d': i, 'loss': _loss, 'coef': paras['coef'], 'intercept': paras['intercept']})
min_index = np.argmin(np.array([i['loss'] for i in degree2loss_paras]))
min_loss_para = degree2loss_paras[min_index]
print(min_loss_para)

X_plot = np.linspace(-3, 1.9, 1000).reshape(-1, 1)
poly_features_d = PolynomialFeatures(degree=min_loss_para['d'], include_bias=False)
X_plot_poly = poly_features_d.fit_transform(X_plot)
y_plot = np.dot(X_plot_poly, min_loss_para['coef'].T) + min_loss_para['intercept']
plt.plot(X_plot, y_plot, 'red', label="degree12")
plt.plot(X, y, 'b.', label="X")
plt.legend(loc='best')
plt.show()


 此时出现过拟合现象,鉴于该问题的普遍性和重要性,在满⾜要求的情况下,能选择简单模型时应该尽量选择简单的模型。

多项式回归是线性回归的一种扩展,它允许模型更复杂的关系,如曲线。在Python多项式回归可以通过`numpy`库或`scikit-learn`库来实现。下面是一个简单的多项式回归实现的示例代码。 首先,你需要安装`scikit-learn`库(如果尚未安装): ```bash pip install scikit-learn ``` 然后,你可以使用以下Python代码: ```python import numpy as np from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures from sklearn.pipeline import make_pipeline # 假设我们有一些数据点 # X是一个二维数组,每一行代表一个样本,每一列代表一个特征 # y是一个一维数组,包含每个样本的目标值 X = np.array([[1], [2], [3], [4], [5]]) y = np.array([1, 4, 9, 16, 25]) # 创建一个多项式回归模型,这里我们设置degree=2来实现二次多项式回归 degree = 2 model = make_pipeline(PolynomialFeatures(degree), LinearRegression()) # 训练模型 model.fit(X, y) # 现在模型已经训练好了,可以用来预测新的数据点 X_new = np.array([[6], [7]]) y_new = model.predict(X_new) print("预测的结果:", y_new) ``` 这段代码首先导入了必要的模块,然后创建了一些样本数据`X`和对应的目标值`y`。接下来,它创建了一个多项式回归模型,该模型通过`PolynomialFeatures`和`LinearRegression`组合而成,其`PolynomialFeatures(degree)`用于生成多项式特征,`LinearRegression`用于拟合线性模型。接着,使用`.fit()`方法训练模型,并使用训练好的模型进行预测。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值