Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 25

Building and Interpreting CART Decision Trees for Lingerie Selection

Recommended resources

In our previous lesson, we explored the Naive Bayes classifier, an efficient predictive model grounded in your familiar territory of Bayesian theory. We noted its main limitation: the "naive" assumption that all features are independent. This means it can't learn that, for instance, Lace fabric combined with a Bralette style might be a particularly strong indicator of photoshoot potential. Today, we move to a model that excels at discovering precisely these kinds of interactions: the decision tree.

This lesson will introduce you to the CART (Classification and Regression Trees) algorithm. Our objective is to build a CART decision tree for lingerie selection using the Gini index as our guide. By the end, you will be able to not only construct the tree but also interpret its decision rules, translating the model's logic directly into actionable design insights.

From Probabilities to Rules: The Decision Tree Concept

While Naive Bayes calculates probabilities, a decision tree provides a set of 'if-then' rules. Imagine a flowchart designed to classify a lingerie piece. Each question in the flowchart is a node, each answer a branch, and the final conclusion is a leaf.

This diagram illustrates a simple tree structure. Each 'Yes/No' question splits the data, leading down a path that ends in a classification. We can read this as a series of if-then rules.

For example, a path in a lingerie selection tree might look like this:

  1. Is the fabric Lace? -> Yes.
  2. Is the support level Low? -> Yes.
  3. Is the color Red or Black? -> Yes.
  4. Conclusion: Predict 'Photoshoot-Worthy'.

This structure is highly interpretable, making it a powerful tool not just for prediction, but for understanding the drivers of that prediction. The CART algorithm is one of the most common methods for automatically creating these trees from data.


The Heart of CART: Gini Impurity

To build an effective tree, the algorithm must decide on the best question to ask at each step. "Best" means the question that does the best job of separating the data into purer groups—that is, groups that are less mixed in terms of their outcome. CART uses a metric called the Gini Impurity (or Gini Index) to measure this.

Gini Impurity quantifies how "mixed" a group of items is.

  • A Gini Impurity of 0 means a perfectly pure node (all items belong to a single class, e.g., all are 'Photoshoot-Worthy').
  • For a two-class problem, a Gini Impurity of 0.5 is the maximum impurity (the items are a 50/50 mix of the two classes).

The goal of the CART algorithm is to find splits that reduce impurity as much as possible. The following resource provides the mathematical definition.

CART Decision Trees: Complete Guide to Classification and ...

This guide offers a clear mathematical foundation for CART. We will focus on the classification part, specifically the Gini Impurity.

Please read two sections. First, in the "Formula - Classification" section, review the definition of Gini Impurity. Pay attention to the formula Gini(S) = 1 - \sum p_i^2 and how the weighted average Gini for a split is calculated. Next, read the "Split Selection Process" section to understand how Gini is used to calculate the Information Gain, which is the metric the algorithm seeks to maximize.

As you read, you saw that the algorithm works by calculating a weighted Gini score for every potential split. It then chooses the split that results in the lowest weighted Gini score, as this corresponds to the largest "Gini Gain" or reduction in impurity.


Building a Tree, Step by Step

The process of building a tree is known as recursive partitioning. It's a "greedy" algorithm because it makes the best possible split at the current step, without looking ahead to see if a different split might be better in the long run.

The process is as follows:

  1. Start with the entire dataset (the root node).
  2. For each feature, test all possible binary splits. For a categorical feature like Fabric = {Lace, Satin, Cotton}, this means testing splits like Fabric = Lace vs. Not Lace, Fabric = Satin vs. Not Satin, etc.
  3. Calculate the weighted Gini impurity for each of these potential splits.
  4. Select the single feature and split that has the lowest weighted Gini impurity. This becomes the first decision node of your tree.
  5. The data is now split into two new datasets (the child nodes).
  6. Repeat steps 2-5 for each child node, recursively, until a stopping condition is met (e.g., the node is pure, or the number of samples in it is too small).

The following blog post provides an excellent, detailed walkthrough of this process with a simple dataset.

A Step by Step CART Decision Tree Example - Sefik Ilkin Serengil

This article by Sefik Ilkin Serengil demonstrates the entire CART algorithm by hand. Following these calculations will solidify your understanding of how the Gini index drives the tree's construction.

Follow the article from the beginning, where the Gini index is introduced. Trace the author's calculations for the Outlook feature, seeing how the Gini for each value (Sunny, Overcast, Rain) is found and then combined into a weighted Gini for the entire feature. Then, see how this is repeated for Temperature, Humidity, and Wind. Finally, observe how the feature with the lowest weighted Gini (Outlook) is chosen as the root node. Skim through the rest of the article (the subsequent splits) to see how the process repeats on the smaller datasets created by the first split.


Your Turn: Building a Lingerie Decision Tree

Now, let's apply this method to our lingerie selection problem. Given your statistical background, the calculations should be straightforward, allowing you to focus on the logic of the algorithm.

Scenario:
You have a dataset of 8 lingerie styles, characterized by binary features, and you want to find the best first question to ask to determine if a style is 'Photoshoot-Worthy'.

Style ID Has Lace? Support Color Tone Photoshoot-Worthy
1 Yes Low Dark Yes
2 No High Dark Yes
3 No Low Light No
4 Yes High Dark Yes
5 No Low Dark No
6 No High Light No
7 Yes Low Dark Yes
8 No High Dark Yes

Your Task:

  1. Calculate the Gini impurity for the root node (the entire dataset).
  2. For each of the three features (Has Lace?, Support, Color Tone), calculate the weighted Gini impurity of a split.
  3. Identify which feature provides the best split (i.e., the lowest weighted Gini impurity).
  4. Draw the top level of the decision tree and interpret the rule it represents.
Click for the step-by-step solution.

1. Gini Impurity of the Root Node

  • Total samples: 8
  • Photoshoot-Worthy (Yes): 5
  • Photoshoot-Worthy (No): 3
  • ,

2. Weighted Gini for Each Feature

a) Feature: Has Lace?

  • Lace = Yes (3 samples): {Yes: 3, No: 0}. This node is pure.
  • Lace = No (5 samples): {Yes: 2, No: 3}.
  • Weighted Gini:

b) Feature: Support

  • Support = High (4 samples): {Yes: 3, No: 1}.
  • Support = Low (4 samples): {Yes: 1, No: 3}.
  • Weighted Gini:

c) Feature: Color Tone

  • Tone = Dark (6 samples): {Yes: 5, No: 1}.
  • Tone = Light (2 samples): {Yes: 0, No: 2}. This node is pure.
  • Weighted Gini:

3. Identify the Best Split

  • Weighted Gini (Has Lace?): 0.3000
  • Weighted Gini (Support): 0.3750
  • Weighted Gini (Color Tone): 0.2085

The lowest weighted Gini impurity belongs to the Color Tone feature. This is the best first split.

4. Draw and Interpret the Tree
The top level of our decision tree would look like this:

          [Root]
      {Yes: 5, No: 3}
        Gini = 0.469
             |
   Is Color Tone 'Dark'?
    /                  \
'Yes'                  'No' (i.e., 'Light')
  |                      |
[Node 1]               [Node 2]
{Yes: 5, No: 1}        {Yes: 0, No: 2}
Gini = 0.278           Gini = 0.0 (Pure)

Interpretation: The single most informative first question to ask is about the lingerie's color tone. Based on this small dataset, if a piece has a 'Light' color tone, we can immediately classify it as 'Not Photoshoot-Worthy'. If the color tone is 'Dark', it is likely 'Photoshoot-Worthy', but the node is still impure, so further questions (about lace, support, etc.) would be needed to refine the prediction. This demonstrates how the tree creates clear, data-driven rules for selection.


Conclusion

In this lesson, you have learned to build and interpret a CART decision tree. We've seen how it moves beyond the probabilistic approach of Naive Bayes to create explicit, hierarchical rules that can capture complex feature interactions. By mastering the Gini impurity calculation, you can now manually trace the logic of the algorithm and understand exactly why it chooses one feature over another to split the data.

Key Takeaways:

  • Decision trees create a flowchart of if-then rules that are highly interpretable.
  • The CART algorithm builds binary trees by recursively partitioning the data.
  • The Gini Impurity is a measure of node purity, and CART seeks to find splits that maximize the reduction in impurity (Gini Gain).
  • The process is "greedy," making the locally optimal choice at each step to construct the tree.

So far, we've discussed Naive Bayes and CART (using the Gini index). However, Gini is not the only metric for building trees. In our next lesson, we will compare CART to another famous algorithm, C4.5, which uses a different splitting criterion called Information Gain (based on entropy). We will explore how this alternative can sometimes lead to different tree structures and decision rules from the same data.