0% found this document useful (0 votes)
30 views13 pages

Coca-Cola Stock Price LSTM Prediction

This project utilizes a Long Short-Term Memory (LSTM) neural network to predict the future closing price of Coca-Cola (KO) stock using historical data from Yahoo Finance. The approach includes data collection, preprocessing, model training, and evaluation, aiming to enhance prediction accuracy compared to traditional methods like ARIMA. The model is trained on a dataset spanning from 2012 to 2019 and forecasts future stock prices based on the last 60 days of data.

Uploaded by

harshit kharka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views13 pages

Coca-Cola Stock Price LSTM Prediction

This project utilizes a Long Short-Term Memory (LSTM) neural network to predict the future closing price of Coca-Cola (KO) stock using historical data from Yahoo Finance. The approach includes data collection, preprocessing, model training, and evaluation, aiming to enhance prediction accuracy compared to traditional methods like ARIMA. The model is trained on a dataset spanning from 2012 to 2019 and forecasts future stock prices based on the last 60 days of data.

Uploaded by

harshit kharka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Stock Price Prediction of Coca-Cola Using LSTM

and Yahoo Finance Data


Description:
This project aims to predict the future closing price of Coca-Cola (KO)
stock using a Long Short-Term Memory (LSTM) neural network. The
dataset is sourced from Yahoo Finance, and the model is trained on
historical stock price data. The project follows a systematic approach,
including data collection, preprocessing, feature engineering, model
training, evaluation, and forecasting. By leveraging LSTM, a type of
recurrent neural network (RNN) designed for time series forecasting,
we aim to improve prediction accuracy over traditional methods like
ARIMA.

Useful Libraries
import pandas as pd
import numpy as np
import math
import time
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
import yfinance as yf
from sklearn.metrics import mean_squared_error
plt.style.use('fivethirtyeight')

# Data collection
df = yf.download('KO',start='2012-01-01',end='2019-12-31')

#top 5 records
df.head()

df.index = pd.to_datetime(df.index)

[*********************100%***********************] 1 of 1 completed
# number of rows and column in the dataset
df.shape

(2011, 5)

# visualizing the dataset


plt.figure(figsize=(12,5))
plt.plot(df.index,df['Close'],label='Close')
plt.title('Close Price History')
plt.show()

Preparing Data for LSTM Model

# Scaling data['Close']
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data= scaler.fit_transform(df['Close'])

# Creating sequences for input and output


def Creating_sequences(data,time_steps):
X,Y=[],[]
for i in range(len(data)-time_steps):
X.append(data[i:i+time_steps])
Y.append(data[i+time_steps])
return np.array(X),np.array(Y)

time_steps = 60
X,Y = Creating_sequences(scaled_data, time_steps)

# Split into training and testing sets (80% training, 20% testing)
X_train, Y_train = X[:round(len(X)*0.8)], Y[:round(len(Y)*0.8)]
X_test, Y_test = X[round(len(X)*0.8):], Y[round(len(Y)*0.8):]
# Building LSTM Model
model = Sequential([
LSTM(50, return_sequences=True, input_shape=(time_steps, 1)),
LSTM(50, return_sequences=False),
Dense(25),
Dense(1) # Output layer
])

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

#model fitting
model.fit(X_train ,Y_train,batch_size=20,epochs=10)

Epoch 1/10

/usr/local/lib/python3.11/dist-packages/keras/src/layers/rnn/
rnn.py:200: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(**kwargs)

79/79 ━━━━━━━━━━━━━━━━━━━━ 3s 9ms/step - loss: 0.0154


Epoch 2/10
79/79 ━━━━━━━━━━━━━━━━━━━━ 1s 8ms/step - loss: 6.2889e-04
Epoch 3/10
79/79 ━━━━━━━━━━━━━━━━━━━━ 1s 9ms/step - loss: 5.6216e-04
Epoch 4/10
79/79 ━━━━━━━━━━━━━━━━━━━━ 1s 9ms/step - loss: 5.9754e-04
Epoch 5/10
79/79 ━━━━━━━━━━━━━━━━━━━━ 1s 8ms/step - loss: 5.8601e-04
Epoch 6/10
79/79 ━━━━━━━━━━━━━━━━━━━━ 1s 9ms/step - loss: 5.2358e-04
Epoch 7/10
79/79 ━━━━━━━━━━━━━━━━━━━━ 1s 8ms/step - loss: 4.6525e-04
Epoch 8/10
79/79 ━━━━━━━━━━━━━━━━━━━━ 1s 8ms/step - loss: 4.5041e-04
Epoch 9/10
79/79 ━━━━━━━━━━━━━━━━━━━━ 1s 12ms/step - loss: 4.1670e-04
Epoch 10/10
79/79 ━━━━━━━━━━━━━━━━━━━━ 1s 12ms/step - loss: 3.8168e-04

<keras.src.callbacks.history.History at 0x7e9aebb2f190>

# Predicting values
prediction = model.predict(X_test)

# converting scaled to original values


prediction = scaler.inverse_transform(prediction)
Y_test_pred= scaler.inverse_transform(Y_test)
# plotting actual vs prediction
plt.figure(figsize=(12,5))
plt.plot(df.index[round(len(X)*0.8) +
time_steps:],Y_test_pred,label='Test_Data')
plt.plot(df.index[round(len(X)*0.8) +
time_steps:],prediction,label='Prediction')
plt.legend()
plt.show()

13/13 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step

# Predicting future values


future_predictions = [] # Store future predictions
current_sequence = scaled_data[-time_steps:] # Last 60 days of data
current_sequence = np.reshape(current_sequence, (1, time_steps, 1))

# next 30 days
for _ in range(180):
next_prediction = model.predict(current_sequence)
future_predictions.append(next_prediction[0][0])
# Reshape the prediction and update the sequence
next_prediction = np.reshape(next_prediction, (1, 1, 1))
current_sequence = np.append(current_sequence[:, 1:, :],
next_prediction, axis=1)

# predictions to get actual values


future_predictions =
scaler.inverse_transform(np.array(future_predictions).reshape(-1, 1))

# future predictions
print("Future Predictions:")
print(future_predictions)
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 44ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 44ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 39ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 39ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 40ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 44ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 43ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 43ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 45ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 43ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 32ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 48ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 54ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 52ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 63ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 44ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 51ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 49ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 48ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 50ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 51ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 61ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 60ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 55ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 60ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 57ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 57ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 54ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 54ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 46ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 47ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 39ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 47ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 57ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 40ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 139ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 128ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 64ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 46ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 36ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 33ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
Future Predictions:
[[46.25721 ]
[46.258213]
[46.209663]
[46.13201 ]
[46.036873]
[45.93114 ]
[45.81908 ]
[45.703403]
[45.58594 ]
[45.46797 ]
[45.350407]
[45.233913]
[45.119007]
[45.006046]
[44.895283]
[44.786907]
[44.681038]
[44.57772 ]
[44.47697 ]
[44.37876 ]
[44.28304 ]
[44.189743]
[44.09878 ]
[44.01006 ]
[43.923454]
[43.838875]
[43.756195]
[43.675312]
[43.59611 ]
[43.51849 ]
[43.442352]
[43.367607]
[43.294163]
[43.221947]
[43.150883]
[43.080914]
[43.01197 ]
[42.944008]
[42.876965]
[42.810806]
[42.745502]
[42.681007]
[42.617294]
[42.554344]
[42.492123]
[42.43062 ]
[42.369812]
[42.30969 ]
[42.250233]
[42.19143 ]
[42.133263]
[42.075726]
[42.018806]
[41.962505]
[41.906803]
[41.851685]
[41.797153]
[41.743195]
[41.689804]
[41.636963]
[41.58468 ]
[41.53294 ]
[41.48174 ]
[41.431065]
[41.380905]
[41.331264]
[41.282124]
[41.233482]
[41.185333]
[41.137665]
[41.09047 ]
[41.043747]
[40.99748 ]
[40.951668]
[40.9063 ]
[40.861374]
[40.81688 ]
[40.772816]
[40.72916 ]
[40.68593 ]
[40.643097]
[40.600666]
[40.558624]
[40.51698 ]
[40.475708]
[40.43482 ]
[40.394295]
[40.354137]
[40.314342]
[40.2749 ]
[40.235806]
[40.197056]
[40.15864 ]
[40.120567]
[40.082817]
[40.045395]
[40.00829 ]
[39.971508]
[39.935028]
[39.89886 ]
[39.863 ]
[39.82743 ]
[39.79216 ]
[39.75718 ]
[39.72249 ]
[39.68808 ]
[39.65395 ]
[39.620094]
[39.586514]
[39.5532 ]
[39.520153]
[39.48737 ]
[39.454845]
[39.422573]
[39.39056 ]
[39.358788]
[39.32727 ]
[39.295994]
[39.264954]
[39.234154]
[39.203587]
[39.173252]
[39.143147]
[39.11327 ]
[39.083614]
[39.054176]
[39.024963]
[38.995964]
[38.967175]
[38.9386 ]
[38.910233]
[38.882072]
[38.854115]
[38.82636 ]
[38.798805]
[38.771442]
[38.744278]
[38.717308]
[38.690525]
[38.663933]
[38.637524]
[38.611305]
[38.585266]
[38.559406]
[38.533726]
[38.508224]
[38.48289 ]
[38.457737]
[38.43275 ]
[38.407936]
[38.38329 ]
[38.358807]
[38.334488]
[38.310333]
[38.28634 ]
[38.262505]
[38.23883 ]
[38.215305]
[38.19194 ]
[38.168728]
[38.145668]
[38.122753]
[38.09999 ]
[38.077374]
[38.0549 ]
[38.032574]
[38.01039 ]
[37.98835 ]
[37.966446]
[37.944683]
[37.923058]
[37.901566]
[37.880215]
[37.85899 ]
[37.837902]
[37.816944]
[37.796116]
[37.775417]
[37.754845]
[37.7344 ]]

# Create 30 future dates


future_dates = pd.date_range(start=df.index[-1] +
pd.Timedelta(days=1), periods=30)

# first 30 predictions
future_predictions_30 = future_predictions[:30]

# Plot historical data and future predictions


plt.figure(figsize=(12, 6))
plt.title('30-Day Forecast of Coca-Cola (KO) Closing Price')
plt.xlabel('Date')
plt.ylabel('Close Price USD')
plt.plot(df.index, df['Close'], label='Historical Data')
plt.plot(future_dates, future_predictions_30, label='Future
Predictions', color='red')
plt.legend()
plt.show()
# Live Prediction Loop every 60 seconds

while True:
# latest stock price
live_data = yf.Ticker('KO').history(period='1d')['Close'].values[-
1]

# Scaling values
live_scaled = scaler.transform([[live_data]]) # Shape: (1,1)

# Reshape live_scaled to to 3d
live_scaled = np.reshape(live_scaled, (1,1,1))

# sequence update
current_sequence = np.append(current_sequence[:, 1:, :],
live_scaled, axis=1)

# Predicting next closing price


next_prediction_scaled = model.predict(current_sequence)
next_prediction = scaler.inverse_transform(next_prediction_scaled)

# Print predicted price


print(f"Predicted Next Close Price: ${next_prediction[0][0]:.2f}")

# Wait for 60 seconds before next prediction


time.sleep(60)

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step

/usr/local/lib/python3.11/dist-packages/sklearn/utils/
validation.py:2739: UserWarning: X does not have valid feature names,
but MinMaxScaler was fitted with feature names
warnings.warn(

Predicted Next Close Price: $41.47


1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 58ms/step

/usr/local/lib/python3.11/dist-packages/sklearn/utils/
validation.py:2739: UserWarning: X does not have valid feature names,
but MinMaxScaler was fitted with feature names
warnings.warn(

Predicted Next Close Price: $46.76

/usr/local/lib/python3.11/dist-packages/sklearn/utils/
validation.py:2739: UserWarning: X does not have valid feature names,
but MinMaxScaler was fitted with feature names
warnings.warn(

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 39ms/step


Predicted Next Close Price: $52.26

You might also like