Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
1.4 kB
2
Indexable
Never
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import matplotlib.pyplot as plt
import numpy as np

# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target
feature_names = iris.feature_names
class_names = iris.target_names

# Convert to DataFrame for easier manipulation
df = pd.DataFrame(X, columns=feature_names)
df['target'] = y

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize the Decision Tree classifier
dt_classifier = DecisionTreeClassifier(criterion='entropy', random_state=42)

# Train the classifier
dt_classifier.fit(X_train, y_train)

# Predict on the test set
y_pred = dt_classifier.predict(X_test)

# Evaluate the model
accuracy = np.mean(y_pred == y_test)
print(f"Accuracy: {accuracy:.2f}")

# Visualize the decision tree
plt.figure(figsize=(20, 10))
tree.plot_tree(dt_classifier, feature_names=feature_names, class_names=class_names, filled=True)
plt.show()

# Classify a new sample
new_sample = [[5.1, 3.5, 1.4, 0.2]]  # Example sample similar to class 'setosa'
new_prediction = dt_classifier.predict(new_sample)
predicted_class = class_names[new_prediction[0]]
print(f"New sample: {new_sample}")
print(f"Predicted class: {predicted_class}")
Leave a Comment