Practice
1.
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6]])
X_meaned = X - np.mean(X, axis=0)
cov_matrix = np.cov(X_meaned, rowvar=False)
print(cov_matrix)What does this code output?easy
Solution
Step 1: Understand the covariance matrix calculation
The covariance matrix is calculated for the features (columns) of the dataset.Step 2: Eliminate wrong options
A 3x3 covariance matrix is incorrect because the covariance matrix is not 3x3 for a 2-feature dataset. A 2x3 covariance matrix and D are incorrect because covariance matrices are square.Final Answer:
Option C -> Option CQuick Check:
Covariance matrix is square, size = features. [OK]
Hint: Covariance matrix is square, size = features. [OK]
Common Mistakes:
2. What does the following Python code output when computing the ROC curve?
from sklearn.metrics import roc_curve
import numpy as np
y_true = np.array([0, 0, 1, 1])
y_scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = roc_curve(y_true, y_scores)
print(fpr, tpr, thresholds)easy
Solution
Step 1: Understand ROC curve output
The ROC curve function returns false positive rates (fpr), true positive rates (tpr), and thresholds.Step 2: Analyze the code
Given the scores and true labels, the ROC curve is calculated correctly, resulting in fpr, tpr, and thresholds as in array([0. , 0.5, 1. ]), array([0. , 0.5, 1. ]), array([0.8, 0.35, 0.1]).Final Answer:
Option C -> Option CQuick Check:
ROC curve outputs fpr, tpr, thresholds [OK]
Hint: ROC curve outputs fpr, tpr, thresholds [OK]
Common Mistakes:
3. In which scenario is gradient checking most useful?
medium
Solution
Step 1: Identify scenarios where gradient checking is applicable
Gradient checking is crucial when implementing new components to ensure correctness.Step 2: Eliminate unrelated scenarios
Options A, C, and D are not directly related to verifying gradient correctness.Final Answer:
Option A -> Option AQuick Check:
Use gradient check for new components. [OK]
Hint: Use gradient check for new components. [OK]
Common Mistakes:
4. In the context of implementing linear regression with gradient descent, how would you handle a dataset with missing values?
medium
Solution
Step 1: Consider impact of missing values
Ignoring or zero-filling can bias results; removing samples reduces data.Step 2: Choose appropriate imputation
Mean imputation is a common strategy that maintains dataset size and reduces bias.Final Answer:
Option A -> Option AQuick Check:
Mean imputation balances bias and data retention. [OK]
Hint: Mean imputation balances bias and data retention. [OK]
Common Mistakes:
5. How can you optimize a KD-Tree implementation for a KNN classifier to handle large datasets more efficiently?
hard
Solution
Step 1: Identify optimization strategies
Approximate nearest neighbors can significantly speed up queries in large datasets.Step 2: Evaluate other options
A: Balancing helps but isn't enough for large datasets. B: Increasing dimensions worsens performance. D: Reducing data points isn't always feasible.Final Answer:
Option B -> Option BQuick Check:
Approximate neighbors speed up KD-Tree [OK]
Hint: Approximate neighbors speed up KD-Tree [OK]
Common Mistakes:
