Exclusive | Stock Price Prediction Using LSTM

Exclusive | Stock Price Prediction Using LSTM

Author: Siddharth M
Translator: Wang Kehan
Proofreader: Ouyang Jin




This article is approximately 1300 words long and is recommended for a 6-minute read.
This article teaches you how to use LSTM networks to predict stock price trends and visualize opening and closing prices.



1. Introduction

Long Short-Term Memory (LSTM) is a model that can enhance the memory of Recurrent Neural Networks (RNNs). RNNs retain short-term memory as they allow for earlier information to be determined in the current neural network. For immediate tasks, RNNs use earlier data, but we may not be utilizing all the early information from the neurons. LSTM has been widely applied in RNNs. LSTM’s effectiveness has been demonstrated in various application fields such as video, natural language processing, geospatial analysis, and time series.
A major problem with RNNs is the gradient vanishing problem, which arises from reusing the same parameters within RNN blocks. We must try to use different parameters at each time step to overcome this issue.
We strive to find a balance in such situations. While generating variable-length sequences, we introduce new parameters at each step while keeping the total number of learnable parameters unchanged. We introduce RNN units based on gated mechanisms, such as LSTM and GRU.
Gated units retain internal variables, utilizing gates among them. The value of each gate at each time step depends on the information of that time step, including previous states. Then, the gate values are multiplied by different weight variables to influence them. Time series data is a series of data values collected over a period, allowing us to track differences over time. Time series data can be tracked in units of milliseconds, days, and years.
Initially, we viewed time series data as static; daily temperature highs and lows, stock market opening and closing prices. Now we will move into the coding section. We will implement LSTM on a stock dataset.

Dataset:

https://github.com/PacktPublishing/Learning-Pandas-Second-Edition/blob/master/data/goog.csv

2. Implementing Time Series Prediction for Stocks Using LSTM

Reading Data:
gstock_data = pd.read_csv('data.csv')
gstock_data.head()

Exclusive | Stock Price Prediction Using LSTM

Dataset Exploration:

This dataset contains 14 columns related to time series (such as date) and various variables (such as close, high, low, and volume). We will use the opening and closing prices for time series prediction with LSTM.
gstock_data = gstock_data[['date','open','close']]
gstock_data['date'] = pd.to_datetime(gstock_data['date'].apply(lambda x: x.split()[0]))
gstock_data.set_index('date', drop=True, inplace=True)
gstock_data.head()

Exclusive | Stock Price Prediction Using LSTM

We performed some feature extraction here. We separately took the date from the entire date variable. Now we can use matplotlib to visualize the available data and see how the price values appear in the data. In the price-date graph shown below, green represents the opening price, and red represents the closing price.
fg, ax = plt.subplots(1, 2, figsize=(20, 7))
ax[0].plot(gstock_data['open'], label='Open', color='green')
ax[0].set_xlabel('Date', size=15)
ax[0].set_ylabel('Price', size=15)
ax[0].legend()
ax[1].plot(gstock_data['close'], label='Close', color='red')
ax[1].set_xlabel('Date', size=15)
ax[1].set_ylabel('Price', size=15)
ax[1].legend()
fg.show()

Exclusive | Stock Price Prediction Using LSTM

Data Preprocessing:

We must preprocess this data before applying LSTM to stock prices. We use the fit_transform function to transform the values in the data. A Min-Max scaler is used to scale the data so that we can normalize all the price values. We then use 80% of the data for training, with the remaining 20% for testing, assigning them to separate variables.
from sklearn.preprocessing import MinMaxScaler
Ms = MinMaxScaler()
gstock_data[gstock_data.columns] = Ms.fit_transform(gstock_data)
training_size = round(len(gstock_data) * 0.80)
train_data = gstock_data[:training_size]
test_data = gstock_data[training_size:]

Splitting Training Data:

Create a function so we can create sequences for training and testing.
def create_sequence(dataset):  
    sequences = []  
    labels = []
    start_idx = 0
    for stop_idx in range(50, len(dataset)):    
        sequences.append(dataset.iloc[start_idx:stop_idx])
        labels.append(dataset.iloc[stop_idx])
        start_idx += 1  
    return (np.array(sequences), np.array(labels))
train_seq, train_label = create_sequence(train_data)
test_seq, test_label = create_sequence(test_data)

Implementing the LSTM Model:

In the next step, we create the LSTM model. In this article, we will use the Sequential model imported from Keras and import the required libraries.
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, Bidirectional
We used two LSTM layers in the model and applied dropout between the layers for regularization. The number of units allocated in the LSTM parameters is 50. Dropout is set at 10%. The loss function is mean squared error and the Adam optimizer optimizes the loss function of the problem. The mean absolute error is the evaluation metric we use in the LSTM network as it relates to time series data.
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(train_seq.shape[1], train_seq.shape[2])))
model.add(Dropout(0.1))
model.add(LSTM(units=50))
model.add(Dense(2))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mean_absolute_error'])
model.summary()
model.fit(train_seq, train_label, epochs=80, validation_data=(test_seq, test_label), verbose=1)
test_predicted = model.predict(test_seq)
test_inverse_predicted = Ms.inverse_transform(test_predicted)

Exclusive | Stock Price Prediction Using LSTM

Visualization:

After fitting the data with our model, we make predictions with it. We must use inverse transformation to return the transformed function to its original value. Now we can use this data to visualize the predictions.
# Merging actual and predicted data for better visualization
gs_slic_data = pd.concat([gstock_data.iloc[-202:].copy(), pd.DataFrame(test_inverse_predicted, columns=['open_predicted', 'close_predicted'], index=gstock_data.iloc[-202:].index)], axis=1)
gs_slic_data[['open', 'close']] = Ms.inverse_transform(gs_slic_data[['open', 'close']])
gs_slic_data.head()
gs_slic_data[['open', 'open_predicted']].plot(figsize=(10, 6))
plt.xticks(rotation=45)
plt.xlabel('Date', size=15)
plt.ylabel('Stock Price', size=15)
plt.title('Actual vs Predicted for open price', size=15)
plt.show()
gs_slic_data[['close', 'close_predicted']].plot(figsize=(10, 6))
plt.xticks(rotation=45)
plt.xlabel('Date', size=15)
plt.ylabel('Stock Price', size=15)
plt.title('Actual vs Predicted for close price', size=15)
plt.show()

Exclusive | Stock Price Prediction Using LSTM

Exclusive | Stock Price Prediction Using LSTM

3. Conclusion

In this article, we used LSTM to study stock prices and visualize the opening and closing prices.

References:

https://the-learning-machine.com/article/dl/long-short-term-memory

https://www.kaggle.com/amarsharma768/stock-price-prediction-using-lstm/notebook

Original Title:
Stock price using LSTM and its implementation
Original Link:
https://www.analyticsvidhya.com/blog/2021/12/stock-price-prediction-using-lstm/

Editor: Huang Jiyan

Proofreader: Lin Yilin

Translator’s Profile

Exclusive | Stock Price Prediction Using LSTM

Wang Kehan, a direct PhD student in the Department of Mechanical Engineering at Tsinghua University. Previously had a background in physics, developed a strong interest in data science during graduate studies, and is curious about machine learning and AI. Looking forward to seeing the sparks of collision between artificial intelligence and mechanical engineering, computational physics in the research path. Hope to make friends and share more stories about data science, viewing the world through the lens of data science.

Recruitment Information for Translation Team

Job Description: Requires a meticulous heart to translate selected foreign articles into fluent Chinese. If you are a data science/statistics/computer student studying abroad, or working in related fields overseas, or are confident in your language skills, you are welcome to join the translation team.

What You Will Gain: Regular translation training to improve volunteers’ translation skills, enhance awareness of cutting-edge data science, and connect overseas friends with domestic technical application development. The THU Data Team’s background provides good development opportunities for volunteers.

Other Benefits: You will become partners with data scientists from well-known companies, students from prestigious universities like Peking University and Tsinghua University, as well as overseas students.

Click the “Read the original” link at the end of the article to join the Data Team~

Reprint Notice

If you need to reprint, please indicate the author and source prominently at the beginning (Reprinted from: Data Team ID: DatapiTHU), and place a prominent QR code for Data Team at the end of the article. For articles with original identification, please send [Article Name – Public Account Name and ID to be authorized] to the contact email to apply for whitelist authorization and edit as required.

After publishing, please provide the link feedback to the contact email (see below). Unauthorized reprints and adaptations will be pursued legally.

Exclusive | Stock Price Prediction Using LSTM

Click “Read the original” to view the original article

Leave a Comment