Bird
Raised Fist0

Implement KNN Classifier - Brute Force and KD-Tree

CODINGintermediateInterview freq: high
Choose your preparation mode6 modes available
β–Ά
Execution Trace
Step 1/5
Data Point A

Calculating distance from Data Point A to all other points.

🎯 Understanding distance computation is crucial for KNN.
distances = [euclidean_distance(A, point) for point in dataset]
Distance List

Distance list sorted: [0.5, 1.2, 1.5, 2.3, 3.1]

🎯 Sorting is necessary to identify nearest neighbors.
sorted_distances = sorted(distances)
k Nearest Neighbors

Selecting 3 nearest neighbors: [0.5, 1.2, 1.5]

🎯 Selection of k nearest neighbors determines classification.
k_nearest = sorted_distances[:k]
KD-Tree Construction

KD-Tree built with root node splitting on median.

🎯 KD-Tree construction optimizes search for nearest neighbors.
kd_tree = KDTree(dataset)
KD-Tree Search

Traversing KD-Tree to find nearest neighbors.

🎯 Tree traversal reduces complexity compared to brute force.
neighbors = kd_tree.query(A, k)
πŸ“Š
Diagram
Diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       KNN Classifier          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Brute Force β”‚ KD-Tree         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1. Compute  β”‚ 1. Build KD-Treeβ”‚
β”‚    distancesβ”‚                 β”‚
β”‚ 2. Sort     β”‚ 2. Traverse treeβ”‚
β”‚    distancesβ”‚    to find k-NN β”‚
β”‚ 3. Select k β”‚ 3. Select k-NN  β”‚
β”‚    nearest  β”‚                 β”‚
β”‚    neighborsβ”‚                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • Brute Force: Computes distances for all points
  • KD-Tree: Efficiently finds nearest neighbors using tree traversal
  • Compute Distances: Calculate Euclidean distance
  • Sort Distances: Order distances to find nearest neighbors
  • Select k-NN: Choose k nearest neighbors for classification
πŸ”‘
Key Moments
β–²
Q: Why is sorting necessary in the brute force method?
A: Sorting is necessary to identify the k nearest neighbors by ordering distances.
Q: How does KD-Tree improve efficiency over brute force?
A: KD-Tree reduces the number of distance calculations by organizing data into a tree structure, allowing efficient traversal.
Q: What is the role of the median in KD-Tree construction?
A: The median is used to split data at each node, balancing the tree and optimizing search efficiency.

This document illustrates the implementation of a KNN classifier using both brute force and KD-Tree methods. The brute force approach involves calculating and sorting distances for all data points to find the nearest neighbors. In contrast, the KD-Tree method constructs a tree structure to efficiently traverse and locate the nearest neighbors, reducing computational complexity. Key insights include the necessity of sorting in brute force, the efficiency of KD-Tree traversal, and the importance of median splits in tree construction.

Primary Focus

βœ“ KNN algorithm implementation using brute force and KD-Tree

Practice

(1/5)
1.
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
A. [6.0, 8.0]
B. [6.0001, 8.0001]
C. [5.9999, 7.9999]
D. [0.0, 0.0]

Solution

  1. Step 1: Understand the numerical gradient calculation

    The function calculates the gradient using central difference approximation.
  2. 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.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Central difference gives exact gradient for quadratic. [OK]
Hint: Central difference gives exact gradient for quadratic. [OK]
Common Mistakes:
2.
import numpy as np

class DropoutLayer:
    def __init__(self, dropout_rate):
        self.dropout_rate = dropout_rate

    def forward(self, inputs, training=True):
        if training:
            self.mask = np.random.binomial(1, 1 - self.dropout_rate, size=inputs.shape)
            return inputs * self.mask
        else:
            return inputs * (1 - self.dropout_rate)

inputs = np.array([1.0, 2.0, 3.0])
dropout_layer = DropoutLayer(0.5)
output = dropout_layer.forward(inputs, training=True)
print(output)
Why might this implementation produce unexpected results during training?
medium
A. The dropout rate is too high.
B. The mask is not applied correctly.
C. The mask is not saved for backpropagation.
D. The inputs are not scaled during training.

Solution

  1. Step 1: Identify Training Mode Requirements

    During training, the mask must be saved for backpropagation to correctly compute gradients.
  2. Step 2: Eliminate Wrong Options

    The dropout rate is too high. is about hyperparameter tuning, not implementation. The mask is not applied correctly. is incorrect as the mask is applied correctly. The inputs are not scaled during training. is irrelevant during training.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Mask must be saved for backpropagation. [OK]
Hint: Mask must be saved for backpropagation. [OK]
Common Mistakes:
3. In the context of logistic regression, which approach is correct for handling a dataset with highly imbalanced classes?
medium
A. Use accuracy as the primary evaluation metric.
B. Apply SMOTE to balance the dataset.
C. Increase the learning rate to improve minority class prediction.
D. Use a linear activation function instead of sigmoid.

Solution

  1. Step 1: Identify the problem with imbalanced classes

    Imbalanced classes can lead to biased predictions towards the majority class.
  2. Step 2: Choose a method to address imbalance

    SMOTE (Synthetic Minority Over-sampling Technique) is effective for balancing datasets.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    SMOTE balances classes effectively [OK]
Hint: SMOTE balances classes effectively [OK]
Common Mistakes:
4. You implemented a train-test split with stratification, but the test set has a different class distribution than expected. What is the most likely cause?
medium
A. Incorrect test_size parameter
B. Stratify parameter not set correctly
C. Data leakage between train and test sets
D. Random state not fixed

Solution

  1. Step 1: Identify the role of stratification

    Stratification ensures class distribution is maintained across splits.
  2. Step 2: Diagnose the issue

    If the stratify parameter is not set correctly, class distribution will not be maintained.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Check stratify parameter for distribution issues. [OK]
Hint: Check stratify parameter for distribution issues. [OK]
Common Mistakes:
5. How can you optimize the memory usage of a learned positional encoding implementation in PyTorch?
hard
A. Use a sparse matrix for the encoding.
B. Reduce the number of dimensions in the encoding.
C. Share the encoding matrix across multiple layers.
D. Use half-precision floating point for the encoding matrix.

Solution

  1. Step 1: Consider memory optimization techniques

    Using half-precision floating point reduces memory usage while maintaining sufficient precision for many applications.
  2. Step 2: Eliminate wrong options

    Reduce the number of dimensions in the encoding. changes the model architecture. Use a sparse matrix for the encoding. is not applicable as the matrix is dense. Share the encoding matrix across multiple layers. may affect model performance and is not a direct memory optimization.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Half-precision reduces memory without major precision loss. [OK]
Hint: Half-precision reduces memory without major precision loss. [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