Unit-6 Notes PART A
Unit-6 Notes PART A
(a) Theoretical
Q: Define the following terms in the context of classification metrics:
1. Confusion Matrix
2. Precision
3. Recall
A:
1. Confusion Matrix: A confusion matrix is a table used to evaluate the
performance of a classification model. It summarizes the predictions made by the
model by comparing actual labels with predicted labels. The four key components
are:
o True Positive (TP): Correctly predicted positive instances.
o True Negative (TN): Correctly predicted negative instances.
o False Positive (FP): Incorrectly predicted positive instances (Type I
error).
o False Negative (FN): Incorrectly predicted negative instances (Type II
error).
2. Precision: Precision measures the proportion of true positive
predictions out of all positive predictions made by the model. It is
given by:
TP
Precision =
TP+FP
3. Recall: Recall (or sensitivity) measures the proportion of actual
positives that were correctly identified by the model. It is given
by:
TP
Recall=
TP+ FN
(b) Mathematical
Q: Given the confusion matrix below, calculate Accuracy, Precision, Recall, and F1
Score.
A:
Step-by-Step Calculations
To verify the results, let’s compute the metrics manually:
The confusion matrix is:
Actual Positive TP = 80 FN = 20
Actual FP = 10 TN = 90
Negative
From this:
TP (True Positives): 80
FN (False Negatives): 20
FP (False Positives): 10
TN (True Negatives): 90
1. Accuracy
TP+ TN
Accuracy=
TP+ TN+ FP+FN
80+90 170
Accuracy= = =0.85
80+90+10+ 20 200
2. Precision
TP
Precision =
TP+FP
80 80
Precision= = ≈ 0.8889
80+10 90
3. Recall
TP
Recall=
TP+ FN
80 80
Recall= = =0.80
80+20 100
4. F1 Score
Precision ⋅Recall
F1 Score=2 ⋅
Precision + Recall
0.8889 ⋅0.80 0.7111
F1 Score=2 ⋅ =2 ⋅ ≈ 2 ⋅0.421=0.8421
0.8889+0.80 1.6889
Predicted labels
y_pred = [1] * 80 + [0] * 20 + [1] * 10 + [0] * 90
# 1 = Predicted Positive, 0 = Predicted Negative
accuracy = accuracy_score(y_true, y_pred)
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
# Harmonic mean of Precision and Recall
print(f"Accuracy: {accuracy:.4f}")
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1 Score: {f1:.4f}")
Output:
Accuracy: 0.85
Precision: 0.89
Recall: 0.80
F1 Score: 0.84
Question 2: (a) Theoretical
What are classification metrics ? compare accuracy and precision ? what is role of
F1-SCORE in classification report ?
A:
Classification metrics are quantitative measures used to evaluate the performance of a
classification model. These metrics help assess how well a model performs in terms of
correctly classifying instances into their respective classes. The choice of metric depends
on the problem context, such as whether false positives or false negatives are more
critical.
Key Difference: Accuracy considers both positive and negative predictions, while
precision focuses only on positive predictions. In imbalanced datasets, accuracy
may give a false sense of good performance, whereas precision highlights the
quality of positive predictions.
F1-Score Balance between Imbalanced datasets; trade- Does not account for
precision and recall off between FP and FN true negatives
Question 2: (b) Mathematical
Problem Overview
calculate the following classification metrics for the given dataset:
1. Accuracy
2. Confusion Matrix
3. Precision, Recall, and F1-Score
And also write the python code with respect to cat.
A:-
Problem Overview
The dataset is as follows:
Step-by-Step Solution
1. Accuracy
Definition: Accuracy measures the proportion of correct predictions out of all
predictions.
Formula:
Number of Correct Predictions
Accuracy=
Total Number of Predictions
2. Confusion Matrix
A confusion matrix provides a detailed breakdown of predictions:
o True Positives (TP): Correctly predicted Cat.
o False Positives (FP): Incorrectly predicted Cat (actual label is Dog).
o False Negatives (FN): Incorrectly predicted Dog (actual label is Cat).
o True Negatives (TN): Correctly predicted Dog.
3. Precision, Recall, and F1-Score
Precision: Measures the accuracy of positive predictions for Cat.
TP
Precision =
TP+FP
Recall: Measures the ability to find all actual instances of Cat.
TP
Recall=
TP+ FN
F1-Score: Harmonic mean of precision and recall.
Precision ⋅ Recall
F1-Score=2⋅
Precision +Recall
# 1. Accuracy
accuracy = accuracy_score(y_true, y_pred)
print(f"Accuracy: {accuracy:.2f}")
# 2. Confusion Matrix
conf_matrix = confusion_matrix(y_true, y_pred, labels=['Cat',
'Dog'])
print("Confusion Matrix:")
print(conf_matrix)
Computed Metrics:
Accuracy:
[ 11 12]
TP (Cat): 1
FP (Dog predicted as Cat): 1
FN (Cat predicted as Dog): 1
TN (Dog): 2
Precision, Recall, and F1-Score for Cat:
TP 1
o Precision = = =0.50
TP+ FP 1+1
TP 1
o Recall = = =0.50
TP+ FN 1+ 1
Precision ⋅Recall 0.50 ⋅0.50
o F1-Score = 2 ⋅ =2 ⋅ =0.50
Precision+Recall 0.50+0.50
Final Output:
Accuracy: 0.60
Confusion Matrix:
[[1 1]
[1 2]]
Precision for Cat: 0.50
Recall for Cat: 0.50
F1-Score for Cat: 0.50
Importance:
Orthogonalization simplifies computations like solving systems of equations and
finding projections.
Orthonormal bases improve numerical stability in algorithms like least squares
regression.
(b) Mathematical
Q: Apply the Gram-Schmidt process to the vectors v 1=[1 ,1 , 0], v 2=[1 , 0 ,1].
Compute the orthogonal vectors u1 and u2. Normalize them to obtain an
orthonormal basis.
A:
Step 1: Start with u1=v 1.
u1=[1, 1 , 0].
1. Compute ⟨ v 2 , u1 ⟩:
⟨ v 2 , u1 ⟩=[1 ,0 ,1]⋅[1 , 1, 0]=1 ⋅1+ 0 ⋅1+ 1⋅ 0=1 .
2. Compute ⟨ u1 ,u1 ⟩:
2 2 2
⟨ u1 ,u1 ⟩=[1, 1 , 0]⋅[1 ,1 , 0]=1 +1 +0 =2 .
3. Compute proju ( v 2 ) :1
1
proju ( v 2 ) = [1 ,1 , 0]=[0.5 , 0.5 , 0].
1
2
∥ u1 ∥= √ ⟨ u1 ,u 1 ⟩= √ 2 .
Normalize u1:
unormalized
1 =
1
√2
[1 ,1 , 0]=
[1 1
,
√2 √ 2
,0 .
]
2. Compute ∥ u2 ∥:
Normalize u2:
unormalized
2 =
1
√ 1.5 [
[0.5 ,−0.5 , 1]=
0.5 −0.5 1
, ,
√ 1.5 √ 1.5 √1.5].
def gram_schmidt(vectors):
ortho_vectors = []
for v in vectors:
v = np.array(v)
for u in ortho_vectors:
v -= (np.dot(v, u) / np.dot(u, u)) * u
ortho_vectors.append(v)
return ortho_vectors
def normalize(vector):
norm = np.linalg.norm(vector)
return vector / norm
# Input vectors
v1 = [1, 1, 0]
v2 = [1, 0, 1]
# Perform Gram-Schmidt
ortho_vectors = gram_schmidt([v1, v2])
print("Orthogonal vectors:")
for i, vec in enumerate(ortho_vectors):
print(f"u{i+1}: {vec}")
print("\nNormalized vectors:")
for i, vec in enumerate(normalized_vectors):
print(f"u{i+1}_normalized: {vec}")
Output:
Orthogonal vectors:
u1: [1. 1. 0.]
u2: [ 0.5 -0.5 1. ]
Normalized vectors:
u1_normalized: [0.70710678 0.70710678 0. ]
u2_normalized: [ 0.40824829 -0.40824829 0.81649658]
Question 4:
Question:
Explain the concept of orthogonalization and its significance in linear algebra. Describe
the Gram-Schmidt process for orthogonalizing a set of vectors. Derive the mathematical
formula for the Gram-Schmidt process, and apply it to orthogonalize the following set of
vectors:
[] [] []
1 1 0
v 1= 1 , v 2= 0 , v 3= 1 .
0 1 1
Show all steps of the computation and verify that the resulting vectors are orthogonal.
Finally, implement the Gram-Schmidt process in Python to confirm your results.
Mathematical Formula
Let {v 1 , v 2 , … , v n } be a set of linearly independent vectors. The Gram-Schmidt process
constructs an orthogonal set {u 1 , u2 , … ,u n } using the following steps:
Here:
o ⟨ a , b ⟩ denotes the dot product of vectors a and b .
⟨ v k ,u j ⟩
o The term u represents the projection of v k onto u j.
⟨ u j, u j ⟩ j
where ∥ uk ∥=√ ⟨ u k , uk ⟩.
Mathematical Example
We are given the vectors:
[] [] []
1 1 0
v 1= 1 , v 2= 0 , v 3= 1 .
0 1 1
[]
1
u1 = 1 .
0
[]
1
⟨ v 2 , u1 ⟩=[ 1 0 1 ] ⋅ 1 =( 1 ) ( 1 ) + ( 0 ) ( 1 ) + ( 1 ) ( 0 )=1 ,
0
[]
1
⟨ u1 ,u1 ⟩= [ 1 1 0 ] ⋅ 1 =( 1 ) ( 1 )+ ( 1 )( 1 ) + ( 0 )( 0 )=2 .
0
Therefore, the projection is:
[]
1
[]
1 2
1
Projection= 1 = 1 .
2
0 2
0
[][][ ][ ]
1 1 1
1−
1 2 2 2
u2=v 2−Projection= 0 − 1 = 1 = −1 .
0−
1 2 2 2
0 1−0 1
u3=v 3−
( ⟨ v 3 ,u1 ⟩
⟨ u 1 , u1 ⟩
u1 +
⟨ v 3 ,u 2 ⟩
⟨ u2 ,u2 ⟩ 2 )
u .
First, compute ⟨ v 3 , u1 ⟩:
[]
1
⟨ v 3 , u1 ⟩=[ 0 1 1 ] ⋅ 1 = ( 0 ) (1 )+ (1 )( 1 ) + ( 1 ) ( 0 )=1 .
0
Next, compute ⟨ v 3 , u2 ⟩:
[]
1
2
⟨ v 3 , u2 ⟩=[ 0 1 1 ] ⋅ −1 =( 0 )
2
1
2
+( 1 )
−1
2() ( )
+ ( 1 ) ( 1 )=
−1
2
1
+1= .
2
1
Now, compute ⟨ u2 ,u 2 ⟩:
[]
1
2
[ ] () ( )
2 2
1 −1 1 −1 2 1 1 3
⟨ u2 ,u 2 ⟩= 1 ⋅ −1 = + + ( 1 ) = + +1= .
2 2 2 2 4 4 2
2
1
[]
1 2
1
Projection onto u1= 1 = 1 ,
2
0 2
0
[ ] [ ][ ]
1
1 1
1 6
2 2
2 1 −1
Projection onto u2 = −1 = −1 = .
3 3 6
2 2
2 1
1 1
3
[( ] [ ]) [ ] [] [][ ]
1 1 1 2 −2
1 +
[]
6 2 6 3 3
2 0 0
−1 1 1 1 2
u3=v 3− 1 + =1− − =1− = .
6 2 6 3 3
2 1 1
1 1 1 2
0 0+
3 3 3 3
[] [ ] [ ]
−2
1
3
1 2
2
u1= 1 , u2= −1 ,u 3= .
3
0 2
2
1
3
Verification of Orthogonality
To verify that the vectors are orthogonal, compute their pairwise dot products:
⟨ u1 ,u 2 ⟩:
[]
1
2
2
() ( )
⟨ u1 ,u 2 ⟩= [ 1 1 0 ] ⋅ −1 =( 1 )
1
2
+( 1 )
−1
2
1 1
+ ( 0 )( 1 )= − =0 .
2 2
1
⟨ u1 ,u3 ⟩:
[]
−2
3
⟨ u1 ,u3 ⟩= [ 1 1 0 ] ⋅
2
3 ( ) () ()
=( 1 )
−2
3
+( 1 )
2
3
+ ( 0)
2 −2 2
3
= + =0 .
3 3
2
3
⟨ u2 ,u 3 ⟩:
[]
−2
3
⟨ u2 ,u 3 ⟩= [ 1
2
−1
2 ]
1 ⋅
2
3
=( )( ) ( )( ) ( )
1 −2 −1 2
2 3
+
2 3
+ ( 1)
2 −1 1 2
3
= − + =0 .
3 3 3
2
3
Since all pairwise dot products are zero, the vectors are orthogonal.
def gram_schmidt(vectors):
"""
Perform the Gram-Schmidt process to orthogonalize a set of vectors.
Parameters:
vectors (list of np.ndarray): A list of linearly independent vectors.
Returns:
list of np.ndarray: A list of orthogonal vectors.
"""
orthogonal_vectors = []
for i, v in enumerate(vectors):
# Start with the current vector
u = v.copy()
return orthogonal_vectors
# Example usage
vectors = [
np.array([1, 1, 0]), # First vector
np.array([1, 0, 1]), # Second vector
np.array([0, 1, 1]) # Third vector
]
orthogonal_vectors = gram_schmidt(vectors)
print("Orthogonal Vectors:")
for vec in orthogonal_vectors:
print(vec)
[] [] []
1 1 0
v 1= 1 , v 2= 0 , v 3= 1 ,
0 1 1
[] [ ] [ ]
1 0.5 −0.6667
u1= 1 , u2= −0.5 , u3= 0.6667 .
0 1 0.6667
Final Answer
The orthogonalized vectors are:
[] []
−2
1
3
[]
1 2
2
u1= 1 , u2= −1 ,u 3= .
3
0 2
2
1
3