规则化

多项式回归分析
#输入训练值和目标值
X_train = [[6],[8],[10],[14],[18]]
y_train = [[7],[9],[13],[17.5],[18]]
#导入线性分类器
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
print('The R-squared value of Linear Regressor on the training data is:',regressor.score(X_train, y_train))
#训练二次型模型,导入多项式特征产生器
from sklearn.preprocessing import PolynomialFeatures
poly2 = PolynomialFeatures(degree=2)
X_train_poly2 = poly2.fit_transform(X_train)
#尽管特征的维度有提升,但是模型基础仍然是线性模型
regressor_poly2 = LinearRegression()
regressor_poly2.fit(X_train_poly2, y_train)
print('The R-squared value of Linear Regressor on the training data is:',
      regressor_poly2.score(X_train_poly2, y_train))
#初始化4次多项式特征生成器
poly4 = PolynomialFeatures(degree=4)
X_train_poly4 = poly4.fit_transform(X_train)
regressor_poly4 = LinearRegression()
regressor_poly4.fit(X_train_poly4, y_train)
print('The R-squared value of Linear Regressor on the training data is:',
      regressor_poly4.score(X_train_poly4, y_train))
#导入numpy
import numpy as np
xx = np.linspace(0, 26, 100)
xx = xx.reshape(xx.shape[0], 1)
yy = regressor.predict(xx)
#从新映射绘图用x轴采集数据
xx_poly2 = poly2.transform(xx)
yy_poly2 = regressor_poly2.predict(xx_poly2)
xx_poly4 = poly4.transform(xx)
yy_poly4 = regressor_poly4.predict(xx_poly4)
#对回归模型进行绘图
import matplotlib.pyplot as plt
plt.scatter(X_train, y_train)
plt1,=plt.plot(xx, yy, label = 'Degree = 1')
plt2,=plt.plot(xx, yy_poly2, label = 'Degree = 2')
plt4,=plt.plot(xx, yy_poly4, label = 'Degree = 4')
plt.axis([0,25,0,25])
plt.xlabel('Diameter of Pizza')
plt.ylabel('Price of Pizza')
plt.legend(handles=[plt1, plt2, plt4], loc = 'lower right')
plt.show()

#评估3种回归模型在测试集上的性能表现
#准备数据
X_test = [[6],[8],[11],[16]]
y_test = [[8],[12],[15],[18]]
#使用测试数据对线性回归模型的性能进行评估
print(regressor.score(X_test, y_test))
#使用测试数据对2次多项式回归模型的性能进行评估
X_test_poly2 = poly2.transform(X_test)
print(regressor_poly2.score(X_test_poly2, y_test))
#对4次多项式回归模型的性能进行评估
X_test_poly4 = poly4.transform(X_test)
print(regressor_poly4.score(X_test_poly4, y_test))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值