Practice
1.
from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [0, 1, 1, 0, 1, 1, 0]
y_pred = [0, 1, 0, 0, 1, 1, 1]
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
print(f"Precision: {precision}, Recall: {recall}, F1: {f1}")What will be the output of this code?easy
Solution
Step 1: Calculate precision.
Precision = TP / (TP + FP) = 2 / (2 + 1) = 0.67Step 2: Calculate recall.
Recall = TP / (TP + FN) = 2 / (2 + 2) = 0.50Step 3: Calculate F1 score.
F1 = 2 * (Precision * Recall) / (Precision + Recall) = 0.57Final Answer:
Option D -> Option DQuick Check:
Precision and recall calculations are key. [OK]
Hint: Precision and recall calculations are key. [OK]
Common Mistakes:
2. In the context of handling class imbalance, which technique is specifically designed to adjust the loss function to focus more on hard-to-classify examples?
easy
Solution
Step 1: Identify the technique that modifies the loss function.
Focal Loss is designed to focus more on hard-to-classify examples by adjusting the loss function.Step 2: Eliminate wrong options.
Oversampling and undersampling adjust the dataset, not the loss function. Cost-sensitive learning modifies the cost, not the focus on hard examples.Final Answer:
Option D -> Option DQuick Check:
Focal Loss focuses on hard examples. [OK]
Hint: Focal Loss focuses on hard examples. [OK]
Common Mistakes:
3.
import torch
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
for epoch in range(10):
for data, target in dataloader:
optimizer.zero_grad()
output = model(data)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
print(optimizer.param_groups[0]['lr'])What will be printed after the loop?easy
Solution
Step 1: Understand the code.
The learning rate is set to 0.1 and not changed in the loop.Step 2: Check for learning rate updates.
SGD without a scheduler keeps the learning rate constant.Final Answer:
Option C -> Option CQuick Check:
SGD without scheduler keeps lr constant. [check]
Hint: SGD without scheduler keeps lr constant. [check]
Common Mistakes:
4. When comparing two ML models using A/B testing, why might you choose causal inference over the holdout method?
medium
Solution
Step 1: Understand the methods
Causal inference helps understand the cause-effect relationship, not just performance.Step 2: Eliminate wrong options
Holdout is not inherently less accurate or unsuitable for large datasets. Causal inference is not necessarily faster.Final Answer:
Option C -> Option CQuick Check:
Causal inference explains why, not just what. [OK]
Hint: Causal inference explains why, not just what. [OK]
Common Mistakes:
5. You need to reduce the dimensionality of a dataset with 10,000 features for a real-time application. Which technique is more suitable and why?
medium
Solution
Step 1: Consider computational efficiency
Real-time applications require fast computations.Step 2: Evaluate each option
t-SNE is computationally expensive, UMAP is faster but still slower than PCA, and LDA is not suitable for unsupervised tasks.Final Answer:
Option B -> Option BQuick Check:
PCA for speed in large datasets [OK]
Hint: PCA for speed in large datasets [OK]
Common Mistakes:
