简单的回归与预测 & matplotlib坐标轴设置参数(刻度值、间隔)
2018-07-26 20:41
776 查看
版权声明:本文为博主学习笔记,欢迎讨论 https://blog.csdn.net/JackSparrow_sjl/article/details/81226971
《机器学习实战》第八章--机器学习分析西湖房价
8.2节讲到了局部加权线性回归,由于我们这里只有一个特征,因此输入X矩阵只有偏置常数(x0=1)和x1
楼主闲来无事,从网上down近几年西湖区的房价变化,结合课程中学到的算法,做了一些分析
[code]# -*- coding: utf-8 -*- """ Created on Wed Jul 25 19:20:59 2018 @author: sjl """ import matplotlib.pyplot as plt import numpy as np def loadDataSet(fileName): #general function to parse tab -delimited floats dataMat = [] fr = open(fileName) for line in fr.readlines(): curLine = line.split() dataMat.append(int(curLine[1])) return dataMat def lwlr(testPoint,xArr,yArr,k=1.0): xMat = mat(xArr); yMat = mat(yArr).T m = shape(xMat)[0] weights = mat(eye((m))) for j in range(m): #next 2 lines create weights matrix diffMat = testPoint - xMat[j,:] # weights[j,j] = exp(diffMat*diffMat.T/(-2.0*k**2)) xTx = xMat.T * (weights * xMat) if linalg.det(xTx) == 0.0: print "This matrix is singular, cannot do inverse" return ws = xTx.I * (xMat.T * (weights * yMat)) return testPoint * ws def lwlrTest(testArr,xArr,yArr,k=1.0): #loops over all the data points and applies lwlr to each one m = shape(testArr)[0] yHat = zeros(m) for i in range(m): yHat[i] = lwlr(testArr[i],xArr,yArr,k) return yHat def made_mat(unit_0,unit_1,unit_2): dataMat=[] delta = unit_2-unit_1+1 for i in range(delta): lineArr = [] lineArr.append(float(unit_0)) lineArr.append(unit_1+delta-i) dataMat.append(lineArr) return dataMat xArr = made_mat(1.00000,1,115) yArr=loadDataSet('house.txt') yHat = lwlrTest(xArr,xArr,yArr,3) xMat = mat(xArr) srtInd =xMat[:,1].argsort(0) #argsort返回从小到大排序后对应的索引值 xSort = xMat[srtInd][:,0,:] xAdd = made_mat(1.00000,116,125) yHatAdd = lwlrTest(xAdd,xArr,yArr,3) xMatAdd = mat(xAdd) srtIndAdd =xMatAdd[:,1].argsort(0) #argsort返回从小到大排序后对应的索引值 xSortAdd = xMatAdd[srtIndAdd][:,0,:] fig = plt.figure() ax = fig.add_subplot(111) ax.plot(xSort[:,1],yHat[srtInd],color='blue',linewidth=3.0,linestyle='-') ax.plot(xSortAdd[:,1],yHatAdd[srtIndAdd],color='red',linewidth=3.0,linestyle='-') ax.scatter(xMat[:,1].flatten().A[0],mat(yArr).T.flatten().A[0],s=2,c='red') ax.axis([0,144,12000,55000]) plt.xlabel('Month(since 2008)') plt.ylabel('Price') year = [2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019] plt.xticks(np.arange(0,144,12),year) plt.title('House Price in West Lake, Hangzhou') plt.show()
house.txt内容如下:
[code]2018年07月房价 38927 元/㎡5.72%↑ 2018年06月房价 36822 元/㎡1.07%↑ 2018年05月房价 36433 元/㎡-- 2018年04月房价 35918 元/㎡0.42%↑ 2018年03月房价 35770 元/㎡-- 2018年02月房价 35551 元/㎡0.01%↓ 2018年01月房价 35554 元/㎡0.02%↑ 2017年12月房价 35549 元/㎡-- 2017年11月房价 35278 元/㎡1.9%↑ 2017年10月房价 34621 元/㎡0.05%↑ 2017年09月房价 34023 元/㎡2.06%↑ 2017年08月房价 33335 元/㎡0.79%↑ 2017年07月房价 33073 元/㎡0.05%↑ 2017年06月房价 32412 元/㎡0.49%↓ 2017年05月房价 32571 元/㎡-- 2017年04月房价 31843 元/㎡2.15%↑ 2017年03月房价 31173 元/㎡0.15%↑ 2017年02月房价 30075 元/㎡1.01%↑ 2017年01月房价 29774 元/㎡10.39%↑ 2016年12月房价 26972 元/㎡0.64%↓ 2016年11月房价 27147 元/㎡5.41%↑ 2016年10月房价 25753 元/㎡6.65%↑ 2016年09月房价 24147 元/㎡1.74%↑ 2016年08月房价 23734 元/㎡0.94%↑ 2016年07月房价 23513 元/㎡0.05%↑ 2016年06月房价 23501 元/㎡2.04%↑ 2016年05月房价 23031 元/㎡0.86%↓ 2016年04月房价 23231 元/㎡0.17%↓ 2016年03月房价 23272 元/㎡2.25%↑ 2016年02月房价 22760 元/㎡1.64%↑ 2016年01月房价 22394 元/㎡0.02%↑ 2015年12月房价 22389 元/㎡1%↓ 2015年11月房价 22559 元/㎡2%↓ 2015年10月房价 23057 元/㎡-- 2015年09月房价 22964 元/㎡1%↑ 2015年08月房价 22696 元/㎡1%↓ 2015年07月房价 22831 元/㎡-- 2015年06月房价 22881 元/㎡-- 2015年05月房价 22861 元/㎡3%↑ 2015年04月房价 22182 元/㎡-- 2015年03月房价 22162 元/㎡1%↓ 2015年02月房价 22305 元/㎡-- 2015年01月房价 22245 元/㎡1%↓ 2014年12月房价 22471 元/㎡0.33%↓ 2014年11月房价 22546 元/㎡0.03%↑ 2014年10月房价 22539 元/㎡2.11%↓ 2014年09月房价 23025 元/㎡0.55%↑ 2014年08月房价 22898 元/㎡0.07%↓ 2014年07月房价 22913 元/㎡1.98%↓ 2014年06月房价 23376 元/㎡0.71%↓ 2014年05月房价 23544 元/㎡2.34%↓ 2014年04月房价 24109 元/㎡1.33%↓ 2014年03月房价 24434 元/㎡0.03%↓ 2014年02月房价 24442 元/㎡0.22%↓ 2014年01月房价 24495 元/㎡1.24%↓ 2013年12月房价 24802 元/㎡0.14%↓ 2013年11月房价 24836 元/㎡1.52%↓ 2013年10月房价 25220 元/㎡0.99%↑ 2013年09月房价 24972 元/㎡0.24%↑ 2013年08月房价 24912 元/㎡1.1%↑ 2013年07月房价 24640 元/㎡0.28%↑ 2013年06月房价 24572 元/㎡0.16%↓ 2013年05月房价 24612 元/㎡1.02%↓ 2013年04月房价 24865 元/㎡0.74%↑ 2013年03月房价 24683 元/㎡1.27%↑ 2013年02月房价 24374 元/㎡0.15%↓ 2013年01月房价 24410 元/㎡1.69%↑ 2012年12月房价 24004 元/㎡1.5%↑ 2012年11月房价 23650 元/㎡0.31%↓ 2012年10月房价 23724 元/㎡0.22%↓ 2012年09月房价 23777 元/㎡0.63%↑ 2012年08月房价 23627 元/㎡0.1%↓ 2012年07月房价 23650 元/㎡2.32%↑ 2012年06月房价 23113 元/㎡1.31%↑ 2012年05月房价 22814 元/㎡1.06%↓ 2012年04月房价 23059 元/㎡0.64%↓ 2012年03月房价 23208 元/㎡2%↑ 2012年02月房价 22754 元/㎡1.63%↓ 2012年01月房价 23132 元/㎡ 2011年12月房价 23854 元/㎡3.22%↓ 2011年11月房价 24647 元/㎡3.34%↓ 2011年10月房价 25498 元/㎡2.09%↓ 2011年09月房价 26042 元/㎡0.73%↑ 2011年08月房价 25852 元/㎡0.21%↓ 2011年07月房价 25906 元/㎡0.46%↑ 2011年06月房价 25788 元/㎡0.14%↓ 2011年05月房价 25825 元/㎡0.21%↓ 2011年04月房价 25880 元/㎡0.17%↑ 2011年03月房价 25835 元/㎡3.36%↑ 2011年02月房价 24994 元/㎡2.05%↑ 2011年01月房价 24491 元/㎡ 2010年12月房价 24276 元/㎡0.6%↑ 2010年11月房价 24132 元/㎡0.73%↑ 2010年10月房价 23957 元/㎡1.35%↑ 2010年09月房价 23639 元/㎡1.44%↑ 2010年08月房价 23304 元/㎡1.07%↓ 2010年07月房价 23555 元/㎡2.21%↓ 2010年06月房价 24088 元/㎡0.25%↑ 2010年05月房价 24027 元/㎡3.34%↑ 2010年04月房价 23251 元/㎡9.03%↑ 2010年03月房价 21325 元/㎡1.53%↑ 2010年02月房价 21004 元/㎡0.58%↑ 2010年01月房价 20882 元/㎡2.87%↑ 2009年12月房价 20300 元/㎡5.59%↑ 2009年11月房价 19225 元/㎡7.68%↑ 2009年10月房价 17854 元/㎡4.41%↑ 2009年09月房价 17100 元/㎡5.62%↑ 2009年08月房价 16190 元/㎡4.73%↑ 2009年07月房价 15459 元/㎡7.9%↑ 2009年06月房价 14327 元/㎡4.41%↑ 2009年05月房价 13722 元/㎡3.66%↑ 2009年04月房价 13238 元/㎡2.21%↑ 2009年03月房价 12952 元/㎡0.66%↓ 2009年02月房价 13038 元/㎡1.89%↓ 2009年01月房价 13289 元/㎡
结果如下:
代码中k是平滑参数,过小会过拟合,过大会欠拟合;
事实上,因为杭州房价因为G20过后疯涨,这次预测并没有实际意义,因为样本特征有80%是无关的,这告诉我们,数据集的选择要正确。
关于matplotlib的一些tips,看备注
[code]fig = plt.figure() #图片对象 ax = fig.add_subplot(111) ax.plot(xSort[:,1],yHat[srtInd],color='blue',linewidth=3.0,linestyle='-') #设置线1参数 ax.plot(xSortAdd[:,1],yHatAdd[srtIndAdd],color='red',linewidth=3.0,linestyle='-') ax.scatter(xMat[:,1].flatten().A[0],mat(yArr).T.flatten().A[0],s=2,c='red') #画散点 ax.axis([0,144,12000,55000]) #画轴的范围 plt.xlabel('Month(since 2008)') #x轴标签 plt.ylabel('Price') #y轴标签 year = [2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019] plt.xticks(np.arange(0,144,12),year) #重新设置x轴间隔和刻度值 plt.title('House Price in West Lake, Hangzhou') #标题 plt.show() #画图
阅读更多
相关文章推荐
- 使用matplotlib的示例:调整字体-设置刻度、坐标、colormap和colorbar等
- 使用matplotlib的示例:调整字体-设置刻度、坐标、colormap和colorbar等
- matplotlib命令与格式:tick坐标轴日期格式(设置日期主副刻度)
- 演示MatPlotLib中如何设置坐标轴主刻度标签和次刻度标签
- Python利用matplotlib.pyplot绘图时如何设置坐标轴刻度
- python matplotlib绘图设置坐标轴刻度、文本
- 学习python中matplotlib绘图设置坐标轴刻度、文本
- matplotlib命令与格式:tick坐标轴主副刻度设置
- matplotlib(刻度,刻度线长短位置相关设置)
- Matplotlib 绘图双纵坐标轴设置及控制设置时间格式
- 【Matplotlib】 刻度设置(2)
- python matplotlib绘图,修改坐标轴刻度为文字的实例
- python matplotlib 中文显示参数设置
- python matplotlib 画图刻度、图例等字体、字体大小、刻度密度、线条样式设置
- python matplotlib 中文显示参数设置
- matplotlib坐标轴设置-【老鱼学matplotlib】
- 【Matplotlib】线设置,坐标显示范围
- 【原】在Matplotlib绘图过程中设置X轴的刻度和显示文本
- python matplotlib绘图,修改坐标轴刻度为文字
- matplotlib--设置坐标轴