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

vertopal.com_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)
7 views4 pages

vertopal.com_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
You are on page 1/ 4

import pandas as pd

from sklearn.datasets import load_iris


from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
import matplotlib.pyplot as plt

iris = load_iris()
X = iris.data
y = iris.target

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))

plt.figure(figsize=(16, 12))
plot_tree(
dt_model,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True,
rounded=True,
fontsize=12
)
plt.title("Decision Tree Using Information Gain (Entropy)",
fontsize=16)
plt.show()

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 sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
import matplotlib.pyplot as plt

iris = load_iris()
X = iris.data
y = iris.target

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))

plt.figure(figsize=(16, 12))
plot_tree(
dt_model,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True,
rounded=True,
fontsize=12
)
plt.title("Decision Tree Using Information Gain (Entropy)",
fontsize=16)
plt.show()

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy