Practice
1. What is the time complexity of applying Min-Max scaling to a dataset with n samples and m features?
easy
Solution
Step 1: Understand Min-Max Scaling
Min-Max scaling requires computing the min and max for each feature, which involves iterating over all samples for each feature.Step 2: Calculate Complexity
For each of the m features, we iterate over n samples, resulting in O(n * m) complexity.Final Answer:
Option A -> Option AQuick Check:
Min-Max scaling is O(n * m). [OK]
Hint: Min-Max scaling is O(n * m). [OK]
Common Mistakes:
2. What does the following Python code snippet output when implementing the K-Means clustering algorithm from scratch?
import numpy as np
# Sample data
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
# Initial centroids
centroids = np.array([[1, 2], [10, 2]])
# Calculate distances
for i in range(len(X)):
distances = np.linalg.norm(X[i] - centroids, axis=1)
print(distances)
easy
Solution
Step 1: Understand the code logic
The code calculates the Euclidean distance from each data point to both centroids.Step 2: Eliminate wrong options
Prints the distances between each point and the first centroid only. and C are incorrect because the code calculates distances to both centroids, not just one. Prints the sum of distances for each point to both centroids. is incorrect because it doesn't sum the distances.Final Answer:
Option B -> Option BQuick Check:
Distances to all centroids, not just one. [OK]
Hint: Distances to all centroids, not just one. [OK]
Common Mistakes:
3.
import numpy as np
from scipy.spatial import distance
X_train = np.array([[1, 2], [3, 4], [5, 6]])
X_test = np.array([[1, 2]])
# Calculate distances
D = distance.cdist(X_test, X_train, 'euclidean')
print(D)What does the above code output?easy
Solution
Step 1: Understand the function
Thecdistfunction computes the distance between each pair of the two collections of inputs.Step 2: Calculate the distances
The Euclidean distance from[1, 2]to each point inX_trainis calculated as[0., 2.828, 4.472].Final Answer:
Option D -> Option DQuick Check:
Euclidean distance formula: √((x2-x1)² + (y2-y1)²) [OK]
Hint: Euclidean distance formula: √((x2-x1)² + (y2-y1)²) [OK]
Common Mistakes:
4. 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:
5. In a dataset with highly imbalanced classes, which cross-validation technique is most appropriate?
medium
Solution
Step 1: Consider class imbalance
Stratified K-Fold ensures each fold has the same class distribution as the entire dataset.Step 2: Eliminate wrong options
Standard K-Fold and D do not preserve class distribution. Leave-One-Out is inefficient for large datasets.Final Answer:
Option A -> Option AQuick Check:
Use Stratified K-Fold for class balance. [OK]
Hint: Use Stratified K-Fold for class balance. [OK]
Common Mistakes:
