📘
Best Split Finder
MediumDecision Trees
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 (≤ 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] has midpoints at 1.5, 2.5, and 3.5.
- Then, we calculate the information gain for each midpoint threshold, e.g., for 2.5, we split the data into [1,2] with labels [0,0] and [3,4] with labels [1,1].
- The information gain for 2.5 is calculated as IG=H([0,0,1,1])−42H([0,0])−42H([1,1])=1−21⋅0−21⋅0=1.0, which is the highest gain among all midpoints.
- Since 2.5 yields the highest information gain of 1.0, the output is (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
Python 3.13.1
Test Results
0/0Run code to see test results.