Python based on long short-term memory network LSTM model data prediction

Table of Contents

Preface

long short term memory network

Practical cases

1. Experimental environment

2. Read data

3. Prepare training data

4.Train the model

5. Model prediction

6. Visualization of prediction results

Welfare at the end of the article

Foreword

 As financial data continues to grow and become more complex, traditional statistical methods and machine learning techniques are facing challenges. Through the construction of multi-layer neural networks and the training and optimization of large-scale data, deep learning algorithms can extract richer and more advanced feature representations from data, thereby providing more accurate and stable prediction and decision-making capabilities.

    In the financial field, deep learning algorithms have been widely used in several key tasks. First of all, risk assessment is one of the important issues that financial institutions must face. Deep learning algorithms can learn from large-scale historical data, identify potential risk factors hidden in the data, and predict future risk situations. Secondly, fraud detection is an essential task in the financial industry. Deep learning algorithms can discover abnormal patterns and fraudulent behaviors by modeling transaction patterns and user behaviors, and improve financial institutions' ability to identify and prevent fraud.

    In addition, deep learning algorithms also play an important role in financial trading. By modeling and predicting market data, historical trading data and other relevant information, deep learning algorithms can help traders make more informed trading decisions and improve the effectiveness and profitability of trading strategies.

    However, the application of deep learning algorithms in the financial field also faces some challenges and limitations. First, the quality and reliability of the data are critical to the performance of the algorithm. Secondly, the interpretability and credibility of algorithms are also the focus of financial supervision and risk control departments. Therefore, in the development and application process of deep learning algorithms, further exploration and research are still needed to ensure its reliability and stability in the financial field.

    This article will briefly introduce the use of the long short-term memory network (LSTM) model to deal with time series prediction problems, and continue the case demonstration using Moutai stock data, so that readers can expand based on the code based on their own data sets and application scenarios.
</code><img class="look-more-preCode contentImg-no-view" src="//i2.wp.com/csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack.png" alt ="" title="">

?About the author: Machine learning, deep learning, convolutional neural network processing, image processing
Station B project actual combat: https://space.bilibili.com/364224477
If the article is helpful to you, please comment Like Collect Add follow +
?♂ CSDN personal homepage: @purple’s personal homepage

Long short-term memory network

 Long Short-Term Memory (LSTM) is a type of Recurrent Neural Network (RNN) specifically designed to handle sequence prediction problems. Unlike traditional RNN, LSTM can effectively capture long-term dependencies in time series data and is therefore very useful in the financial field.

    These networks contain memory units capable of storing information over long sequences, allowing them to overcome the vanishing gradient problem in traditional RNNs. LSTM's ability to remember and utilize past information makes it suitable for analyzing financial time series data, such as stock prices or economic indicators.

    Application cases: LSTM has various applications in the financial field, such as stock price prediction, algorithmic trading, portfolio optimization and fraud detection. They can also analyze economic indicators to predict market trends, helping investors make more informed decisions.

Here is a sample code for implementing LSTM in Python:

from keras.models import Sequential
from keras.layers import LSTM, Dense

# define the model
model = Sequential()
model.add(LSTM(50, input_shape=(timesteps, feature_dim)))
model.add(Dense(1, activation='sigmoid'))

# compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# fit the model to the training data
model.fit(X_train, y_train, batch_size=32, epochs=10, validation_data=(X_test, y_test))

The above code is simplified and needs to be brought in data variables to run. Below we will use Moutai stock data to conduct a detailed operation demonstration, which is for your reference and learning only.

Practical cases

1. Experimental environment

Python3.9

Code editing tool: jupyter notebook

2. Read data

from keras.models import Sequential
from keras.layers import LSTM, Dense
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
import numpy as np
# Read Maotai stock data and use date as index
data = pd.read_csv('maotai_stock.csv',index_col='date')
data

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

3. Prepare training data

First, the closing prices before 2022 are extracted from the original data set as training data.

# Extract training data
new_data = data['close'] # We predict the closing price, so we extract the close closing price data separately.
train_data = new_data[:'2022'] # Use the closing price data before 2022 as training data
train_prices = train_data.values.reshape(-1, 1)
train_prices

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

Then normalize the training data

# Data normalization
scaler = MinMaxScaler(feature_range=(0, 1))
train_scaled = scaler.fit_transform(train_prices)
train_scaled

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

Finally create our training data set

# Create training data set
X_train = []
y_train = []
timesteps = 30 # Time steps, can be adjusted according to needs
for i in range(timesteps, len(train_scaled)):
    X_train.append(train_scaled[i - timesteps:i, 0])
    y_train.append(train_scaled[i, 0])
# Convert training data to array form
X_train, y_train = np.array(X_train), np.array(y_train)
#Adjust the dimensions of input data
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_train

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

4. Training model

Build the LSTM model and compile the fit

# Build LSTM model
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
model.add(LSTM(50))
model.add(Dense(1))
# Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
# Fit model
model.fit(X_train, y_train, epochs=50, batch_size=32)

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

5. Model prediction

After the model is trained, we need to prepare test data for model testing

# Extract test data
test_data = new_data['2022':] # Use data after 2022 as test data
test_prices = test_data.values.reshape(-1, 1)
# Data normalization
test_scaled = scaler.transform(test_prices)
#Create test data set
X_test = []
y_test = []
for i in range(timesteps, len(test_scaled)):
    X_test.append(test_scaled[i - timesteps:i, 0])
    y_test.append(test_scaled[i, 0])
# Convert test data into array form
X_test, y_test = np.array(X_test), np.array(y_test)
#Adjust the dimensions of input data
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
X_test
</code><img class="look-more-preCode contentImg-no-view" src="//i2.wp.com/csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack.png" alt ="" title="">

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

Use the model to continue making predictions on test data

# Use the model to predict
predicted_prices = model.predict(X_test)
predicted_prices

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

6. Visualization of prediction results

Finally, matplotlib is used to visually compare the model prediction results with the test data to intuitively display the model’s prediction effect.

# Visualization of prediction results
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font='SimHei')
plt.rcParams['font.sans-serif'] = ['SimHei'] #Solve Chinese display
plt.rcParams['axes.unicode_minus'] = False #Solve the problem that symbols cannot be displayed

# Denormalize the price data of the training set and test set
train_prices_scaled = scaler.inverse_transform(train_scaled)
test_prices_scaled = scaler.inverse_transform(test_scaled)
# Denormalize prediction results
predicted_prices_scaled = scaler.inverse_transform(predicted_prices)
#Create date index
test_dates = pd.to_datetime(test_data.index[timesteps:])
plt.figure(figsize=(15, 7))
plt.plot(test_dates, test_prices_scaled[timesteps:], label='Moutai stock closing price-test data')
plt.plot(test_dates, predicted_prices_scaled, label='LSTM predicted closing price')
plt.legend()
plt.show()
</code><img class="look-more-preCode contentImg-no-view" src="//i2.wp.com/csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack.png" alt ="" title="">

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

It can be seen from the visualization results that the blue line is the real data and the orange line is the model prediction data. The overall trend is not much different, indicating that the model effect is not bad.

Design project case demonstration address: https://space.bilibili.com/364224477

,