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 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:
3. What does the following Python code snippet output when implementing Mini-Batch SGD with Momentum?
import numpy as np
velocity = np.array([0.0, 0.0])
gradient = np.array([0.1, 0.2])
learning_rate = 0.01
momentum = 0.9
velocity = momentum * velocity - learning_rate * gradient
print(velocity)easy
Solution
Step 1: Calculate the velocity update
The formula for velocity update isvelocity = momentum * velocity - learning_rate * gradient. Given the initial velocity is zero, the update becomesvelocity = -learning_rate * gradient.Step 2: Substitute the values
Substitutelearning_rate = 0.01andgradient = [0.1, 0.2]into the formula:velocity = -0.01 * [0.1, 0.2] = [-0.001, -0.002].Final Answer:
Option C -> Option CQuick Check:
Velocity update is negative of learning rate times gradient [OK]
Hint: Velocity update is negative of learning rate times gradient [OK]
Common Mistakes:
4. In a recommendation system, why might you choose NDCG over precision at k?
medium
Solution
Step 1: Understand NDCG vs Precision
NDCG accounts for the position of relevant items, giving higher scores to relevant items at higher ranks.Step 2: Evaluate Options
Precision at k does not consider the order of items, only their presence.Final Answer:
Option B -> Option BQuick Check:
NDCG values position. [OK]
Hint: NDCG values position. [OK]
Common Mistakes:
5. A Random Forest implementation is producing poor results. The trees seem to be too similar. What is the most likely cause?
medium
Solution
Step 1: Identify the Cause of Similar Trees
Similar trees often result from insufficient randomness in feature selection.Step 2: Eliminate Other Causes
Too many trees (A) or high variance (D) don't cause similarity; too much data (C) affects overfitting, not similarity.Final Answer:
Option A -> Option AQuick Check:
Feature sampling adds diversity [OK]
Hint: Feature sampling adds diversity [OK]
Common Mistakes:
