日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/11/29 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 时间序列分析 lstm_LSTM —时间序列分析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

時間序列分析 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.

神經(jīng)網(wǎng)絡(luò)可能是一個難以理解的概念。 我認(rèn)為這主要是由于它們可以用于許多不同的事情,例如分類,識別或僅用于回歸。

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和一些數(shù)字。

First thing we always do? Import!

我們總是做的第一件事? 進(jìn)口!

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.

接下來,我們需要一些數(shù)字來處理。 您可以通過幾種方法來執(zhí)行此操作,但是最好的方法是使用一些確實有意義的數(shù)據(jù),并且建議我轉(zhuǎn)到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文件后,我們需要將其放入數(shù)據(jù)框,僅使用具有我們要使用的所有值的功能。 我的是第一列。

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將其標(biāo)準(zhǔn)化。

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.

接下來要做的是將數(shù)據(jù)分為兩組,第一組是一組訓(xùn)練數(shù)據(jù),供我們的LSTM模型學(xué)習(xí)。 我喜歡使用大約80%的數(shù)據(jù)進(jìn)行訓(xùn)練,但是您可以使用該數(shù)字來查看在模型給您滿意的結(jié)果之前實際需要訓(xùn)練多少數(shù)據(jù)。

#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.

我們接下來要對數(shù)據(jù)進(jìn)行的操作是創(chuàng)建一個輔助功能(如果可以的話),從該數(shù)據(jù)開始,基本上是我們想回溯的距離,以查看當(dāng)前值與之前(假設(shè))三個值相比有多少變化。前。

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編寫的,在下面進(jìn)行了重寫,并且在底部是指向他的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ùn)練數(shù)據(jù)和測試數(shù)據(jù)創(chuàng)建了“ 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.

數(shù)據(jù)全部設(shè)置好之后,我們只需將其添加到要分析的模型中即可。

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.

一旦模型運行了設(shè)定的次數(shù)(在本例中為5次),我們可以要求模型進(jìn)行預(yù)測。

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!

不要忘記從一開始就扭轉(zhuǎn)我們的轉(zhuǎn)型!

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對其進(jìn)行繪制。

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

總結(jié)

以上是生活随笔為你收集整理的时间序列分析 lstm_LSTM —时间序列分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。