1. Importing Libraries

PHOTO EMBED

Sun Jun 01 2025 14:50:13 GMT+0000 (Coordinated Universal Time)

Saved by @nani ##python

Week-6
import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.tree import DecisionTreeClassifier, plot_tree import matplotlib.pyplot as plt pandas: Used for handling and manipulating the dataset in DataFrame format. LabelEncoder: A class from sklearn.preprocessing used to convert categorical values (like "Sunny", "Rainy", etc.) into numeric values because machine learning algorithms require numeric input. DecisionTreeClassifier: A class from sklearn.tree that implements the Decision Tree algorithm for classification. plot_tree: A function from sklearn.tree used to visualize the trained decision tree. matplotlib.pyplot: Used for plotting and visualizing the tree.
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt

# Sample "Play Tennis" dataset
data = {
    'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rainy', 'Rainy', 'Rainy',
                'Overcast', 'Sunny', 'Sunny', 'Rainy', 'Sunny', 'Overcast',
                'Overcast', 'Rainy'],
    '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']
}

# Load data into DataFrame
df = pd.DataFrame(data)

# Encode categorical variables using  Label Encoder
label_encoders = {}
for column in df.columns:
    le = LabelEncoder()
    df[column] = le.fit_transform(df[column])
    label_encoders[column] = le

# Separate features and target
X = df.drop('Play', axis=1)
y = df['Play']

# Train Decision Tree model
dt = DecisionTreeClassifier(max_depth=3, random_state=1)
dt.fit(X, y)

# Plot the Decision Tree
plt.figure(figsize=(12, 6))
plot_tree(
    dt,
    feature_names=X.columns,
    class_names=label_encoders['Play'].classes_,  # ['No', 'Yes']
    filled=True,
    rounded=True
)
plt.title("Decision Tree - Play Tennis")
plt.tight_layout()
plt.show()
content_copyCOPY