forked from frankstar007/kNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.py
More file actions
21 lines (19 loc) · 721 Bytes
/
classify.py
File metadata and controls
21 lines (19 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import dtree
import treeplotter
def classify(inputTree,featLabels,testVec):
firstStr = inputTree.keys()[0]
secondDict = inputTree[firstStr]
featIndex = featLabels.index(firstStr)
for key in secondDict.keys():
if testVec[featIndex] == key:
if type(secondDict[key]).__name__=='dict':
classLabel = classify(secondDict[key],featLabels,testVec)
else:
classLabel = secondDict[key]
return classLabel
myDat,labels = dtree.createDataSet()
print(labels)
myTree = treeplotter.retrieveTree(0)
print(myTree)
print('classify(myTree,labels,[1,0]):',classify(myTree,labels,[1,0]))
print('classify(myTree,labels,[1,1]):',classify(myTree,labels,[1,1]))