【问题标题】:Making One Step Forecast Predictions进行一步预测
【发布时间】:2021-02-01 17:51:19
【问题描述】:

我正在制作一个 LSTM 模型,并在我在 kaggle 上找到的 TSLA 数据集上对其进行训练。所以我的问题是,当我调用 model.predict 时,这个预测是否给了我第二天的股票价格?这是一步预测吗?当我打印 model.predict 时,我得到一个巨大的列表,所以我使用 numpy argmax 函数给我一个数字。代码如下:

import tensorflow as tf 
from tensorflow.keras.layers import LSTM, Dense, Dropout, Input, GlobalMaxPooling1D
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.optimizers import Adam


df = pd.read_csv('TSLA.csv')

series = df['Close'].values.reshape(-1, 1)

scaler = StandardScaler()
scaler.fit(series[:len(series)//2])
series = scaler.transform(series).flatten()

X = []
Y = []
T = 10
D = 1

for t in range(len(series) - T):

    X.append(series[t:t+T])
    Y.append(series[t+T])

X = np.array(X).reshape(-1, T, D)
Y = np.array(Y)
N = len(X)

print(X.shape, Y.shape)

model = tf.keras.Sequential([
    Input(shape=(T, D)),
    LSTM(50),
    Dense(100, activation='relu'),
    Dropout(0.25),
    Dense(1)
])

model.compile(optimizer=Adam(lr=0.01), loss='mse')
r = model.fit(X[:-N//2], Y[:-N//2], validation_data=(X[-N//2:], Y[-N//2:]), epochs=200)

plt.plot(r.history['loss'])
plt.plot(r.history['val_loss'])
plt.show()

preds = model.predict(X)
outs = preds[:,0]

print(outs)
print(np.argmax(outs))

【问题讨论】:

    标签: python tensorflow machine-learning keras lstm


    【解决方案1】:

    Argmax 在这里没有意义。这 90 个值是训练集第二天的 90 个预测值。当你运行这个:

    preds = model.predict(X)
    

    它为您的火车组的所有 90 个数据点提供第二天的值。这一行:

    print(np.argmax(outs))
    

    没有意义。

    顺便说一句,您可以使用 Python 获取股票价格,而不需要 CSV。

    pip install pandas-datareader
    
    from pandas_datareader import data as wb
    
    ticker=wb.DataReader('TSLA',start='2015-1-1',data_source='yahoo')
    print(ticker)
    
                      High         Low  ...      Volume   Adj Close
    Date                                ...                        
    2015-01-02   44.650002   42.652000  ...  23822000.0   43.862000
    2015-01-05   43.299999   41.431999  ...  26842500.0   42.018002
    2015-01-06   42.840000   40.841999  ...  31309500.0   42.256001
    2015-01-07   42.956001   41.956001  ...  14842000.0   42.189999
    2015-01-08   42.759998   42.001999  ...  17212500.0   42.124001
                    ...         ...  ...         ...         ...
    2020-10-13  448.890015  436.600006  ...  34463700.0  446.649994
    2020-10-14  465.899994  447.350006  ...  48045400.0  461.299988
    2020-10-15  456.570007  442.500000  ...  35672400.0  448.880005
    2020-10-16  455.950012  438.850006  ...  32620000.0  439.670013
    2020-10-19  447.000000  437.649994  ...   9422697.0  442.840607
    

    【讨论】:

    • 天哪,非常感谢!!!!!!!还有一个问题。从列表中我怎样才能使它成为一个整数?示例:像 250 美元。无论如何谢谢!!!!!!!!!!
    • 如果答案是olansweredntour问题,请不要犹豫投票
    • 没有答案是我需要的信息,但我是新手,这就是为什么大声笑。还有一个问题。从列表中我如何获得整数这里是我的模型在我调用 model.predict 时给我的列表。 [-1.7686734 -1.8435252 -1.8228512 ... 2.5835307 2.428977 2.554374]。谢谢,祝你有美好的一天!
    • 哦。查找“numpy convert floats to integers”之类的,你会在这个网站上找到答案。您可以使用 np.round 或 array.astype(int)
    • 然后如果我将数字相加,我会得到第二天的价格还是?
    猜你喜欢
    • 2021-03-18
    • 2017-09-14
    • 2015-09-17
    • 1970-01-01
    • 2019-07-16
    • 2021-04-02
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多