Practice
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?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]
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(accuracy_score(y_test, y_pred))Solution
Step 1: Understand the impact of data leakage
Data leakage often results in overly optimistic performance metrics.Step 2: Analyze the code behavior
The code will execute without errors, but if leakage exists, the model will perform better than it should.Final Answer:
Option D -> Option DQuick Check:
Leakage inflates accuracy [OK]
import numpy as np
from sklearn.utils import resample
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([0, 0, 1])
X_resampled, y_resampled = resample(X, y, replace=True, n_samples=6, stratify=y)
print(y_resampled)Solution
Step 1: Understand stratified resampling.
Stratified resampling maintains the class distribution in the resampled dataset.Step 2: Calculate expected output.
Original distribution is 2:1, so resampled should be 4:2.Final Answer:
Option B -> Option BQuick Check:
Stratified resampling keeps class ratios. [OK]
Solution
Step 1: Identify the technique focused on fairness.
Adversarial debiasing is specifically designed to reduce bias by ensuring equal accuracy across groups.Step 2: Eliminate wrong options.
Data augmentation (A) is for increasing data diversity, cross-validation (C) is for model validation, and gradient boosting (D) is for improving model accuracy, not fairness.Final Answer:
Option B -> Option BQuick Check:
Adversarial debiasing ensures fairness across groups. [OK]
Solution
Step 1: Identify convergence issues
Centroids initialized too close can cause slow convergence or getting stuck in local minima.Step 2: Eliminate other causes
Too many clusters can cause overfitting but not convergence issues. Data normalization affects distance calculation but not convergence. Euclidean distance is standard for K-Means.Final Answer:
Option A -> Option AQuick Check:
Close centroids = slow convergence [OK]
