Skip to content

Label cat_continuous "High" bins with the percentiles they use - #1094

Open
vishalhabib99 wants to merge 1 commit into
uber:masterfrom
vishalhabib99:fix-cat-continuous-high-bins
Open

vishalhabib99 wants to merge 1 commit into
uber:masterfrom
vishalhabib99:fix-cat-continuous-high-bins

Conversation

@vishalhabib99

Copy link
Copy Markdown

Proposed changes

causalml.inference.tree.cat_continuous(x, granularity="High") (also exported from causalml.inference.tree and used by kpi_transform for a single feature) computes 20 cut-offs at p5, p10, ..., p95 and p99, but its labeling code was a copy of the "Medium" branch. It compared each value with only the first nine cut-offs (p5 to p45), named those bins p10 to p90, and put every value above p45 into a "> p90" bin. The other 11 cut-offs were computed and never used.

Reproduction on current master (089a882):

import numpy as np
from collections import Counter
from causalml.inference.tree import cat_continuous

res = cat_continuous(np.arange(1, 101), granularity="High")
for label, n in sorted(Counter(res).items()):
    print(n, label)

Before (10 bins; 55 of 100 values labeled "> p90", with the p45 value as the bound):

5  <= p10 (5.95)
5  <= p20 (10.9)
...
5  <= p90 (45.550000000000004)
55  > p90 (45.550000000000004)

After (21 bins, each named after the percentile whose value bounds it):

5  <= p5 (5.95)
5  <= p10 (10.9)
...
5  <= p95 (95.05)
4  <= p99 (99.01)
1  > p99 (99.01)

The "High" branch now labels each value with the first cut-off it falls under. The "Medium" and "Low" branches are untouched.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

Added test_cat_continuous_high_granularity_uses_its_percentiles to tests/test_utils.py. It fails on master (assert 10 == 21 distinct bins) and passes with the fix. pytest tests/test_utils.py: 5 passed. pytest tests/test_causal_trees.py tests/test_uplift_trees.py: 97 passed, 1 failed; the failure (test_uplift_tree_visualization, FileNotFoundError for the graphviz dot binary, which is not installed locally) fails the same way on master. black --check and flake8 are clean on the changed files.

With granularity="High", cat_continuous computes cut-offs at p5, p10, ...,
p95 and p99, but the labeling code was the "Medium" one: it compared each
value with the first nine cut-offs (p5 to p45) and named them p10 to p90,
then put every value above p45 in a "> p90" bin. On 1..100 that bin held
55 of the 100 values and the other 15 cut-offs were never used.

Label each value with the first cut-off it falls under, giving the 21 bins
the computed percentiles describe. "Medium" and "Low" are unchanged.
@cla-assistant

cla-assistant Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant