Practice
1. 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:
2.
import numpy as np
def euclidean_distance(a, b):
return np.sqrt(np.sum((a - b) ** 2))
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5])
result = euclidean_distance(vector1, vector2)
print(result)medium
Solution
Step 1: Check vector shapes
vector1 has shape (3,), vector2 has shape (2,).Step 2: Identify operation failure
Subtraction fails due to shape mismatch, raising a ValueError.Final Answer:
Option B -> Option BQuick Check:
Shape mismatch leads to ValueError. [OK]
Hint: Shape mismatch leads to ValueError. [OK]
Common Mistakes:
3. 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:
4. How would you modify the Mini-Batch SGD with Momentum to handle sparse data more efficiently?
medium
Solution
Step 1: Identify the challenge with sparse data
Sparse data often leads to many zero gradients, making full updates inefficient.Step 2: Choose the appropriate modification
Lazy updates only update non-zero elements, reducing unnecessary computations and improving efficiency.Final Answer:
Option D -> Option DQuick Check:
Use lazy updates for sparse data efficiency [OK]
Hint: Use lazy updates for sparse data efficiency [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:
