vertopal.com_decision_tree
vertopal.com_decision_tree
iris = load_iris()
X = iris.data
y = iris.target
dt_model = DecisionTreeClassifier(criterion="entropy",
random_state=42)
dt_model.fit(X_train, y_train)
y_pred = dt_model.predict(X_test)
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
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
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()
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): "))
Accuracy: 1.00
Classification Report:
precision recall f1-score support
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30