Professional Documents
Culture Documents
Question 1:
Preprocessing
The preprocessing phase involves loading the dataset from a publicly accessible URL
and preparing it for analysis. The dataset is loaded into a DataFrame using the pandas
library. The dataset includes features and labels, with the assumption that the first two
columns are the features and the last column represents the label.
1 import numpy a s np
2 import pandas a s pd
3
4 # URL t o t h e raw CSV f i l e i n t h e GitHub r e p o
5 d a t a u r l = ’ h t t p s : / / raw . g i t h u b u s e r c o n t e n t . com / . . . / data . c s v ’
6 # Load t h e d a t a s e t
7 d f = pd . r e a d c s v ( d a t a u r l )
8 # Assuming t h e l a s t column i s t h e l a b e l and t h e f i r s t two columns a r e t h e
features
9 data = d f . v a l u e s
LDA Steps:
1. Splitting the Dataset by Class: The dataset is divided based on the class labels,
resulting in two subsets of data corresponding to each class.
1 d a t a c l a s s 0 = data [ data [ : , −1] == 0 ] [ : , : −1]
2 d a t a c l a s s 1 = data [ data [ : , −1] == 1 ] [ : , : −1]
2. Calculating Class Means: We compute the mean vectors for each class, which will
be used to determine the direction of the projection that maximizes the distance
between the class means.
1
1 mean 0 = np . mean ( d a t a c l a s s 0 , a x i s =0)
2 mean 1 = np . mean ( d a t a c l a s s 1 , a x i s =0)
Visualization
The scatter plot shows the two classes with different colors, along with their respec-
tive means. The LDA projection vector is drawn emanating from the mean of Class 0,
highlighting the direction that best separates the two classes. This visualization aids in
understanding the geometric interpretation of LDA as a linear separator.
1 p r o j l i n e = m a x e i g v e c ∗ 10 # S c a l e f o r v i s u a l i z a t i o n
2 p l t . q u i v e r ( ∗ mean 0 , ∗ p r o j l i n e , c o l o r= ’ g r e e n ’ , s c a l e =1, s c a l e u n i t s= ’ xy ’ ,
a n g l e s= ’ xy ’ , width =0.005 , l a b e l= ’LDA P r o j e c t i o n Vector ’ )
2
Figure 1: Visualization of the LDA projection vector.
3
– Train and test the 1-NN classifier on the LDA projected data.
– Compare accuracies to observe the impact of LDA.
Observations:
• The 1-NN classifier’s accuracy on the original data is 89.00%. This performance
serves as a baseline for comparing the effectiveness of dimensionality reduction
techniques.
• After projecting the data using LDA, the 1-NN classifier’s accuracy decreased to
88.75%.
• The slight decrease in accuracy might also suggest that the LDA projection, while
maintaining most of the class-separability information, could have led to a slightly
less generalized model. This is a trade-off often encountered in dimensionality
reduction techniques, balancing between maintaining important features and the
risk of losing some generalizability due to the removal of features.
• Although not explicitly mentioned, one should note that using LDA-projected data
likely improved the computational efficiency of the 1-NN classification. Dimension-
ality reduction reduces the number of features that the classifier needs to consider,
which can significantly speed up the classification process, especially in cases of
high-dimensional data.
The observed increase in accuracy, albeit slight, suggests that LDA can help improve
classification performance by maximizing class separability in the projected space. This
result underscores the utility of LDA not only as a technique for dimensionality reduction
but also as a tool for enhancing the discriminative power of classifiers in lower-dimensional
spaces.
4
Question 2:
• P (Play=yes) = 0.5833333333333334
• P (Play=no) = 0.4166666666666667
These values indicate the overall likelihood of playing or not playing before considering
the conditions described by the features.
1
Feature=Value P(Value — Play=yes) (%) P(Value — Play=no) (%)
Outlook=Rainy 28.57 60.00
Outlook=Overcast 42.86 0.00
Outlook=Sunny 28.57 40.00
Temp=Hot 28.57 40.00
Temp=Cool 42.86 20.00
Temp=Mild 28.57 40.00
Humidity=High 28.57 80.00
Humidity=Normal 71.43 20.00
Windy=f 71.43 40.00
Windy=t 28.57 60.00
• Outlook:
• Temperature:
– Cooler conditions are slightly more favorable, though temperature has a lesser
impact overall.
• Humidity:
• Windy:
• General Insights:
– The ideal conditions for playing include overcast skies, normal humidity, and
calm winds.
– High humidity and windy conditions are the primary adverse factors.
– Understanding these patterns is crucial for making predictions with the Naive
Bayes classifier, especially in applications related to planning outdoor activities
based on weather conditions.
2
Task 3: Calculating Posterior Probabilities
In this task, we use the Naive Bayes formula to calculate the posterior probabilities for
both classes (Play = yes and Play = no) for a given test instance. The process involves
multiplying the prior probabilities by the likelihoods of each feature given the class, and
normalizing these products to get probabilities that sum to 1.
Procedure
Given a test instance, we perform the following steps:
1. Initialize the product of likelihoods for each class with the prior probability of the
class.
2. For each feature in the test instance, multiply the current product by the likelihood
of the feature value given the class.
3. Sum the products for both classes to get the total likelihood.
4. Normalize each class product by the total likelihood to get the posterior probabili-
ties.
• Posterior Probability of Playing (Yes): 81.999%
• Posterior Probability of Not Playing (No): 18.0005%
Decision: Based on the higher posterior probability, the predicted decision for the
given test instance is Play = yes.
Prediction Function
This function calculates the posterior probabilities for both classes and makes a prediction
based on which probability is higher.
1 def predict naive bayes ( test instance , prior yes , prior no , likelihoods ,
features ) :
2 likelihood product yes = prior yes
3 likelihood product no = prior no
4
3
Sample Predictions
• Test instance 1: Details={’Outlook’: ’Sunny’, ’Temp’: ’Mild’, ’Humidity’: ’Nor-
mal’, ’Windy’: ’f’}
Predicted Play=yes
where Nxi ,y is the count of feature xi in class y, Ny is the total count of class y, α is the
pseudocount, and D is the number of distinct values of feature xi .
4
• Test instance 2: Details={’Outlook’: ’Overcast’, ’Temp’: ’Mild’, ’Humidity’: ’High’,
’Windy’: ’t’}
Predicted Play=yes
Accuracy = 100%
Comparison of Predictions
The impact of Laplace smoothing on predictions is analyzed by comparing likelihoods
and posterior probabilities with and without Laplace smoothing.
Observations
Below are the summarized observations:
• Cooler temperatures are preferred for playing, as seen from the higher probability
under the Cool category.
• Windy conditions are less preferred for playing, as indicated by the lower probability
when it is true (t).
5
Figure 1: Visualization of the LDA projection vector.
6
Figure 3: Visualization of the LDA projection vector.