4th Module
4th Module
The fundamental role of a neuron is to receive, process, and transmit information throughout
the body.
BIOLOGICAL NEURONS
A typical biological neuron consists of four main components: dendrites, soma (cell body),
axon, and synapse. The dendrites receive input signals from other neurons and transmit
them to the soma, where the information is processed. Each neuron can connect to thousands
of other neurons through its axon, allowing the transmission of processed signals. When the
received input exceeds a certain threshold, the neuron becomes activated and transmits
signals to other neurons through synapses in the form of electrical impulses, known as
spikes. A neuron may receive signals from one or multiple neurons, and collectively, these
interconnected neurons form complex networks that process information and produce
responses. The basic structure of a biological neuron is illustrated in Figure 10.1.
1|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
ARTIFICIAL NEURONS
Artificial neurons, also referred to as nodes, are modelled after biological neurons. Each node
can receive one or more input signals and process them to produce an output. These nodes are
connected through links, and each connection is associated with a synaptic weight that
represents the strength or importance of the signal transmitted between neurons. The structure
of a single neuron is shown in Figure 10.2.
𝑁𝑒𝑡 − 𝑠𝑢𝑚 = ∑ 𝑥𝑖 𝑤𝑖
𝑖=1
The activation function is a binary step function that produces an output of 1 when the net input
exceeds the threshold value (∅ᵢ), and 0 when it falls below the threshold. Thus, the activation
function is applied to the net sum to determine whether the neuron is activated, as represented
in Eq. (10.2).
2|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
The McCulloch–Pitts neuron model can represent only simple Boolean functions, where inputs
and outputs are binary. For instance, an AND function produces an output only when all inputs
are 1, while an OR function produces an output if at least one input is 1. In this model, both the
weights and threshold values are fixed, limiting its flexibility
3|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
The input layer receives input values (𝑥1 , 𝑥2 , … , 𝑥𝑛 ) and forwards them to the hidden layer
through weighted connections (synaptic weights). Each neuron processes these inputs by
computing a weighted sum, adding a bias, and passing the result through an activation
function. If the computed value exceeds a certain threshold, the neuron becomes activated.
The activation function introduces non-linearity by transforming the weighted sum into an
output value. Finally, the output layer produces the result, typically as a single output value.
Activation Functions
Activation functions are mathematical functions used in neural networks to convert input
signals into output signals. They determine whether a neuron should activate based on the
received inputs. These functions also normalize the output, typically within ranges such as (0,
1) or (−1, 1).
Activation functions can be either linear or non-linear. Linear functions are generally applied
in simple models like binary perceptrons, where data is classified into two groups. In
contrast, non-linear functions are continuous and are essential for handling complex, high-
dimensional data such as audio, images, and video. Common activation functions used in
ANNs include both linear and non-linear types.
𝑓(𝑥) = 𝑥 ∀𝑥
The value of 𝑓(𝑥) increases linearly or proportionally with the value of x. This function is
useful when we do not want to apply any threshold. The output would be just the weighted sum
of input values. The output value ranges between − ∞ 𝑎𝑛𝑑 + ∞
The output value is binary, i.e., 0 or 1 based on the threshold value ∅. If value of 𝑓(𝑥) is greater
than or equal to ∅, it outputs 1 or else it outputs 0.
The output value is bipolar, i.e., +1 or -1 based on the threshold value ∅. If value of 𝑓(𝑥) is
greater than or equal to ∅, it outputs +1 or else it outputs -1.
4|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
It is a commonly used non-linear activation function that produces an S-shaped curve, with
output values ranging between 0 and 1. However, it suffers from the vanishing gradient
problem, where extremely low or high input values result in minimal changes in output,
limiting learning effectiveness.
The Tanh function is a non-linear activation function and a scaled form of the sigmoid function.
It produces output values in the range of −1 to 1 and, like the sigmoid function, it is affected
by the vanishing gradient problem.
2
tan ℎ(𝑥) = −1
1 + 𝑒 −2𝑥
This activation function is widely used in deep learning models, especially in hidden layers, as
it helps reduce the vanishing gradient problem. It outputs 0 for negative input values and
behaves like a linear function for positive inputs.
𝑥 𝑖𝑓 𝑥 ≥ 0
f(𝑥) = max(0, 𝑥) { 0 𝑖𝑓 𝑥 < 0
9. SoftMax Function
This is a non-linear activation function used in the output layer for multi-class classification.
It computes the probability of each class, with outputs ranging between 0 and 1. The probability
for a class is obtained by dividing the exponential of its input value by the sum of exponentials
of all input values.
𝑒 𝑥𝑖
𝑠(𝑥𝑖 ) = 𝑤ℎ𝑒𝑟𝑒 𝑖 = 0, … 𝑘
∑𝑘𝑗=0 𝑒 𝑥𝑖
5|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
Thus, the modified neuron model receives a set of 𝑖𝑛𝑝𝑢𝑡𝑠 𝑥1 , 𝑥2 . . . , 𝑥𝑛 , their associated
𝑤𝑒𝑖𝑔ℎ𝑡𝑠 𝑤1 , 𝑤2 . . . , 𝑤𝑛 and a bias. The summation function 'Net-sum' Eq. (10.13) computes the
weighted sum of the inputs received by the neuron.
𝑛
𝑁𝑒𝑡 − 𝑠𝑢𝑚 = ∑ 𝑥𝑖 𝑤𝑖
𝑖=1
After computing the Net-sum', bias value is added to it and inserted in the activation function
as shown below:
The activation function is a binary step function which outputs a value 1 if f(x) is above the
threshold value ∅, and 𝑎 0 𝑖𝑓 𝑓(𝑥) is below the threshold value ∅. Then, output of a neuron:
1 𝑖𝑓 𝑓(𝑥) ≥ ∅
𝑌={
0 𝑖𝑓 𝑓(𝑥) < ∅
Before learning how a neural network works, let us learn about how a perceptron model works.
6|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
1. Compute the weighted sum by multiplying the inputs with the weights and add the
products.
2. Apply the activation function on the weighted sum:
𝑌 = 𝑆𝑡𝑒𝑝 ((𝑥1 𝑤1 + 𝑥 2 𝑤2 ) − ∅)
3. If the sum is above the threshold value, output the value as positive else output the
value as negative.
4. Calculate the error by subtracting the estimated output 𝑌𝑒𝑠𝑡𝑖𝑚𝑎𝑡𝑒𝑑 from the desired
𝑜𝑢𝑡𝑝𝑢𝑡 𝑌𝑑𝑒𝑠𝑖𝑟𝑒𝑑 .
𝑒𝑟𝑟𝑜𝑟 𝑒(𝑡) = 𝑌𝑑𝑒𝑠𝑖𝑟𝑒𝑑 − 𝑌𝑒𝑠𝑡𝑖𝑚𝑎𝑡𝑒𝑑
[𝐼𝑓 𝑒𝑟𝑟𝑜𝑟 𝑒(𝑡) 𝑖𝑠 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒, 𝑖𝑛𝑐𝑟𝑒𝑎𝑠𝑒 𝑡ℎ𝑒 𝑝𝑒𝑟𝑐𝑒𝑝𝑡𝑟𝑜𝑛 𝑜𝑢𝑡𝑝𝑢𝑡 𝑌 𝑎𝑛𝑑 𝑖𝑓 𝑖𝑡 𝑖𝑠 𝑛𝑒𝑔𝑎𝑡𝑖𝑣𝑒,
𝑑𝑒𝑐𝑟𝑒𝑎𝑠𝑒 𝑡ℎ𝑒 𝑝𝑒𝑟𝑐𝑒𝑝𝑡𝑟𝑜𝑛 𝑜𝑢𝑡𝑝𝑢𝑡 𝑌. ]
5. Update the weights if there is an error:
∆𝑤 = ∝× 𝑒(𝑡) × 𝑥𝑖
𝑤𝑖 = 𝑤𝑖 + ∆𝑤𝑖
𝑤ℎ𝑒𝑟𝑒, 𝑥𝑖 is the input value, 𝑒(𝑡) is the error at step t, ∝ is the learning rate and ∆𝑤𝑖 , is the
difference in weight that has to be added to 𝑤𝑖 .
Example 10.1: Consider a perceptron to represent the Boolean function AND with the initial
weights 𝑤1 = 0.3, 𝑤2 = −0.2, learning rate ∝ = 0.2 and bias ∅ = 0.4 as shown in Figure
10.6. The activation function used here is the Step 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑓(𝑥) which gives the output value
as binary, i.e., 0 𝑜𝑟 1. If value of 𝑓(𝑥) is greater than or equal to 0, it outputs 1 or else it outputs
0. Design a perceptron that performs the Boolean function AND and update the weights until
the Boolean function gives the desired output.
Solution:
Desired output for Boolean function AND is shown in Table 10.1.
Table 10.1: AND Truth Table
𝒙𝟏 𝒙𝟐 𝒀𝒅𝒆𝒔
0 0 0
0 1 0
1 0 0
1 1 1
For each Epoch, weighted sum is calculated and the activation function is applied to compute
the estimated output 𝑌𝑒𝑠𝑡 . Then, 𝑌𝑒𝑠𝑡 is compared with 𝑌𝑑𝑒𝑠 to find the error. If there is an error,
the weights are updated. Tables 10.2 to 10.5 show how the weights are updated in the four
Epochs.
8|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
9|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
XOR Problem
A perceptron model can solve all Boolean functions which are linearly separable. However, the
XOR problem was identified in 1969 by Minsky and Papert. An XOR function returns a 1, if
the two inputs are not equal and a 0 if they are equal. Following is the truth table of an XOR
function shown in Table 10.6.
𝒙𝟏 𝒙𝟐 𝒀
0 0 1
0 1 0
1 0 0
1 1 1
Since the XOR problem is not linearly separable, a single-layer perceptron fails to classify it,
leading to the development of the Multi-Layer Perceptron (MLP). Early MLPs were limited
due to the absence of an effective learning algorithm. In 1974, Paul Werbos introduced the
concept of backpropagation for a three-layer network, and in 1986, David Rumelhart and James
McClelland generalized it for multi-layer networks.
This advancement led to the success of Artificial Neural Networks and Deep Neural Networks
in solving complex problems. The MLP, in particular, can handle non-linearly separable
problems effectively.
This learning approach, called the delta rule (also known as the Widrow-Hoff Rule), is a form
of backpropagation used for training. The training error is commonly defined as half of the
squared difference between the target output and the actual output.
1 (𝑂 − 𝑂𝐸𝑠𝑡𝑜𝑚𝑎𝑡𝑒𝑑 )2
𝑇𝑟𝑎𝑖𝑛𝑖𝑛𝑔 𝐸𝑟𝑟𝑜𝑟 = ∑ 𝐷𝑒𝑠𝑖𝑟𝑒𝑑
2
𝑑𝑒𝑡
𝑤ℎ𝑒𝑟𝑒, 𝑇 is the training dataset, 𝑂𝐷𝑒𝑠𝑖𝑟𝑒𝑑 and 𝑂Estimated are the desired target output and
estimated actual output, respectively, for a training instance d.
Gradient descent is an optimization technique used to minimize the cost function by iteratively
moving toward a local minimum in the direction opposite to the gradient. The step size at each
iteration is controlled by the learning rate and the slope of the gradient.
10 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
This method forms the basis of the backpropagation algorithm used in Multi-Layer Perceptrons
(MLPs). Before exploring MLPs in detail, it is important to understand the various types of
neural networks, which differ in structure, activation functions, and learning mechanisms.
11 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
12 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
These networks are commonly used in deep learning for tasks such as classification, speech
recognition, medical diagnosis, and forecasting. However, they are more complex and
computationally slower than simpler models. The MLP model is illustrated in Figure 10.9.
𝑥1 : (1, 0, 1, 0) → 𝑈𝑛𝑖𝑡 1
𝑥4 : (0, 1, 1, 0) → 𝑈𝑛𝑖𝑡 2
This process is continued for many epochs until the feature map does not change.
13 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
3. Banking and finance: Credit and loan prediction, fraud detection, risk assessment,
currency forecasting, and real-estate valuation.
Limitations of ANN
1. Requires high computational power and parallel processing, especially for large
networks and multiple training epochs.
14 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
15 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
Bayesian Learning
Introduction to Probability-Based Learning
Probability-based learning is an important practical learning approach that combines prior
knowledge (prior probabilities) with observed data. It uses principles of probability theory to
model uncertainty, randomness, and noise, enabling the prediction of future events and inference
of unknown values. This approach is widely used for handling large datasets and applies Bayes’
rule to learn from data and make predictions.
In a probabilistic model, randomness plays a key role, resulting in outcomes that follow a
probability distribution. In contrast, a deterministic model does not involve randomness; it
produces the same output for the same input every time, leading to a single definite outcome.
Bayesian Learning
Bayesian learning differs from general probabilistic learning by incorporating subjective
probabilities, which are based on an individual’s belief or interpretation of an event. These
probabilities are not fixed and can be updated over time as new information becomes available.
Bayesian learning uses prior probabilities along with observed data and applies Bayes’ rule to
estimate the parameters of a model. Two widely used algorithms in this approach are Naïve Bayes
and Bayesian Belief Networks (BBN). These methods utilize prior knowledge and update it
systematically to derive meaningful insights from data.
Fundamentals of Bayes Theorem
The Naïve Bayes model is built on Bayes’ theorem, which is based on three fundamental types of
probabilities:
✓ Prior probability
✓ Likelihood probability
✓ Posterior probability
Prior Probability
Prior probability represents the probability of an event before any evidence or observation is taken
into account. It reflects the initial belief about an event based on existing knowledge.
Likelihood Probability
Likelihood probability refers to the probability of observing the given data under a specific
hypothesis. It is expressed as:
𝑃(𝐸𝑣𝑖𝑑𝑒𝑛𝑐𝑒 ∣ 𝐻𝑦𝑝𝑜𝑡ℎ𝑒𝑠𝑖𝑠)
This indicates how likely the observed evidence is when the hypothesis is assumed to be true.
1|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
Posterior Probability
Posterior probability is the revised probability of a hypothesis after considering new evidence or
data. It is expressed as:
𝑃(𝐻𝑦𝑝𝑜𝑡ℎ𝑒𝑠𝑖𝑠 ∣ 𝐸𝑣𝑖𝑑𝑒𝑛𝑐𝑒)
It represents the updated belief about the hypothesis after incorporating the observed data.
Bayes’ theorem provides a systematic way to update prior beliefs using new evidence. Thus, the
posterior probability is obtained by combining prior knowledge with the likelihood of the observed
data.
Classification Using Bayes Model
Naïve Bayes classification is based on Bayes' Theorem, which provides a way to compute the
probability of a class given observed data. It determines the most likely hypothesis by combining
prior knowledge with new evidence.
The posterior probability 𝑃( ℎ ∣ 𝐸 ) is computed using the prior probability 𝑃(ℎ), the likelihood
𝑃( 𝐸 ∣ ℎ ),and the marginal probability 𝑃(𝐸) It is expressed as:
𝑃( 𝐸 ∣ ℎ )𝑃(ℎ)
𝑃( ℎ ∣ 𝐸 ) = 8.1
𝑃(𝐸)
P(h) represents the prior probability of hypothesis h, which is the initial belief about the
hypothesis before considering any training data or evidence.
𝑃(𝐸) denotes the marginal probability of the evidence E, calculated without knowing which
hypothesis is true.
𝑃(𝐸 ∣ ℎ) is the likelihood, i.e., the probability of observing evidence E assuming that hypothesis
h is true.
𝑃(ℎ ∣ 𝐸)is the posterior probability, which indicates how likely the hypothesis h is after
observing the evidence E.
From Bayes’ theorem, it can be understood that:
Posterior Probability ∝ Prior Probability × Likelihood
Bayes’ theorem enables the computation of posterior probabilities for multiple hypotheses, and
the hypothesis with the highest probability is selected. This selection process is known as the
Maximum A Posteriori (MAP) hypothesis.
2|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
3|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
𝑃(𝐴|𝐵)𝑃(𝐵) 2
𝑃(𝐵|𝐴) = ==
𝑃(𝐴) 5
Example: Application of Bayes Theorem
Consider a boy who has a volleyball tournament the next day, but he is feeling sick today.
Normally, there is a 40% chance that he falls sick. The boy is highly interested in volleyball, so
there is a 90% probability that he participates in the tournament. Additionally, there is a 20%
chance that he becomes sick given that he participates.
Given Data
𝑃(𝑃𝑎𝑟𝑡𝑖𝑐𝑖𝑝𝑎𝑡𝑖𝑛𝑔) = 0.9
𝑃( 𝑆𝑖𝑐𝑘 ∣ 𝑃𝑎𝑟𝑡𝑖𝑐𝑖𝑝𝑎𝑡𝑖𝑛𝑔 ) = 0.2
𝑃(𝑆𝑖𝑐𝑘) = 0.4
Required: Find the probability that the boy participates in the tournament given that he is sick:
𝑃( 𝑃𝑎𝑟𝑡𝑖𝑐𝑖𝑝𝑎𝑡𝑖𝑛𝑔 ∣ 𝑆𝑖𝑐𝑘 )
Solution: Using Bayes’ theorem:
𝑃( 𝑆𝑖𝑐𝑘 ∣ 𝑃𝑎𝑟𝑡𝑖𝑐𝑖𝑝𝑎𝑡𝑖𝑛𝑔 )
𝑃( 𝑃𝑎𝑟𝑡𝑖𝑐𝑖𝑝𝑎𝑡𝑖𝑛𝑔 ∣ 𝑆𝑖𝑐𝑘 ) = 𝑃(𝑃𝑎𝑟𝑡𝑖𝑐𝑖𝑝𝑎𝑡𝑖𝑛𝑔) ×
𝑃(𝑆𝑖𝑐𝑘)
0.9 × 0.2
= = 0.45
0.4
Final Answer: The probability that the boy will participate in the tournament given that he is sick
is 0.45 (or 45%).
Naïve Bayes Algorithm
Naïve Bayes is a supervised classification algorithm used for both binary and multi-class
problems. It is based on Bayes’ theorem and consists of a family of classifiers that follow a
common probabilistic approach.
The algorithm assumes that all features are independent of each other and contribute equally to
the outcome. Although, in reality, features may depend on the target class, they are treated as
independent during computation. Each feature independently contributes to the probability
calculation, which simplifies the classification process.
Naïve Bayes is particularly effective for large datasets and is known for its simplicity, speed, and
efficiency, making it one of the most widely used classification techniques.
Applications
Some common applications of Naïve Bayes include:
✓ Text classification
✓ Recommendation systems
4|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
✓ Face recognition
Example 8.2
Assess a student’s performance using the Naïve Bayes algorithm based on the dataset provided
in Table 8.1. The objective is to predict whether a student will receive a job offer in the final year.
Practical Communication
[Link] CGPA Interactiveness Job Offer
Knowledge Skills
1 ≥9 Yes Very Good Good Yes
2 ≥8 No Good Moderate Yes
3 ≥9 No Average Poor No
4 <8 No Average Good No
5 ≥8 Yes Good Moderate Yes
6 ≥9 Yes Good Moderate Yes
7 <8 Yes Good Poor No
8 ≥9 No Very Good Good Yes
9 ≥8 Yes Good Good Yes
9 ≥8 Yes Average Good Yes
Solution
The training dataset T consists of 10 instances with attributes such as CGPA, Interactiveness,
Practical Knowledge, and Communication Skills, as shown in Table 8.1. The target variable is
Job Offer, which has two possible classes: Yes or No.
5|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
Prior Probabilities
7
𝑃(𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠) =
10
3
𝑃(𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) =
10
Thus, the prior probabilities for the target variable are obtained based on the frequency of each
class in the dataset.
Table 8.2: Frequency Matrix and Prior Probability of Job Offer
Table 8.3 shows the frequency matrix for the feature CGPA.
It is calculated as the number of instances of each attribute value and for a given class value divided
by the number of instances with that class value.
6|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
For example P (CGPA 29 | Job Offer = Yes) denotes the number of instances with ′CGPA ≥
9 and ′Job Offer = Yes′ divided by the total number of instances with 'Job Offer = Yes'. From
the Table 8.3
Frequency Matrix of CGPA, number of instances with 'CGPA 29' and 'Job Offer = Yes' is 3. The
total number of instances with 'Job Offer = Yes' is 7. Hence, P (CGPA ≥9 | Job Offer = Yes) =3/7.
Similarly, the Likelihood probability is calculated for all attribute values of feature CGPA.
Table 8.7 shows the frequency matrix for the feature Practical Knowledge.
Table 8.7: Frequency Matrix of Practical Knowledge
7|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
Table 8.9 shows the frequency matrix for the feature Communication Skills.
Step 3: Use Bayes theorem Eq. (8.1) to calculate the probability of all hypotheses.
𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠 | 𝑇𝑒𝑠𝑡 𝑑𝑎𝑡𝑎) = (𝑃(𝐶𝐺𝑃𝐴 ≥ 9 |𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 𝑌𝑒𝑠) 𝑃 (𝐼𝑛𝑡𝑒𝑟𝑎𝑐𝑡𝑖𝑣𝑒𝑛𝑒𝑠𝑠
= 𝑌𝑒𝑠 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠) 𝑃 (𝑃𝑟𝑎𝑐𝑡𝑖𝑐𝑎𝑙 𝑘𝑛𝑜𝑤𝑙𝑒𝑑𝑔𝑒
= 𝐴𝑣𝑒𝑟𝑎𝑔𝑒 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠) 𝑃 (𝐶𝑜𝑚𝑚𝑢𝑛𝑖𝑐𝑎𝑡𝑖𝑜𝑛 𝑆𝑘𝑖𝑙𝑙𝑠
= 𝐺𝑜𝑜𝑑 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠) 𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠)))/(𝑃 (𝑇𝑒𝑠𝑡 𝐷𝑎𝑡𝑎))
We can ignore P (Test Data) in the denominator since it is common for all cases to be considered.
Hence,
8|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠 | 𝑇𝑒𝑠𝑡 𝑑𝑎𝑡𝑎) = (𝑃(𝐶𝐺𝑃𝐴 ≥ 9 𝑙𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠) 𝑃 (𝐼𝑛𝑡𝑒𝑟𝑎𝑐𝑡𝑖𝑣𝑒𝑛𝑒𝑠𝑠
= 𝑌𝑒𝑠 |𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠) 𝑃 (𝑃𝑟𝑎𝑐𝑡𝑖𝑐𝑎𝑙 𝑘𝑛𝑜𝑤𝑙𝑒𝑑𝑔𝑒 = 𝐴𝑣𝑒𝑟𝑎𝑔𝑒 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑌𝑒𝑠) 𝑃 (𝐶𝑜𝑚𝑚𝑢𝑛𝑖𝑐𝑎𝑡𝑖𝑜𝑛 𝑆𝑘𝑖𝑙𝑙𝑠 = 𝐺𝑜𝑜𝑑 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠) 𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑌𝑒𝑠)
3 5 1 4 7
= × × × ×
7 7 7 7 10
= 𝟎. 𝟎𝟏𝟕𝟓
𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜! 𝑇𝑒𝑠𝑡 𝑑𝑎𝑡𝑎) = (𝑃(𝐶𝐺𝑃𝐴 29 |𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) 𝑃 (𝐼𝑛𝑡𝑒𝑟𝑎𝑐𝑡𝑖𝑣𝑒𝑛𝑒𝑠𝑠
= 𝑌𝑒𝑠 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) 𝑃 (𝑃𝑟𝑎𝑐𝑡𝑖𝑐𝑎𝑙 𝑘𝑛𝑜𝑤𝑙𝑒𝑑𝑔𝑒 = 𝐴𝑣𝑒𝑟𝑎𝑔𝑒 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑁𝑜) 𝑃 (𝐶𝑜𝑚𝑚𝑢𝑛𝑖𝑐𝑎𝑡𝑖𝑜𝑛 𝑆𝑘𝑖𝑙𝑙𝑠 = 𝐺𝑜𝑜𝑑 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) 𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑁𝑜))/(𝑃(𝑇𝑒𝑠𝑡 𝐷𝑎𝑡𝑎)).
Step 4: Use Maximum A Posteriori (MAP) 𝑯𝒚𝒑𝒐𝒕𝒉𝒆𝒔𝒊𝒔, 𝑴𝑨𝑷 𝑬𝒒. (𝟖. 𝟐) to classify the test
object to the hypothesis with the highest probability.
Since
𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠 | 𝑇𝑒𝑠𝑡 𝑑𝑎𝑡𝑎)
ℎ𝑎𝑠 𝑡ℎ𝑒 ℎ𝑖𝑔ℎ𝑒𝑠𝑡 𝑝𝑟𝑜𝑏𝑎𝑏𝑖𝑙𝑖𝑡𝑦 𝑣𝑎𝑙𝑢𝑒, 𝑡ℎ𝑒 𝑡𝑒𝑠𝑡 𝑑𝑎𝑡𝑎 𝑖𝑠 𝑐𝑙𝑎𝑠𝑠𝑖𝑓𝑖𝑒𝑑 𝑎𝑠
′𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠′.
Zero Probability Error
In Example 8.1, consider the test data to be (CGPA ≥ 8, Interactiveness = Yes,
9|Page
Dept. of CSE, RNSIT
MACHINE LEARNING
4 5 1 4 7
= × × × ×
7 7 7 7 10
𝟎. 𝟎𝟐𝟑𝟑
Similarly, for the other case ′𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜,
When we compute the probability:
𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜| 𝑇𝑒𝑠𝑡 𝑑𝑎𝑡𝑎) = (𝑃(𝐶𝐺𝑃𝐴 ≥ 8 |𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) 𝑃 (𝐼𝑛𝑡𝑒𝑟𝑎𝑐𝑡𝑖𝑣𝑒𝑛𝑒𝑠𝑠
= 𝑌𝑒𝑠 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) 𝑃 (𝑃𝑟𝑎𝑐𝑡𝑖𝑐𝑎𝑙 𝑘𝑛𝑜𝑤𝑙𝑒𝑑𝑔𝑒
= 𝐴𝑣𝑒𝑟𝑎𝑔𝑒 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) 𝑃 (𝐶𝑜𝑚𝑚𝑢𝑛𝑖𝑐𝑎𝑡𝑖𝑜𝑛 𝑆𝑘𝑖𝑙𝑙𝑠
= 𝐺𝑜𝑜𝑑 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) 𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜))/(𝑃(𝑇𝑒𝑠𝑡 𝐷𝑎𝑡𝑎))
Since the probability value is zero, the model fails to predict, and this is called as Zero Probability
error. This problem arises because there are no instances in the given Table 8.1 for the attribute
value CGPA ≥8 and Job Offer = No and hence the probability value of this case is zero. This zero-
probability error can be solved by applying a smoothing technique called Laplace correction
which means given 1000 data instances in the training dataset, if there are zero instances for a
particular value of a feature we can add 1 instance for each attribute value pair of that feature
which will not make much difference for 1000 data instances and the overall probability does not
become zero. Now, let us scale the values given in Table 8.1 for 1000 data instances.
The scaled values without Laplace correction are shown in Table 8.11.
10 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
Now, add 1 instance for each CGPA-value pair for 'Job Offer = No'. Then,
𝑃 (𝐶𝐺𝑃𝐴 ≥ 9 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) = 101/303 = 0.333
𝑃 (𝐶𝐺𝑃𝐴 ≥ 8 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) = 1/303 = 0.0033
𝑃 (𝐶𝐺𝑃𝐴 < 8 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) = 201/300 = 0.6634
With scaled values to 1003 data instances, we get
𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠 | 𝑇𝑒𝑠𝑡 𝑑𝑎𝑡𝑎) = (𝑃(𝐶𝐺𝑃𝐴 ≥ 8 |𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑌𝑒𝑠) 𝑃 (𝐼𝑛𝑡𝑒𝑟𝑎𝑐𝑡𝑖𝑣𝑒𝑛𝑒𝑠𝑠 = 𝑌𝑒𝑠 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑌𝑒𝑠) 𝑃 (𝑃𝑟𝑎𝑐𝑡𝑖𝑐𝑎𝑙 𝑘𝑛𝑜𝑤𝑙𝑒𝑑𝑔𝑒 = 𝐴𝑣𝑒𝑟𝑎𝑔𝑒 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑌𝑒𝑠) 𝑃 (𝐶𝑜𝑚𝑚𝑢𝑛𝑖𝑐𝑎𝑡𝑖𝑜𝑛 𝑆𝑘𝑖𝑙𝑙𝑠 = 𝐺𝑜𝑜𝑑 𝑖 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑌𝑒𝑠) 𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠)
400 500 100 400 700
= × × × ×
700 700 700 700 1003
𝟎. 𝟎𝟐𝟑𝟐𝟓
𝟎. 𝟎𝟎𝟎𝟎𝟕𝟑𝟖𝟓
Thus, using Laplace Correction, Zero Probability error can be solved with Naïve Bayes classifier.
Brute Force Bayes Algorithm
Applying Bayes theorem, Brute Force Bayes algorithm relies on the idea of concept learning
wherein given a hypothesis space H for the training dataset T, the algorithm computes the
posterior probabilities for all the hypothesis hi ∈ H. Then, Maximum A Posteriori (MAP)
Hypothesis, hMAP is used to output the hypothesis with maximum posterior probability. The
algorithm is quite expensive since it requires computations for all the hypotheses. Although
computing posterior probabilities is inefficient, this idea is applied in various other algorithms
which is also quite interesting.
11 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
Example 8.3 Given the hypothesis space with 4 ℎ𝑦𝑝𝑜𝑡ℎ𝑒𝑠𝑖𝑠 ℎ1 , ℎ2 ℎ3 𝑎𝑛𝑑 ℎ4 . Determine if the
patient is diagnosed as COVID positive or COVID negative using Bayes Optimal classifier.
Solution: From the 𝑡𝑟𝑎𝑖𝑛𝑖𝑛𝑔 𝑑𝑎𝑡𝑎𝑠𝑒𝑡 𝑇, the posterior probabilities of the four different
hypotheses for a new instance are given in Table 8.12.
Table 8.12: Posterior Probability Values
P (h. T) P (COVID Positive |𝒉) P (COVID Negative |𝒉)
0.3 0 1
0.1 1 0
0.2 1 0
0.1 1 0
ℎ𝑀𝐴𝑃 𝑐ℎ𝑜𝑜𝑠𝑒𝑠 ℎ, which has the maximum probability 𝑣𝑎𝑙𝑢𝑒 0.3 as the solution and gives the
result that the patient is COVID negative. But Bayes Optimal classifier combines the predictions
of ℎ2 , ℎ3 𝑎𝑛𝑑 ℎ4 𝑤ℎ𝑖𝑐ℎ 𝑖𝑠 0.4 and gives the result that the patient is COVID positive.
𝑚𝑎𝑥 𝐶𝑖𝑑𝑖(𝐶𝑜𝑣𝑖𝑑 𝑃𝑜𝑠𝑖𝑡𝑣𝑒 ,𝐶𝑜𝑣𝑖𝑑 𝑁𝑒𝑔𝑎𝑡𝑣𝑖𝑒) ∑ℎ𝑖 ∈𝐻 𝑃(𝐶𝑖 |ℎ𝑖 )𝑃(ℎ𝑖 | 𝑇) = 𝐶𝑜𝑣𝑖𝑑 𝑃𝑜𝑠𝑖𝑡𝑖𝑣𝑒
Thus, this algorithm, diagnoses the new instance to be COVID positive.
Gibbs Algorithm
The main drawback of Bayes optimal classifier is that it computes the posterior probability for all
hypotheses in the hypothesis space and then combines the predictions to classify a new instance.
Gibbs algorithm is a sampling technique which randomly selects a hypothesis from the hypothesis
space according to the posterior probability distribution and classifies a new instance. It is found
that the prediction error occurs twice with the Gibbs algorithm when compared to Bayes Optimal
classifier.
12 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
Solution: Step 1: Compute the prior probability for the target feature ′𝑱𝒐𝒃 𝑶𝒇𝒇𝒆𝒓′ .
Prior probabilities of both the classes are calculated using the same formula (refer to Table 8.14).
Table 8.14: Prior Probability of Target Class
13 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
where,
𝑋𝑖 is the 𝑖 𝑡ℎ continuous attribute in the given dataset and 𝑥𝑘 is a value of the attribute. 𝐶𝑖 denotes
the 𝐽𝑡ℎ class of the target feature. 𝜇𝑖𝑗 denotes the mean of the values of that continuous attribute
𝑋𝑖 , with respect to the 𝑐𝑙𝑎𝑠𝑠 𝑗 of the target feature. 𝜎𝑖𝑗 denotes the standard deviation of the
values of that continuous attribute 𝑋𝑖 ,with respect to the 𝑐𝑙𝑎𝑠𝑠 𝑗 of the target feature.
Hence, the normal distribution formula is given as:
2
1 (𝑥𝑘 − 𝜇𝑖𝑗 )
𝑃(𝑋, = 𝑥𝑘 |𝐶𝑖 ) = 𝑒^ 8.6
𝜎𝑖𝑗 √2𝜋 2 𝜎𝑖𝑗2
Step 2(a): Consider the feature CGPA
In this example CGPA is a continuous attribute,
To calculate the likelihood probability for this continuous attribute, first compute the mean and
standard deviation for CGPA with respect to the target 𝑐𝑙𝑎𝑠𝑠 ′𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟′.
Here, 𝑋, = 𝐶𝐺РА
𝐶 = ′𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠′
Mean and Standard Deviation for 𝑐𝑙𝑎𝑠𝑠 ′𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠′ are given as:
𝜇𝑖𝑗 = 𝜇𝐶𝐺𝑃𝐴−𝑌𝐸𝑆 =8.814286
𝜎𝑖𝑗 = 𝜎𝐶𝐺𝑃𝐴−𝑌𝐸𝑆 =0.58146
Mean and Standard Deviation for 𝑐𝑙𝑎𝑠𝑠 ′𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜′ are given as:
𝜇𝑖𝑗 = 𝜇𝐶𝐺𝑃𝐴−𝑁𝑜 =8.133333
𝜎𝑖𝑗 = 𝜎𝐶𝐺𝑃𝐴−𝑁𝑜 =1.011599
Once Mean and Standard Deviation are computed, the likelihood probability for any test value
using Gaussian distribution formula can be calculated.
Step 2(b): Consider the feature Interactiveness
Interactiveness is a discrete feature whose probability is calculated as earlier. Table 8.15 shows
the frequency matrix for the feature Interactiveness.
Table 8.15: Frequency Matrix of Interactiveness
Table 8.16 shows how the likelihood probability is calculated for Interactiveness using conditional
probability.
14 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
Hence:
𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠 | 𝑇𝑒𝑠𝑡 𝑑𝑎𝑡𝑎) = (𝑃(𝐶𝐺𝑃𝐴 = 8.5 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑌𝑒𝑠) 𝑥 𝑃 (𝐼𝑛𝑡𝑒𝑟𝑎𝑐𝑡𝑖𝑣𝑒𝑛𝑒𝑠𝑠 = 𝑌𝑒𝑠 |𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠) 𝑥 𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑌𝑒𝑠)
5 7
0.594 × ×
7 10
0.297
15 | P a g e
Dept. of CSE, RNSIT
MACHINE LEARNING
Hence,
𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜 | 𝑇𝑒𝑠𝑡 𝑑𝑎𝑡𝑎) = 𝑃 (𝐶𝐺𝑃𝐴 = 8.5 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑁𝑜) 𝑃 (𝐼𝑛𝑡𝑒𝑟𝑎𝑐𝑡𝑖𝑣𝑒𝑛𝑒𝑠𝑠 = 𝑌𝑒𝑠 | 𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑁𝑜) 𝑥 𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟
= 𝑁𝑜) = 0.369 𝑥 1/3 𝑥 3/10 = 0.0369
Step 4: Use Maximum A Posteriori (MAP) Hypothesis, MAP to classify the test object to the
hypothesis with the highest probability. Since
𝑃 (𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠 | 𝑇𝑒𝑠𝑡 𝑑𝑎𝑡𝑎)
has the highest probability 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 0.297,the test data is classified as ′𝐽𝑜𝑏 𝑂𝑓𝑓𝑒𝑟 = 𝑌𝑒𝑠′.
16 | P a g e
Dept. of CSE, RNSIT