机器学习读书笔记

3.1.2.1欠拟合与过拟合

2017-12-02  本文已影响21人  idatadesign

3.1.2模型正则化

任何机器学习模型在训练集上的性能表现,都不能作为其对未知测试数据预测能力的评估。3.1.2.1欠拟合与过拟合将首先阐述模型复杂度与泛化力的关系;紧接着,3.1.2.2L1范数正则化与3.1.2.3范数L2正则化将分别介绍将这两种正则化的方式来加强模型的泛化力,避免模型参数过拟合。

3.1.2.1欠拟合与过拟合

所谓拟合,是指机器学习模型在训练过程中,通过更新参数,使得模型不断契合可观测数据(训练集)的过程。

使用线性回归模型在比萨训练样本上进行拟合
#输出训练样本的特征以及目标值,分别存储在变量X_train与y_train
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)

import numpy as np
#从x轴上0至25均匀采样100个数据点,生成了1行100列的一个矩阵  
xx=np.linspace(0,26,100)
#将1行100列的矩阵转化成100行1列的矩阵形式 
xx=xx.reshape(xx.shape[0],1)
#以上上述100个数据点作为基准,预测回归直线。
yy=regressor.predict(xx)

#对回归预测到的直线进行作
import matplotlib.pyplot as plt
plt.scatter(X_train,y_train)

plt1,=plt.plot(xx,yy,label='Degree=1')

plt.axis([0,25,0,25])
plt.xlabel('Diameter of Pizza')
plt.ylabel('Price of Pizza')
plt.legend(handles=[plt1])
plt.show()

#输出线性回归模型在训练样本上的R-squared值
print('The R-squared value of Linear Regressor(Degree=1) performing on the training data is',regressor.score(X_train,y_train))

The R-squared value of Linear Regressor(Degree=1) performing on the training data is 0.910001596424


使用2次多项式回归模型在比萨训练样本上进行拟合
from sklearn.preprocessing import PolynomialFeatures
#使用PolynomialFeatures(degree=2)映射出2次多项式特征,存储在变量X_train_poly2中。
poly2=PolynomialFeatures(degree=2)
X_train_poly2=poly2.fit_transform(X_train)

#以线性回归器为基础,初始化回归模型。尽管特征的维度有提升,但是模型基础仍然是线性模型。
regressor_poly2=LinearRegression()

#对2次多项式回归模型进行训练
regressor_poly2.fit(X_train_poly2,y_train)

#从新映射绘图用x轴采样数据
xx_poly2=poly2.transform(xx)
#使用2次多项式回归模型进行训练对应x轴采样数据进行回归预测
yy_ploy2=regressor_poly2.predict(xx_poly2)
#分别对训练数据点、线性回归直线、2次多项式回归曲线进行作图
plt.scatter(X_train,y_train)

plt1,=plt.plot(xx,yy,label='Degree=1')
plt2,=plt.plot(xx,yy_poly2,label='Degree=2')

plt.axis([0,25,0,25])
plt.xlabel('Diameter of Pizza')
plt.ylabel('Price of Pizza')
plt.legend(handles=[plt1])
plt.show()

#输出线性回归模型在训练样本上的R-squared值
print('The R-squared value of Linear Regressor(Degree=2) performing on the training data is',regressor_poly2.score(X_train_poly2,y_train))

The R-squared value of Linear Regressor(Degree=2) performing on the training data is 0.98164216396

果然,在升高了特征维度之后,2次多项式回归模型在训练样本上的性能更加突出,R-squared值从0.910上升到0.982。2次多项式回归曲线(绿色)比起线性回归曲线(蓝色),对训练数据的拟合程度也增加了许多。

使用4次多项式回归模型在比萨训练样本上进行拟合
from sklearn.preprocessing import PolynomialFeatures
#初始化4次多项式特征生成器
poly4=PolynomialFeatures(degree=4)
X_train_poly4=poly4.fit_transform(X_train)

#使用默认配置初始化4次多项式回归模型
regressor_poly4=LinearRegression()
#对4次多项式回归模型进行训练
regressor_poly4.fit(X_train_poly4,y_train)

#从新映射绘图用x轴采样数据
xx_poly4=poly4.transform(xx)
#使用4次多项式回归模型对应x轴采样数据进行回归预测
yy_poly4=regressor_poly4.predict(xx_poly4)

#分别对训练数据点、线性回归直线、2次多项式以及4次多项式回归曲线进行作图
plt.scatter(X_train,y_train)
plt1,=plt.plot(xx,yy,label='Degree=1')
plt2,=plt.plot(xx,yy_poly2,label='Degree=2')

poly4,=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])
plt.show()

print('The R-squared value of Linear Regressor(Degree=4) performing on the training data is',regressor_poly4.score(X_train_poly4,y_train))

The R-squared value of Linear Regressor(Degree=4) performing on the training data is 1.0

评估3种回归模型在测试集上的性能表现
X_test=[[6],[8],[11],[16]]
y_test=[[8],[12],[15],[18]]

#使用测试数据对线性回归模型的性能进行评估
regressor.score(X_test,y_test)

0.80972679770766498

#使用测试数据对2次多项式回归模型的性能进行评估
X_test_poly2=poly2.transform(X_test)
regressor_poly2.score(X_test_poly2,y_test)

0.86754436563450732

#使用测试数据对4次多项式回归模型的性能进行评估
X_test_poly4=poly4.transform(X_test)
regressor_poly4.score(X_test_poly4,y_test)

0.8095880795781909

评估3种模型在测试集上的表现,并将输出对比之前在训练数据的拟合情况,最终的结果却令人咋舌:当模型复杂度很低(Degree=1)时,模型不仅没有对训练集上的数据有良好的拟合状态,而且在测试集上也表现平平,这种情况叫欠拟合;但是,当我们一味追求很高的模型复杂度(Degree=4),尽管模型几乎完全拟合了所有的训练数据,但是模型也变得非常波动,几乎丧失了对未知数据的预测能力,这种情况叫做过拟合。这两种情况都是缺乏模型泛化力的表现。

上一篇 下一篇

猜你喜欢

热点阅读