Practice
1.
import numpy as np
from scipy.spatial import distance
X_train = np.array([[1, 2], [3, 4], [5, 6]])
X_test = np.array([[1, 2]])
# Calculate distances
D = distance.cdist(X_test, X_train, 'euclidean')
print(D)What does the above code output?easy
Solution
Step 1: Understand the function
Thecdistfunction computes the distance between each pair of the two collections of inputs.Step 2: Calculate the distances
The Euclidean distance from[1, 2]to each point inX_trainis calculated as[0., 2.828, 4.472].Final Answer:
Option D -> Option DQuick Check:
Euclidean distance formula: √((x2-x1)² + (y2-y1)²) [OK]
Hint: Euclidean distance formula: √((x2-x1)² + (y2-y1)²) [OK]
Common Mistakes:
2.
import numpy as np
from sklearn.model_selection import train_test_split
X = np.array([1, 2, 3, 4, 5, 6])
y = np.array([0, 0, 1, 1, 0, 1])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, stratify=y)
print(y_train)easy
Solution
Step 1: Understand stratification
Stratification ensures that the class distribution in the train and test sets is similar to the original dataset.Step 2: Calculate class distribution
Original y has 3 zeros and 3 ones. With stratification, y_train should have 1 zero and 2 ones.Final Answer:
Option D -> Option DQuick Check:
Stratify maintains class distribution. [OK]
Hint: Stratify maintains class distribution. [OK]
Common Mistakes:
3. You implemented gradient checking but the numerical and analytical gradients differ significantly. What is the most likely cause?
medium
Solution
Step 1: Identify potential causes for gradient mismatch
Significant differences often indicate errors in the analytical gradient.Step 2: Eliminate other options
The step size for numerical gradient is too large. would cause small numerical errors, not significant differences. The learning rate is too high. and C are unrelated to gradient checking.Final Answer:
Option C -> Option CQuick Check:
Mismatch often means analytical error. [OK]
Hint: Mismatch often means analytical error. [OK]
Common Mistakes:
4. A Gaussian Naive Bayes model is giving poor performance on a dataset. Which of the following is the most likely cause?
medium
Solution
Step 1: Identify Gaussian Naive Bayes assumptions
Gaussian Naive Bayes assumes that features are normally distributed.Step 2: Evaluate each option
The features are not normally distributed. is correct because non-normal distribution violates the model's assumptions. The model has too many parameters. is incorrect as GaussianNB is simple. The dataset is too small. is less likely as small datasets affect all models. The features are highly correlated. is incorrect as GaussianNB can handle correlated features.Final Answer:
Option B -> Option BQuick Check:
GaussianNB assumes normal distribution. [OK]
Hint: GaussianNB assumes normal distribution. [OK]
Common Mistakes:
5. How would you modify the scaled dot-product attention mechanism to handle variable-length sequences efficiently?
medium
Solution
Step 1: Identify the challenge
Variable-length sequences require handling of different sequence lengths.Step 2: Choose the correct approach
Padding with masking is efficient and maintains sequence integrity.Final Answer:
Option A -> Option AQuick Check:
Pad and mask for variable lengths [OK]
Hint: Pad and mask for variable lengths [OK]
Common Mistakes:
