0% found this document useful (0 votes)
20 views4 pages

Decision Tree

The document outlines a Python script that uses a Decision Tree Classifier to predict iris flower species based on their features. The model achieves an accuracy of 100% on the test set, and a classification report confirms perfect precision, recall, and F1 scores for all species. Additionally, the script allows user input for flower features to make predictions on the species.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Decision Tree

The document outlines a Python script that uses a Decision Tree Classifier to predict iris flower species based on their features. The model achieves an accuracy of 100% on the test set, and a classification report confirms perfect precision, recall, and F1 scores for all species. Additionally, the script allows user input for flower features to make predictions on the species.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import pandas as pd

from [Link] import load_iris


from [Link] import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
from [Link] import classification_report, accuracy_score
import [Link] as plt

iris = load_iris()
X = [Link]
y = [Link]

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

dt_model = DecisionTreeClassifier(criterion="entropy",
random_state=42)
dt_model.fit(X_train, y_train)

y_pred = dt_model.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)


print(f"Accuracy: {accuracy:.2f}")

print("\nClassification Report:")
print(classification_report(y_test, y_pred,
target_names=iris.target_names))

[Link](figsize=(16, 12))
plot_tree(
dt_model,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True,
rounded=True,
fontsize=12
)
[Link]("Decision Tree Using Information Gain (Entropy)",
fontsize=16)
[Link]()

Accuracy: 1.00

Classification Report:
precision recall f1-score support

setosa 1.00 1.00 1.00 10


versicolor 1.00 1.00 1.00 9
virginica 1.00 1.00 1.00 11

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

import pandas as pd
from [Link] import load_iris
from [Link] import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
from [Link] import classification_report, accuracy_score
import [Link] as plt

iris = load_iris()
X = [Link]
y = [Link]

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

dt_model = DecisionTreeClassifier(criterion="entropy",
random_state=42)
dt_model.fit(X_train, y_train)

y_pred = dt_model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print("\nClassification Report:")
print(classification_report(y_test, y_pred,
target_names=iris.target_names))

[Link](figsize=(16, 12))
plot_tree(
dt_model,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True,
rounded=True,
fontsize=12
)
[Link]("Decision Tree Using Information Gain (Entropy)",
fontsize=16)
[Link]()

print("\nEnter the following flower features to predict the species:")

try:
sepal_length = float(input("Sepal length (cm): "))
sepal_width = float(input("Sepal width (cm): "))
petal_length = float(input("Petal length (cm): "))
petal_width = float(input("Petal width (cm): "))

user_input = [[sepal_length, sepal_width, petal_length,


petal_width]]
prediction = dt_model.predict(user_input)
predicted_species = iris.target_names[prediction[0]]

print(f"\nThe predicted species is: {predicted_species}")


except ValueError:
print("\nInvalid input! Please enter numeric values for all
features.")

Accuracy: 1.00

Classification Report:
precision recall f1-score support

setosa 1.00 1.00 1.00 10


versicolor 1.00 1.00 1.00 9
virginica 1.00 1.00 1.00 11

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

Enter the following flower features to predict the species:

Sepal length (cm): 67


Sepal width (cm): 34
Petal length (cm): 23
Petal width (cm): 12

The predicted species is: virginica

You might also like