Bird
Raised Fist0

Implement Decision Tree - Information Gain, Recursive Split

CODINGintermediateInterview freq: high
Choose your preparation mode6 modes available
๐ŸŽฏ
Implement a Decision Tree using Information Gain and Recursive Split
30 min ยท mid
mid

You are tasked with implementing a decision tree classifier from scratch using NumPy. The dataset is large, with 1 million samples and 50 features. The decision tree must handle missing values and categorical data. The implementation should be efficient enough to train within 5 minutes on a standard laptop.

  • Dataset size: 1 million samples, 50 features
  • Must handle missing values
  • Must handle categorical data
  • Training time limit: 5 minutes on a standard laptop
๐Ÿ“‹
Guided Steps
1/4
Step 1
Calculate Information Gain
How will you calculate the information gain for a potential split?
๐Ÿ’ก Hint
Consider how entropy changes with different splits and how to efficiently compute it.
Use entropy to measure information gain because it quantifies the uncertainty reduction after a split.
Mid: Correctly calculates entropy and information gain. Senior: Optimizes the calculation using vectorized operations in NumPy.
โš ๏ธCommon Wrong Answer
Using Gini impurity instead of entropy, leading to a mismatch with the problem requirements.
Step 2
Handle Missing Values
How will you handle missing values during the split?
๐Ÿ’ก Hint
Think about how missing values can affect the calculation of information gain.
Impute missing values or use a separate branch for missing data because it ensures all data is utilized.
Mid: Implements a basic imputation strategy. Senior: Discusses the impact of different imputation strategies on model performance.
โš ๏ธCommon Wrong Answer
Ignoring missing values, which leads to biased splits and poor model performance.
Step 3
Handle Categorical Data
How will you handle categorical features in your decision tree?
๐Ÿ’ก Hint
Consider how categorical data can be split and how it affects the tree structure.
Use one-hot encoding or treat each category as a separate branch because it allows the tree to handle categorical splits effectively.
Mid: Implements one-hot encoding. Senior: Evaluates the trade-offs between one-hot encoding and other methods like label encoding.
โš ๏ธCommon Wrong Answer
Using label encoding without considering its impact on the tree's decision boundaries.
Step 4
Optimize Recursive Splitting
How will you ensure the recursive splitting is efficient?
๐Ÿ’ก Hint
Think about the computational complexity of recursive operations and how to minimize redundant calculations.
Use memoization or iterative deepening because it reduces redundant calculations and speeds up the process.
Mid: Implements basic recursion. Senior: Optimizes recursion with memoization or iterative techniques.
โš ๏ธCommon Wrong Answer
Using naive recursion without optimization, leading to excessive computation time.
๐Ÿ“Š
Scoring Rubric
โ–ผ Show
Must Have - 60%
  • Correctly calculates information gain using entropy.
  • Handles missing values appropriately.
  • Implements handling of categorical data.
  • Ensures recursive splitting is efficient.
Good to Have - 30%
  • Uses vectorized operations for entropy calculation.
  • Discusses different imputation strategies for missing values.
  • Evaluates trade-offs between different encoding methods for categorical data.
  • Optimizes recursion with memoization or iterative techniques.
Bonus - 10%
  • Implements a method to visualize the decision tree.
  • Discusses the impact of tree depth on overfitting.
  • Considers parallel processing to speed up training.
โœ…
Model Answer
โ–ผ Reveal

The ideal solution involves implementing a decision tree that calculates information gain using entropy, handles missing values and categorical data, and optimizes recursive splitting for efficiency. The solution should be able to train on a large dataset within the given time constraints.

  • Calculate information gain using entropy to measure uncertainty reduction.
  • Handle missing values by imputation or separate branches to utilize all data.
  • Use one-hot encoding for categorical data to allow effective splits.
  • Optimize recursive splitting with memoization or iterative techniques to reduce computation time.
