Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views1 page

Sethu

Uploaded by

lokeshponraj03
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)
4 views1 page

Sethu

Uploaded by

lokeshponraj03
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/ 1

from sklearn.

tree import DecisionTreeClassifier


from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import metrics

# Load the Iris dataset


iris = load_iris()
X = iris.data # Features
y = iris.target # Target variable

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=1)

# Create a Decision Tree Classifier object


clf = DecisionTreeClassifier()

# Train the Decision Tree Classifier


clf = clf.fit(X_train, y_train)

# Predict the response for test dataset


y_pred = clf.predict(X_test)

# Model Accuracy
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))

Plot:

import matplotlib.pyplot as plt


from sklearn.tree import plot_tree

plt.figure(figsize=(20, 15)) # Adjust figure size for better


readability
plot_tree(clf,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True,
rounded=True,
fontsize=10)
plt.title("Decision Tree Visualization")
plt.show()

You might also like