Practice
1.
import numpy as np
def cosine_similarity(a, b):
dot_product = np.dot(a, b)
norm_a = np.linalg.norm(a)
norm_b = np.linalg.norm(b)
return dot_product / (norm_a * norm_b)
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])
result = cosine_similarity(vector1, vector2)
print(result)easy
Solution
Step 1: Calculate dot product and norms
Dot product = 32, norm_a = 3.7416573867739413, norm_b = 8.774964387392123.Step 2: Compute cosine similarity
Cosine similarity = 32 / (3.7416573867739413 * 8.774964387392123) = 0.9746318461970762.Final Answer:
Option A -> Option AQuick Check:
Dot product over product of norms. [OK]
Hint: Dot product over product of norms. [OK]
Common Mistakes:
2. What is the time complexity of computing the forward pass for a single layer in a neural network using NumPy?
easy
Solution
Step 1: Identify operations
Forward pass involves matrix-vector multiplication.Step 2: Determine complexity
Matrix-vector multiplication is O(n) where n is the number of neurons.Final Answer:
Option B -> Option BQuick Check:
Matrix-vector multiplication is O(n) [OK]
Hint: Matrix-vector multiplication is O(n) [OK]
Common Mistakes:
3. What is the time complexity of updating the embeddings for a single word pair in the Skip-Gram model with negative sampling?
easy
Solution
Step 1: Identify the operations involved
Updating embeddings involves computing gradients and updating weights for the target word and k negative samples.Step 2: Determine the complexity
Each update involves a constant number of operations per sample, leading to O(k) complexity where k is the number of negative samples.Final Answer:
Option A -> Option AQuick Check:
Complexity is O(k) due to k negative samples [OK]
Hint: Complexity is O(k) due to k negative samples [OK]
Common Mistakes:
4. You implemented PCA but the variance explained by the first principal component is unexpectedly low. What is the most likely cause?
medium
Solution
Step 1: Identify the impact of centering data
Centering data is crucial for PCA as it ensures the covariance matrix reflects true variance.Step 2: Eliminate wrong options
Eigenvectors were not normalized. is incorrect because normalization affects direction, not variance. Covariance matrix was computed using rowvar=True. affects orientation, not variance. Too few principal components were selected. is irrelevant to the variance of the first component.Final Answer:
Option A -> Option AQuick Check:
Center data before PCA to capture variance. [OK]
Hint: Center data before PCA to capture variance. [OK]
Common Mistakes:
5. How can you optimize the implementation of linear regression using gradient descent for large datasets?
hard
Solution
Step 1: Identify scalability issues
Full-batch gradient descent is computationally expensive for large datasets.Step 2: Choose an optimization strategy
Mini-batch gradient descent reduces computation per iteration and can converge faster.Final Answer:
Option D -> Option DQuick Check:
Mini-batch reduces computation, speeds convergence. [OK]
Hint: Mini-batch reduces computation, speeds convergence. [OK]
Common Mistakes:
