# Prediction Model using LSTM with Keras

In this tutorial, we will learn to build a recurrent neural network (LSTM) using Keras library. Keras is a simple tool used to construct neural networks.

There will be the following sections:

- Importing libraries
- Importing Dataset
- Data Preprocessing
- Building an LSTM model
- Training the model on the dataset
- Predicting the test results

**What is a recurrent neural network?**

These are a class of neural networks that allow previous outputs to be used as inputs having hidden states. Recurrent neural networks are mostly used for NLP(Natural Language Processing), Speech Recognition, Time series prediction, etc.

**What is LSTM?**

Long Short Term Memory is a type of recurrent neural network. An LSTM unit contains an input gate, output gate, and a forget gate.

## PREDICTION MODEL using LSTM

We will be building a model to predict the stock price of a company.

**1. IMPORTING LIBRARIES**

import math import numpy as np import pandas as pd from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense, LSTM

Pandas: A python package which is a fast, powerful, and open-source data manipulation tool.

Numpy: A python package used for scientific computing.

Math: A python package used for mathematical functions like ceil.

MinMaxScaler: Transforms the features to a given range.

Sequential: The model used will be sequential.

Dense, LSTM: These are the layers we will use.

So, these are the initial libraries you need to have. You can find documentation of pandas, NumPy, and sklearn libraries at the end of this tutorial.

**2. IMPORTING DATASET**

I am using a dataset of an NSE company. So, I will be using close, high, and low prices as inputs.

dataset = pd.read_csv('NSE-TATAGLOBAL.csv') training_set = dataset.iloc[:, 1:2] dataset = training_set.values training_data_len = math.ceil(len(dataset) * 0.8)

80 percent of the data has been used as a training dataset.

**3. DATA PREPROCESSING**

**Scaling:**

scaler = MinMaxScaler(feature_range=(0,1)) scaled_data = scaler.fit_transform(dataset) train_data = scaled_data[0:training_data_len , :] print(train_data)

We are scaling the data between 0 and 1 as specified in the range.

OUTPUT:

[[0.6202352 ] [0.62226277] [0.64436334] ... [0.13260341] [0.13807786] [0.15794809]]

Now next we will create data with timesteps. And here I am giving 80 timesteps.

x_train = [] y_train = [] for i in range(80, len(train_data)): x_train.append(train_data[i-80:i, 0]) y_train.append(train_data[i, 0])

Next, convert the data into a 3D array with x_train samples, 80 timestamps, and one feature at each step.

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))

**x_train:**

print(x_train)

OUTPUT:

[[[0.6202352 ] [0.62226277] [0.64436334] ... [0.75344688] [0.736618 ] [0.70721817]] [[0.62226277] [0.64436334] [0.61719384] ... [0.736618 ] [0.70721817] [0.74574209]] [[0.64436334] [0.61719384] [0.61820762] ... [0.70721817] [0.74574209] [0.7648013 ]] ... [[0.14557989] [0.14497161] [0.14801298] ... [0.14760746] [0.15369019] [0.14801298]] [[0.14497161] [0.14801298] [0.14476886] ... [0.15369019] [0.14801298] [0.13260341]] [[0.14801298] [0.14476886] [0.11719384] ... [0.14801298] [0.13260341] [0.13807786]]]

**y_train:**

print(y_train)

OUTPUT:

[0.74574209 0.7648013 0.75385239 ... 0.13260341 0.13807786 0.15794809]

**4. BUILDING THE LSTM MODEL**

Now, we will construct a model with three lstm layers, one hidden layer, and an output layer.

model = Sequential() model.add(LSTM(50, return_sequences=True, input_shape= (x_train.shape[1], 1))) model.add(LSTM(50, return_sequences= True)) model.add(LSTM(50, return_sequences= False)) model.add(Dense(25)) model.add(Dense(1))

Here, for the first lstm layer we give input shape since we have considered it as the input layer. And return sequences will be ‘True’ as we have the next layer as lstm layer. Even in the second layer return sequences is ‘True’ for the above-mentioned reason.

Next, we will compile the model.

model.compile(optimizer='adam', loss='mean_squared_error')

I have used adam as the optimizer and mean square error as the loss function.

Thus, the model has been built.

**5. TRAINING THE MODEL ON THE DATASET**

To train our model with the dataset, we will pass x_train and y_train into the fit() function. And I am giving the number of epochs as 1 as it takes a lot of time. This takes time as I am using three lstm layers.

model.fit(x_train, y_train, batch_size=1, epochs=1)

OUTPUT:

## Leave a Reply