-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml9.py
More file actions
27 lines (27 loc) · 875 Bytes
/
ml9.py
File metadata and controls
27 lines (27 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import accuracy_score
#importing iris dataset from sklearn and spliting input and output
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
#Train-test split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=42)
Performing Decision Tree Classifier
dtc = DecisionTreeClassifier()
dtc.fit(X_train, y_train)
y_pred=dtc.predict(X_test)
#Checking accuracy
acc=accuracy_score(y_test,y_pred)
print("Accuracy of model=", acc)
Output:
Accuracy: 1.0
#Visualizing decision tree
plt.figure(figsize=(12, 8))
plot_tree(dtc, feature_names=iris.feature_names,
class_names=iris.target_names, filled=True)
plt.show()