MathIsimple
Machine Learning/Learning Center/Decision Trees/Continuous & Missing Values

Continuous & Missing Values

Learn advanced techniques for handling continuous attributes and missing data in decision trees— essential skills for real-world messy datasets

Real-World Data Challenges

Practical Techniques

Basic decision tree algorithms (like ID3) assume categorical features with complete data. However, real-world datasets contain continuous attributes (age, price, temperature) and missing values (incomplete surveys, sensor failures, data entry errors). Advanced algorithms like C4.5 and CART include sophisticated methods to handle both challenges.

Continuous Attributes

Features with infinite possible values within a range (e.g., income: $20k-$200k, age: 18-100 years).

Challenges: Can't create branches for every value; need thresholds or discretization.

Missing Values

Samples where some feature values are unknown, marked as NULL, ?, NaN, or empty.

Challenges: Can't test missing attributes; need strategies for splitting and prediction.

Handling Continuous Attributes

Discretization and threshold-based splitting

There are two main approaches to handle continuous features: (1) Pre-discretization—convert to categorical before tree building, or (2) Dynamic threshold selection—find optimal split points during tree construction. Modern algorithms use the second approach for better performance.

Method 1: Pre-Discretization (Binning)

Convert continuous attributes into categorical ones by defining ranges (bins). This is done before tree construction.

Common Binning Strategies

Equal-Width Binning

Divide the range into k equal-sized intervals.

Example: Age 0-100 → 5 bins: [0-20), [20-40), [40-60), [60-80), [80-100]
Simple, but sensitive to outliers

Equal-Frequency Binning (Quantile)

Each bin contains approximately the same number of samples.

Example: 100 samples → 4 bins with 25 samples each
Better handles skewed distributions

Domain-Knowledge Binning

Use meaningful thresholds based on domain expertise.

Example: BMI → Underweight (<18.5), Normal (18.5-25), Overweight (25-30), Obese (>30)
Most interpretable, requires expertise

Trade-offs:

  • Pro: Simple, works with ID3 algorithm unchanged
  • Con: Information loss, must choose bin count beforehand, non-adaptive

Method 2: Dynamic Threshold Selection (C4.5, CART)

During tree construction, dynamically find the optimal split threshold for each continuous attribute at each node. This creates binary splits: (attribute ≤ threshold) vs (attribute > threshold).

Threshold Selection Algorithm

  1. Sort samples by continuous attribute (e.g., sort by mileage: 28000, 32000, 41000, ...)
  2. Identify candidate split points: Midpoints between adjacent distinct values
    • Between 28000 and 32000 → candidate: 30000
    • Between 32000 and 41000 → candidate: 36500
    • And so on...
  3. For each candidate threshold:
    • Create binary split: (mileage ≤ threshold) vs (mileage > threshold)
    • Calculate information gain or Gini reduction
  4. Select threshold with maximum gain/minimum Gini
  5. Compare with other attributes (categorical and continuous) and choose overall best split
Worked Example: Used Car Mileage

Sorted mileage values: 28000, 32000, 41000, 45000, 52000, 61000, 78000, 95000

Candidate 1: mileage ≤ 30000 (1 sample) vs > 30000 (7 samples)

Gain = 0.12 (very imbalanced, low gain)

Candidate 2: mileage ≤ 43000 (3 samples) vs > 43000 (5 samples)

Gain = 0.38 (good balance, high gain) ✓ SELECTED

Candidate 3: mileage ≤ 70000 (6 samples) vs > 70000 (2 samples)

Gain = 0.21 (imbalanced again)

Result: Split becomes "mileage ≤ 43000" (Low) vs "mileage > 43000" (High).

Advantages:

  • Adaptive: Finds optimal threshold based on data and target
  • No information loss: Uses actual continuous values
  • Different thresholds at different nodes: Can split on same feature multiple times
  • Better performance: Typically outperforms pre-discretization

Key Insight: Reusability

Unlike categorical attributes (which are "used up" after splitting), continuous attributes can be split multiple times at different thresholds in different parts of the tree. Example: Root splits on "age ≤ 30", later a subtree splits on "age ≤ 50". This allows modeling complex non-linear relationships.

Example: Used Car Price Prediction

Predicting car price categories (Low/Mid/High) using mixed features including continuous mileage and year:

IDMakeMileageYearPriceConditionPrice Cat
1Honda45,0002018$18,500GoodMid
2Toyota32,0002019$22,000ExcellentHigh
3Ford78,0002015$12,000FairLow
4BMW52,0002017$25,000GoodHigh
5Chevrolet95,0002013$8,500FairLow
6Honda28,0002020$24,000ExcellentHigh
7Ford61,0002016$14,500GoodMid
8Toyota41,0002018$19,500GoodMid

Tree Construction with Continuous Features

Step 1: Root Node Split

Algorithm evaluates all attributes (Make categorical, Mileage continuous, Year continuous, Condition categorical).

Best split found: Mileage ≤ 50000 (Gain = 0.42)

Step 2: Left Subtree (Mileage ≤ 50000)

Samples: IDs 1, 2, 4, 6, 8 (mostly High and Mid prices)

Next best split: Year ≤ 2018 (separates Mid from High)

Step 3: Right Subtree (Mileage > 50000)

Samples: IDs 3, 5, 7 (all Low or Mid prices)

Next split: Condition = Fair vs others

Notice: Mileage used once at root, Year used once in subtree. Both are continuous but treated naturally by the algorithm—no manual binning required!

Handling Missing Values

Strategies for incomplete data in decision trees

Missing values are ubiquitous in real data: sensor failures, survey non-responses, data entry errors, privacy redactions. Simple approaches like dropping samples or imputing mean values lose information. Advanced decision tree algorithms (particularly C4.5) have built-in methods to handle missing values during both training and prediction.

Two Key Problems to Solve

Problem 1: Attribute Selection

When calculating information gain for an attribute with missing values, how do we account for samples where that attribute is unknown? We can't test them on that attribute.

Problem 2: Sample Assignment

When we split on an attribute, which child node do we send samples with missing values for that attribute? We don't know which branch they should take.

Solution 1: Adjusted Information Gain (C4.5)

When evaluating an attribute with missing values, C4.5 calculates information gain using only samples with known values, then scales down the gain proportionally to the fraction of known values.

Formula:

Adjusted_Gain(D, a) = ρ × Gain(D̃, a)

Where: ρ = |D̃| / |D| (fraction of samples with known value for attribute a)
D̃ = subset of D where attribute a is not missing

Example:

Dataset D: 100 samples. Attribute "Blood Pressure" is missing for 20 samples.

D̃ = 80 samples with known Blood Pressure

ρ = 80/100 = 0.8

Gain(D̃, BloodPressure) = 0.35 (calculated on 80 samples)

Adjusted_Gain(D, BloodPressure) = 0.8 × 0.35 = 0.28

The gain is penalized because 20% of samples can't be used. Attributes with fewer missing values are naturally favored.

Solution 2: Probabilistic Sample Distribution

When splitting on an attribute, samples with missing values for that attribute are distributed to all child nodes with fractional weights proportional to the number of known-value samples going to each child.

Algorithm:

Step 1: Split known-value samples normally

Example: Split on "Exercise Level" (Low/Moderate/High). 60 known samples split: 20 to Low, 25 to Moderate, 15 to High.

Step 2: Calculate distribution weights

Weight for Low child: 20/60 = 0.333
Weight for Moderate child: 25/60 = 0.417
Weight for High child: 15/60 = 0.250

Step 3: Distribute missing-value samples

10 samples have missing Exercise Level. Each of these 10 samples is sent to:

  • • Low child with weight 0.333 (contributes 3.33 "fractional" samples)
  • • Moderate child with weight 0.417 (contributes 4.17)
  • • High child with weight 0.250 (contributes 2.50)
Result:

Each missing-value sample contributes fractionally to all children. This maintains information—the sample isn't discarded, but its influence is spread according to the probability distribution learned from known-value samples.

Solution 3: Surrogate Splits (CART)

CART uses surrogate splits—backup attributes that correlate highly with the primary split attribute. During prediction, if the primary attribute is missing, use a surrogate.

Example:

Primary split: "Blood Pressure ≤ 130"

But for some patients, BP is missing. The algorithm finds correlated attributes.

Surrogate split 1: "BMI ≤ 27" (90% agreement with BP split)

If BP is missing but BMI is known, use BMI split instead.

Surrogate split 2: "Age ≤ 45" (75% agreement)

If both BP and BMI are missing but Age is known, use Age.

Benefit: Handles missing values during prediction without needing all features. Particularly useful in production where data collection might be incomplete.

Example: Healthcare Diagnosis with Missing Data

Predicting health risk from patient data with missing values (marked as "?"):

IDAgeBlood PressureGlucoseBMISmokingDiagnosis
145140/9018028.5YesAt Risk
232?9522.1NoHealthy
358150/95?31.2YesAt Risk
441120/808824.3NoHealthy
5?138/8816529.8YesAt Risk
629115/758221.5NoHealthy
752?192?YesAt Risk
836118/789123.7NoHealthy

How C4.5 Handles This Data

During Training:

  • 1.For "Blood Pressure" attribute: 6 known values, 2 missing (IDs 2, 7). ρ = 6/8 = 0.75
  • 2.Calculate gain using 6 known samples, then multiply by 0.75
  • 3.If BP is selected for split: Known samples go to appropriate children, missing samples distributed fractionally

During Prediction:

New patient: Age=50, BP=?, Glucose=155, BMI=28, Smoking=Yes

  • 1.Tree splits on "Glucose ≤ 150" at root → Goes right (Glucose=155)
  • 2.Next split: "BP ≤ 135" but BP is missing!
  • 3.C4.5 sends patient down both branches with weights (e.g., 0.6 left, 0.4 right)
  • 4.Final prediction: Weighted average of leaf predictions → "At Risk" with 85% confidence

Key Advantages of Built-in Missing Value Handling

  • No data loss: All samples are used, even with missing values
  • No imputation artifacts: Doesn't introduce false certainty from mean/mode filling
  • Uncertainty preserved: Missing values contribute fractionally, reflecting uncertainty
  • Works during prediction: Can make predictions even when test samples have missing features

Comparison: Handling Strategies

StrategyContinuousMissing ValuesAlgorithm
ID3Manual discretization requiredNot supported (drop samples)1986, educational use
C4.5Dynamic thresholds, reusableAdjusted gain + probabilistic distribution1993, research standard
CARTDynamic thresholds, binary splitsSurrogate splits1984, industry default

Frequently Asked Questions

Q: Should I bin continuous features before using scikit-learn DecisionTreeClassifier?

A: No! Scikit-learn's implementation (based on CART) automatically handles continuous features using dynamic threshold selection. Binning beforehand loses information and typically hurts performance. Only bin if you have a specific reason (e.g., domain knowledge suggests natural categories, or you need fewer split points for interpretability).

Q: What's the best way to handle missing values if my algorithm doesn't support them natively?

A: If using basic ID3 or a simple implementation, options include: (1) Drop samples with missing values (simple but loses data), (2) Impute mean/median for continuous features and mode for categorical (adds artificial certainty), (3) Create "missing" categoryfor categorical features (treats missingness as information), (4) Multiple imputation (run multiple trees with different imputations and average). For production, use C4.5, CART, or modern libraries with built-in missing value support.

Q: How many split points does the algorithm check for continuous features?

A: In the worst case, for n samples, there are (n-1) possible split points (between each pair of sorted values). However, optimization: if consecutive samples have the same class label, you don't need to check splits between them. Real datasets often have far fewer useful candidates. For very large datasets, some implementations sample a subset of candidate splits for speed.

Q: Can the same continuous attribute be used for splitting at different depths with different thresholds?

A: Yes! This is a key advantage over categorical attributes. For example, the root might split on "age ≤ 30", creating young vs old groups. Later, the "old" subtree might split again on "age ≤ 60" to distinguish middle-aged from elderly. This allows modeling complex non-monotonic relationships that categorical features can't capture in a single split.

Q: What if I have high cardinality categorical features (like zip codes)?

A: High cardinality categoricals can be problematic (multi-value bias in ID3/C4.5, or many binary splits in CART). Solutions: (1) Group into meaningful categories (zip codes → regions), (2) Target encoding (replace with target mean for that category), converting to continuous, (3) Use CART with binary splits (better than multi-way), (4) Feature engineering(extract useful information like "urban vs rural" from zip code). Avoid letting the tree memorize individual rare categories.

Key Takeaways

Continuous features handled via discretization (bins) or dynamic thresholds (better)

C4.5 and CART automatically find optimal split thresholds during tree construction

Continuous attributes can be reused at different nodes with different thresholds

Missing values: C4.5 uses adjusted gain and probabilistic distribution

CART uses surrogate splits as backup attributes for missing primary features

Built-in missing value handling avoids data loss and false certainty from imputation

Modern libraries (scikit-learn) handle both challenges automatically—don't preprocess!

Real-world messy data is the norm; these techniques make trees practical