Practice
1. 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:
2. In the implementation of Adam optimizer, the model is not converging. Which line of code is most likely causing the issue?
medium
Solution
Step 1: Identify the potential issue
The lineparameter -= learning_rate * m_hat / (np.sqrt(v_hat) + epsilon)is crucial for parameter updates. Ifepsilonis not properly set, it can cause division by zero or very small updates.Step 2: Eliminate other options
Options A, B, and C are standard parts of the Adam algorithm and unlikely to cause convergence issues unless there's a typo or logical error.Final Answer:
Option A -> Option AQuick Check:
Check epsilon in Adam's update step [OK]
Hint: Check epsilon in Adam's update step [OK]
Common Mistakes:
3. In a multi-class classification problem, how would you modify the softmax function to handle very large input values?
medium
Solution
Step 1: Address numerical stability
Subtracting the max input value prevents overflow in exponentials.Step 2: Ensure correct probabilities
This adjustment maintains the relative differences between inputs.Final Answer:
Option A -> Option AQuick Check:
Subtract max for stability in softmax [OK]
Hint: Subtract max for stability in softmax [OK]
Common Mistakes:
4. How can you optimize the K-Means implementation to improve its performance on large datasets?
hard
Solution
Step 1: Identify performance bottlenecks
The Elkan algorithm optimizes K-Means by reducing the number of distance calculations needed.Step 2: Eliminate wrong options
Increase the number of iterations. increases computation. Decrease the number of clusters. may not be suitable for the data's natural structure. Use a smaller subset of data for training. reduces accuracy.Final Answer:
Option A -> Option AQuick Check:
Elkan reduces distance calculations. [OK]
Hint: Elkan reduces distance calculations. [OK]
Common Mistakes:
5. How can you optimize the mAP calculation for a large dataset with thousands of images and bounding boxes?
hard
Solution
Step 1: Identify the Bottleneck
Calculating IoU for many boxes is computationally expensive.Step 2: Evaluate Optimization Strategies
Use parallel processing to compute IoU for each image: Parallel processing can significantly speed up IoU calculations. Reduce the number of bounding boxes by increasing the confidence threshold: Reducing boxes might miss important detections. Precompute IoU values for all possible box pairs: Precomputing is infeasible due to combinatorial explosion. Use a more efficient data structure for storing bounding boxes: Efficient data structures help but don't address computation directly.Final Answer:
Option A -> Option AQuick Check:
Parallelize IoU for large datasets [OK]
Hint: Parallelize IoU for large datasets [OK]
Common Mistakes:
