Practice
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]
Solution
Step 1: Analyze the loop structure
The numerical gradient computation involves a loop over n parameters, each requiring constant time operations.Step 2: Determine the complexity
Each parameter requires a constant number of operations, leading to O(n) complexity.Final Answer:
Option D -> Option DQuick Check:
Loop over n parameters, each constant time. [OK]
import numpy as np
# Sample data
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
# Initial centroids
centroids = np.array([[1, 2], [10, 2]])
# Calculate distances
for i in range(len(X)):
distances = np.linalg.norm(X[i] - centroids, axis=1)
print(distances)
Solution
Step 1: Understand the code logic
The code calculates the Euclidean distance from each data point to both centroids.Step 2: Eliminate wrong options
Prints the distances between each point and the first centroid only. and C are incorrect because the code calculates distances to both centroids, not just one. Prints the sum of distances for each point to both centroids. is incorrect because it doesn't sum the distances.Final Answer:
Option B -> Option BQuick Check:
Distances to all centroids, not just one. [OK]
Solution
Step 1: Analyze the DCG Calculation
DCG involves iterating over the list once and performing constant-time operations for each element.Step 2: Determine Complexity
Since each element is processed once, the complexity is O(n).Final Answer:
Option A -> Option AQuick Check:
DCG iterates once over n elements. [OK]
Solution
Step 1: Identify the problem with imbalanced classes
Imbalanced classes can lead to biased predictions towards the majority class.Step 2: Choose a method to address imbalance
SMOTE (Synthetic Minority Over-sampling Technique) is effective for balancing datasets.Final Answer:
Option B -> Option BQuick Check:
SMOTE balances classes effectively [OK]