Using Gini impurity instead of entropy.
Ignoring missing values, leading to biased splits.
Using label encoding without considering its impact on decision boundaries.
Implementing naive recursion without optimization, leading to excessive computation time.

Practice

(1/5)
1. What is the time complexity of the forward pass of batch normalization for a dataset with n samples and d features?
easy
A. O(n + d)
B. O(n^2)
C. O(d^2)
D. O(n * d)

Solution

  1. Step 1: Analyze the operations

    Batch normalization involves computing mean and variance, both O(n * d) operations.
  2. Step 2: Consider the normalization step

    Normalizing each feature is also O(n * d).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Batch norm: O(n * d) for mean/variance [OK]
Hint: Batch norm: O(n * d) for mean/variance [OK]
Common Mistakes:
2. What does the following Python code snippet output when calculating the Intersection over Union (IoU) for two bounding boxes?
import numpy as np

def iou(box1, box2):
    x1 = max(box1[0], box2[0])
    y1 = max(box1[1], box2[1])
    x2 = min(box1[2], box2[2])
    y2 = min(box1[3], box2[3])
    intersection = max(0, x2 - x1) * max(0, y2 - y1)
    area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
    area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
    union = area1 + area2 - intersection
    return intersection / union

box1 = [0, 0, 2, 2]
box2 = [1, 1, 3, 3]
print(iou(box1, box2))
easy
A. 0.14285714285714285
B. 0.3333333333333333
C. 0.5
D. 0.25

Solution

  1. Step 1: Calculate Intersection and Union

    The intersection area is 1 (from (1,1) to (2,2)). The union area is 7 (4 from box1 + 4 from box2 - 1 intersection).
  2. Step 2: Calculate IoU

    IoU = 1/7 = 0.14285714285714285.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Intersection over Union formula [OK]
Hint: Intersection over Union formula [OK]
Common Mistakes:
3. In which scenario is gradient checking most useful?
medium
A. When implementing a new custom layer in a neural network.
B. When training a model with a large dataset.
C. When optimizing hyperparameters.
D. When deploying a model to production.

Solution

  1. Step 1: Identify scenarios where gradient checking is applicable

    Gradient checking is crucial when implementing new components to ensure correctness.
  2. Step 2: Eliminate unrelated scenarios

    Options A, C, and D are not directly related to verifying gradient correctness.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Use gradient check for new components. [OK]
Hint: Use gradient check for new components. [OK]
Common Mistakes:
4. In a PCA implementation, you need to project data onto the first two principal components. Which approach is correct?
medium
A. Multiply the data matrix by the first two eigenvectors.
B. Multiply the data matrix by the first two eigenvalues.
C. Multiply the covariance matrix by the first two eigenvectors.
D. Multiply the covariance matrix by the first two eigenvalues.

Solution

  1. Step 1: Understand projection in PCA

    Projection involves multiplying the data matrix by the eigenvectors corresponding to the desired components.
  2. Step 2: Eliminate wrong options

    Multiply the data matrix by the first two eigenvalues. is incorrect because eigenvalues are not used for projection. Multiply the covariance matrix by the first two eigenvectors. and D incorrectly use the covariance matrix instead of the data matrix.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Project data using eigenvectors, not eigenvalues. [OK]
Hint: Project data using eigenvectors, not eigenvalues. [OK]
Common Mistakes:
5. How can you optimize the computation of softmax and cross-entropy loss for a batch of inputs in a neural network?
hard
A. Compute softmax and cross-entropy loss separately for each input.
B. Use in-place operations to reduce memory usage.
C. Combine softmax and cross-entropy into a single operation.
D. Use a higher precision data type for calculations.

Solution

  1. Step 1: Identify optimization opportunity

    Combining softmax and cross-entropy avoids intermediate storage of probabilities.
  2. Step 2: Reduce computational overhead

    This reduces both memory usage and computational cost.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Combine operations to save resources [OK]
Hint: Combine operations to save resources [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard