SOS Final Submission
SOS Final Submission
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.
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:
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.
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.
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.
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:
Add a term called bias ‘b’ to this weighted sum to improve the model’s performance.
Y=f(∑wi*xi + b)
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:
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
REGRESSION LOSSES
(1)
Python3
import numpy as np
# Mean Squared Error
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
Source: Wikipedia
(2)
Python3
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
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
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
l = 0
size = np.size(y)
for i in range(size):
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) :
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.
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.
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
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
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
72 71 ... 10 9 8 7 6
\
Date ...
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
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
72 71 ... 10 9 8 7 6
\
Date ...
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
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']);