0% found this document useful (0 votes)
6 views7 pages

Mid-Term A2 ML Solution

The document outlines the mid-term exam details for a Machine Learning course at the University of Management & Technology, including instructions and questions for students. It covers topics such as deriving the best-fit line using the least squares method, probability calculations, Stochastic Gradient Descent, Naive Bayes classifier, and building a linear regression model. Each question specifies marks and learning outcomes, requiring students to demonstrate their understanding of machine learning concepts and techniques.
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)
6 views7 pages

Mid-Term A2 ML Solution

The document outlines the mid-term exam details for a Machine Learning course at the University of Management & Technology, including instructions and questions for students. It covers topics such as deriving the best-fit line using the least squares method, probability calculations, Stochastic Gradient Descent, Naive Bayes classifier, and building a linear regression model. Each question specifies marks and learning outcomes, requiring students to demonstrate their understanding of machine learning concepts and techniques.
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/ 7

University of Management & Technology

Department of Artificial Intelligence


School of Systems and Technology

Student ID:_____________________ Section: A2


Name:__________________________ Semester: Fall 2024
Program: BS AI Exam: Mid Term
Course Title: Machine Learning Academic Year: 2024-2025
Exam Date: 16 December, 2024 Time Allowed: 75 Mins
Resource Person: Dr. Ashfaq Ahmad Total Marks: 35

DO NOT OPEN THIS PAPER UNTIL TOLD TO DO SO


The instructions below must be followed strictly.
Failure to do so can result in serious grade loss
• Keep your eyes on your page.
• Switch off your mobile phones completely.
• Draw neat, labelled diagrams where necessary.
• No extra sheet will be given.
• Use blank spaces on the sheet for rough work.
• Use blue or black ball point or pen, please do not use lead pencils or any other
color.
• Lending/ Borrowing of calculators or anything is strictly prohibited.
• All Questions are self-explanatory. No queries will be entertained.

Questions Q#1 Q#2 Q#3 Q#4 TOTAL


Marks CLO 1 CLO 1 CLO 2 CLO 2
Obtained
Total
Marks
10 08 08 09 35
Question # 1 (CLO-1) (10 marks)
a) States the process to derive the best-fit line using the least squares method for the dataset with input
values X=[1,2,3,4,5] and corresponding output values Y=[2,4,6,8,10]. Calculate the slope and
intercept of the line. Show all steps clearly. (8 marks)

Solution:
∑𝑛𝑖=1(𝑥𝑖 − 𝑚𝑒𝑎𝑛 (𝑥)) × (𝑦𝑖 − 𝑚𝑒𝑎𝑛 (𝑦))
𝐵1 =
∑𝑛𝑖=1(𝑥𝑖 − 𝑚𝑒𝑎𝑛 (𝑥)2

𝐵0 = 𝑚𝑒𝑎𝑛(𝑦) − 𝐵1 𝑚𝑒𝑎𝑛(𝑥)
𝑛 = 5,

∑ 𝑋 = 1 + 2 + 3 + 4 + 5 = 15

∑ 𝑌 = 2 + 4 + 6 + 8 + 10 = 30

𝑋 − 𝑚𝑒𝑎𝑛(𝑋) = −14, −13, −12, −11, −10


𝑌 − 𝑚𝑒𝑎𝑛(𝑌) = −28, −26, −24, −22, −20
(𝑋 − 𝑚𝑒𝑎𝑛(𝑋))(𝑌 − 𝑚𝑒𝑎𝑛(𝑌) = 392, 338, 288, 242, 200

∑(𝑋 − 𝑚𝑒𝑎𝑛(𝑋))(𝑌 − 𝑚𝑒𝑎𝑛(𝑌) = 392 + 338, +288, +242, +200 = 1460

(𝑥𝑖 − 𝑚𝑒𝑎𝑛 (𝑥)2 = 196, 169, 144, 121, 100

∑(𝑥𝑖 − 𝑚𝑒𝑎𝑛 (𝑥)2 = 196 + 169 + 144 + 121 + 100 = 730

∑(𝑋 − 𝑚𝑒𝑎𝑛(𝑋))(𝑌 − 𝑚𝑒𝑎𝑛(𝑌) 1460


𝐵1 = = =2
∑(𝑥𝑖 − 𝑚𝑒𝑎𝑛 (𝑥)2 730
𝐵0 = 𝑚𝑒𝑎𝑛(𝑦) − 𝐵1 𝑚𝑒𝑎𝑛(𝑥)
𝐵0 = 30 − (2)(15)
𝐵0 = 30 − 30 = 0
Final Equation
𝑌 = 𝐵0 + 𝐵1 × 𝑋
𝑌 = 0 + 2𝑋
b) Identifies the missing lines in the Python code below to calculate the predictions based on the given
slope and intercept: (2 marks)
import numpy as np
X = np.array([1, 2, 3, 4, 5])
slope = ____ # Provide the calculated value
intercept = ____ # Provide the calculated value
Y_pred = ________ # Predict Y values
print(Y_pred)
Answer:
import numpy as np
X = np.array([1, 2, 3, 4, 5])
slope = 2 # Derived from the least squares method
intercept = 0 # Derived from the least squares method
Y_pred = slope * X + intercept
print(Y_pred)
𝑌_𝑝𝑟𝑒𝑑 = [ 2 4 6 8 10 ]

Question # 2 (CLO-1) (08 marks)


a) Calculate and state the probability of drawing an Ace from a standard deck of 52 playing cards. Then,
identify the probability of drawing an Ace given that the card drawn is a Spade. (4 marks)
Solution:
1. Probability of drawing an Ace (P(Ace)):
There are 4 Aces in a deck of 52 cards.
4 1
(𝑃(𝐴𝑐𝑒)) = = = 0.077
52 13
2. Probability of drawing an Ace given it's a Spade (P(Ace | Spade)):
There is 1 Ace among the 13 Spades.
𝟏
(𝑷(𝑨𝒄𝒆 | 𝑺𝒑𝒂𝒅𝒆)) = = 𝟎. 𝟎𝟕𝟕
𝟏𝟑
b) Using Stochastic Gradient Descent, calculate and state the updated weight and bias values after one
epoch. Given: (4 marks)
• Initial weight w=0, initial bias b=0
• Learning rate α=0.1
• Training data point: (2, 3).

Solution

Data Point (2, 3):


a. 𝑷𝒓𝒆𝒅𝒊𝒄𝒕𝒆𝒅 𝒗𝒂𝒍𝒖𝒆: 𝑦 ^ = 𝑤 ⋅ 𝑥 + 𝑏 = 0 × 2 + 0 = 0
b. 𝑳𝒐𝒔𝒔: 𝐿 = (𝑦 − 𝑦 ^ )2 = (3 − 0)2 = 9
c. Gradients
𝜕𝐿 𝜕𝐿 𝜕𝑦 ^
= ×
𝜕𝑤 𝜕𝑦 ^ 𝜕𝑤
𝜕𝐿
= −2(𝟑 − 𝟎) × (𝟐) = −𝟏𝟐
𝜕𝑤
𝜕𝐿
= −2(𝟑 − 𝟎) = −6
𝜕𝑏
➢ Update parameters:
𝑤 = 𝑤 − 𝛼 ⋅ (−12)
𝑤 = 0 − 0.1(−12) = 0 + 1.2 = 1.2
𝑏 = 𝑏 − 𝛼 ⋅ (−3.6)
𝑏 = 0 − 0.1(−6) = 0 + 0.6 = 0.6
Question # 3 (CLO-2) (08 marks)
a) You are building a Naive Bayes classifier for a dataset with three classes 𝐶1 , 𝐶2 , 𝐶3 . For a new data
point X, you calculate the following probabilities:
𝑃(𝐶1|𝑋) = 0.4, 𝑃(𝐶2 |𝑋) = 0.35 𝑃(𝐶3 |𝑋) = 0.25
1. Uses the following probabilities for a Naive Bayes classifier with three classes to determine if
they are valid:
2. If 𝑃(𝑋|𝐶1 ) = 0.5, 𝑃(𝐶1 ) = 0.6 𝑎𝑛𝑑 𝑃(𝑋) = 0.75, 𝑐𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑒 𝑃(𝐶1 |𝑋) using Bayes’ theorem.
Does this match the given probability? (5 marks)

Solution:

1. Validity Check
𝑃(𝐶1 |𝑋) + 𝑃(𝐶2 |𝑋) + 𝑃(𝐶3 |𝑋) = 0.4 + 0.35 + 0.25 = 1
Probabilities are valid.
2. Use Bayes’ Theorem:
𝑃(𝑋|𝐶1 ) + 𝑃(𝐶1 )
𝑃(𝐶1|𝑋) =
𝑃 (𝑋 )
0.5 + 0.6
𝑃(𝐶1 |𝑋) = = 0.4
0.75
Matches the given probability.
b) Identifies the missing lines in the following code for computing the hypothesis using a sigmoid
function in logistic regression. Complete the code by filling in the missing lines for 𝑧, the sigmoid
function, and the hypothesis ℎ0 (𝑥) (3 marks)

import numpy as np
# Given weights and features
theta = np.array([0.5, 1.2])
x = np.array([1, 2]) # Includes bias term
# Compute z
z = __________ # Fill in the equation for z
# Sigmoid function
def sigmoid(z):
return __________ # Fill in the equation for the sigmoid function
# Hypothesis
h_theta_x = __________ # Fill in the equation for the hypothesis
print("Hypothesis (h_theta_x):", h_theta_x)

Answer:

z = np.dot(theta, x)
return 1 / (1 + np.exp(-z))
h_theta_x = sigmoid(z)

Question # 4 (CLO-2) (09 marks)


Demonstrates the process of building a linear regression model using the hypothesis ℎ(𝑥) = 𝑤 ⋅ 𝑥 + 𝑏
to predict house prices (𝑦) based on the size of the house (𝑥). given a dataset with corresponding values:
House Size (𝑥) (in 𝑚2 ) Price (y) (in $)
50 200
100 400
150 600

1. Assuming the initial weights are 𝑤 = 0 𝑎𝑛𝑑 𝑏 = 0, calculate the predictions ℎ(𝑥) for the given data
points and the Mean Squared Error (MSE).
2. Explain what the high MSE at 𝑤 = 0 𝑎𝑛𝑑 𝑏 = 0 implies about the relationship between 𝑥 𝑎𝑛𝑑 𝑦.
3. Update the weight 𝑤 using one step of gradient descent with a learning rate 𝛼 = 0.01, assuming 𝑏 =
0 for simplicity.
4. How does optimizing 𝑤 while keeping 𝑏 = 0 affect the predictions and MSE? What does this suggest
about the importance of 𝑏 in the hypothesis function?

Solution:

You might also like