分享

机器学习教程 六-用scikit-learn求解多项式回归问题

fc013 发表于 2016-7-3 10:58:02 [显示全部楼层] 只看大图 回帖奖励 阅读模式 关闭右栏 1 28719


问题导读:
1.怎样利用scikit-learn解决多项式回归问题?
2.python怎样做线性回归?
3.python怎样做多项式回归?



thumb_136_default_big.jpeg
多元真实情况未必是线性的,有时需要增加指数项,也就是多项式回归,现实世界的曲线关系都是通过增加多项式实现的,本节介绍用scikit-learn解决多项式回归问题

住房价格样本

样本面积(平方米) 价格(万元)
150150
2100200
3150250
4200280
5250310
6300330

做图像

[mw_shl_code=python,true]# coding:utf-8

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

import matplotlib.pyplot as plt
import numpy as np

plt.figure() # 实例化作图变量
plt.title('single variable') # 图像标题
plt.xlabel('x') # x轴文本
plt.ylabel('y') # y轴文本
plt.axis([30, 400, 100, 400])
plt.grid(True) # 是否绘制网格线

xx = [[50],[100],[150],[200],[250],[300]]
yy = [[150],[200],[250],[280],[310],[330]]
plt.plot(xx, yy, 'k.')
plt.show() # 展示图像[/mw_shl_code]

06bb15f689c21e7b1bbd25d6832cd4ab6f963947.png

用线性回归
在上述代码中加上

[mw_shl_code=python,true]model = LinearRegression()
model.fit(xx, yy)
x2 = [[30], [400]]
y2 = model.predict(x2)
plt.plot(x2, y2, 'g-')[/mw_shl_code]

得到回归图像:

8593d143cdeb54f7c44dba20e601b597acc13ed7.png

但是实际情况是,如果房屋面积一味的增加,房价并不会线性增长,因此线性关系已经无法描述真实的房价问题

采用多项式回归
首先我们用二次多项式

[mw_shl_code=python,true]# coding:utf-8

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

plt.figure() # 实例化作图变量
plt.title('single variable') # 图像标题
plt.xlabel('x') # x轴文本
plt.ylabel('y') # y轴文本
plt.axis([30, 400, 100, 400])
plt.grid(True) # 是否绘制网格线

X = [[50],[100],[150],[200],[250],[300]]
y = [[150],[200],[250],[280],[310],[330]]
X_test = [[250],[300]] # 用来做最终效果测试
y_test = [[310],[330]] # 用来做最终效果测试
plt.plot(X, y, 'k.')

model = LinearRegression()
model.fit(X, y)
X2 = [[30], [400]]
y2 = model.predict(X2)
plt.plot(X2, y2, 'g-')

xx = np.linspace(30, 400, 100) # 设计x轴一系列点作为画图的x点集
quadratic_featurizer = PolynomialFeatures(degree=2) # 实例化一个二次多项式特征实例
X_train_quadratic = quadratic_featurizer.fit_transform(X) # 用二次多项式对样本X值做变换
xx_quadratic = quadratic_featurizer.transform(xx.reshape(xx.shape[0], 1)) # 把训练好X值的多项式特征实例应用到一系列点上,形成矩阵
regressor_quadratic = LinearRegression() # 创建一个线性回归实例
regressor_quadratic.fit(X_train_quadratic, y) # 以多项式变换后的x值为输入,代入线性回归模型做训练
plt.plot(xx, regressor_quadratic.predict(xx_quadratic), 'r-') # 用训练好的模型作图

print '一元线性回归 r-squared', model.score(X_test, y_test)
X_test_quadratic = quadratic_featurizer.transform(X_test)
print '二次回归     r-squared', regressor_quadratic.score(X_test_quadratic, y_test)

plt.show() # 展示图像[/mw_shl_code]

作图如下:

f6f64375ff641a2d17ce3332d0e4f211fdc0be39.png

红色为二次多项式回归图像,可以看到比线性模型吻合度高,输出的R方结果为:

[mw_shl_code=text,true]一元线性回归 r-squared 0.0755555555556
二次回归     r-squared 0.999336734694[/mw_shl_code]

可以看到二次回归效果更好

我们继续尝试一下三次回归

增加如下代码:

[mw_shl_code=python,true]cubic_featurizer = PolynomialFeatures(degree=3)
X_train_cubic = cubic_featurizer.fit_transform(X)
regressor_cubic = LinearRegression()
regressor_cubic.fit(X_train_cubic, y)
xx_cubic = cubic_featurizer.transform(xx.reshape(xx.shape[0], 1))
plt.plot(xx, regressor_cubic.predict(xx_cubic))

X_test_cubic = cubic_featurizer.transform(X_test)
print '三次回归     r-squared', regressor_cubic.score(X_test_cubic, y_test)[/mw_shl_code]

图像如下:

ed631453a514ce152ab0c1c01075b4b06dab6f9b.png

R方输出如下:

[mw_shl_code=text,true]一元线性回归 r-squared 0.0755555555556
二次回归     r-squared 0.999336734694
三次回归     r-squared 0.999464600659[/mw_shl_code]

可以看到三次回归比二次回归效果又好了一些,但是不是很明显。所以二次回归更可能是最适合的回归模型,三次回归可能有过拟合现象




相关文章

机器学习教程 一-不懂这些线性代数知识 别说你是搞机器学习的
http://www.aboutyun.com/forum.php?mod=viewthread&tid=18997



机器学习教程 二-安装octave绘制3D函数图像
http://www.aboutyun.com/thread-19006-1-1.html


机器学习教程 三-用scikit-learn求解一元线性回归问题
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19020


机器学习教程 四-用scikit-learn求解多元线性回归问题
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19042


机器学习教程 五-用matplotlib绘制精美的图表
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19060


机器学习教程 六-用scikit-learn求解多项式回归问题
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19073


机器学习教程 七-用随机梯度下降法(SGD)做线性拟合
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19086


机器学习教程 八-用scikit-learn做特征提取
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19095


机器学习教程 九-二元分类效果的评估方法
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19107


机器学习教程十-用scikit-learn的网格搜索快速找到最优模型参数
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19120


机器学习教程 十一-用scikit-learn做聚类分析大数据
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19129


机器学习教程 十二-神经网络模型的原理 大数据
http://www.aboutyun.com/forum.php?mod=viewthread&tid=19339



已有(1)人评论

跳转到指定楼层
szcountryboy 发表于 2016-7-29 09:25:26
320、370平米的预测价格是多少?

看完内容后,结论是什么呢?

是展示学习多项回归的过程,还是通过多项回归处理了预测结果的问题?

谢谢

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条