-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2) Decision Tree.txt
44 lines (43 loc) · 1.34 KB
/
2) Decision Tree.txt
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
Decision Tree
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("ads.csv")
data.head()
data.isnull().any()
x = data.iloc[:,1:4].values
y = data.iloc[:,4:5].values
x
y[:10]
from sklearn.preprocessing import LabelEncoder
lb = LabelEncoder()
x[:,0] = lb.fit_transform(x[:,0])
x
from sklearn.model_selection import train_test_split as tts
x_train,x_test,y_train,y_test = tts(x, y, test_size = 0.1,random_state=0)
x_train
x_test[:10]
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier(criterion='entropy')
dt.fit(x_train,y_train)
y_pred = dt.predict(x_test)
y_pred
from sklearn.metrics import accuracy_score
accuracy_score(y_test,y_pred)
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test,y_pred)
import sklearn.metrics as metrics
fpr, tpr ,threshold = metrics.roc_curve(y_test,y_pred)
roc_auc = metrics.auc(fpr,tpr)
roc_auc
plt.plot(fpr,tpr)
plt.xlim([0,1])
plt.ylim([0,1])
plt.style.use("dark_background")
from sklearn.tree import export_graphviz
export_graphviz(dt, out_file ='tree.dot',
feature_names = ["Gender","Age","Salary"], class_names = ['0','1'],
rounded = True, proportion = False, precision = 2, filled = True)
!pip install pydotplus
!conda install graphviz
!dot tree.dot -Tpng -o image.png