Saturday, September 14, 2024

Decision Trees vs. Logistic Regression: How They Separate Data

Decision Trees vs Logistic Regression: Complete Guide to Dataset Separation, Mathematics, Decision Boundaries and Rule Extraction

Decision Trees vs Logistic Regression: Understanding Dataset Separation, Mathematics, Decision Boundaries and Rule Extraction

Machine learning is fundamentally about finding patterns in data and using those patterns to make predictions. Among the many supervised learning algorithms available today, two of the most commonly used and widely taught algorithms are Decision Trees and Logistic Regression.

At first glance, both algorithms appear to solve similar problems. Both can classify emails as spam or not spam. Both can determine whether a customer will churn. Both can predict whether a loan should be approved or rejected.

However, internally they operate in completely different ways.

One relies on a sequence of logical decisions. The other relies on mathematical probability estimation.

Understanding these differences is critical because selecting the wrong model can reduce accuracy, decrease interpretability, and make business decisions difficult to explain.


Table of Contents


Introduction to Classification

Classification is one of the most important tasks in machine learning.

The objective is simple:

Assign observations into predefined categories.

For example:

  • Email → Spam or Not Spam
  • Customer → Churn or Retain
  • Loan Application → Approved or Rejected
  • Medical Scan → Healthy or Diseased
  • Transaction → Fraudulent or Legitimate

A classification algorithm learns patterns from historical data and uses those patterns to predict outcomes for new data.

The way these patterns are learned determines how well the algorithm performs and how interpretable the final predictions become.

Key Idea:

Decision Trees learn through sequential decisions. Logistic Regression learns through probability estimation.


What is a Decision Tree?

A Decision Tree is a supervised learning algorithm that mimics human decision-making.

Imagine purchasing a smartphone.

You may ask:

  • Budget greater than ₹30,000?
  • Need good camera?
  • Need gaming performance?
  • Need 5G support?

Based on answers, you reach a final recommendation.

That is exactly how a Decision Tree operates.

Components of a Decision Tree

Component Description
Root Node Starting point of the tree
Internal Node Decision condition
Branch Outcome of decision
Leaf Node Final prediction

Simple Tree Example


Income > 50000 ?

      /      \

   No          Yes

Reject     Credit Score > 700 ?

             /         \

           No         Yes

        Reject      Approve

The path from root to leaf forms a decision rule.


What is Logistic Regression?

Despite the word "Regression", Logistic Regression is mainly used for classification.

Rather than asking a sequence of questions, Logistic Regression calculates the probability that a record belongs to a specific class.

For example:

Customer Probability of Churn Prediction
A 0.92 Churn
B 0.78 Churn
C 0.14 Retain

Typically, a threshold of 0.5 is used.

If probability exceeds 0.5, assign one class. Otherwise assign the other.


Understanding Dataset Separation

Dataset separation refers to how a machine learning algorithm divides different classes.

Logistic Regression Separation

Logistic Regression creates a linear decision boundary.


Class A

***********

----------- Decision Boundary -----------

###########

Class B

The model searches for the best line separating categories.

In higher dimensions, this line becomes a hyperplane.

Decision Tree Separation

Decision Trees separate datasets using recursive splits.


+----+----+----+

| A  | A  | B  |

+----+----+----+

| A  | B  | B  |

+----+----+----+

| A  | B  | B  |

+----+----+----+

Instead of drawing one line, the tree repeatedly partitions space.

This allows extremely complex non-linear boundaries.

Why Trees Handle Complex Data Better

Decision Trees do not assume linear relationships. Each split introduces another level of flexibility. By combining multiple splits, trees approximate highly irregular boundaries.


Mathematics Behind Logistic Regression

The core equation is:

p = 1 / (1 + e^-(β₀ + β₁x₁ + β₂x₂ + ... + βₙxₙ))

Where:

  • p = probability
  • β = coefficients
  • x = features
  • e = Euler's number

Why Not Use Linear Regression?

Linear regression can generate values outside the range 0 to 1.

Probabilities must remain between 0 and 1.

Therefore, Logistic Regression uses a sigmoid function.

Sigmoid Function

σ(z)=1/(1+e^-z)
Input Output
-10 0.000045
0 0.5
10 0.99995

The sigmoid compresses any real number into the interval [0,1].

Odds

Odds = p/(1-p)

Log-Odds

log(p/(1-p))

Logistic Regression actually models log-odds rather than probability directly.


Entropy and Information Gain

Decision Trees must determine where to split.

To do this they measure uncertainty.

Entropy is one of the most popular methods.

Entropy Formula

Entropy = -Σ pᵢ log₂(pᵢ)

Interpretation

Entropy Meaning
0 Perfectly Pure
1 Maximum Uncertainty

Example

Suppose a node contains:

  • 50 Positive
  • 50 Negative
Entropy = -0.5log₂(0.5)-0.5log₂(0.5) Entropy = 1

Maximum disorder.

Now suppose:

  • 100 Positive
  • 0 Negative
Entropy = 0

Perfect purity.

Information Gain

Information Gain measures reduction in entropy.

IG = Parent Entropy - Weighted Child Entropy

The feature providing highest information gain is selected for splitting.


Gini Impurity

Another common splitting criterion is Gini Impurity.

Gini = 1 - Σ(pᵢ²)

Example:

Class A = 50%

Class B = 50%

1-(0.5²+0.5²) =0.5

Higher values indicate more impurity.

Lower values indicate purer nodes.

Entropy vs Gini

Feature Entropy Gini
Speed Slower Faster
Interpretability Information Theory Simpler
Usage ID3/C4.5 CART

Why Trees Overfit

Decision Trees can continue splitting until every training example is perfectly classified.

This often memorizes noise.

The result is overfitting.

Solutions

  • Maximum Depth
  • Minimum Samples Split
  • Minimum Samples Leaf
  • Pruning

Pruning Explained

Pruning removes unnecessary branches.

The goal is to simplify the model while maintaining performance.

Benefits

  • Improved Generalization
  • Reduced Complexity
  • Better Interpretability
  • Faster Prediction

Decision Tree vs Logistic Regression

Category Decision Tree Logistic Regression
Interpretability Excellent Good
Complex Boundaries Excellent Limited
Training Speed Fast Very Fast
Feature Scaling No Yes
Categorical Data Easy Requires Encoding
Overfitting Risk High Low
Probability Estimation Moderate Excellent
Regulatory Explainability Excellent Excellent

Extracting Decision Rules

One of the strongest advantages of Decision Trees is transparency.

Every path from root to leaf becomes a rule.

Example Rule


IF Income > 50000

AND CreditScore > 700

AND ExistingLoans < 2

THEN Approve Loan

Another Rule


IF Income <= 50000

AND CreditScore < 600

THEN Reject Loan

These rules are understandable even to non-technical stakeholders.

Benefits

  • Regulatory Compliance
  • Business Rule Generation
  • Explainable AI
  • Auditing
  • Debugging
  • Feature Analysis
How Rule Extraction Works Internally

The algorithm traverses every path from root node to leaf node. Conditions encountered during traversal are concatenated using logical AND operations. Once a leaf node is reached, the final class label is attached to create a complete human-readable rule.


Python Implementation

Train a Decision Tree


from sklearn.tree import DecisionTreeClassifier

tree = DecisionTreeClassifier(
    max_depth=4,
    random_state=42
)

tree.fit(X_train,y_train)

Extract Rules


from sklearn.tree import export_text

rules = export_text(
tree,
feature_names=list(features)
)

print(rules)

Train Logistic Regression


from sklearn.linear_model import LogisticRegression

model = LogisticRegression()

model.fit(X_train,y_train)

pred = model.predict(X_test)

Probability Prediction


prob = model.predict_proba(X_test)

print(prob)

CLI Output Examples

Decision Tree Training


$ python train_tree.py

Loading dataset...

Dataset Size: 10,000 rows

Training...

Tree Depth: 4

Accuracy: 92.8%

Model Saved Successfully

Rule Extraction CLI


$ python extract_rules.py

Rule 1

IF Income > 50000

AND CreditScore > 700

THEN Approve


Rule 2

IF Income <= 50000

AND CreditScore < 600

THEN Reject

Logistic Regression Training


$ python train_lr.py

Loading Data...

Training Logistic Regression...

Accuracy: 90.7%

AUC Score: 0.94

Model Saved


Real World Applications

Healthcare

  • Disease Prediction
  • Risk Assessment
  • Treatment Recommendation

Finance

  • Credit Scoring
  • Fraud Detection
  • Loan Approval

Marketing

  • Customer Segmentation
  • Churn Prediction
  • Lead Scoring

E-Commerce

  • Purchase Prediction
  • Product Recommendation
  • User Classification

Common Mistakes Beginners Make

  • Using Logistic Regression on highly non-linear datasets.
  • Ignoring feature scaling for Logistic Regression.
  • Allowing trees to grow too deep.
  • Ignoring class imbalance.
  • Evaluating only accuracy.
  • Not validating using cross-validation.
  • Trusting predictions without explanation.

Frequently Asked Questions

Is Logistic Regression a Regression Algorithm?

Technically yes in naming, but primarily used for classification.

Can Decision Trees Handle Non-linear Data?

Yes. This is one of their greatest strengths.

Which is More Interpretable?

Decision Trees because rules can be visualized directly.

Which is Faster?

Logistic Regression generally trains faster.

Which Handles Outliers Better?

Decision Trees are generally less sensitive to outliers.

Can Logistic Regression Produce Probabilities?

Yes. Probability estimation is one of its strongest advantages.


Interview Questions

  1. What is a Decision Tree?
  2. What is Logistic Regression?
  3. What is a Decision Boundary?
  4. What is Entropy?
  5. What is Information Gain?
  6. What is Gini Impurity?
  7. Difference between Entropy and Gini?
  8. What causes overfitting in trees?
  9. What is pruning?
  10. What is the sigmoid function?
  11. Explain odds and log-odds.
  12. Why not use Linear Regression for classification?
  13. What is maximum likelihood estimation?
  14. How do you extract rules from a Decision Tree?
  15. How do you interpret Logistic Regression coefficients?
  16. How does CART work?
  17. What are leaf nodes?
  18. How are split points selected?
  19. What metrics evaluate classifiers?
  20. When would you choose Logistic Regression over Decision Trees?

Key Takeaways

  • Decision Trees separate data through recursive splitting.
  • Logistic Regression separates data through probability estimation.
  • Logistic Regression generally creates linear decision boundaries.
  • Decision Trees can create highly non-linear boundaries.
  • Entropy measures uncertainty.
  • Information Gain determines the best split.
  • Gini Impurity is a faster alternative to entropy.
  • Every root-to-leaf path becomes a decision rule.
  • Rule extraction enables explainable AI.
  • Decision Trees excel at transparency.
  • Logistic Regression excels at probability estimation.
  • Choosing the correct algorithm depends on the underlying structure of the data.

Conclusion

Decision Trees and Logistic Regression remain two of the most important algorithms in machine learning. Although they solve similar classification problems, they approach learning in fundamentally different ways.

Logistic Regression relies on mathematical probability modeling and produces smooth decision boundaries. It works exceptionally well when relationships are approximately linear and when probability estimation is important.

Decision Trees rely on sequential logical decisions and recursive partitioning of the feature space. Their ability to handle non-linear patterns and generate understandable rules makes them one of the most interpretable machine learning algorithms available.

Perhaps the most powerful aspect of Decision Trees is that every prediction can be traced back to a clear chain of reasoning. This capability is invaluable in industries where explainability, transparency, compliance, and trust are mandatory.

Whether you choose Decision Trees or Logistic Regression should depend on the nature of your data, the complexity of the underlying relationships, and the level of interpretability required by stakeholders.

Mastering both algorithms provides a strong foundation for understanding supervised learning, model explainability, and modern machine learning workflows.

No comments:

Post a Comment

Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts