Practice
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?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]
import numpy as np
def entropy(y):
unique, counts = np.unique(y, return_counts=True)
probabilities = counts / counts.sum()
return -np.sum(probabilities * np.log2(probabilities))
labels = np.array([1, 1, 0, 0, 1])
print(entropy(labels))What does this code output?Solution
Step 1: Calculate probabilities
For labels [1, 1, 0, 0, 1], probabilities are [0.4, 0.6].Step 2: Calculate entropy
Entropy = -[0.4*log2(0.4) + 0.6*log2(0.6)] = 0.9709505944546686.Final Answer:
Option B -> Option BQuick Check:
Entropy formula: -Σ(p*log2(p)) [OK]
StepLR scheduler for each epoch?Solution
Step 1: Analyze StepLR update mechanism
StepLR updates the learning rate by multiplying it with a constant factor, which is a constant time operation.Step 2: Determine time complexity
Since the update is a simple multiplication, the time complexity is O(1).Final Answer:
Option C -> Option CQuick Check:
StepLR update is a constant time operation. [OK]
Solution
Step 1: Handle categorical features
One-hot encoding converts categorical features into a numerical format suitable for K-Means.Step 2: Eliminate wrong options
Use Euclidean distance for all features. is incorrect because Euclidean distance isn't suitable for categorical data. Ignore categorical features during clustering. is incorrect as it loses information. Use a different clustering algorithm like DBSCAN. suggests a different algorithm, not a modification.Final Answer:
Option D -> Option DQuick Check:
Use one-hot encoding for categorical data. [OK]
Solution
Step 1: Identify Memory Optimization Techniques
Using smaller data types can significantly reduce memory usage.Step 2: Evaluate Options
Using a single tree (A) reduces model complexity, not memory; reducing features (B) affects accuracy; serialization (D) affects storage, not runtime memory.Final Answer:
Option B -> Option BQuick Check:
Smaller data types save memory [OK]
