Practice
1. What does the following Python code output when applied to an image array
img?
import numpy as np
def flip_image(img):
return np.flip(img, axis=1)
img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
flipped_img = flip_image(img)
print(flipped_img)easy
Solution
Step 1: Understand the flip operation
Thenp.flipfunction flips the array along the specified axis. Here, axis=1 means flipping horizontally.Step 2: Apply the flip to the array
Flipping horizontally changes the order of elements in each row.Final Answer:
Option A -> Option AQuick Check:
Horizontal flip reverses row elements. [OK]
Hint: Horizontal flip reverses row elements. [OK]
Common Mistakes:
2. A Random Forest implementation is producing poor results. The trees seem to be too similar. What is the most likely cause?
medium
Solution
Step 1: Identify the Cause of Similar Trees
Similar trees often result from insufficient randomness in feature selection.Step 2: Eliminate Other Causes
Too many trees (A) or high variance (D) don't cause similarity; too much data (C) affects overfitting, not similarity.Final Answer:
Option A -> Option AQuick Check:
Feature sampling adds diversity [OK]
Hint: Feature sampling adds diversity [OK]
Common Mistakes:
3. How can you optimize the following data augmentation pipeline for memory efficiency?
def augment_image(img):
img = np.flip(img, axis=1)
img = img[10:110, 10:110]
img = (img - np.mean(img)) / np.std(img)
return imghard
Solution
Step 1: Identify memory inefficiencies
Intermediate assignments create unnecessary copies.Step 2: Evaluate optimization strategies
Chaining operations reduces memory usage by avoiding intermediate arrays.Final Answer:
Option A -> Option AQuick Check:
Chain operations to reduce memory usage. [OK]
Hint: Chain operations to reduce memory usage. [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 following non-max suppression code for large datasets?
def non_max_suppression(boxes, scores, iou_threshold):
indices = np.argsort(scores)[::-1]
keep = []
while len(indices) > 0:
current = indices[0]
keep.append(current)
indices = indices[1:]
indices = [i for i in indices if iou(boxes[current], boxes[i]) < iou_threshold]
return keephard
Solution
Step 1: Identify bottleneck
IoU calculations are the most computationally expensive part.Step 2: Optimize bottleneck
Parallelizing IoU calculations can significantly reduce runtime for large datasets.Final Answer:
Option A -> Option AQuick Check:
Parallelize expensive operations [OK]
Hint: Parallelize expensive operations [OK]
Common Mistakes:
