Practice
1.
import numpy as np
from sklearn.metrics import confusion_matrix
y_true = [0, 1, 0, 1, 0, 1, 1]
y_pred = [0, 0, 0, 1, 1, 1, 1]
cm = confusion_matrix(y_true, y_pred)
print(cm)What does the code output?easy
Solution
Step 1: Understand confusion matrix structure
The confusion matrix is structured as [[TN, FP], [FN, TP]].Step 2: Calculate values
TN=2 (true negatives), FP=1 (false positives), FN=1 (false negatives), TP=3 (true positives).Final Answer:
Option A -> Option AQuick Check:
Confusion matrix is [[TN, FP], [FN, TP]]. [OK]
Hint: Confusion matrix is [[TN, FP], [FN, TP]]. [OK]
Common Mistakes:
2.
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:
3.
import numpy as np
confusion_matrix = np.array([[50, 10], [5, 35]])
precision = confusion_matrix[0, 0] / (confusion_matrix[0, 0] + confusion_matrix[1, 0])
recall = confusion_matrix[0, 0] / (confusion_matrix[0, 0] + confusion_matrix[0, 1])
print(f"Precision: {precision}, Recall: {recall}")easy
Solution
Step 1: Calculate Precision
Precision is calculated as TP / (TP + FP) = 50 / (50 + 5) = 0.9091.Step 2: Calculate Recall
Recall is calculated as TP / (TP + FN) = 50 / (50 + 10) = 0.8333.Final Answer:
Option D -> Option DQuick Check:
Precision uses FP, Recall uses FN. [OK]
Hint: Precision uses FP, Recall uses FN. [OK]
Common Mistakes:
4. What does the following Python code output?
import numpy as np
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
x = np.array([2.0, 1.0, 0.1])
print(softmax(x))easy
Solution
Step 1: Calculate softmax
Compute the exponentials of the input array after subtracting the max value for numerical stability.Step 2: Normalize
Divide each exponential by the sum of all exponentials to get probabilities.Final Answer:
Option A -> Option AQuick Check:
Softmax outputs sum to 1 [OK]
Hint: Softmax outputs sum to 1 [OK]
Common Mistakes:
5. How can you optimize the dropout layer implementation to reduce computational overhead during training?
hard
Solution
Step 1: Identify Optimization Opportunities
In-place operations reduce memory usage and computational overhead by modifying data directly.Step 2: Eliminate Wrong Options
Use a fixed mask for all training iterations. reduces randomness, harming model performance. Precompute masks for all layers before training. increases memory usage. Reduce the dropout rate to minimize mask application. changes model behavior, not efficiency.Final Answer:
Option A -> Option AQuick Check:
In-place operations save memory and time. [OK]
Hint: In-place operations save memory and time. [OK]
Common Mistakes:
