Practice
1.
import numpy as np
def dcg_at_k(r, k):
r = np.asfarray(r)[:k]
if r.size:
return np.sum(r / np.log2(np.arange(2, r.size + 2)))
return 0.0
relevance_scores = [3, 2, 3, 0, 1, 2]
k = 3
print(dcg_at_k(relevance_scores, k))What does this code output?easy
Solution
Step 1: Understand DCG Calculation
The DCG is calculated as the sum of relevance scores divided by log base 2 of their positions.Step 2: Calculate for k=3
For k=3, the scores are [3, 2, 3]. The calculation is 3/1 + 2/log2(3) + 3/log2(4).Final Answer:
Option A -> Option AQuick Check:
DCG uses log2 positions. [OK]
Hint: DCG uses log2 positions. [OK]
Common Mistakes:
2. What is the time complexity of computing the softmax function for an input array of size n?
easy
Solution
Step 1: Analyze operations
Softmax involves computing exponentials and a sum, both linear in n.Step 2: Determine complexity
Each operation is O(n), so overall complexity is O(n).Final Answer:
Option D -> Option DQuick Check:
Softmax is linear in input size [OK]
Hint: Softmax is linear in input size [OK]
Common Mistakes:
3. In the context of implementing linear regression with gradient descent, how would you handle a dataset with missing values?
medium
Solution
Step 1: Consider impact of missing values
Ignoring or zero-filling can bias results; removing samples reduces data.Step 2: Choose appropriate imputation
Mean imputation is a common strategy that maintains dataset size and reduces bias.Final Answer:
Option A -> Option AQuick Check:
Mean imputation balances bias and data retention. [OK]
Hint: Mean imputation balances bias and data retention. [OK]
Common Mistakes:
4. In a Random Forest implementation, how would you apply feature sampling to handle high-dimensional data effectively?
medium
Solution
Step 1: Understand Feature Sampling
Feature sampling involves selecting a random subset of features for each tree to reduce dimensionality.Step 2: Evaluate Options
Using all features (A) or a fixed subset (C) doesn't reduce dimensionality; correlation-based selection (D) isn't random.Final Answer:
Option C -> Option CQuick Check:
Random subsets reduce dimensionality [OK]
Hint: Random subsets reduce dimensionality [OK]
Common Mistakes:
5. How can you optimize the implementation of linear regression using gradient descent for large datasets?
hard
Solution
Step 1: Identify scalability issues
Full-batch gradient descent is computationally expensive for large datasets.Step 2: Choose an optimization strategy
Mini-batch gradient descent reduces computation per iteration and can converge faster.Final Answer:
Option D -> Option DQuick Check:
Mini-batch reduces computation, speeds convergence. [OK]
Hint: Mini-batch reduces computation, speeds convergence. [OK]
Common Mistakes:
