Practice
1.
import numpy as np
data = np.array([1, 2, 3, 4, 5])
mean = np.mean(data)
std = np.std(data)
standardized_data = (data - mean) / std
print(standardized_data)What does the code output?easy
Solution
Step 1: Understand Standardization
Standardization transforms data to have a mean of 0 and a standard deviation of 1.Step 2: Calculate the Output
The standardized values are calculated as (data - mean) / std, resulting in [-1.41, -0.71, 0, 0.71, 1.41].Final Answer:
Option C -> Option CQuick Check:
Standardization centers data around 0. [OK]
Hint: Standardization centers data around 0. [OK]
Common Mistakes:
2. In a scenario where training time is critical, which approach is better for implementing early stopping?
medium
Solution
Step 1: Consider training time constraints
In time-critical scenarios, stopping early can save resources.Step 2: Choose the appropriate patience value
A small patience value allows for quicker termination if no improvement is seen, aligning with the need to save time.Final Answer:
Option A -> Option AQuick Check:
Small patience saves time in critical scenarios. [OK]
Hint: Small patience saves time in critical scenarios. [OK]
Common Mistakes:
3. In your implementation of K-Means, the algorithm is not converging. What is the most likely cause?
medium
Solution
Step 1: Identify convergence issues
Poorly chosen initial centroids can lead to non-convergence as they may not represent the data well.Step 2: Eliminate wrong options
The number of clusters is too large. is incorrect because a large number of clusters doesn't prevent convergence. The dataset is too small. is incorrect as a small dataset doesn't inherently cause non-convergence. The distance metric is incorrect. is incorrect if the metric is consistent.Final Answer:
Option C -> Option CQuick Check:
Check initial centroids for convergence issues. [OK]
Hint: Check initial centroids for convergence issues. [OK]
Common Mistakes:
4. How can you optimize the following logistic regression implementation for faster computation?
import numpy as np
# Initialize weights
weights = np.zeros((m, 1))
# Training loop
for epoch in range(epochs):
# Compute predictions
predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
# Compute gradient
gradient = np.dot(X.T, (predictions - y)) / n
# Update weights
weights -= learning_rate * gradienthard
Solution
Step 1: Identify bottlenecks
Matrix operations can be slow if X is dense and large.Step 2: Optimize matrix operations
Using a sparse matrix representation for X reduces memory usage and speeds up computations.Final Answer:
Option A -> Option AQuick Check:
Sparse matrices optimize large, sparse datasets [OK]
Hint: Sparse matrices optimize large, sparse datasets [OK]
Common Mistakes:
5. How can the calculation of precision, recall, and F1-score be optimized for a large confusion matrix?
hard
Solution
Step 1: Identify bottlenecks
Calculating metrics for each class can be parallelized.Step 2: Choose the best optimization strategy
Parallel processing allows simultaneous computation, reducing time complexity.Final Answer:
Option C -> Option CQuick Check:
Parallelize class-wise computations. [OK]
Hint: Parallelize class-wise computations. [OK]
Common Mistakes:
