Practice
1.
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:
2. A logistic regression model is not converging during training. Which line in the code is most likely causing the issue?
import numpy as np
# Initialize weights
weights = np.zeros((m, 1))
# Training loop
for epoch in range(epochs):
# Compute predictions
predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
# Compute gradient
gradient = np.dot(X.T, (predictions - y)) / n
# Update weights
weights -= learning_rate * gradientmedium
Solution
Step 1: Identify potential issues
Convergence issues often arise from incorrect weight updates or learning rate problems.Step 2: Analyze weight update line
If learning_rate is too high, weights update too aggressively, causing divergence.Final Answer:
Option A -> Option AQuick Check:
Check learning rate in weight updates [OK]
Hint: Check learning rate in weight updates [OK]
Common Mistakes:
3. In a PCA implementation, you need to project data onto the first two principal components. Which approach is correct?
medium
Solution
Step 1: Understand projection in PCA
Projection involves multiplying the data matrix by the eigenvectors corresponding to the desired components.Step 2: Eliminate wrong options
Multiply the data matrix by the first two eigenvalues. is incorrect because eigenvalues are not used for projection. Multiply the covariance matrix by the first two eigenvectors. and D incorrectly use the covariance matrix instead of the data matrix.Final Answer:
Option A -> Option AQuick Check:
Project data using eigenvectors, not eigenvalues. [OK]
Hint: Project data using eigenvectors, not eigenvalues. [OK]
Common Mistakes:
4. How can you optimize the following logistic regression implementation for faster computation?
import numpy as np
# Initialize weights
weights = np.zeros((m, 1))
# Training loop
for epoch in range(epochs):
# Compute predictions
predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
# Compute gradient
gradient = np.dot(X.T, (predictions - y)) / n
# Update weights
weights -= learning_rate * gradienthard
Solution
Step 1: Identify bottlenecks
Matrix operations can be slow if X is dense and large.Step 2: Optimize matrix operations
Using a sparse matrix representation for X reduces memory usage and speeds up computations.Final Answer:
Option A -> Option AQuick Check:
Sparse matrices optimize large, sparse datasets [OK]
Hint: Sparse matrices optimize large, sparse datasets [OK]
Common Mistakes:
5. Which optimization would most effectively reduce the computational cost of the scaled dot-product attention mechanism for very large input sequences?
hard
Solution
Step 1: Identify the bottleneck
Computational cost is high due to dense matrix operations.Step 2: Choose the best optimization
Sparse matrices reduce computation by focusing on non-zero elements.Final Answer:
Option D -> Option DQuick Check:
Sparse matrices for large inputs [OK]
Hint: Sparse matrices for large inputs [OK]
Common Mistakes:
