Practice
1.
import numpy as np
class ContinualLearner:
def __init__(self):
self.model = None
def update(self, new_data):
if self.model is None:
self.model = new_data
else:
self.model += new_data
learner = ContinualLearner()
learner.update(np.array([1, 2, 3]))
learner.update(np.array([4, 5, 6]))
print(learner.model)What does this code output?easy
Solution
Step 1: Understand the update mechanism.
The code initializes the model with the first data and adds subsequent data to it.Step 2: Calculate the output.
First update sets model to [1, 2, 3]. Second update adds [4, 5, 6], resulting in [5, 7, 9].Final Answer:
Option A -> Option AQuick Check:
First set, then add. [OK]
Hint: First set, then add. [OK]
Common Mistakes:
2. In the context of decision trees, which metric is used to measure the impurity of a node?
easy
Solution
Step 1: Identify the metric for node impurity.
Gini Impurity is specifically used to measure the impurity of a node in decision trees.Step 2: Eliminate wrong options.
Entropy is used for information gain, Mean Squared Error is for regression tasks, and Information Gain is a criterion for splitting, not impurity.Final Answer:
Option B -> Option BQuick Check:
Gini measures impurity in nodes. [OK]
Hint: Gini measures impurity in nodes. [OK]
Common Mistakes:
3. Given the following Python code snippet, what will be the output if the model is trained with biased data?
from sklearn.metrics import accuracy_score
y_true = [0, 1, 1, 0, 1]
y_pred = [0, 1, 1, 0, 0]
accuracy = accuracy_score(y_true, y_pred)
print(accuracy)easy
Solution
Step 1: Calculate the accuracy.
Accuracy is the ratio of correctly predicted instances to the total instances. Here, 3 out of 5 predictions are correct.Step 2: Compute the accuracy.
Correct predictions: [0, 1, 1] -> 0.6Final Answer:
0.6 -> 0.6Quick Check:
Count correct predictions, divide by total. [OK]
Hint: Count correct predictions, divide by total. [OK]
Common Mistakes:
4. Given the following Python code, what will be the output when calculating the ROC-AUC for a highly imbalanced dataset?
from sklearn.metrics import roc_auc_score
y_true = [0, 0, 0, 0, 1]
y_scores = [0.1, 0.2, 0.3, 0.4, 0.9]
roc_auc = roc_auc_score(y_true, y_scores)
print(roc_auc)easy
Solution
Step 1: Calculate ROC-AUC
ROC-AUC measures the ability to distinguish between classes. Here, the positive class is perfectly ranked higher than the negative class.Step 2: Eliminate wrong options
0.5 would indicate random guessing. 0.75 and 0.9 are incorrect as the positive class is perfectly ranked.Final Answer:
Option A -> Option AQuick Check:
Perfect ranking gives ROC-AUC of 1.0. [OK]
Hint: Perfect ranking gives ROC-AUC of 1.0. [OK]
Common Mistakes:
5. A model trained on a dataset shows unexpected bias against a minority group. What is the most likely root cause?
medium
Solution
Step 1: Identify the symptom.
Bias against a minority group indicates potential data imbalance.Step 2: Analyze the options.
Imbalanced data (B) is a common cause of bias. Other options relate to model complexity or training dynamics, not bias directly.Final Answer:
Option A -> Option AQuick Check:
Imbalanced data often leads to bias. [OK]
Hint: Imbalanced data often leads to bias. [OK]
Common Mistakes:
