-
Notifications
You must be signed in to change notification settings - Fork 4
/
decision_tree_entropy.py
57 lines (41 loc) · 1.21 KB
/
decision_tree_entropy.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""Decision_tree_Entropy.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1HKuoV1n_leDTGFMIsBhJjIGQTlqgXyH8
"""
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
import numpy as np
df=pd.read_csv("/content/heart.csv")
df
X=df.drop('output',axis=1)
Y=df['output']
X.head
Y.head()
X_train,X_test,y_train,y_test = train_test_split(X,Y,test_size=0.2)
X_train
y_train
X_train = StandardScaler().fit_transform(X_train)
X_train
X_test = StandardScaler().fit_transform(X_test)
X_test
from sklearn.tree import DecisionTreeClassifier
dtree = DecisionTreeClassifier(criterion='entropy', random_state=0)
dtree = dtree.fit(X_train,y_train)
dtree
y_test
y_pred=dtree.predict(X_test)
y_pred
from sklearn import metrics
import matplotlib.pyplot as plt
cm=metrics.confusion_matrix(y_pred,y_test)
cm
plt.plot(cm)
from sklearn.tree import export_graphviz
import graphviz
graphviz.Source(export_graphviz(dtree,feature_names=X.columns,filled=True,class_names=['0','2','3','4','5','6']))
from sklearn import tree
tree.plot_tree(dtree)