时间序列分析 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 mathimport 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 setstrain_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+1look_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 = 1model = 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 pltplt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()
翻譯自: https://medium.com/@trevohearn/lstm-a-time-series-analysis-b90517fcac9e
時間序列分析 lstm
總結
以上是生活随笔為你收集整理的时间序列分析 lstm_LSTM —时间序列分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到下雨收衣服是什么意思
- 下一篇: 女人梦到性梦宫缩什么意思