# A Beginner's Guide to Recurrent Neural Networks (Part 2 of 2)

By the end of this tutorial, you’ll have this.

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1213d224-0e86-46e3-a0ff-b1df3cacbb03_1000x600.jpeg align="left")

Let’s get started.

For the purpose of this tutorial, I am using [Netflix Stock Price Data available on Kaggle](https://www.kaggle.com/datasets/jainilcoder/netflix-stock-price-prediction/) and only choose to predict the Daily High of the stock price.

**Step 1 - Preprocess the Data**

```python
from sklearn.preprocessing import MinMaxScaler

high_data = data["High"].values.reshape(-1,1)

scaler = MinMaxScaler()
scaled_high_data = scaler.fit_transform(high_data)
```

The code snippet above does 2 things.

First, it selects only the “High” column data and converts it into a NumPy Array. Then it changes its shape. Understand this from the example.

```python
data["High"].values
```

This part of the code is responsible for converting the “High” column data to a NumPy Array. It will give the output like

```plaintext
[10,20,30,40,50]
```

Then, `.reshape(-1,1)` converts this array into a two-dimensional array where in each dimension, you have only 1 element. The output of the above array now looks like this

```plaintext
[[10],
 [20],
 [30],
 [40], 
 [50]]
```

**Step 2 - Preparing the Dataset**

```python
def create_dataset(dataset, time_step=1):
    X, Y = [], []
    for i in range(len(dataset)-time_step-1):
        a = dataset[i:(i+time_step), 0]
        X.append(a)
        Y.append(dataset[i + time_step, 0])
    return np.array(X), np.array(Y)


X, y = create_dataset(scaled_high_data)

X = X.reshape(X.shape[0], X.shape[1], 1)
```

In Step 2 of this tutorial, we're preparing our dataset for the RNN model.

The `` `create_dataset` `` function is key here. It takes our scaled stock price data and creates sequences for training. Each sequence consists of consecutive days' 'High' prices, determined by `time_step`. If \``` time_step` `` is 1, we use today's price to predict tomorrow's. The function goes through the entire dataset, creating these sequences (`X`) and their next day's price (`Y`). Finally, we reshape `X` to fit the RNN's expected input shape, making it ready for the neural network to process.

This step is crucial as it aligns our data with the way RNNs learn temporal patterns.

**Step 3 - Keras Model**

```python
model = Sequential()

model.add(SimpleRNN(units=50, activation="relu", input_shape=(1,1)))
model.add(Dense(units=1))

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

model.fit(X, y, epochs=2, batch_size=16, verbose=1)
```

The process of developing a model with Keras begins by initializing a `Sequential` model, which allows us to stack layers linearly. Within this model, we add a `SimpleRNN` layer with 50 units. The activation function 'relu' helps the model learn non-linear patterns. The `input_shape` is set according to our preprocessed data. Next, we add a `Dense` layer with a single unit. This is our output layer that will predict the next day's 'High' value.

The model is compiled with the 'adam' optimizer and 'mean\_squared\_error' loss function, both standard choices for regression problems. Finally, we fit the model to our prepared data (`X` and `y`). The `epochs` parameter controls how many times the model will see the entire dataset, and `batch_size` determines how many data points the model sees before updating its internal parameters. With these steps, our model is ready to learn from the data and make predictions.

And that’s it, you are ready to run the code and build your own Stock Price Prediction Model.
