-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.py
More file actions
40 lines (31 loc) · 1.52 KB
/
Copy pathDecisionTree.py
File metadata and controls
40 lines (31 loc) · 1.52 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
import pandas as pd
import numpy as np
from sklearn import tree
import pydotplus
# Generate a decision Tree
def createTree(trainingdata):
data = trainingdata.iloc[:, :-1] #Feature matrix
labels = trainingdata.iloc[:, -1] # Labels
trainedTree = tree.DecisionTreeClassifier(criterion="entropy") #Decision tree classifier
trainedTree.fit(data,labels) # Train the model
return trainedTree
# Next is to define the function for saving the generated tree diagram
def showtree2pdf(trainedTree,filename):
dot_data = tree.export_graphviz(trainedTree, out_file=None) # Export the the tree in Graphviz format
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf(filename) #Save the tree diagram to the local machine in PDF format
# define the function for generating Vectorized data
def data2vector(data):
names = data.columns[:-1]
for i in names:
col = pd.Categorical(data[i])
data[i] = col.codes
return data
# Now we invoke the function for prediction
data = pd.read_table("./ML/tennis.txt",header=None,sep='\t') # This reads the training data
trainingvec=data2vector(data) # To vectorize data
decisionTree = createTree(trainingvec) # this creates a Decision Tree
showtree2pdf(decisionTree,"tennis.pdf") #to plot the decision tree
# the following predicts a new sample
testVec = [0,0,1,1] #This indicates that the weather is sunny,temperature is low,humidity is high and the wind is strong
print(decisionTree.predict(np.array(testVec).reshape(1,-1)))