时间序列分析 lstm_LSTM —时间序列分析

时间序列分析 lstm

Neural networks can be a hard concept to wrap your head around. I think this is mostly due to the fact that they can be used for so many different things such as classification, identification or just simply regression.

神经网络可能是一个难以理解的概念。 我认为这主要是由于它们可以用于许多不同的事情,例如分类,识别或仅用于回归。

In this article, we will look at how easy it is to set up a simple LSTM model. All you need is your helpful friend KERAS and some array of numbers to throw into it.

在本文中,我们将探讨建立一个简单的LSTM模型有多么容易。 您所需要的只是您乐于助人的朋友KERAS和一些数字。

First thing we always do? Import!

我们总是做的第一件事? 进口!

import math
import pandas as pd
import numpy as np#keras models
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM#scaler
from sklearn.preprocessing import MinMaxScaler#analysis tool
from sklearn.metrics import mean_squared_error

Next we need some set of numbers to play with. There are a couple ways you can go about doing this, but the best way is to use some data that actually has meaning and to do that I recommend going to Kaggle.

接下来,我们需要一些数字来处理。 您可以通过几种方法来执行此操作,但是最好的方法是使用一些确实有意义的数据,并且建议我转到Kaggle。

Once you have your .csv file downloaded we need to put it into a dataframe and only take the feature that has all the values we want to play with. Mine is the first column.

下载完.csv文件后,我们需要将其放入数据框,仅使用具有我们要使用的所有值的功能。 我的是第一列。

df = pd.read_csv('test_df_w_timeshift.csv', usecols=[1])
dataset = df.values
#normalize dataset
scaler = MinMaxScaler(feature_range=(0,1))
dataset = scaler.fit_transform(dataset)

We pull the values from the file and normalize them using the MinMaxScaler from sklearn.

我们从文件中提取值,并使用sklearn的MinMaxScaler将其标准化。

The next thing to do is to separate the data into two groups, the first is a set a training data for our LSTM model to learn from. I like to use about eighty percent of the data to train to, however you can play with this number to see how much of the data is actually needed to train with before the model gives you a satisfactory result.

接下来要做的是将数据分为两组,第一组是一组训练数据,供我们的LSTM模型学习。 我喜欢使用大约80%的数据进行训练,但是您可以使用该数字来查看在模型给您满意的结果之前实际需要训练多少数据。

#split into train and test sets
train_size = int(len(dataset) * 0.80)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]

The next thing we do to the data is create a secondary feature, if you will, from that data that is basically how far back we wish to look to see how much the current value has changed from the value before, lets say, three values ago.

我们接下来要对数据进行的操作是创建一个辅助功能(如果可以的话),从该数据开始,基本上是我们想回溯的距离,以查看当前值与之前(假设)三个值相比有多少变化。前。

This create_dataset method was written by Jason Brownlee, it is rewritten below, and a link to his LSTM time series model is at the bottom. I recommend checking it out!

这个create_dataset方法是由Jason Brownlee编写的,在下面进行了重写,并且在底部是指向他的LSTM时间序列模型的链接。 我建议检查一下!

#https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/
def create_dataset(dataset, lookback=1):
dataX, dataY = [], []
for i in range(len(dataset) - lookback - 1):
a = dataset[i: i + lookback, 0]
dataX.append(a)
dataY.append(dataset[i + lookback, 0])
return np.array(dataX), np.array(dataY)

So we use the method above to add a secondary column to be analyzed by the LSTM model and create a ‘X’ and a ‘Y’ for both the training data and the testing data.

因此,我们使用上述方法添加了要由LSTM模型分析的辅助列,并为训练数据和测试数据创建了“ X”和“ Y”。

# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back
# reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = np.reshape(testX, (testX.shape[0], testX.shape[1], 1)

Once the data is all set up, we simply add it to the model to be analyzed.

数据全部设置好之后,我们只需将其添加到要分析的模型中即可。

batch_size = 1
model = Sequential()
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True, return_sequences=True))
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
for i in range(5):
model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False)
model.reset_states()

Once the model has run a set number of times, in this case its five, we can ask the model to predict.

一旦模型运行了设定的次数(在本例中为5次),我们可以要求模型进行预测。

trainPredict = model.predict(trainX, batch_size=batch_size)
model.reset_states()
testPredict = model.predict(testX, batch_size=batch_size)

Don’t forget to reverse our transformation from the beginning!

不要忘记从一开始就扭转我们的转型!

trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])

To see how well your model did you can use mean squared error below.

要查看您的模型效果如何,您可以在下面使用均方误差。

trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))

If you wish to see it visually, feel free to plot it using matplotlib.

如果您希望直观地看到它,请随时使用matplotlib对其进行绘制。

import matplotlib as plt
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()

翻译自: https://medium.com/@trevohearn/lstm-a-time-series-analysis-b90517fcac9e

时间序列分析 lstm

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392129.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于计算圆周率PI的经典程序

短短几行代码&#xff0c;却也可圈可点。如把变量s放在PI表达式中&#xff0c;还有正负值的处理&#xff0c;都堪称经典。尤其是处处考虑执行效率的思想令人敬佩。 /* pi/41-1/31/5-1/71/9-…… */ #include <stdio.h> int main(){ int s1; float pi0.,n1.,…

华为产品技术学习笔记之路由原理(一)

路由器&#xff1a;路由器是一种典型的网络连接设备&#xff0c;用来进行路由选择和报文转发。路由器与它直接相连的网络的跳数为0&#xff0c;通过一台路由器可达的网络的跳数为1.路由协议&#xff1a;路由器之间维护路由表的规则&#xff0c;用以发现路由&#xff0c;生成路由…

Linux网络配置:设置IP地址、网关DNS、主机名

查看网络信息 1、ifconfig eth0 2、ifconfig -a 3、ip add 设置主机名需改配置文件&#xff1a; /etc/hosts /etc/sysconfig/network vim /etc/sysconfig/network NETWORKINGyes NETWORKING_IPV6no HOSTNAMEwendyhost Linux配置网络 方法一&#xff1a; 1、使用setup命令进入如…

编译原理—小型(简化)高级语言分析器前端(Java)

实现一个一遍扫描的编译前端&#xff0c;将简化高级语言的部分语法成分&#xff08;含赋值语句、分支语句、循环语句等&#xff09;翻译成四元式&#xff08;或三地址代码&#xff09;&#xff0c;还要求有合理的语法出错报错和错误恢复功能。 测试样例 beginwhile a<b do…

linux boot菜单列表,Bootstrap 下拉菜单(Dropdowns)简介

Bootstrap 下拉菜单是可切换的&#xff0c;是以列表格式显示链接的上下文菜单。这可以通过与 下拉菜单(Dropdown) JavaScript 插件 的互动来实现。如需使用下拉菜单&#xff0c;只需要在 class .dropdown 内加上下拉菜单即可。下面的实例演示了基本的下拉菜单&#xff1a;实例主…

dynamodb管理ttl_如何使用DynamoDB TTL和Lambda安排临时任务

dynamodb管理ttlby Yan Cui崔燕 如何使用DynamoDB TTL和Lambda安排临时任务 (How to schedule ad-hoc tasks with DynamoDB TTL and Lambda) CloudWatch Events let you easily create cron jobs with Lambda. However, it’s not designed for running lots of ad-hoc tasks,…

5g创业的构想_数据科学项目的五个具体构想

5g创业的构想Do you want to enter the data science world? Congratulations! That’s (still) the right choice.您想进入数据科学世界吗&#xff1f; 恭喜你&#xff01; 那(仍然)是正确的选择。 The market currently gets tougher. So, you must be mentally prepared f…

Microsoft Windows Phone 7 Toolkit Silverlight SDK XNA Game Studio 4.0 开发工具套件正式版下载...

Windows Phone 7开发工具套件包括Visual Studio 2010 Express for Windows Phone、Windows Phone模拟器、Expression Blend 4 for Windows Phone、XNA Game Studio 4.0和新增加的必应地图SDK。 英文版的光盘镜像&#xff1a;点击下载 文档中心&#xff1a;Windows Phone develo…

数据挖掘—Apriori算法(Java实现)

算法描述 &#xff08;1&#xff09;扫描全部数据&#xff0c;产生候选1-项集的集合C1&#xff1b; &#xff08;2&#xff09;根据最小支持度&#xff0c;由候选1-项集的集合C1产生频繁1-项集的集合L1&#xff1b; &#xff08;3&#xff09;对k>1&#xff0c;重复执行步骤…

怎么汇报一周开发工作情况_如何在没有经验的情况下获得第一份开发人员工作

怎么汇报一周开发工作情况Whether you’ve done a coding bootcamp or taught yourself, getting your first developer job with only a few months of coding under your belt is hard.无论您是完成了编码训练营还是自学了&#xff0c;仅靠几个月的编码就很难拿到第一份开发人…

vue.js的认知

Vue.js&#xff08;读音 /vjuː/, 类似于 view&#xff09; 是一套构建用户界面的渐进式框架。 Vue 只关注视图层&#xff0c; 采用自底向上增量开发的设计。 Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。 Vue 学习起来非常简单&#xff0c;。转载于…

c语言中的无符号字节,C语言之有符号数和无符号数

我们知道&#xff0c;在C语言中存在无符号数和有符号数(一些高级语言如Java里面是没有无符号数的)&#xff0c;但是对于计算机而言&#xff0c;其本身并不区别有符号数和无符号数&#xff0c;因为在计算机里面都是0或者1&#xff0c;但是在我们的实际使用中有时候需要使用有符号…

8种排序算法比较

8种排序算法&#xff0c;各算法名称见下表或见源码。运行程序时&#xff0c;将需要你输入一数值&#xff0c;以确定对多少随机数进行排序。然后将会显示各排序算法的耗时。并且你可选择时否进行正序和反序测试。 由于水平有限&#xff0c;可能存在一些错误&#xff0c;还请各位…

两个问题,关于XP进程优化及SVSP虚拟存储平台

这两个问题让我有点头痛&#xff0c;是Boss这阵子布置给我的&#xff0c;都一段时间了&#xff0c;我还是没找出合适的解决方案来答复Boss.第一个问题是&#xff1a;查查X200或X61中的进程&#xff0c;看哪些是可以不要的&#xff0c;停掉&#xff0c;但又不影响用户使用。&…

数据挖掘—朴素贝叶斯分类算法(Java实现)

算法描述 &#xff08;1&#xff09;扫描训练样本数据集&#xff0c;分别统计训练集中类别 Ci 的个数 Di 和属于类别Ci 的样本中属性Ak取值Xk为 Dik 的实例样本个数&#xff0c;构成统计表&#xff1b; &#xff08;2&#xff09;计算先验概率和条件概率&#xff0c;构成概率表…

net core 获取网站目录

AppContext.BaseDirectory 获取项目的根目录转载于:https://www.cnblogs.com/zxs-onestar/p/7147265.html

泰晤士报下载_《泰晤士报》和《星期日泰晤士报》新闻编辑室中具有指标的冒险活动-第1部分:问题

泰晤士报下载TLDR: Designing metrics that help you make better decisions is hard. In The Times and The Sunday Times newsrooms, we have spent a lot of time trying to tackle three particular problems.TLDR &#xff1a;设计度量标准以帮助您做出更好的决策非常困难…

速度一半永远追不上_您将永远不会知道自己应该怎么做的一半-没关系。

速度一半永远追不上by Ken Gilb肯吉尔伯(Ken Gilb) 您将永远不会知道自己应该怎么做的一半-没关系。 (You will never know half of what you think you should — and that’s ok.) Impostor syndrome is a real thing in software development. After 20 years in the indus…

c语言自学门槛,初学C语言的人最常问的几个问题

初学C语言的人最常问的几个问题C语言是一门通用计算机编程语言&#xff0c;应用广泛。对于新手来说学习C语言并不是那么容易&#xff0c;下面是C语言初学者最常问的几个问题&#xff0c;欢迎阅读!1.多久能学会编程?这是一个没有答案的问题。每个人投入的时间、学习效率和基础都…

背景消除的魔力

图片的功能非常强大&#xff0c;有一图胜千言的效果&#xff0c;所以在文档或演示文稿中使用图片来增加趣味性是一种很棒的想法。但问题是&#xff0c;图片通常会变为文字中间的独立矩形&#xff0c;而不是真正与内容融合在一起。您可以在图片中放置边框或效果&#xff0c;使其…