Mid-Term A2 ML Solution
Mid-Term A2 ML Solution
Solution:
∑𝑛𝑖=1(𝑥𝑖 − 𝑚𝑒𝑎𝑛 (𝑥)) × (𝑦𝑖 − 𝑚𝑒𝑎𝑛 (𝑦))
𝐵1 =
∑𝑛𝑖=1(𝑥𝑖 − 𝑚𝑒𝑎𝑛 (𝑥)2
𝐵0 = 𝑚𝑒𝑎𝑛(𝑦) − 𝐵1 𝑚𝑒𝑎𝑛(𝑥)
𝑛 = 5,
∑ 𝑋 = 1 + 2 + 3 + 4 + 5 = 15
∑ 𝑌 = 2 + 4 + 6 + 8 + 10 = 30
Solution
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)
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: