python 预测 位置_Python:核岭回归预测,KRR
1 #-*- coding: utf-8 -*-
2 """
3 Created on Wed Oct 17 21:14:44 20184
5 @author: Luove6 """
7 #KRR适合分类和回归训练集很少时,非线性方法
8 importos9 importnumpy as np10 importmatplotlib.pyplot as plt11 import dateutil.parser as dparser #dateutil模块主要有两个函数,parser和rrule。parser是根据字符串解析成datetime,而rrule是则是根据定义的规则来生成datetime;https://blog.csdn.net/cherdw/article/details/55224341
12 from pylab import * #将matplotlib和numpy封装在一起,模拟MATLAB编程环境
13 from sklearn.cross_validation importtrain_test_split14 from sklearn importlinear_model15 from sklearn importdatasets16 importmlpy17 from mlpy importKernelRidge18
19 #np.hamming 汉明窗,构造一个函数(仅处理窗内数据)。这个函数在某一区间有非零值,而在其余区间皆为0.汉明窗就是这样的一种函数
20 #阶梯图,又叫瀑布图,可以用于企业成本、销售等数据的变化和构成情况的分析;plot.step()
21 x1 = np.linspace(1,100,500)22 x2 = np.linspace(1,100,50)23 y1 =np.cos(x1)24 y2 =np.cos(x2)25
26 axs1 = plt.subplot(211)27 axs2 = plt.subplot(212)28 axs1.step(x1,y1)29 axs2.step(x2,y2)30 plt.show()31
32
33 goldfile = "D:\Analyze\Python Matlab\Python\BookCodes\PDA_Book-master\PDA_Book-master\Chapter7\Gold.csv"
34 #tsa,时间序列分析,将时间序列平滑化,(本身包含:趋势T,季节性/周期性S,波动性V)
35 defsmooth(x,window_length):36 s = np.r_[2*x[0]-x[window_length-1::-1], x, 2*x[-1]-x[-1:-window_length:-1]]37 w =np.hamming(window_length)38 y = np.convolve(w/w.sum(), s, mode='same') #卷积函数,移动平均滤波(平滑方法),第一个参数长度要大于等于第二参数长度,否则会交换位置;mode={'full','same','valid'},默认full
39 return y[window_length:-window_length+1]40
41 #金价走势,注意下面dtype变化:日期用object,值用None(各列内容识别,)
42 x = np.genfromtxt(goldfile,dtype='object',delimiter=',',skip_header=1,usecols=(0),converters={0:dparser.parse}) #第一列日期,dateutil.parser.parse,字符串中解析出日期
43 y = np.genfromtxt(goldfile,dtype=None,delimiter=',',skip_header=1,usecols=(1)) #获取第二列
44 y_smoothed =smooth(y,len(y))45 plt.step(x,y,'r*',label='raw data')46 plt.step(x,y_smoothed,label='smoothed data')47 plt.legend()48 #x = [2,3,9,634,32,4,676,4,234,43,7,-13,0]
49 #x = np.array(x)
50 #np.round(smooth(x,len(x)))
51 #[ 33., 80., 124., 165., 189., 199., 192., 169., 137., 104., 66., 35., 16.]
52 #plt.plot(x)
53 #plt.plot(np.round(smooth(x,len(x)))) # 加载pylab,不必plt.show()?
54 ##plt.show()
55 #window_length=x.shape[0]
56
57 house =datasets.load_boston()58 houseX = house.data[:,np.newaxis] #添加一个新轴,添加一维度,由(506, 13)转成(506, 1,13)
59 houseX_temp = houseX[:,:,2]60
61 x_train,xtest,ytrain,ytest=train_test_split(houseX_temp,house.target,test_size=1.0/3)62 lreg =linear_model.LinearRegression()63 lreg.fit(x_train,ytrain)64 plt.scatter(xtest,ytest,color='green')65 plt.plot(xtest,lreg.predict(xtest),color='blue',linewidth=2)66
67 np.random.seed(0)68 targetvalues = np.genfromtxt(goldfile,skip_header=1,dtype=None,delimiter=',',usecols=(1)) #usecols筛选感兴趣列
69 type(targetvalues)70 trainingpoints = np.arange(125).reshape(-1,1) #transform ,转换成一列,行自适应
71 testpoint = np.arange(126).reshape(-1,1)72 knl = mlpy.kernel_gaussian(trainingpoints,trainingpoints,sigma=1) #训练核矩阵,对称半正定,(125, 125)
73 knltest = mlpy.kernel_gaussian(testpoint,trainingpoints,sigma=1) #测试核矩阵,(126, 125)
74
75 knlridge = KernelRidge(lmb=0.01)76 knlridge.learn(knl,targetvalues)77 resultpoints =knlridge.pred(knltest)78
79 fig = plt.figure(1)80 plt.plot(trainingpoints,targetvalues,'o')81 plt.plot(testpoint,resultpoints)82 #plt.show()
83 len(resultpoints)84 resultpoints[-5:-1]85
86 #采用平滑后的数据,即smooth后的targetvalues
87 targetvalues_smoothed =smooth(targetvalues,len(targetvalues))88 knlridge.learn(knl,targetvalues_smoothed)89 resultpoints_smoothed =knlridge.pred(knltest)90 plt.step(trainingpoints,targetvalues_smoothed,'o')91 plt.step(testpoint,resultpoints_smoothed)92 #plt.show()
93 len(resultpoints_smoothed)94 resultpoints_smoothed[-5:-1] #平滑前126期预测值:1389.8;平滑后126期预测值1388.6
95 #x = np.arange(0, 2, 0.05).reshape(-1, 1) # training points
96 #y = np.ravel(np.exp(x)) + np.random.normal(1, 0.2, x.shape[0]) # target values
97 #xt = np.arange(0, 2, 0.01).reshape(-1, 1) # testing points
98 #K = mlpy.kernel_gaussian(x, x, sigma=1) # training kernel matrix
99 #Kt = mlpy.kernel_gaussian(xt, x, sigma=1) # testing kernel matrix
100 #krr = KernelRidge(lmb=0.01)
101 #krr.learn(K, y)
102 #yt = krr.pred(Kt)
103 #fig = plt.figure(1)
104 #plot1 = plt.plot(x[:, 0], y, 'o')
105 #plot2 = plt.plot(xt[:, 0], yt)
106 #plt.show()
python 预测 位置_Python:核岭回归预测,KRR相关推荐
- python预测股票价格_python用线性回归预测股票价格
原标题:python用线性回归预测股票价格 线性回归在整个财务中广泛应用于众多应用程序中.在之前的教程中,我们使用普通最小二乘法(OLS)计算了公司的beta与相对索引的比较.现在,我们将使用线性回归 ...
- python图例位置_Python | 图例位置
python图例位置 Legends are one of the key components of data visualization and plotting. Matplotlib can ...
- python股票预测代码_python用线性回归预测股票价格的实现代码
线性回归在整个财务中广泛应用于众多应用程序中.在之前的教程中,我们使用普通最小二乘法(OLS)计算了公司的beta与相对索引的比较.现在,我们将使用线性回归来估计股票价格. 线性回归是一种用于模拟因变 ...
- 如何查看python解释器位置_Python:查看解释器的位置
以前学Python时,有时出现这样的情况:明明记得装了scipy包,怎么打import scipy报错说我没这个包? 问题出在,你的电脑上安装了不止一个Python... 而每安装一个包,仅仅在这个P ...
- python函数调用位置_python函数定义,调用,传参,位置参数及关键字参数,返回值
使用函数是真正开始编程的第一步,函数y=f(x)我们并不陌生,对x进行一顿操作得到一个值y.给不同的x,进行相同的操作,得到相应的y值. 程序层面函数是执行特定任务的一段代码,将一段代码定义成函数并为 ...
- python函数调用位置_Python: 浅谈函数局部变量快在哪
前言 这两天在 CodeReview 时,看到这样的代码 # 伪代码 import somelib class A(object): def load_project(self): self.proj ...
- python 预测算法_Python 与金融数据使用机器学习算法预测交易策略
记得 关注.分享.点在看 呀- 这样您就能持续收到优质的推送啦 这一期,我们将使用上一期处理好的数据特征和标签训练机器,然后预测交易策略.我们将分别使用 K近邻算法和集成学习两种方法分别完成模型的训练 ...
- python预测发展趋势_Python预测算法哪家强?权游龙妈是生还是凉凉?
这个世界上只有两种人,看「权游」(权利的游戏)的,和不看「权游」的. 你们心心念的权游终于迎来了最终季,狼家史塔克的家训「Winter is coming」终终终终终于是应验了,人类与异鬼的战争一触即 ...
- python 分位数 位置_Python解释数学系列——分位数Quantile
1. 分位数计算案例与Python代码 案例1 Ex1: Given a data = [6, 47, 49, 15, 42, 41, 7, 39, 43, 40, 36],求Q1, Q2, Q3, ...
最新文章
- 【组队学习】【32期】scikit-learn教程
- (Incomplete) UVa 719 Glass Beads
- 你在用什么思想编码:事务脚本 OR 面向对象?
- 【新插件发布】AzureAD运维Excel版插件,增删改查快10倍c以上!
- 经济学自身利益最大化_劳动经济学:研究劳动力市场运作的专业
- LeetCode LCP 33. 蓄水(暴力枚举)
- 服务器linux启动,Linux 服务器环境启动
- 数据工作-百度统计初体验
- LayaAir UI 组件 # Image 位图、Label 标签
- 可曾听闻【大话】二字
- 从csrss弹出的ASSERT对话框谈起
- VulnHub靶场-Tiki
- 第五届四川省职工职业技能大赛决赛计算机程序设计员技术文件大纲
- img src 无法显示图片问题
- excel熵值法计算权重_熵值法的Excel基本步骤
- 【微信小程序】2、SpringBoot整合WxJava接入微信客服
- WebGL入门之基于WebGL的3D可视化引擎介绍
- 千锋逆战班学习第二十三天 集合练习(一)
- 系统分析师教程——目录
- 如何用Typora记笔记? | 附带Markdown基础教程
热门文章
- Mysql 主从复制线程的等待时间问题
- jeecg中的树形控件demo
- Ubuntu 下无法Tab键自动补全功能解决办法
- java dev guide
- linux 窗口不能移动的替换命令
- firefox浏览器优化-速度超chrome
- [转载]如何做一个出色的程序员
- file_get_contents js没有渲染数据_浏览器渲染页面那些事
- linux diff 远程文件,登录diff命令,以单独的文件输出在linux
- 红安一中高考2021成绩查询,红安一中2019高考喜报成绩、一本二本上线人数情况...