0% found this document useful (0 votes)
51 views36 pages

SOS Final Submission

This document provides an overview of an artificial intelligence project to analyze the stock market using machine learning and AI. It first defines AI and describes how it can be applied in domains like online shopping, transportation, marketing, surveillance, agriculture, customer service, healthcare, banking, homes, virtual assistance and more. The document then explains that the main parts of AI used in the project are deep learning, machine learning and neural networks. It provides descriptions of neural networks, machine learning, and deep learning and how they differ. It also gives examples of neural network types and describes how deep learning works through forward and backpropagation. Finally, it discusses perceptrons as the basic unit of neural networks and their components and working.

Uploaded by

Ayush Jadia
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)
51 views36 pages

SOS Final Submission

This document provides an overview of an artificial intelligence project to analyze the stock market using machine learning and AI. It first defines AI and describes how it can be applied in domains like online shopping, transportation, marketing, surveillance, agriculture, customer service, healthcare, banking, homes, virtual assistance and more. The document then explains that the main parts of AI used in the project are deep learning, machine learning and neural networks. It provides descriptions of neural networks, machine learning, and deep learning and how they differ. It also gives examples of neural network types and describes how deep learning works through forward and backpropagation. Finally, it discusses perceptrons as the basic unit of neural networks and their components and working.

Uploaded by

Ayush Jadia
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
You are on page 1/ 36

Artificial

Intelligence
Project-Stock market analysis using Machine
Learning and Artificial Intelligence

-AYUSH JADIA
What is Artificial intelligence?
Artificial intelligence (AI) is intelligence—perceiving, synthesizing, and inferring
information—demonstrated by machines, as opposed to intelligence displayed by
humans or by other animals. "Intelligence" encompasses the ability to learn and to reason, to
generalize, and to infer meaning. You can say that AI is implemented in machines to perform
tasks that actually require human intelligence
How can it help?
Artificial intelligence and its applications are endless. A few of them are listed below:
1.Personalized Online Shopping, 2.Smart Cars,
3. Marketing Enhanced Images, 4.Social Media Surveillance, 5.Agriculture,
6.Customer Service,
7.Video Games, 8.Healthcare,
9.Banks, 10.Smart Homes,
11.Virtual Assistance, 12.Space Exploration,
13.Chatbots.
We will be using AI in our project to predict the stock market.
Parts of AI
AI can be divided into many parts the
main fields are deep learning ,machine
learning, neural networks, natural
language processing, computer vision,
cognitive computing, natural language
processing. The most widely used parts
are deep learning, nueral network and
machine learning . These three will go
hand in hand during our project. Lets get
a brief introduction about them one by
one.
Nueral Network
Artificial neural networks (ANNs) are comprised of a
node layers, containing an input layer, one or more
hidden layers, and an output layer. Each node, or
artificial neuron, connects to another and has an
associated weight and threshold. If the output of any
individual node is above the specified threshold
value, that node is activated, sending data to the
next layer of the network. Otherwise, no data is
passed along to the next layer of the network.

Neural networks rely on training data to learn and


improve their accuracy over time. However, once
these learning algorithms are fine-tuned for
accuracy, they are powerful tools in computer
science and artificial intelligence, allowing us to
classify and cluster data at a high velocity.
Types of neural networks:
there are different types of neural networks to address specific problems or datasets.
Prominents ones are:

Convolutional neural networks (CNNs), used primarily in computer vision and image classification
applications, can detect features and patterns within an image, enabling tasks, like object detection
or recognition. In 2015, a CNN bested a human in an object recognition challenge for the first time.

Recurrent neural network (RNNs) are typically used in natural language and speech recognition
applications as it leverages sequential or times series data.
Machine learning
Machine learning is a branch of artificial
intelligence (AI) and computer science
which focuses on the use of data and
algorithms to imitate the way that
humans learn, gradually improving its
accuracy. Machine learning is more
dependent on human intervention to
learn. Human experts determine the set
of features to understand the differences
between data inputs, usually requiring
more structured data to learn.
Deep learning
Deep learning is a essentially a neural network with
three or more layers. These neural networks attempt
to simulate the behavior of the human brain—albeit
far from matching its ability—allowing it to “learn”
from large amounts of data. While a neural network
with a single layer can still make approximate
predictions, additional hidden layers can help to
optimize and refine for accuracy. The way in which
deep learning and machine learning differ is in how
each algorithm learns. "Deep" machine learning can
use labeled datasets, also known as supervised
learning, to inform its algorithm, but it doesn’t
necessarily require a labeled dataset. Deep learning
can ingest unstructured data in its raw form (e.g.,
text or images), and it can automatically determine
the set of features which distinguish different
categories of data from one another
How deep learning works :
Deep neural networks consist of multiple layers of interconnected nodes, each building
upon the previous layer to refine and optimize the prediction or categorization. This
progression of computations through the network is called forward propagation. The input
and output layers of a deep neural network are called visible layers. The input layer is where
the deep learning model ingests the data for processing, and the output layer is where the
final prediction or classification is made.
Another process called backpropagation uses algorithms, like gradient descent, to calculate
errors in predictions and then adjusts the weights and biases of the function by moving
backwards through the layers in an effort to train the model. Together, forward propagation
and backpropagation allow a neural network to make predictions and correct for any errors
accordingly. Over time, the algorithm becomes gradually more accurate
 Ar ficial neural network

Perceptron:

 Basic introduction:

Perceptron was introduced by Frank Rosenblatt in 1957. He proposed a Perceptron learning


rule based on the original MCP neuron. A Perceptron is an algorithm for supervised learning
of binary classifiers. This algorithm enables neurons to learn and processes elements in the
training set one at a time. Perceptron is a type of artificial neural network, which is a
fundamental concept in machine learning.

 Basic Components of Perceptron


The basic components of a perceptron are:

1. Input Layer: The input layer consists of one or more input neurons, which
receive input signals from the external world or from other layers of the
neural network.

2. Weights: Each input neuron is associated with a weight, which represents


the strength of the connection between the input neuron and the output
neuron.

3. Bias: A bias term is added to the input layer to provide the perceptron with
additional flexibility in modeling complex patterns in the input data.

4. Activation Function: The activation function determines the output of the


perceptron based on the weighted sum of the inputs and the bias term.
Common activation functions used in perceptrons include the step
function, sigmoid function, and ReLU function.

5. Output: The output of the perceptron is a single binary value, either 0 or 1,


which indicates the class or category to which the input data belongs.

6. Training Algorithm: The perceptron is typically trained using a supervised


learning algorithm such as the perceptron learning algorithm or
backpropagation. During training, the weights and biases of the perceptron
are adjusted to minimize the error between the predicted output and the
true output for a given set of training examples.

7. Overall, the perceptron is a simple yet powerful algorithm that can be used
to perform binary classification tasks and has paved the way for more
complex neural networks used in deep learning today.

How Does Perceptron Work?

Perceptron is considered a single-layer neural link with four main parameters. The
perceptron model begins with multiplying all input values and their weights, then
adds these values to create the weighted sum. Further, this weighted sum is applied
to the activation function ‘f’ to obtain the desired output. This activation function is
also known as the step function and is represented by ‘f.’
This step function or Activation function is vital in ensuring that output is mapped
between (0,1) or (-1,1). Take note that the weight of input indicates a node’s
strength. Similarly, an input value gives the ability the shift the activation function
curve up or down.

Step 1: Multiply all input values with corresponding weight values and then add to
calculate the weighted sum. The following is the mathematical expression of it:

∑wi*xi = x1*w1 + x2*w2 + x3*w3+……..x4*w4

Add a term called bias ‘b’ to this weighted sum to improve the model’s performance.

Step 2: An activation function is applied with the above-mentioned weighted sum


giving us an output either in binary form or a continuous value as follows:

Y=f(∑wi*xi + b)

Activation Functions of Perceptron

The activation function applies a step rule (convert the numerical output into +1 or -
1) to check if the output of the weighting function is greater than zero or not.
For example:

If ∑ wixi> 0 => then final output “o” = 1.

Else, final output “o” = -1.

Step function gets triggered above a certain value of the neuron output; else it
outputs zero. Sign Function outputs +1 or -1 depending on whether neuron output is
greater than zero or not. Sigmoid is the S-curve and outputs a value between 0 and 1.

 Use of perceptron:
Mostly preceptron is used in decision making where the output can be
true or false. Now perceptrons are able to differen ate between linearly
differen able classes/data only. Since by the algorithm we can see that
the we can assign 0(false) and 1(true) depending on the output which is
nothing but

∑wi*xi where I foes from 0 to n where w0=1 and


x0=b (bias)
Now you can see that this equa on is a line in 2-d plane(n=2) plane in 3-
d(n=3) and hyperplane for n>=4.
Therefore this perceptron works efficiently when the data is linearly
differen able.
 Loss functions:
The loss function estimates how well a particular algorithm models the
provided data. Loss functions are classified into two classes based on the
type of learning task
 Regression Models: predict continuous values.
 Classification Models: predict the output from a set of finite
categorical values.

REGRESSION LOSSES

1.) Mean Squared Error (MSE) / Quadratic Loss / L2 Loss


 It is the Mean of Square of Residuals for all the datapoints in the
dataset. Residuals is the difference between the actual and the
predicted prediction by the model.
 Squaring of residuals is done to convert negative values to positive
values. The normal error can be both negative and positive. If some
positive and negative numbers are summed up, the sum maybe 0.
This will tell the model that the net error is 0 and the model is
performing well but contrary to that, the model is still performing
badly. Thus, to get the actual performance of the model, only
positive values are taken to get positive, squaring is done.
 Squaring also gives more weightage to larger errors. When the
cost function is far away from its minimal value, squaring the error
will penalize the model more and thus helping in reaching the
minimal value faster.
 Mean of the Square of Residuals is taking instead of just taking the
sum of square of residuals to make the loss function independent
of number of datapoints in the training set.
 MSE is sensitive to outliers.

(1)
 Python3

import numpy as np
# Mean Squared Error

def mse( y, y_pred ) :

return np.sum( ( y - y_pred ) ** 2 ) / np.size( y )

where,
i - ith training sample in a dataset
n - number of training samples
y(i) - Actual output of ith training sample
y-hat(i) - Predicted value of ith training sample

2.) Mean Absolute Error (MAE) / La Loss


 It is the Mean of Absolute of Residuals for all the datapoints in the
dataset. Residuals is the difference between the actual and the
predicted prediction by the model.
 The absolute of residuals is done to convert negative values to
positive values.
 Mean is taken to make the loss function independent of number of
datapoints in the training set.
 One advantage of MAE is that is robust to outliers.
 MAE is generally less preferred over MSE as it is harder to
calculate the derivative of the absolute function because absolute
function is not differentiable at the minima.

Source: Wikipedia
(2)
 Python3

# Mean Absolute Error

def mae( y, y_pred ) :

return np.sum( np.abs( y - y_pred ) ) / np.size( y )

Output

3.) Mean Bias Error: It is the same as MSE. but less accurate and can
could conclude if the model has a positive bias or negative bias.

(3)
 Python3

# Mean Bias Error

def mbe( y, y_pred ) :

return np.sum( y - y_pred ) / np.size( y )

4.) Huber Loss / Smooth Mean Absolute Error


 It is the combination of MSE and MAE. It takes the good properties
of both the loss functions by being less sensitive to outliers and
differentiable at minima.
 When the error is smaller, the MSE part of the Huber is utilized
and when the error is large, the MAE part of Huber loss is used.
 A new hyper-parameter ‘????‘ is introduced which tells the loss
function where to switch from MSE to MAE.
 Python3

def Huber(y, y_pred, delta):

condition = np.abs(y - y_pred) < delta

l = np.where(condition, 0.5 * (y - y_pred) ** 2,

delta * (np.abs(y - y_pred) - 0.5 * delta))

return np.sum(l) / np.size(y)

CLASSIFICATION LOSSES
Cross-Entropy Loss: Also known as Negative Log Likelihood. It is the
commonly used loss function for classification. Cross-entropy loss progress
as the predicted probability diverges from the actual label.
 Python3

# Binary Loss

def cross_entropy(y, y_pred):

return - np.sum(y * np.log(y_pred) + (1 - y) * np.log(1 - y_pred))


/ np.size(y)

5.) Hinge Loss: Also known as Multi-class SVM Loss. Hinge loss is
applied for maximum-margin classification, prominently for
support vector machines. It is a convex function used in the convex
optimizer.

 Python3

# Hinge Loss

def hinge(y, y_pred):

l = 0

size = np.size(y)

for i in range(size):

l = l + max(0, 1 - y[i] * y_pred[i])

return l / size
We can define all of the loss func on in general way as:
Resources Used :
(1) IBM newsle ers
(2) GeeksforGeeks
(3) h ps://scikit-learn.org/stable/

Tenta ve deadlines:
(1) Week8 ( ll July 2):
ANN ,MLP,
(2) Week 9 ( ll July 9):
backpropoga on ,Keras tuner
(3) Week 10 ( ll July 13)
CNN,RNN.
(4) Week 11( ll July 14):
Comple on of project tes ng and debugging.
 A brief introduction to Convolutional Neural
Network(CNN) :

A Convolutional Neural Network (CNN) is a deep learning


algorithm specifically designed for image recognition and
processing tasks. It comprises several layers, including
convolutional layers, pooling layers, and fully connected
layers.
The fundamental component of a CNN is the convolutional
layers, responsible for applying filters to the input image to
extract significant features like edges, textures, and shapes.
These convolutional layers produce feature maps, which are
then passed through pooling layers to down-sample the data,
reducing spatial dimensions while preserving essential
information. Finally, the output of the pooling layers is fed
into one or more fully connected layers, enabling the CNN to
make predictions or classify the image.
In summary, CNNs are specialized deep learning algorithms
suited for image-related tasks, with their unique
convolutional, pooling, and fully connected layers allowing
them to automatically learn and identify crucial patterns and
features within images.
 What are convolutional neural
networks?
Neural networks are a subset of machine learning and are
at the heart of deep learning algorithms. They are
composed of node layers, including an input layer, one or
more hidden layers, and an output layer. Each node has an
associated weight and threshold and connects to another
node. If the output of any individual node is above the
specified threshold value, that node is activated, sending
data to the next layer of the network. Otherwise, no data is
passed along to the next layer of the network. While the
article primarily focused on feedforward networks, there
are various types of neural nets used for different use
cases and data types. For example, recurrent neural
networks are commonly used for natural language
processing and speech recognition, while convolutional
neural networks (ConvNets or CNNs) are more often
utilized for classification and computer vision tasks. Prior
to CNNs, manual, time-consuming feature extraction
methods were used to identify objects in images. However,
convolutional neural networks now provide a more
scalable approach to image classification and object
recognition tasks, leveraging principles from linear
algebra, specifically matrix multiplication, to identify
patterns within an image. That said, they can be
computationally demanding, requiring graphical
processing units (GPUs) to train models.

The advantages of neural networks include the ability to


learn and model non-linear and complex relationships, the
ability to work with insufficient knowledge, good fault
tolerance, distributed memory, and the ability to perform
multiple tasks in parallel without affecting the system
performance. Neural networks can store information on
the entire network, and they can perform thousands of
routine, repetitive tasks within a relatively shorter period
of time compared to what it would take for a human being.
Additionally, neural networks can execute feature
engineering by themselves, leading to effective visual
analysis. Their structure is adaptive in nature, meaning
that they alter their course of the structure according to
the purpose. Neural networks can perform handwriting
recognition, stock-exchange prediction, and face
recognition, among other applications. However, there are
also some disadvantages to neural networks, such as the
need for large amounts of data, the potential for
overfitting, and the difficulty of interpreting the results .

Convolutional neural networks are distinguished from


other neural networks by their superior performance with
image, speech, or audio signal inputs. They have three
main types of layers, which are:
 Convolutional layer
 Pooling layer
 Fully-connected (FC) layer
The convolutional layer is the first layer of a convolutional
network. While convolutional layers can be followed by
additional convolutional layers or pooling layers, the fully-
connected layer is the final layer. With each layer, the CNN
increases in its complexity, identifying greater portions of
the image. Earlier layers focus on simple features, such as
colors and edges. As the image data progresses through
the layers of the CNN, it starts to recognize larger elements
or shapes of the object until it finally identifies the
intended object.

Convolutional Layer
The convolutional layer is the core building block of a
CNN, and it is where the majority of computation occurs. It
requires a few components, which are input data, a filter,
and a feature map. Let’s assume that the input will be a
color image, which is made up of a matrix of pixels in 3D.
This means that the input will have three dimensions—a
height, width, and depth—which correspond to RGB in an
image. We also have a feature detector, also known as a
kernel or a filter, which will move across the receptive
fields of the image, checking if the feature is present. This
process is known as a convolution.
The feature detector is a two-dimensional (2-D) array of
weights, which represents part of the image. While they
can vary in size, the filter size is typically a 3x3 matrix; this
also determines the size of the receptive field. The filter is
then applied to an area of the image, and a dot product is
calculated between the input pixels and the filter. This dot
product is then fed into an output array. Afterwards, the
filter shifts by a stride, repeating the process until the
kernel has swept across the entire image. The final output
from the series of dot products from the input and the
filter is known as a feature map, activation map, or a
convolved feature.
After each convolution operation, a CNN applies a
Rectified Linear Unit (ReLU) transformation to the feature
map, introducing nonlinearity to the model.
As we mentioned earlier, another convolution layer can
follow the initial convolution layer. When this happens,
the structure of the CNN can become hierarchical as the
later layers can see the pixels within the receptive fields of
prior layers. As an example, let’s assume that we’re trying
to determine if an image contains a bicycle. You can think
of the bicycle as a sum of parts. It is comprised of a frame,
handlebars, wheels, pedals, et cetera. Each individual part
of the bicycle makes up a lower-level pattern in the neural
net, and the combination of its parts represents a higher-
level pattern, creating a feature hierarchy within the CNN.

Pooling Layer
Pooling layers, also known as downsampling, conducts dimensionality
reduction, reducing the number of parameters in the input. Similar to
the convolutional layer, the pooling operation sweeps a filter across
the entire input, but the difference is that this filter does not have any
weights. Instead, the kernel applies an aggregation function to the
values within the receptive field, populating the output array. There
are two main types of pooling:

 Max pooling: As the filter moves across the input, it selects the
pixel with the maximum value to send to the output array. As an
aside, this approach tends to be used more often compared to
average pooling.
 Average pooling: As the filter moves across the input, it
calculates the average value within the receptive field to send to
the output array.

While a lot of information is lost in the pooling layer, it also has a


number of benefits to the CNN. They help to reduce complexity,
improve efficiency, and limit risk of overfitting.

Fully-Connected Layer
The name of the full-connected layer aptly describes
itself. As mentioned earlier, the pixel values of the input
image are not directly connected to the output layer in
partially connected layers. However, in the fully-
connected layer, each node in the output layer connects
directly to a node in the previous layer.
This layer performs the task of classification based on
the features extracted through the previous layers and
their different filters. While convolutional and pooling
layers tend to use ReLu functions, FC layers usually
leverage a softmax activation function to classify inputs
appropriately, producing a probability from 0 to 1.
 Recurrent Neural Networks
A recurrent neural network (RNN) is a type of artificial neural
network that works with sequential or time series data. These deep
learning algorithms are commonly used for temporal or ordinal
problems, such as natural language processing (NLP), language
translation, speech recognition, and image captioning. They are
incorporated into popular applications such as Siri, voice search,
and Google Translate. Unlike feedforward and convolutional neural
networks (CNNs), recurrent neural networks have "memory" and
take information from prior inputs to influence the current input
and output. While traditional deep neural networks assume that
inputs and outputs are independent of each other, the output of
recurrent neural networks depends on the prior elements within the
sequence. However, unidirectional recurrent neural networks cannot
account for future events in their predictions. There are various
types of neural nets used for different use cases and data types. For
example, recurrent neural networks are commonly used for natural
language processing and speech recognition, while convolutional
neural networks (ConvNets or CNNs) are more often utilized for
classification and computer vision tasks.

Recurrent neural networks use the Backpropagation Through Time


(BPTT) algorithm to determine gradients. This is specific to sequence
data, so it differs slightly from traditional backpropagation. The
principle of BPTT is the same as traditional backpropagation, training
the model itself by computing the error from the output layer to the
input layer. These calculations can be used to tune and tune the
parameters of the model accordingly. BPTT differs from traditional
approaches in that BPTT sums the error at each time step, whereas
feedforward networks do not share parameters across all layers, so
there is no need to sum the errors. Through this process, RNNs tend
to run into two problems known as exploding gradients and
vanishing gradients. These problems are defined by the magnitude of
the gradient, which is the slope of the loss function along the error
curve. If the gradient is too small, it gets smaller and smaller and the
weight parameters are updated until it becomes irrelevant. H. 0. In
this case, the algorithm stops learning. Too large a gradient will
result in explosive gradients and an unstable model. In this case, the
weights in the model are too large and are represented as NaN. A
solution to these problems is to reduce the number of hidden layers
in the neural network, thereby he removes some of the complexity
of the RNN model.Different types of recurrent neural networks

Long short-term memory (LSTM):


