码迷,mamicode.com
首页 > 其他好文 > 详细

多项式回归

时间:2017-03-12 10:49:21      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:reg   线性   images   var   border   play   atp   show   code   

撰写日期:2017-03-12

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

1、住房价格成本

样本 面积(平方米) 价格(万元)

样本 面积(平方米)  价格(万元)
1 50 150
2 100 200
3 150 250
4 200 280
5 250 310
6 300 330

 2、绘图

技术分享
 1 import sys
 2 reload(sys)
 3 sys.setdefaultencoding("utf-8")
 4 import matplotlib.pyplot as plt
 5 import numpy as np
 6 
 7 plt.figure()## 实例化作图变量
 8 plt.title("single variable")#图像标题
 9 plt.xlabel("x")
10 plt.ylabel("y")
11 plt.axis([30, 400, 100, 400])
12 plt.grid(True) # 是否绘制网格线
13 
14 xx = [[50],[100], [150], [200], [250], [300]]
15 yy = [[150], [200], [250], [280], [310], [330]]
16 plt.plot(xx, yy, k.)
17 plt.show()
View Code

技术分享

2.1 使用线性回归

技术分享
1 from sklearn.linear_model import LinearRegression
2 model = LinearRegression()
3 model.fit(xx, yy)
4 x2 = [[30], [400]]
5 y2 = model.predict(x2)
6 print(type(y2))
7 print(y2)
8 plt.plot(x2, y2, g-)
9 plt.show()
View Code

技术分享

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

2.1 使用多项式回归

 

多项式回归

标签:reg   线性   images   var   border   play   atp   show   code   

原文地址:http://www.cnblogs.com/yuzhuwei/p/6536913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!