时间序列预测,使用lstm模型预测股票价格

Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.

Towards Data Science编辑的注意事项:虽然我们允许独立作者按照我们的规则和指南发表文章,但我们不认可每位作者的贡献。 您不应在未征求专业意见的情况下依赖作者的作品。 有关详细信息,请参见我们的阅读器条款

1.简介 (1. Introduction)

1.1。 时间序列和预测模型 (1.1. Time-series & forecasting models)

Traditionally most machine learning (ML) models use as input features some observations (samples / examples) but there is no time dimension in the data.

传统上,大多数机器学习(ML)模型使用一些观察值(样本/示例)作为输入特征,但是数据中没有时间维度

Time-series forecasting models are the models that are capable to predict future values based on previously observed values. Time-series forecasting is widely used for non-stationary data. Non-stationary data are called the data whose statistical properties e.g. the mean and standard deviation are not constant over time but instead, these metrics vary over time.

时间序列预测模型是能够基于先前观察到的预测未来值的模型。 时间序列预测已广泛用于非平稳数据非平稳数据称为其统计属性(例如,平均值和标准偏差)随时间变化的数据不是恒定的,而是这些度量随时间变化的数据。

These non-stationary input data (used as input to these models) are usually called time-series. Some examples of time-series include the temperature values over time, stock price over time, price of a house over time etc. So, the input is a signal (time-series) that is defined by observations taken sequentially in time.

这些非平稳输入数据(用作这些模型的输入)通常称为时间序列。 时间序列的一些示例包括随时间变化的温度值,随时间变化的股票价格,随时间变化的房屋价格等。因此,输入是一个信号(时间序列),由按时间顺序进行的观察定义

A time series is a sequence of observations taken sequentially in time.

时间序列是按时间顺序进行的一系列观察。

时间序列预测,使用lstm模型预测股票价格_第1张图片
An example of a time-series. Plot created by the author in Python. 时间序列的示例。 作者在Python中创建的图。

Observation: Time-series data is recorded on a discrete time scale.

观察:时间序列数据以离散的时间刻度记录。

Disclaimer (before we move on): There have been attempts to predict stock prices using time series analysis algorithms, though they still cannot be used to place bets in the real market. This is just a tutorial article that does not intent in any way to “direct” people into buying stocks.

免责声明(在继续之前):尽管仍然不能使用时间序列分析算法来预​​测实际市场中的价格,但已经尝试进行预测。 这只是一篇教程文章,无意以任何方式“引导”人们购买股票。

2. LSTM模型 (2. The LSTM model)

Long short-term memory (LSTM) is an artificial recurrent neural network (RNN) architecture used in the field of deep learning. Unlike standard feedforward neural networks, LSTM has feedback connections. It can not only process single data points (e.g. images), but also entire sequences of data (such as speech or video inputs).

长短期记忆( LSTM )是一种在深度学习领域中使用的人工循环神经网络(RNN)架构。 与标准前馈神经网络不同,LSTM具有反馈连接。 它不仅可以处理单个数据点(例如图像),而且可以处理整个数据序列(例如语音或视频输入)。

LSTM models are able to store information over a period of time.

LSTM模型能够在一段时间内存储信息。

In order words, they have a memory capacity. Remember that LSTM stands for Long Short-Term Memory Model.

换句话说,它们具有存储容量。 请记住,LSTM代表长期短期记忆模型。

This characteristic is extremely useful when we deal with Time-Series or Sequential Data. When using an LSTM model we are free and able to decide what information will be stored and what discarded. We do that using the “gates”. The deep understanding of the LSTM is outside the scope of this post but if you are interested in learning more, have a look at the references at the end of this post.

当我们处理时间序列或顺序数据时,此特征非常有用。 使用LSTM模型时,我们可以自由决定是否存储哪些信息以及丢弃哪些信息。 我们使用“门”来做到这一点。 对LSTM的深入了解超出了本文的范围,但是,如果您有兴趣了解更多信息,请查看本文结尾处的参考资料。

3.获取股价历史数据 (3. Getting the stock price history data)

Thanks to Yahoo finance we can get the data for free. Use the following link to get the stock price history of TESLA: https://finance.yahoo.com/quote/TSLA/history?period1=1436486400&period2=1594339200&interval=1d&filter=history&frequency=1d

多亏了Yahoo的资助,我们可以免费获得数据。 使用以下链接获取TESLA的股价历史记录: https : //finance.yahoo.com/quote/TSLA/history ? period1 = 1436486400 & period2 = 1594339200 & interval = 1d & filter = history& frequency = 1d

You should see the following:

您应该看到以下内容:

时间序列预测,使用lstm模型预测股票价格_第2张图片

Click on the Download and save the .csv file locally on your computer.

单击下载,然后将.csv文件本地保存在您的计算机上。

The data are from 2015 till now (2020) !

数据为2015年至今( 2020年)!

4,Python的工作示例 (4.Python working example)

Modules needed: Keras, Tensorflow, Pandas, Scikit-Learn & Numpy

所需模块:Keras,Tensorflow,Pandas,Scikit-Learn和Numpy

We are going to build a multi-layer LSTM recurrent neural network to predict the last value of a sequence of values i.e. the TESLA stock price in this example.

我们将建立一个多层LSTM递归神经网络,预测一系列值最后一个值,例如本例中的TESLA股票价格。

Let’s load the data and inspect them:

让我们加载数据检查它们:

import math
import matplotlib.pyplot as plt
import keras
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from keras.layers import *
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from keras.callbacks import EarlyStoppingdf=pd.read_csv("TSLA.csv")
print(‘Number of rows and columns:’, df.shape)
df.head(5)
时间序列预测,使用lstm模型预测股票价格_第3张图片
Output of the above code 上面代码的输出

The next step is to split the data into training and test sets to avoid overfitting and to be able to investigate the generalization ability of our model. To learn more about overfitting read this article:

下一步是将数据分割训练测试设备,以避免过度拟合,并能够进一步调查我们的模型的泛化能力。 要了解有关过度拟合的更多信息,请阅读本文:

The target value to be predicted is going to be the “Close” stock price value.

要预测的目标值将是“收盘”股价。

training_set = df.iloc[:800, 1:2].values
test_set = df.iloc[800:, 1:2].values

It’s a good idea to normalize the data before model fitting. This will boost the performance. You can read more here for the Min-Max Scaler:

模型拟合之前对数据进行标准化是一个好主意。 这将提高性能。 您可以在此处阅读有关Min-Max Scaler的更多信息

Let’s build the input features with time lag of 1 day (lag 1):

让我们以1天的时间延迟(延迟1)构建输入功能

# Feature Scaling
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)# Creating a data structure with 60 time-steps and 1 output
X_train = []
y_train = []
for i in range(60, 800):
X_train.append(training_set_scaled[i-60:i, 0])
y_train.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
#(740, 60, 1)

We have now reshaped the data into the following format (#values, #time-steps, #1 dimensional output).

现在,我们将数据重塑为以下格式(#values,#time-steps,#1维输出)。

Now, it’s time to build the model. We will build the LSTM with 50 neurons and 4 hidden layers. Finally, we will assign 1 neuron in the output layer for predicting the normalized stock price. We will use the MSE loss function and the Adam stochastic gradient descent optimizer.

现在,该构建模型了。 我们将用50个神经元和4个隐藏层构建LSTM 。 最后,我们将在输出层中分配1个神经元,以预测标准化股价。 我们将使用MSE损失函数和亚当随机梯度下降优化器。

Note: the following will take some time (~5min).

注意:以下过程将花费一些时间(〜5分钟)。

model = Sequential()#Adding the first LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))
model.add(Dropout(0.2))# Adding a second LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))# Adding a third LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))# Adding a fourth LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50))
model.add(Dropout(0.2))# Adding the output layer
model.add(Dense(units = 1))# Compiling the RNN
model.compile(optimizer = 'adam', loss = 'mean_squared_error')# Fitting the RNN to the Training set
model.fit(X_train, y_train, epochs = 100, batch_size = 32)

When the fitting is finished you should see something like this:

装配完成后,您应该会看到以下内容:

时间序列预测,使用lstm模型预测股票价格_第4张图片

Prepare the test data (reshape them):

准备测试数据(重塑它们):

# Getting the predicted stock price of 2017
dataset_train = df.iloc[:800, 1:2]
dataset_test = df.iloc[800:, 1:2]dataset_total = pd.concat((dataset_train, dataset_test), axis = 0)inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].valuesinputs = inputs.reshape(-1,1)
inputs = sc.transform(inputs)
X_test = []
for i in range(60, 519):
X_test.append(inputs[i-60:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))print(X_test.shape)
# (459, 60, 1)

Make Predictions using the test set

使用测试集进行预测

predicted_stock_price = model.predict(X_test)
predicted_stock_price = sc.inverse_transform(predicted_stock_price)

Let’s visualize the results now:

让我们现在可视化结果:

# Visualising the results
plt.plot(df.loc[800:, ‘Date’],dataset_test.values, color = ‘red’, label = ‘Real TESLA Stock Price’)
plt.plot(df.loc[800:, ‘Date’],predicted_stock_price, color = ‘blue’, label = ‘Predicted TESLA Stock Price’)
plt.xticks(np.arange(0,459,50))
plt.title('TESLA Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('TESLA Stock Price')
plt.legend()
plt.show()

5.结果 (5. Results)

Using a lag of 1 (i.e. step of one day):

使用1的滞后时间(即一天的步长):

时间序列预测,使用lstm模型预测股票价格_第5张图片

Observation: Huge drop in March 2020 due to the COVID-19 lockdown !

观察:由于COVID-19锁定,2020年3月大幅下降!

We can clearly see that our model performed very good. It is able to accuretly follow most of the unexcepted jumps/drops however, for the most recent date stamps, we can see that the model expected (predicted) lower values compared to the real values of the stock price.

我们可以清楚地看到我们的模型表现非常好。 它能够准确地跟踪大多数无例外的跳跃/下降,但是,对于最新的日期戳,我们可以看到该模型对股票实际价格的预期(预测)值较低。

关于滞后的注意事项 (A note about the lag)

The initial selected lag in this article was 1 i.e. using a step of 1 day. This can be easily changed by altering the code that builds the 3D inputs.

本文最初选择的滞后为1,即使用1天的时间。 这可以通过更改构建3D输入的代码轻松更改。

Example: One can change the following 2 blocks of code:

示例:可以更改以下2个代码块:

X_train = []
y_train = []
for i in range(60, 800):
X_train.append(training_set_scaled[i-60:i, 0])
y_train.append(training_set_scaled[i, 0])

and

X_test = []
y_test = []
for i in range(60, 519):
X_test.append(inputs[i-60:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

with the following new code:

使用以下新代码:

X_train = []
y_train = []
for i in range(60, 800):
X_train.append(training_set_scaled[i-50:i, 0])
y_train.append(training_set_scaled[i, 0])

and

X_test = []
y_test = []
for i in range(60, 519):
X_test.append(inputs[i-50:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

In that case the results look like this:

在这种情况下,结果如下所示:

时间序列预测,使用lstm模型预测股票价格_第6张图片

That’s all folks ! Hope you liked this article!

那就是所有的人! 希望您喜欢这篇文章!

看看我在另一篇文章中用来预测GOOGLE股票价格的Facebook Prophet模型。 (Have a look at my Facebook Prophet model that I used to predict the GOOGLE stock price in another article.)

还要检查我最近使用ARIMA模型的文章: (Check also my recent article using an ARIMA model:)

请继续关注并支持这项工作 (Stay tuned & support this effort)

If you liked and found this article useful, follow me to be able to see all my new posts.

如果您喜欢并认为本文很有用,请关注我以查看我的所有新帖子。

Questions? Post them as a comment and I will reply as soon as possible.

有什么问题吗将其发布为评论,我会尽快回复。

最新的帖子 (Latest posts)

与我取得联系(Get in touch with me)

  • LinkedIn: https://www.linkedin.com/in/serafeim-loukas/

    领英: https : //www.linkedin.com/in/serafeim-loukas/

  • ResearchGate: https://www.researchgate.net/profile/Serafeim_Loukas

    ResearchGate : https : //www.researchgate.net/profile/Serafeim_Loukas

  • EPFL profile: https://people.epfl.ch/serafeim.loukas

    EPFL个人资料: https : //people.epfl.ch/serafeim.loukas

  • Stack Overflow: https://stackoverflow.com/users/5025009/seralouk

    堆栈溢出: https : //stackoverflow.com/users/5025009/seralouk

翻译自: https://towardsdatascience.com/time-series-forecasting-predicting-stock-prices-using-an-lstm-model-d0056cd5e055

你可能感兴趣的:(python,机器学习,java,人工智能,算法)