Practice
1.
import numpy as np
from sklearn.metrics import confusion_matrix
y_true = [0, 1, 0, 1, 0, 1, 1]
y_pred = [0, 0, 0, 1, 1, 1, 1]
cm = confusion_matrix(y_true, y_pred)
print(cm)What does the code output?easy
Solution
Step 1: Understand confusion matrix structure
The confusion matrix is structured as [[TN, FP], [FN, TP]].Step 2: Calculate values
TN=2 (true negatives), FP=1 (false positives), FN=1 (false negatives), TP=3 (true positives).Final Answer:
Option A -> Option AQuick Check:
Confusion matrix is [[TN, FP], [FN, TP]]. [OK]
Hint: Confusion matrix is [[TN, FP], [FN, TP]]. [OK]
Common Mistakes:
2. What is the time complexity of computing the covariance matrix for a dataset with n samples and d features?
easy
Solution
Step 1: Analyze the covariance matrix computation
Computing the covariance matrix involves calculating pairwise covariances for d features, each requiring O(n) operations.Step 2: Eliminate wrong options
O(n^2) is incorrect as it doesn't account for features. O(d^2) is incorrect because it ignores the sample size. O(nd^2) overestimates the complexity.Final Answer:
Option D -> Option DQuick Check:
Covariance: O(nd) for n samples, d features. [OK]
Hint: Covariance: O(nd) for n samples, d features. [OK]
Common Mistakes:
3. You implemented Robust Scaling but your model's performance dropped. What is the most likely cause?
medium
Solution
Step 1: Understand Robust Scaling
Robust Scaling uses the median and IQR, which reduces the influence of outliers.Step 2: Identify the Issue
If outliers are important, reducing their influence can harm model performance.Final Answer:
Option D -> Option DQuick Check:
Robust Scaling reduces outlier impact. [OK]
Hint: Robust Scaling reduces outlier impact. [OK]
Common Mistakes:
4. How can you optimize a decision tree implementation to handle large datasets more efficiently?
hard
Solution
Step 1: Consider parallel processing
Parallel processing can significantly speed up tree construction on large datasets.Step 2: Eliminate wrong options
Complex criteria (A) slow down; increasing depth (C) can overfit; single-threaded (D) is less efficient.Final Answer:
Option A -> Option AQuick Check:
Large data? Use parallel processing [OK]
Hint: Large data? Use parallel processing [OK]
Common Mistakes:
5. How can you optimize the computation of eigenvectors for a large covariance matrix in PCA?
hard
Solution
Step 1: Identify efficient eigenvector computation methods
The power iteration method is efficient for finding the largest eigenvectors of large matrices.Step 2: Eliminate wrong options
Use the full SVD decomposition. is computationally expensive. Use incremental PCA. is for streaming data, not large matrices. Use the Gram-Schmidt process. is for orthogonalization, not eigenvector computation.Final Answer:
Option B -> Option BQuick Check:
Power iteration for large matrix eigenvectors. [OK]
Hint: Power iteration for large matrix eigenvectors. [OK]
Common Mistakes:
