首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为LSTM/GRU模型提供多个独立的时间序列?

如何为LSTM/GRU模型提供多个独立的时间序列?
EN

Stack Overflow用户
提问于 2021-07-31 11:03:21
回答 2查看 309关注 0票数 1

为了简单地解释一下:我有53口油井测量,每口井每天测量6年,我们记录了多个变量(压力、水产量、气体production...etc),我们的主要组成部分(我们要研究和预测的)是采收率。我如何利用所有的数据来训练我的LSTM/GRU模型,因为我知道油井是独立的,每一个油井的测量都是在同一时间完成的?

EN

回答 2

Stack Overflow用户

发布于 2021-07-31 13:03:35

如果你想假设井是独立的,那么“每口井都是在同一时间进行的测量”这一知识是不必要的。(你为什么认为这些知识是有用的?)

因此,如果这些井被认为是独立的,就把它们当作单独的样本。像往常一样,将它们分成训练集、验证集和测试集。训练一个普通的LSTM或GRU。

顺便说一句,你可能想要使用注意力机制,而不是循环网络。这是比较容易的训练,通常会产生类似的结果。

即使是复杂的网络也可能足够好。如果您怀疑长期相关性,请参阅像WaveNet这样的方法。

票数 0
EN

Stack Overflow用户

发布于 2021-08-01 14:25:25

这些井测量听起来像是特定的独立事件。我在金融部门工作。我们总是看不同的股票,每只股票的特定时间使用LSTM,但不是10只股票混在一起。下面是一些分析特定股票的代码。修改代码以满足您的需要。

代码语言:javascript
复制
from pandas_datareader import data as wb
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pylab import rcParams
from sklearn.preprocessing import MinMaxScaler

start = '2019-06-30'
end = '2020-06-30'

tickers = ['GOOG']

thelen = len(tickers)

price_data = []
for ticker in tickers:
    prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Open','Adj Close']]
    price_data.append(prices.assign(ticker=ticker)[['ticker', 'Open', 'Adj Close']])

#names = np.reshape(price_data, (len(price_data), 1))

df = pd.concat(price_data)
df.reset_index(inplace=True)

for col in df.columns: 
    print(col) 
    
#used for setting the output figure size
rcParams['figure.figsize'] = 20,10
#to normalize the given input data
scaler = MinMaxScaler(feature_range=(0, 1))
#to read input data set (place the file name inside  ' ') as shown below


df['Adj Close'].plot()
plt.legend(loc=2)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

ntrain = 80
df_train = df.head(int(len(df)*(ntrain/100)))
ntest = -80
df_test = df.tail(int(len(df)*(ntest/100)))


#importing the packages 
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM

#dataframe creation
seriesdata = df.sort_index(ascending=True, axis=0)
new_seriesdata = pd.DataFrame(index=range(0,len(df)),columns=['Date','Adj Close'])
length_of_data=len(seriesdata)
for i in range(0,length_of_data):
    new_seriesdata['Date'][i] = seriesdata['Date'][i]
    new_seriesdata['Adj Close'][i] = seriesdata['Adj Close'][i]
#setting the index again
new_seriesdata.index = new_seriesdata.Date
new_seriesdata.drop('Date', axis=1, inplace=True)
#creating train and test sets this comprises the entire data’s present in the dataset
myseriesdataset = new_seriesdata.values
totrain = myseriesdataset[0:255,:]
tovalid = myseriesdataset[255:,:]
#converting dataset into x_train and y_train
scalerdata = MinMaxScaler(feature_range=(0, 1))
scale_data = scalerdata.fit_transform(myseriesdataset)
x_totrain, y_totrain = [], []
length_of_totrain=len(totrain)
for i in range(60,length_of_totrain):
    x_totrain.append(scale_data[i-60:i,0])
    y_totrain.append(scale_data[i,0])
x_totrain, y_totrain = np.array(x_totrain), np.array(y_totrain)
x_totrain = np.reshape(x_totrain, (x_totrain.shape[0],x_totrain.shape[1],1))


#LSTM neural network
lstm_model = Sequential()
lstm_model.add(LSTM(units=50, return_sequences=True, input_shape=(x_totrain.shape[1],1)))
lstm_model.add(LSTM(units=50))
lstm_model.add(Dense(1))
lstm_model.compile(loss='mean_squared_error', optimizer='adadelta')
lstm_model.fit(x_totrain, y_totrain, epochs=10, batch_size=1, verbose=2)
#predicting next data stock price
myinputs = new_seriesdata[len(new_seriesdata) - (len(tovalid)+1) - 60:].values
myinputs = myinputs.reshape(-1,1)
myinputs  = scalerdata.transform(myinputs)
tostore_test_result = []
for i in range(60,myinputs.shape[0]):
    tostore_test_result.append(myinputs[i-60:i,0])
tostore_test_result = np.array(tostore_test_result)
tostore_test_result = np.reshape(tostore_test_result,(tostore_test_result.shape[0],tostore_test_result.shape[1],1))
myclosing_priceresult = lstm_model.predict(tostore_test_result)
myclosing_priceresult = scalerdata.inverse_transform(myclosing_priceresult)
    
totrain = df_train
tovalid = df_test

#predicting next data stock price
myinputs = new_seriesdata[len(new_seriesdata) - (len(tovalid)+1) - 60:].values


#  Printing the next day’s predicted stock price. 
print(len(tostore_test_result));
print(myclosing_priceresult);

最终结果:

代码语言:javascript
复制
1
[[1396.532]]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68601505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档