-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.py
More file actions
63 lines (49 loc) · 1.99 KB
/
DecisionTree.py
File metadata and controls
63 lines (49 loc) · 1.99 KB
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
58
59
60
61
62
63
import graphviz as graphviz
import numpy as np
from matplotlib import pyplot as plt
from sklearn import tree
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from Utils import RawData, Plotter
# Taking clean data from RawData() class
df = RawData().df
# Separating features and label
X = df[RawData().feature_cols] # Features
y = df.music_genre # Target variable
# Separating train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)
# Initializing DecisionTreeClassifier models with hyperparameters
clf = tree.DecisionTreeClassifier(criterion="gini", splitter="best", max_depth=12) # hyperparameters
# Applying standardization
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Training model with train sets
clf = clf.fit(X_train, y_train)
# Testing model with test sets
y_pred = clf.predict(X_test)
# Printing accuracy score
print("Accuracy:", accuracy_score(y_test, y_pred))
# Plot Common Graphs
Plotter().plot_cofusion_matrix(y_test, y_pred, "Decision Tree")
Plotter().plot_traning_curves(X, y, clf, "Decision Tree")
# Drawing a leaf graph it may not fit to pdf if you want to see all tree use smaller max_depth values
dot_data = tree.export_graphviz(clf, out_file=None)
graph = graphviz.Source(dot_data, filename="dt.gv", format="pdf")
graph.render("DT")
graph.save()
# This algorithm try different max_depht values and plot a diagram
error = list()
for i in range(1, 40):
clf = tree.DecisionTreeClassifier(criterion="gini", splitter="best", max_depth=i)
clf.fit(X_train, y_train)
pred_i = clf.predict(X_test)
error.append(np.mean(pred_i != y_test))
plt.figure(figsize=(12, 6))
plt.plot(range(1, 40), error, color='red', linestyle='dashed', marker='o',
markerfacecolor='blue', markersize=10)
plt.title('Error Rate max depth Value')
plt.xlabel('Max Depth')
plt.ylabel('Mean Error')
plt.show()