PIXELBANKv8.2.1
Menu

Best Split Finder

Find the best threshold for splitting a feature to maximize information gain.

Given a list of feature values and corresponding labels, try every midpoint between consecutive sorted unique feature values as a potential split threshold. For each threshold, split the data into left (\leq threshold) and right (>> threshold) subsets.

Return the threshold that gives the highest information gain, along with the gain value, as a tuple (threshold, gain). Both rounded to 4 decimal places.

If there's no possible split (all values identical), return (None, 0.0).

Example:

Input:
feature_values = [1, 2, 3, 4]
labels = [0, 0, 1, 1]
Output:
(2.5, 1.0)
Reasoning:
  • First, we sort the unique feature values and find midpoints: [1,2,3,4][1, 2, 3, 4] has midpoints at 1.51.5, 2.52.5, and 3.53.5.
  • Then, we calculate the information gain for each midpoint threshold, e.g., for 2.52.5, we split the data into [1,2][1, 2] with labels [0,0][0, 0] and [3,4][3, 4] with labels [1,1][1, 1].
  • The information gain for 2.52.5 is calculated as IG=H([0,0,1,1])24H([0,0])24H([1,1])=1120120=1.0IG = H([0, 0, 1, 1]) - \frac{2}{4}H([0, 0]) - \frac{2}{4}H([1, 1]) = 1 - \frac{1}{2} \cdot 0 - \frac{1}{2} \cdot 0 = 1.0, which is the highest gain among all midpoints.
  • Since 2.52.5 yields the highest information gain of 1.01.0, the output is (2.5,1.0)(2.5, 1.0).

Constraints:

  • feature_values: list of numeric values
  • labels: list of class labels (same length)
  • Split: left = values <= threshold, right = values > threshold
  • Return (best_threshold, best_gain) rounded to 4 decimal places
Editor

Test Results

0/0
Run code to see test results.