Practice
1. 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:
2. What is the time complexity of calculating precision, recall, and F1-score from a confusion matrix?
easy
Solution
Step 1: Analyze the operations
Calculating precision, recall, and F1-score involves basic arithmetic operations on a fixed-size matrix.Step 2: Determine complexity
These operations are constant time, O(1), as they do not depend on the size of the input data.Final Answer:
Option A -> Option AQuick Check:
Fixed-size matrix operations are O(1). [OK]
Hint: Fixed-size matrix operations are O(1). [OK]
Common Mistakes:
3. What does the following Python code output?
import numpy as np
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
x = np.array([2.0, 1.0, 0.1])
print(softmax(x))easy
Solution
Step 1: Calculate softmax
Compute the exponentials of the input array after subtracting the max value for numerical stability.Step 2: Normalize
Divide each exponential by the sum of all exponentials to get probabilities.Final Answer:
Option A -> Option AQuick Check:
Softmax outputs sum to 1 [OK]
Hint: Softmax outputs sum to 1 [OK]
Common Mistakes:
4. How can you optimize the backward pass of batch normalization to improve computational efficiency?
hard
Solution
Step 1: Identify optimization techniques
Fusing operations can reduce computational overhead.Step 2: Evaluate each option
Fusing batch normalization with linear layers reduces redundant computations.Final Answer:
Option A -> Option AQuick Check:
Fuse layers to optimize batch norm [OK]
Hint: Fuse layers to optimize batch norm [OK]
Common Mistakes:
5. How can you optimize the dropout layer implementation to reduce computational overhead during training?
hard
Solution
Step 1: Identify Optimization Opportunities
In-place operations reduce memory usage and computational overhead by modifying data directly.Step 2: Eliminate Wrong Options
Use a fixed mask for all training iterations. reduces randomness, harming model performance. Precompute masks for all layers before training. increases memory usage. Reduce the dropout rate to minimize mask application. changes model behavior, not efficiency.Final Answer:
Option A -> Option AQuick Check:
In-place operations save memory and time. [OK]
Hint: In-place operations save memory and time. [OK]
Common Mistakes:
