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. You implemented a 2D convolution but the output is all zeros. What is the most likely cause?
medium
Solution
Step 1: Identify the zero output cause
If the kernel is filled with zeros, every convolution operation will result in zero, regardless of the image content.Step 2: Eliminate wrong options
The kernel is not normalized. affects scaling, not zero output. The image is padded incorrectly. affects boundary handling, not zero output. The stride is set too high. affects output size, not zero output.Final Answer:
Option B -> Option BQuick Check:
Zero kernel leads to zero output. [OK]
Hint: Zero kernel leads to zero output. [OK]
Common Mistakes:
3. In a multi-class classification problem, how should you modify the confusion matrix implementation to handle more than two classes?
medium
Solution
Step 1: Understand multi-class needs
Each class needs its own row and column in the matrix.Step 2: Choose correct matrix size
A (n_classes, n_classes) matrix captures all class interactions.Final Answer:
Option D -> Option DQuick Check:
Multi-class needs (n_classes, n_classes) matrix. [OK]
Hint: Multi-class needs (n_classes, n_classes) matrix. [OK]
Common Mistakes:
4. In the context of logistic regression, which approach is correct for handling a dataset with highly imbalanced classes?
medium
Solution
Step 1: Identify the problem with imbalanced classes
Imbalanced classes can lead to biased predictions towards the majority class.Step 2: Choose a method to address imbalance
SMOTE (Synthetic Minority Over-sampling Technique) is effective for balancing datasets.Final Answer:
Option B -> Option BQuick Check:
SMOTE balances classes effectively [OK]
Hint: SMOTE balances classes effectively [OK]
Common Mistakes:
5. How can you optimize a decision tree implementation to handle large datasets more efficiently?
hard
Solution
Step 1: Consider parallel processing
Parallel processing can significantly speed up tree construction on large datasets.Step 2: Eliminate wrong options
Complex criteria (A) slow down; increasing depth (C) can overfit; single-threaded (D) is less efficient.Final Answer:
Option A -> Option AQuick Check:
Large data? Use parallel processing [OK]
Hint: Large data? Use parallel processing [OK]
Common Mistakes:
