To create a decision tree using the provided dataset and entropy as the impurity measure, we can follow these steps:
-
Define the Dataset: The dataset consists of five features (Income, Marital Status, Living Area, Car Type) that need to predict the target variable (Subscribe).
-
Calculate Entropy: For each feature, split the dataset based on its values and calculate the entropy. Entropy is calculated as:
[
\text{Entropy}(S) = -\sum_{i=1}^c p_i \log_2(p_i)
]
where ( p_i ) is the proportion of examples in class ( i ).
-
Select Split Using Information Gain: Use entropy to calculate information gain, which is the reduction in entropy. The formula for information gain is:
[
\text{Gain}(S, A) = \text{Entropy}(S) - \sum_{v \in \text{Values}(A)} \frac{|S_v|}{|S|} \text{Entropy}(S_v)
]
where ( \text{Values}(A) ) are the possible values of attribute A, ( S_v ) is the subset of S for which attribute A has value v, and ( |S| ) is the number of items in the set S.
-
Build the Tree: Choose the feature with the highest information gain for the root node. Repeat the process for each branch, excluding the feature already used, until leaf nodes are reached (nodes with entropy of 0).
-
Extract Classification Rules: After constructing the decision tree, extract rules from root to leaf, representing paths that lead to a decision.
Let's begin constructing the decision tree:
Step 1: Calculate Entropy of the Target Variable Subscribe
Out of 14 instances, 7 are Yes and 7 are No.
[
\text{Entropy}(S) = -\left( \frac{7}{14} \log_2 \frac{7}{14} + \frac{7}{14} \log_2 \frac{7}{14} \right) = 1
]
Step 2: Calculate Entropy and Information Gain for Each Feature
Feature: Income
- Values: Low, Medium, High
- Calculate entropy for each subset and then information gain.
[
\begin{align*}
S_{\text{Low}} &: 3 \text{ (Yes)}, 2 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{3}{5} \log_2 \frac{3}{5} + \frac{2}{5} \log_2 \frac{2}{5} \right) \approx 0.971,\
S_{\text{Medium}} &: 3 \text{ (Yes)}, 4 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{3}{7} \log_2 \frac{3}{7} + \frac{4}{7} \log_2 \frac{4}{7} \right) \approx 0.985,\
S_{\text{High}} &: 1 \text{ (Yes)}, 3 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{1}{4} \log_2 \frac{1}{4} + \frac{3}{4} \log_2 \frac{3}{4} \right) \approx 0.811.
\end{align*}
]
[
\text{Gain}(\text{Income}) = 1 - \left( \frac{5}{14} \times 0.971 + \frac{7}{14} \times 0.985 + \frac{4}{14} \times 0.811 \right) \approx 0.029
]
Feature: Marital Status
[
\begin{align*}
S_{\text{Single}} &: 5 \text{ (Yes)}, 4 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{5}{9} \log_2 \frac{5}{9} + \frac{4}{9} \log_2 \frac{4}{9} \right) \approx 0.991,\
S_{\text{Married}} &: 2 \text{ (Yes)}, 3 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{2}{5} \log_2 \frac{2}{5} + \frac{3}{5} \log_2 \frac{3}{5} \right) \approx 0.971.
\end{align*}
]
[
\text{Gain}(\text{Marital Status}) = 1 - \left( \frac{9}{14} \times 0.991 + \frac{5}{14} \times 0.971 \right) \approx 0.005
]
Feature: Living Area
[
\begin{align*}
S_{\text{NJ}} &: 2 \text{ (Yes)}, 5 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{2}{7} \log_2 \frac{2}{7} + \frac{5}{7} \log_2 \frac{5}{7} \right) \approx 0.863,\
S_{\text{NY}} &: 3 \text{ (Yes)}, 2 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{3}{5} \log_2 \frac{3}{5} + \frac{2}{5} \log_2 \frac{2}{5} \right) \approx 0.971,\
S_{\text{CT}} &: 1 \text{ (Yes)}, 2 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{1}{3} \log_2 \frac{1}{3} + \frac{2}{3} \log_2 \frac{2}{3} \right) \approx 0.918.
\end{align*}
]
[
\text{Gain}(\text{Living Area}) = 1 - \left( \frac{7}{14} \times 0.863 + \frac{5}{14} \times 0.971 + \frac{2}{14} \times 0.918 \right) \approx 0.063
]
Feature: Car Type
- Values: Sports, Sedan, Van
[
\begin{align*}
S_{\text{Sports}} &: 3 \text{ (Yes)}, 1 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{3}{4} \log_2 \frac{3}{4} + \frac{1}{4} \log_2 \frac{1}{4} \right) \approx 0.811,\
S_{\text{Sedan}} &: 2 \text{ (Yes)}, 5 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{2}{7} \log_2 \frac{2}{7} + \frac{5}{7} \log_2 \frac{5}{7} \right) \approx 0.863,\
S_{\text{Van}} &: 2 \text{ (Yes)}, 2 \text{ (No)} \rightarrow \text{Entropy} = -\left( \frac{2}{4} \log_2 \frac{2}{4} + \frac{2}{4} \log_2 \frac{2}{4} \right) = 1.
\end{align*}
]
[
\text{Gain}(\text{Car Type}) = 1 - \left( \frac{4}{14} \times 0.811 + \frac{7}{14} \times 0.863 + \frac{3}{14} \times 1 \right) \approx 0.096
]
Step 3: Decision Tree Construction
The feature with the highest information gain is Car Type. Thus, it will be the root of the tree, and we'll split based on Car Type.
- Car Type = Sports: 3/4 Yes, 1/4 No ⇒ Majority Yes.
- Car Type = Sedan: 2/7 Yes, 5/7 No ⇒ Majority No.
- Car Type = Van: 2/4 Yes, 2/4 No ⇒ Mixed, evaluate deeper splits.
Further Splits for Car Type = Van
Continuing the assessment for Van, divide further based on the feature with the second-highest gain in this subset, such as Income or Living Area.
Construct the Tree:
- Root: Car Type
- Sports: Yes
- Sedan: No
- Van:
- Split further by selected feature (for simplicity here, say
Income), or can be multidimensional.
- Suppose choose
Income, any split that resolves:
Step 4: Extract Rules from the Decision Tree
- If Car Type = Sports, then Subscribe = Yes.
- If Car Type = Sedan, then Subscribe = No.
- If Car Type = Van and Income = Low, then Subscribe = Yes.
- If Car Type = Van and Income = Medium, then Subscribe = No.
Note: The decision tree can be simplified or expanded further by considering deeper features or combinations (e.g., Living Area, Marital Status), as decided by the further gain calculation and insightful subset selections.