Practice
1. What does the following Python code snippet output when implementing linear regression using gradient descent?
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([1, 2, 3])
weights = np.zeros(X.shape[1])
learning_rate = 0.01
for _ in range(1):
predictions = X.dot(weights)
errors = predictions - y
gradient = X.T.dot(errors) / len(y)
weights -= learning_rate * gradient
print(weights)easy
Solution
Step 1: Calculate initial predictions and errors
Initial predictions are [0, 0, 0] since weights are zeros. Errors are [-1, -2, -3].Step 2: Compute gradient and update weights
Gradient is [-3, -6]. Updated weights are [0.01, 0.02].Final Answer:
Option A -> Option AQuick Check:
Initial weights zero, single update step. [OK]
Hint: Initial weights zero, single update step. [OK]
Common Mistakes:
2. In a multi-class classification problem, how should you modify the confusion matrix implementation to handle more than two classes?
medium
Solution
Step 1: Understand multi-class needs
Each class needs its own row and column in the matrix.Step 2: Choose correct matrix size
A (n_classes, n_classes) matrix captures all class interactions.Final Answer:
Option D -> Option DQuick Check:
Multi-class needs (n_classes, n_classes) matrix. [OK]
Hint: Multi-class needs (n_classes, n_classes) matrix. [OK]
Common Mistakes:
3.
import numpy as np
def ndcg_at_k(r, k):
dcg_max = dcg_at_k(sorted(r, reverse=True), k)
if not dcg_max:
return 0.
return dcg_at_k(r, k) / dcg_max
relevance_scores = [3, 2, 3, 0, 1, 2]
k = 3
print(ndcg_at_k(relevance_scores, k))The output is unexpectedly low. What is the most likely cause?medium
Solution
Step 1: Check Ideal DCG Calculation
The ideal DCG should be calculated using the sorted relevance scores.Step 2: Identify the Error
If the ideal DCG is not calculated correctly, the NDCG will be incorrect.Final Answer:
Option D -> Option DQuick Check:
Check ideal DCG calculation. [OK]
Hint: Check ideal DCG calculation. [OK]
Common Mistakes:
4. In a PCA implementation, you need to project data onto the first two principal components. Which approach is correct?
medium
Solution
Step 1: Understand projection in PCA
Projection involves multiplying the data matrix by the eigenvectors corresponding to the desired components.Step 2: Eliminate wrong options
Multiply the data matrix by the first two eigenvalues. is incorrect because eigenvalues are not used for projection. Multiply the covariance matrix by the first two eigenvectors. and D incorrectly use the covariance matrix instead of the data matrix.Final Answer:
Option A -> Option AQuick Check:
Project data using eigenvectors, not eigenvalues. [OK]
Hint: Project data using eigenvectors, not eigenvalues. [OK]
Common Mistakes:
5. In a dataset with highly imbalanced classes, how should you implement train-test split with stratification to ensure robust model evaluation?
medium
Solution
Step 1: Understand stratification in imbalanced datasets
Stratification should focus on maintaining the distribution of the minority class.Step 2: Apply correct strategy
Ensuring stratification based on the minority class helps in capturing its distribution in both sets.Final Answer:
Option A -> Option AQuick Check:
Stratify on minority class for balance. [OK]
Hint: Stratify on minority class for balance. [OK]
Common Mistakes:
