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 calculating the Euclidean distance between two vectors of length n?
easy
Solution
Step 1: Understand Euclidean distance calculation
Euclidean distance involves element-wise operations and a square root.Step 2: Analyze operations
Each element is processed once, leading to O(n) complexity.Final Answer:
Option D -> Option DQuick Check:
Element-wise operations imply O(n). [OK]
Hint: Element-wise operations imply O(n). [OK]
Common Mistakes:
3. What does the following Python code snippet output when implementing a simple bagging technique for a Random Forest?
import numpy as np
from sklearn.utils import resample
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
# Bagging
bagged_X = resample(X, n_samples=3, replace=True, random_state=42)
print(bagged_X)easy
Solution
Step 1: Understand Bagging with Replacement
Bagging involves sampling with replacement, meaning samples can be repeated.Step 2: Analyze Code Output
The code usesresamplewithreplace=True, so duplicates are possible.Final Answer:
Option A -> Option AQuick Check:
Bagging allows duplicates [OK]
Hint: Bagging allows duplicates [OK]
Common Mistakes:
4. What is the time complexity of performing a train-test split with stratification on a dataset with n samples?
easy
Solution
Step 1: Analyze the operation
Train-test split with stratification involves iterating over the dataset once to ensure class distribution.Step 2: Determine complexity
Since each element is processed once, the complexity is O(n).Final Answer:
Option A -> Option AQuick Check:
Stratified split processes each sample once. [OK]
Hint: Stratified split processes each sample once. [OK]
Common Mistakes:
5. In the context of implementing backpropagation using NumPy, which of the following is a correct approach to handle vanishing gradients?
medium
Solution
Step 1: Identify vanishing gradient issue
Vanishing gradients occur when gradients become too small to update weights effectively.Step 2: Correct approach
Gradient clipping prevents gradients from becoming too small or too large.Final Answer:
Option C -> Option CQuick Check:
Clip gradients to prevent vanishing [OK]
Hint: Clip gradients to prevent vanishing [OK]
Common Mistakes:
