【发布时间】:2021-06-05 07:48:49
【问题描述】:
我正在学习 LSTM 网络,我得到的输出是出乎意料的。 我创建了一个 LSTM 并用两个 numpy 数组喂它:
- input.shape --> (2158, 2, 1) --> 这包含股票的收盘价和成交量
- output.shape --> (2158, 1, 1) --> 包含股票第二天的收盘价
在我使用模型预测输入后,我希望预测形状为 (2158, 1, 1) 但它是 (2158, 2, 1)。
这是为什么呢?如何更改它以预测目标价格?
def main():
training_data=gettrainingdata()
training_set_scaled=scale(training_data)
x,y = getxy(training_set_scaled)
x,y = reshape(x,y)
model=returnmodel(x, y)
model=train(model,x,y)
predicted=predict(model,x)
def individual_stock(price_df,vol_df,name):
return pd.DataFrame({ "Date":price_df["Date"],"Close":price_df[name],"Volume":vol_df[name] })
def gettrainingdata():
path= (pathlib.Path(__file__).parent.absolute())
stock_price_df = pd.read_csv(f"{path}/stock_prices.csv")
stock_vol_df = pd.read_csv(f"{path}/stock_volume.csv")
stock_price_df = stock_price_df.sort_values(by = ["Date"])
stock_vol_df = stock_vol_df.sort_values(by = ["Date"])
#Concatenates the data of one stock date prices and volume as dictionaries
price_volume_df = individual_stock(stock_price_df,stock_vol_df,"sp500")
training_data=price_volume_df.iloc[:,1:3].values
print("""
1)
returning training data as:
<class 'numpy.ndarray'>
shape: (2159, 2)
_________________________________
""")
# print(training_data)
# print(type(training_data))
# print(training_data.shape)
return training_data
def scale(training_data):
sc = MinMaxScaler(feature_range=(0,1))
training_set_scaled = sc.fit_transform(training_data)
print("""
2)
MixMaxScalar
returning scaled training data as:
<class 'numpy.ndarray'>
shape: (2159, 2)
_________________________________
""")
# print(training_set_scaled)
# print(type(training_set_scaled))
# print(training_set_scaled.shape)
return training_set_scaled
def getxy(training_set_scaled):
dim1,dim2=training_set_scaled.shape
X=[]
Y=[]
for i in range(1,(dim1)):
X.append(training_set_scaled[i-1,:dim2])
Y.append(training_set_scaled[i,0])
X,Y=np.asarray(X),np.asarray(Y)
print("""
3)
return input and output:
<class 'numpy.ndarray'> <class 'numpy.ndarray'>
(2158, 2) (2158,)
_________________________________
""")
# print(type(X),type(Y))
# print((X).shape,(Y).shape)
return X,Y
def reshape(x,y):
dimX1,dimX2=x.shape
x = np.reshape( x, (dimX1,dimX2,1) )
y = np.reshape( y, (y.shape[0],1,1) )
print("""
4)
return 3d arrays:
<class 'numpy.ndarray'> <class 'numpy.ndarray'>
(2158, 2, 1) (2158, 1, 1)
_________________________________
""")
# print(type(x),type(y))
# print((x).shape,(y).shape)
return x,y
def returnmodel(x,y):
dimX1,dimX2,dimX3=x.shape
inputs=keras.layers.Input(shape= (dimX2,dimX3))
new = keras.layers.LSTM(150,return_sequences=True)(inputs)
new=keras.layers.LSTM(150,return_sequences=True)(new)
new=keras.layers.LSTM(150,return_sequences=True)(new)
outputs=keras.layers.Dense(1,activation="linear")(new)
model=keras.Model(inputs=inputs,outputs=outputs)
model.compile(optimizer="adam",loss="mse")
print("""
5)
created LSTM model
_________________________________
""")
model.summary()
return model
def train(model,x,y):
model.fit(x,y, epochs=2, batch_size=32, validation_split=0.2 )
print("""
6)
the model is trained
_________________________________
""")
return model
def predict(model,x):
predicted=model.predict(x)
print("""
7)
the model is making predictions
returning an array
_________________________________
""")
print(type(predicted))
print(predicted.shape)
dim1,dim2,dim3=predicted.shape
new = []
for i in range(dim1):
new.append([predicted[i][0][0],predicted[i][1][0]])
df=pd.DataFrame(new)
print(df)
return predicted
if __name__ == "__main__":
main()
我打印的最后一部分:
7)
the model is making predictions
returning an array
_________________________________
<class 'numpy.ndarray'>
(2158, 2, 1)
0 1
0 0.068643 0.054690
1 0.066402 0.020197
2 0.068003 0.052936
3 0.073034 0.067877
4 0.075300 0.107198
... ...
2153 0.891596 1.002645
2154 0.901476 1.022127
2155 0.911437 0.985692
2156 0.912426 0.970935
2157 0.916713 0.995224
[2158 rows x 2 columns]
【问题讨论】:
标签: python tensorflow lstm stock