Netflix is one of the most popular OTT streaming platforms. It offers a vast collection of television series and films and owns its productions known as Netflix Originals. People who are highly active in stock market investments always keep an eye on companies like Netflix because of its popularity. If you are one of them who would like to learn about how to predict the stock prices of Netflix with machine learning, this article is for you. In this article, I will take you through the task of Netflix stock price prediction with Machine Learning using Python.
Netflix Stock Price Prediction with Machine Learning
To predict the stock prices of Netflix with machine learning, I will be using the LSTM neural network as it is one of the best approaches for regression analysis and time series forecasting. So here, I will start by importing the necessary Python libraries and collecting the latest stock price data of Netflix:
import pandas as pd import yfinance as yf import datetime from datetime import date, timedelta today = date.today() d1 = today.strftime("%Y-%m-%d") end_date = d1 d2 = date.today() - timedelta(days=5000) d2 = d2.strftime("%Y-%m-%d") start_date = d2 data = yf.download('NFLX', start=start_date, end=end_date, progress=False) data["Date"] = data.index data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]] data.reset_index(drop=True, inplace=True) print(data.tail())
Date Open High ... Close Adj Close Volume 3442 2022-02-01 432.959991 458.480011 ... 457.130005 457.130005 22568100 3443 2022-02-02 448.250000 451.980011 ... 429.480011 429.480011 14346000 3444 2022-02-03 421.440002 429.260010 ... 405.600006 405.600006 9905200 3445 2022-02-04 407.309998 412.769989 ... 410.170013 410.170013 7782400 3446 2022-02-07 410.170013 412.350006 ... 402.100006 402.100006 8228000 [5 rows x 7 columns]
The above dataset is collected using the yfinance API in Python. You can learn more about it from here. Now let’s visualize the stock price data of Netflix by using a candlestick chart as it gives a clear picture of the increase and decrease of stock prices:
import plotly.graph_objects as go figure = go.Figure(data=[go.Candlestick(x=data["Date"], open=data["Open"], high=data["High"], low=data["Low"], close=data["Close"])]) figure.update_layout(title = "Netflix Stock Price Analysis", xaxis_rangeslider_visible=False) figure.show()

Now let’s have a look at the correlation of all the columns with the Close column:
correlation = data.corr() print(correlation["Close"].sort_values(ascending=False))
Close 1.000000 Adj Close 1.000000 High 0.999837 Low 0.999831 Open 0.999643 Volume -0.395022 Name: Close, dtype: float64
Training LSTM for Netflix Stock Price Prediction
Now I will train the LSTM neural network model for the task of Netflix stock price prediction using Python. Here I will first split the data into training and test sets:
x = data[["Open", "High", "Low", "Volume"]] y = data["Close"] x = x.to_numpy() y = y.to_numpy() y = y.reshape(-1, 1) from sklearn.model_selection import train_test_split xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.2, random_state=42)
Now I will prepare the LSTM neural network architecture:
from keras.models import Sequential from keras.layers import Dense, LSTM model = Sequential() model.add(LSTM(128, return_sequences=True, input_shape= (xtrain.shape[1], 1))) model.add(LSTM(64, return_sequences=False)) model.add(Dense(25)) model.add(Dense(1)) model.summary()
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm (LSTM) (None, 4, 128) 66560 lstm_1 (LSTM) (None, 64) 49408 dense (Dense) (None, 25) 1625 dense_1 (Dense) (None, 1) 26 ================================================================= Total params: 117,619 Trainable params: 117,619 Non-trainable params: 0 _________________________________________________________________
Now here’s how we can train an LSTM neural network model for predicting the stock prices of Netflix using Python:
model.compile(optimizer='adam', loss='mean_squared_error') model.fit(xtrain, ytrain, batch_size=1, epochs=30)
Epoch 1/30 2757/2757 [==============================] - 26s 8ms/step - loss: 7604.0146 Epoch 2/30 2757/2757 [==============================] - 21s 8ms/step - loss: 631.5478 Epoch 3/30 2757/2757 [==============================] - 21s 8ms/step - loss: 464.4248 Epoch 4/30 2757/2757 [==============================] - 21s 8ms/step - loss: 372.2728 Epoch 5/30 2757/2757 [==============================] - 23s 8ms/step - loss: 393.3465 Epoch 6/30 2757/2757 [==============================] - 23s 8ms/step - loss: 388.6576 Epoch 7/30 2757/2757 [==============================] - 24s 9ms/step - loss: 256.3762 Epoch 8/30 2757/2757 [==============================] - 24s 9ms/step - loss: 233.4001 Epoch 9/30 2757/2757 [==============================] - 24s 9ms/step - loss: 277.1848 Epoch 10/30 2757/2757 [==============================] - 24s 9ms/step - loss: 271.6196 Epoch 11/30 2757/2757 [==============================] - 21s 8ms/step - loss: 314.4778 Epoch 12/30 2757/2757 [==============================] - 22s 8ms/step - loss: 310.0940 Epoch 13/30 2757/2757 [==============================] - 22s 8ms/step - loss: 230.9971 Epoch 14/30 2757/2757 [==============================] - 22s 8ms/step - loss: 320.0016 Epoch 15/30 2757/2757 [==============================] - 21s 8ms/step - loss: 266.8751 Epoch 16/30 2757/2757 [==============================] - 22s 8ms/step - loss: 184.3275 Epoch 17/30 2757/2757 [==============================] - 23s 8ms/step - loss: 187.5371 Epoch 18/30 2757/2757 [==============================] - 23s 8ms/step - loss: 262.0464 Epoch 19/30 2757/2757 [==============================] - 23s 9ms/step - loss: 124.5728 Epoch 20/30 2757/2757 [==============================] - 22s 8ms/step - loss: 235.3674 Epoch 21/30 2757/2757 [==============================] - 22s 8ms/step - loss: 210.1229 Epoch 22/30 2757/2757 [==============================] - 22s 8ms/step - loss: 149.8269 Epoch 23/30 2757/2757 [==============================] - 22s 8ms/step - loss: 137.0508 Epoch 24/30 2757/2757 [==============================] - 22s 8ms/step - loss: 182.6988 Epoch 25/30 2757/2757 [==============================] - 22s 8ms/step - loss: 139.5222 Epoch 26/30 2757/2757 [==============================] - 23s 8ms/step - loss: 174.5475 Epoch 27/30 2757/2757 [==============================] - 23s 9ms/step - loss: 139.0385 Epoch 28/30 2757/2757 [==============================] - 23s 8ms/step - loss: 135.3190 Epoch 29/30 2757/2757 [==============================] - 21s 8ms/step - loss: 122.6136 Epoch 30/30 2757/2757 [==============================] - 21s 8ms/step - loss: 182.0294 <keras.callbacks.History at 0x7f0057f21790>
Now let’s test the model by giving the inputs according to the features that we used to train this model for predicting the final results:
import numpy as np #features = [Open, High, Low, Adj Close, Volume] features = np.array([[401.970001, 427.700012, 398.200012, 20047500]]) model.predict(features)
array([[408.57364]], dtype=float32)
So this is how we can train an LSTM neural network model for the task of Netflix stock price prediction with machine learning using Python.
Summary
So this is how we can use machine learning to predict the stock prices of Netflix. Netflix is one of the most popular OTT streaming platforms. People who are highly active in stock market investments always keep an eye on companies like Netflix because of its popularity. I hope you liked this article on the task of Netflix stock price prediction with machine learning using Python. Feel free to ask valuable questions in the comments section below.
Hello and many thanks for the lesson. I just faced with a problem and hope you could help me to resolve it.
I run the code properly. Just when I want to fit the mode, I receive the following error:
“ValueError: Error when checking input: expected lstm_13_input to have shape (4, 1) but got array with shape (1, 4)”
Could you please why this problem happens, and how I can resole it.
Many thanks for your guidance.
check your x and y values, you must have y should only have the label column!