Practice
1. What is the time complexity of cropping an image of size
n x n to a smaller size m x m using NumPy slicing?easy
Solution
Step 1: Understand NumPy slicing
NumPy slicing creates a view, not a copy, so it operates in constant time.Step 2: Evaluate the complexity
The operation does not depend on the size of the arrays but on the slicing indices.Final Answer:
Option B -> Option BQuick Check:
NumPy slicing is O(1) due to views. [OK]
Hint: NumPy slicing is O(1) due to views. [OK]
Common Mistakes:
2.
import numpy as np
def numerical_gradient(f, x):
h = 1e-4
grad = np.zeros_like(x)
for i in range(x.size):
x[i] += h
fxh1 = f(x)
x[i] -= 2 * h
fxh2 = f(x)
grad[i] = (fxh1 - fxh2) / (2 * h)
x[i] += h
return grad
# Test function
f = lambda x: x[0]**2 + x[1]**2
x = np.array([3.0, 4.0])
print(numerical_gradient(f, x))easy
Solution
Step 1: Understand the numerical gradient calculation
The function calculates the gradient using central difference approximation.Step 2: Calculate the gradient for each element
For x = [3.0, 4.0], the gradient is [6.0, 8.0] because the derivative of x^2 is 2x.Final Answer:
Option A -> Option AQuick Check:
Central difference gives exact gradient for quadratic. [OK]
Hint: Central difference gives exact gradient for quadratic. [OK]
Common Mistakes:
3. In the context of implementing Skip-Gram with negative sampling, which approach is best for handling rare words?
medium
Solution
Step 1: Understand the problem with rare words
Rare words can be overshadowed by frequent words, leading to poor embeddings.Step 2: Evaluate solutions
Subsampling frequent words reduces their dominance, allowing rare words to have more influence during training.Final Answer:
Option A -> Option AQuick Check:
Subsampling frequent words helps rare words [OK]
Hint: Subsampling frequent words helps rare words [OK]
Common Mistakes:
4. How can you optimize the memory usage of a Min-Max scaling implementation for a large dataset?
hard
Solution
Step 1: Identify Memory Bottlenecks
Loading large datasets entirely into memory can be inefficient.Step 2: Optimize Memory Usage
Processing data in chunks reduces memory footprint, making it feasible to handle large datasets.Final Answer:
Option A -> Option AQuick Check:
Chunk processing saves memory. [OK]
Hint: Chunk processing saves memory. [OK]
Common Mistakes:
5. How can you optimize the memory usage of a Random Forest implementation with bagging and feature sampling?
hard
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]
Hint: Smaller data types save memory [OK]
Common Mistakes:
