Time Series Forecasting: Use Recurrent Neural Networks (RNN) or Long Short-Term Memory Networks (LSTM) to predict stock prices, weather, or sales data.

Table of contents

Part One: Overview of Time Series Forecasting

RNN and LSTM

Part 2: Data preparation

Dataset introduction

Part 3: Data Preprocessing

Time series data processing

Part 4: Model Building

Build RNN model

Part 5: Model training

Part 6: Model Evaluation

Part Seven: Visualizing Prediction Results

Part 8: Summary


Building time series prediction models is an important application in the fields of machine learning and deep learning. In this blog, we will use TensorFlow to actually build a time series prediction model to predict stock prices. We will cover the basic concepts of time series forecasting, data preparation, model building and training, and finally evaluation and visualization.

Part One: Overview of Time Series Forecasting

Time series forecasting is a technique that uses data from past time steps to predict future time steps. It is widely used in finance, meteorology, sales and other fields. This article will take stock price prediction as an example to introduce how to use Recurrent Neural Network (RNN) and Long Short-Term Memory Network (LSTM) to build a time series prediction model.

RNN and LSTM

RNN and LSTM are two deep learning models suitable for time series prediction. They have memory capabilities and can capture long-term dependencies in time series. LSTM is more suitable for processing long sequences than standard RNN because it has a more complex internal structure and can effectively prevent the vanishing gradient problem.

Part 2: Data preparation

Dataset introduction

In order to build a stock price prediction model, we need a dataset containing historical stock price data. We can use data sources such as Yahoo Finance to obtain this data. In this article, we will use an example dataset.

First, we need to load the data and preprocess it:

import pandas as pd

# 读取股票价格数据
stock_data = pd.read_csv('stock_prices.csv')

# 数据预处理
stock_data['Date'] = pd.to_datetime(stock_data['Date'])
stock_data.set_index('Date', inplace=True)

Part 3: Data Preprocessing

Time series data processing

Time series data usually need to be smoothed, differentiated, and normalized to make them suitable for model training. These steps can help the model better capture trends and seasonality in the data.

import numpy as np

# 平稳化处理
log_returns = np.log(stock_data['Close']) - np.log(stock_data['Close'].shift(1))
log_returns = log_returns.dropna()

# 归一化处理
mean, std = log_returns.mean(), log_returns.std()
normalized_returns = (log_returns - mean) / std

Part 4: Model Building

Build RNN model

We will use TensorFlow to build an LSTM-based RNN model. The following is the architecture of the model:

import tensorflow as tf

# 定义模型参数
input_seq_len = 30
output_seq_len = 1
hidden_dim = 50

# 构建RNN模型
model = tf.keras.Sequential([
    tf.keras.layers.LSTM(hidden_dim, input_shape=(input_seq_len, 1)),
    tf.keras.layers.Dense(output_seq_len)
])

Part 5: Model training

Now we can use the prepared time series data and model for training:

# 准备训练数据
X_train = []
y_train = []

for i in range(len(normalized_returns) - input_seq_len - output_seq_len):
    X_train.append(normalized_returns[i:i+input_seq_len])
    y_train.append(normalized_returns[i+input_seq_len:i+input_seq_len+output_seq_len])

X_train = np.array(X_train)
y_train = np.array(y_train)

# 编译和训练模型
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X_train, y_train, epochs=50, batch_size=32, verbose=1)

Part 6: Model Evaluation

After training is complete, we need to evaluate the performance of the model. We can use the root mean square error (RMSE) to evaluate the prediction accuracy of the model:

from sklearn.metrics import mean_squared_error
import numpy as np

# 准备测试数据
X_test = []
y_test = []

for i in range(len(normalized_returns) - input_seq_len - output_seq_len, len(normalized_returns) - input_seq_len):
    X_test.append(normalized_returns[i:i+input_seq_len])
    y_test.append(normalized_returns[i+input_seq_len:i+input_seq_len+output_seq_len])

X_test = np.array(X_test)
y_test = np.array(y_test)

# 预测股票价格
predicted_returns = model.predict(X_test)

# 反归一化
predicted_returns = (predicted_returns * std) + mean
y_test = (y_test * std) + mean

# 计算均方根误差
mse = mean_squared_error(y_test, predicted_returns)
rmse = np.sqrt(mse)
print("RMSE:", rmse)

Part Seven: Visualizing Prediction Results

Finally, we can use matplotlib to visualize the model’s predictions:

import matplotlib.pyplot as plt

# 绘制原始股票价格和预测股票价格
plt.figure(figsize=(12, 6))
plt.plot(stock_data.index[-len(y_test):], y_test, label='实际股价', color='blue')
plt.plot(stock_data.index[-len(y_test):], predicted_returns, label='预测股价', color='red')
plt.xlabel('日期')
plt.ylabel('归一化股价')
plt.legend()
plt.show()

Part 8: Summary

In this article, we implemented a time series prediction model using TensorFlow to predict stock prices. We introduced the basic concepts of time series forecasting, the steps of data preparation, preprocessing, model building, training, evaluation, and visualization. With this practical example, you can learn how to build a time series forecasting model and apply it to data from different fields, such as stock prices, weather, or sales data. Hope this blog is helpful to you!

Guess you like

Origin blog.csdn.net/m0_68036862/article/details/133491274