In [1]:
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt
In [2]:
#Load dataset
iris = load_iris()
X, y = iris.data, iris.target
In [3]:
# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42)
In [4]:
# Build Decision Tree
clf = DecisionTreeClassifier(criterion="gini", max_depth=3, random_state=42)
clf.fit(X_train, y_train)
Out[4]:
DecisionTreeClassifier(max_depth=3, random_state=42)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
criterion  'gini'
splitter  'best'
max_depth  3
min_samples_split  2
min_samples_leaf  1
min_weight_fraction_leaf  0.0
max_features  None
random_state  42
max_leaf_nodes  None
min_impurity_decrease  0.0
class_weight  None
ccp_alpha  0.0
monotonic_cst  None
In [5]:
# Evaluate
print("Accuracy", clf.score(X_test, y_test))
Accuracy 1.0
In [6]:
# Test with 3 example samples. Start continueing lab 4 record from here
samples = [
    [5.1, 3.5, 1.4, 0.2], # Setosa
    [6.0, 2.9, 4.5, 1.5], # Versicolor
    [6.9, 3.1, 5.4, 2.1], # Virginica
]

predictions = clf.predict(samples)

for i, pred in enumerate(predictions):
    print(f"Sample {i+1}: Predicted class -> {iris.target_names[pred]}")
Sample 1: Predicted class -> setosa
Sample 2: Predicted class -> versicolor
Sample 3: Predicted class -> virginica
In [7]:
# Visualize the decision tree
plt.figure(figsize=(12, 8))
plot_tree(clf, feature_names=iris.feature_names, class_names=iris.target_names, filled = True)
plt.show()
No description has been provided for this image
Class Sepal Length Sepal Width Petal Length Petal Width
Setosa 4.3-5.8 2.3-4.4 1.0-1.9 0.1-0.6
Versicolor 4.9-7.0 2.0-3.4 3.0-5.1 1.0-1.8
Virginica 4.0-7.9 2.2-3.8 4.5-6.9 1.4-2.5