This is a popular RNN architecture introduced by Sepp Hochreiter and
Jürgen Schmidhuber as a solution to the vanishing gradient problem.
They deal with long term dependency issues. This means that the RNN
model may not be able to accurately predict the current state if the
previous states that influence the current prediction are not recent. For
example, suppose you want to predict the following words in italics:
"Alice is allergic to nuts." She cannot eat peanut butter. The context of
nut allergies helps predict foods you should not eat. Contains nuts.
However, it is difficult or even impossible for RNNs to link information
when the context is several sentences ago. To deal with this, LSTMs have
"cells" in the hidden layer of the neural network that have three gates: an
entry gate, an exit gate, and a forget gate. These gates control the flow of
information needed to predict the output on the network.
Project: Stock Market
Prediction Using
Artificial Intelligence
Prediction using Linear Regression Model
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from urllib.request import urlretrieve

df1=pd.read_csv("https://raw.githubusercontent.com/ayushjadia/SOS-
final-submission/master/Tatacsv.csv",usecols=["Date","Close
Price"],parse_dates=["Date"],index_col='Date')
df1

Close Price
Date
2023-06-01 535.25
2023-05-31 526.45
2023-05-30 518.80
2023-05-29 521.25
2023-05-26 518.55
... ...
2022-06-07 435.95
2022-06-06 432.35
2022-06-03 431.90
2022-06-02 439.00
2022-06-01 444.70

[249 rows x 1 columns]

df = df1.iloc[::-1]
df

Close Price
Date
2022-06-01 444.70
2022-06-02 439.00
2022-06-03 431.90
2022-06-06 432.35
2022-06-07 435.95
... ...
2023-05-26 518.55
2023-05-29 521.25
2023-05-30 518.80
2023-05-31 526.45
2023-06-01 535.25

[249 rows x 1 columns]


stockprices=df["Close Price"]

y_train=stockprices[80:180]
y_test=stockprices[180:]
y_test

Date
2023-02-17 439.90
2023-02-20 442.85
2023-02-21 436.55
2023-02-22 429.50
2023-02-23 433.20
...
2023-05-26 518.55
2023-05-29 521.25
2023-05-30 518.80
2023-05-31 526.45
2023-06-01 535.25
Name: Close Price, Length: 69, dtype: float64

plt.plot(df)

[<matplotlib.lines.Line2D at 0x202b74d5fd0>]

list(df['2023-06-01':"2023-05-26"])
['Close Price']

window_size = 80
data = []
for i in range(100):
window_data = df.iloc[i:i+window_size]['Close Price'].tolist()
data.append(window_data)
X_train = pd.DataFrame(data, columns=range(80, 0, -1),
index=y_train.index)
X_train

80 79 78 77 76 75 74
73 \
Date

2022-09-26 444.70 439.00 431.90 432.35 435.95 435.60 428.55


428.20
2022-09-27 439.00 431.90 432.35 435.95 435.60 428.55 428.20
406.70
2022-09-28 431.90 432.35 435.95 435.60 428.55 428.20 406.70
405.05
2022-09-29 432.35 435.95 435.60 428.55 428.20 406.70 405.05
413.85
2022-09-30 435.95 435.60 428.55 428.20 406.70 405.05 413.85
393.00
... ... ... ... ... ... ... ...
...
2023-02-10 404.30 399.05 398.05 397.95 401.55 404.60 407.65
409.85
2023-02-13 399.05 398.05 397.95 401.55 404.60 407.65 409.85
412.60
2023-02-14 398.05 397.95 401.55 404.60 407.65 409.85 412.60
421.50
2023-02-15 397.95 401.55 404.60 407.65 409.85 412.60 421.50
422.10
2023-02-16 401.55 404.60 407.65 409.85 412.60 421.50 422.10
416.25

72 71 ... 10 9 8 7 6
\
Date ...

2022-09-26 406.70 405.05 ... 451.00 456.95 450.20 447.00


432.40
2022-09-27 405.05 413.85 ... 456.95 450.20 447.00 432.40
425.50
2022-09-28 413.85 393.00 ... 450.20 447.00 432.40 425.50
434.25
2022-09-29 393.00 389.15 ... 447.00 432.40 425.50 434.25
427.70
2022-09-30 389.15 382.75 ... 432.40 425.50 434.25 427.70
432.30
... ... ... ... ... ... ... ... ..
.
2023-02-10 412.60 421.50 ... 445.55 443.55 452.15 446.65
444.65
2023-02-13 421.50 422.10 ... 443.55 452.15 446.65 444.65
445.45
2023-02-14 422.10 416.25 ... 452.15 446.65 444.65 445.45
441.90
2023-02-15 416.25 425.35 ... 446.65 444.65 445.45 441.90
435.25
2023-02-16 425.35 434.90 ... 444.65 445.45 441.90 435.25
440.10

5 4 3 2 1
Date
2022-09-26 425.50 434.25 427.70 432.30 423.05
2022-09-27 434.25 427.70 432.30 423.05 397.65
2022-09-28 427.70 432.30 423.05 397.65 398.80
2022-09-29 432.30 423.05 397.65 398.80 399.05
2022-09-30 423.05 397.65 398.80 399.05 402.30
... ... ... ... ... ...
2023-02-10 445.45 441.90 435.25 440.10 436.65
2023-02-13 441.90 435.25 440.10 436.65 445.60
2023-02-14 435.25 440.10 436.65 445.60 441.10
2023-02-15 440.10 436.65 445.60 441.10 440.35
2023-02-16 436.65 445.60 441.10 440.35 444.00

[100 rows x 80 columns]

window_size = 80
data = []
for i in range(100,169):
window_data2 = df.iloc[i:i+window_size]['Close Price'].tolist()
data.append(window_data2)
X_test = pd.DataFrame(data, columns=range(80, 0, -1),
index=y_test.index)
X_test

80 79 78 77 76 75 74
73 \
Date

2023-02-17 404.60 407.65 409.85 412.60 421.50 422.10 416.25


425.35
2023-02-20 407.65 409.85 412.60 421.50 422.10 416.25 425.35
434.90
2023-02-21 409.85 412.60 421.50 422.10 416.25 425.35 434.90
433.00
2023-02-22 412.60 421.50 422.10 416.25 425.35 434.90 433.00
412.20
2023-02-23 421.50 422.10 416.25 425.35 434.90 433.00 412.20
423.55
... ... ... ... ... ... ... ...
...
2023-05-26 419.00 445.55 443.55 452.15 446.65 444.65 445.45
441.90
2023-05-29 445.55 443.55 452.15 446.65 444.65 445.45 441.90
435.25
2023-05-30 443.55 452.15 446.65 444.65 445.45 441.90 435.25
440.10
2023-05-31 452.15 446.65 444.65 445.45 441.90 435.25 440.10
436.65
2023-06-01 446.65 444.65 445.45 441.90 435.25 440.10 436.65
445.60

72 71 ... 10 9 8 7 6
\
Date ...

2023-02-17 434.90 433.00 ... 445.45 441.90 435.25 440.10


436.65
2023-02-20 433.00 412.20 ... 441.90 435.25 440.10 436.65
445.60
2023-02-21 412.20 423.55 ... 435.25 440.10 436.65 445.60
441.10
2023-02-22 423.55 433.60 ... 440.10 436.65 445.60 441.10
440.35
2023-02-23 433.60 437.05 ... 436.65 445.60 441.10 440.35
444.00
... ... ... ... ... ... ... ... ..
.
2023-05-26 435.25 440.10 ... 515.65 530.80 521.05 515.55
508.40
2023-05-29 440.10 436.65 ... 530.80 521.05 515.55 508.40
524.75
2023-05-30 436.65 445.60 ... 521.05 515.55 508.40 524.75
522.35
2023-05-31 445.60 441.10 ... 515.55 508.40 524.75 522.35
528.50
2023-06-01 441.10 440.35 ... 508.40 524.75 522.35 528.50
520.20

5 4 3 2 1
Date
2023-02-17 445.60 441.10 440.35 444.00 441.55
2023-02-20 441.10 440.35 444.00 441.55 439.90
2023-02-21 440.35 444.00 441.55 439.90 442.85
2023-02-22 444.00 441.55 439.90 442.85 436.55
2023-02-23 441.55 439.90 442.85 436.55 429.50
... ... ... ... ... ...
2023-05-26 524.75 522.35 528.50 520.20 514.70
2023-05-29 522.35 528.50 520.20 514.70 518.55
2023-05-30 528.50 520.20 514.70 518.55 521.25
2023-05-31 520.20 514.70 518.55 521.25 518.80
2023-06-01 514.70 518.55 521.25 518.80 526.45

[69 rows x 80 columns]

lr=LinearRegression()
lr.fit(X_train,y_train)
y_lr = lr.predict(X_test)
y_lr = pd.Series(y_lr,index=y_test.index)

plt.plot(df,label="actual")
plt.plot(y_lr,label="Predicted")
plt.legend(['Actual','Predicted']);

You might also like