A categorical feature splits a node by value; a continuous one has infinitely many possible cut points. Fortunately only cut points between observed values can change which samples go left, so a finite set of candidates is enough.
Implement best_threshold(x, y). Sort the distinct values of x, take the midpoint of every pair of consecutive values as a candidate threshold, send samples with x <= t left and the rest right, and return the candidate with the highest information gain (base-2 entropy) together with that gain.
x is a one-dimensional float array and y an integer label array of the same length; neither is guaranteed sorted.(threshold, gain) of two Python floats.x has fewer than two distinct values there is nothing to split: return (None, 0.0).Open one at a time. Each gives away a little more than the last.
Candidates come from np.unique(x), which already sorts and de-duplicates.
For each candidate, gain is the parent entropy minus the size-weighted entropy of the left and right groups. Keep a candidate only if its gain beats the best so far by more than a tiny tolerance, so earlier (smaller) thresholds win ties.
No results yet
Run your code to check it against the examples, or submit to run every test.