简体   繁体   English

如何通过深度学习预测未来价值?

[英]How to predict future values with deep learning?

I'm using a deep learning algorithm for price prediction in cryptocurrency markets.我正在使用深度学习算法来预测加密货币市场的价格。 This code is only predicting dates which are already in datasets.此代码仅预测数据集中已经存在的日期。 Code is using unix time as date input.代码使用 unix 时间作为日期输入。 How can i get future predictions?我怎样才能得到未来的预测?

My dataset is from 1 Aug 2015 to 1 Aug 2020. I want to predict 1 Aug 2020 - 1 Sep 2020;我的数据集是从 2015 年 8 月 1 日到 2020 年 8 月 1 日。我想预测 2020 年 8 月 1 日到 2020 年 9 月 1 日; but it predicts to 1 Aug 2020 last.但它预计最后到 2020 年 8 月 1 日。

Shortly, I want to predict the dates which are not included already in my dataset.不久,我想预测我的数据集中尚未包含的日期。

def train_test_split(df, test_size=0.2):
    split_row = len(df) - int(test_size * len(df))
    train_data = df.iloc[:split_row]
    test_data = df.iloc[split_row:]
    return train_data, test_data
train, test = train_test_split(hist, test_size=0.2)

def line_plot(line1, line2, label1=None, label2=None, title=Crypto, lw=2):
    fig, ax = plt.subplots(1, figsize=(13, 7))
    ax.plot(line1, label=label1, linewidth=lw)
    ax.plot(line2, label=label2, linewidth=lw)
    ax.set_ylabel('price [USD]', fontsize=14)
    #ax.set_xlabel('Time [day]', fontsize=14)
    ax.set_title(title, fontsize=16)
    ax.legend(loc='best', fontsize=16)
line_plot(train[target_col], test[target_col], 'training', 'test', title='')

def normalise_zero_base(df):
    return df / df.iloc[0] - 1

def normalise_min_max(df):
    return (df - df.min()) / (df.max() - df.min())

def extract_window_data(df, window_len=5, zero_base=True):
    window_data = []
    for idx in range(len(df) - window_len):
        tmp = df[idx: (idx + window_len)].copy()
        if zero_base:
            tmp = normalise_zero_base(tmp)
        window_data.append(tmp.values)
    return np.array(window_data)

def prepare_data(df, target_col, window_len=10, zero_base=True, test_size=0.2):
    train_data, test_data = train_test_split(df, test_size=test_size)
    X_train = extract_window_data(train_data, window_len, zero_base)
    X_test = extract_window_data(test_data, window_len, zero_base)
    y_train = train_data[target_col][window_len:].values
    y_test = test_data[target_col][window_len:].values
    if zero_base:
        y_train = y_train / train_data[target_col][:-window_len].values - 1
        y_test = y_test / test_data[target_col][:-window_len].values - 1

    return train_data, test_data, X_train, X_test, y_train, y_test

def build_lstm_model(input_data, output_size, neurons=100, activ_func='linear', dropout=0.2, loss='mse', optimizer='adam'):
    model = Sequential()
    model.add(LSTM(neurons, input_shape=(input_data.shape[1], input_data.shape[2])))
    model.add(Dropout(dropout))
    model.add(Dense(units=output_size))
    model.add(Activation(activ_func))
    model.compile(loss=loss, optimizer=optimizer)
    return model

np.random.seed(42)
window_len = 5
test_size = 0.2
zero_base = True
lstm_neurons = 100
epochs = 100
batch_size = 32
loss = 'mse'
dropout = 0.2
optimizer = 'adam'

train, test, X_train, X_test, y_train, y_test = prepare_data(
    hist, target_col, window_len=window_len, zero_base=zero_base, test_size=test_size)
model = build_lstm_model(
    X_train, output_size=1, neurons=lstm_neurons, dropout=dropout, loss=loss,
    optimizer=optimizer)
history = model.fit(
    X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1, shuffle=True)

targets = test[target_col][window_len:]
preds = model.predict(X_test).squeeze()
mean_absolute_error(preds, y_test)
# 0.027955859325876943

preds = test[target_col].values[:-window_len] * (preds + 1)
preds = pd.Series(index=targets.index, data=preds)
line_plot(targets, preds, 'actual', 'prediction', lw=3)

数据集示例 Dataset example photo数据集示例照片

训练 Training Output Photo培训 Output 照片

预言 Prediction Output Photo预测 Output 照片

Ok so you use 10 time steps to forecast the following time step.好的,所以您使用 10 个时间步来预测以下时间步。 If you want to predict the future price with your trained model you have to use your predictions as input.如果您想使用经过训练的 model 预测未来价格,您必须使用您的预测作为输入。

The following figure allows you better understand how it works.下图可以让你更好地理解它是如何工作的。 The * indicates a predicted value, opposed to real values. *表示预测值,与实际值相反。 So 1... 8 9 10 are your 10 latest real values.所以1... 8 9 10是你最近的 10 个真实值。 The -> indicates your model function. ->表示您的 model function。

input -> output*

1 ... 8 9 10 -> 11*

2 ... 9 10 11* -> 12*

3 ... 10 11* 12* -> 13*

...

n-9* ... n-2* n-1* n* -> n+1*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM