Train a Decision Tree Classifier using the Play Tennis dataset(apply label encoding to convert categorical features into numeric form), display the learned tree rules, visualize the tree, and predict whether to play tennis for a given new weather condition.¶
In [1]:
# Import libraries
import pandas as pd
from sklearn.tree import DecisionTreeClassifier, export_text, plot_tree
import matplotlib.pyplot as plt
In [2]:
# 1. Create Play Tennis dataset
data = {
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast', 'Sunny', 'Sunny', 'Rain', 'Sunny', 'Overcast', 'Overcast', 'Rain'],
'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cool', 'Mild', 'Mild', 'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Strong'],
'Play': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No']
}
df = pd.DataFrame(data)
In [3]:
# 2. Convert categorical data into numeric (Label Encoding)
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for col in df.columns:
df[col] = le.fit_transform(df[col])
In [4]:
# Features (X) and target (y)
X = df[['Outlook', 'Temperature', 'Humidity', 'Wind']]
y = df['Play']
In [5]:
# 3. Train Decision Tree
clf = DecisionTreeClassifier(criterion="entropy") #ID3 uses entropy
clf = clf.fit(X, y)
In [6]:
# 4. Print the tree rules
tree_rules = export_text(clf, feature_names=['Outlook', 'Temperature', 'Humidity', 'Wind'])
print(tree_rules)
|--- Outlook <= 0.50 | |--- class: 1 |--- Outlook > 0.50 | |--- Humidity <= 0.50 | | |--- Outlook <= 1.50 | | | |--- Wind <= 0.50 | | | | |--- class: 0 | | | |--- Wind > 0.50 | | | | |--- class: 1 | | |--- Outlook > 1.50 | | | |--- class: 0 | |--- Humidity > 0.50 | | |--- Wind <= 0.50 | | | |--- Temperature <= 1.00 | | | | |--- class: 0 | | | |--- Temperature > 1.00 | | | | |--- class: 1 | | |--- Wind > 0.50 | | | |--- class: 1
In [7]:
# 5. Plot the tree
plt.figure(figsize=(12, 8))
plot_tree(clf, feature_names=['Outlook', 'Temperature', 'Humidity', 'Wind'], class_names = ['No', 'Yes'], filled = True, rounded=True)
plt.show()
In [8]:
# Example test case
# test_sample = [[2, 1, 0, 1]] # Encoded values for Outlook, Temperature, Humidity, Wind
test_sample = [[0, 2, 0, 1]] # Sunny, Cool, High, Strong
prediction = clf.predict(test_sample)
print("Play Tennis? ->", "Yes" if prediction[0] == 1 else "No")
Play Tennis? -> Yes
/opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but DecisionTreeClassifier was fitted with feature names warnings.warn(
In [ ]: