def test_treePlotter1(self): print('============test1=============') mytree = treePlotter.retrieveTree(0) print(mytree) num = treePlotter.getNumLeafs(mytree) print("num=" + str(num)) depth = treePlotter.getTreeDepth(mytree) print("depth=" + str(depth)) self.assertTrue(num > 0, "num should be more than zero") self.assertTrue(depth > 0, "depth should be more than zero")
def test_treePlotter5(self): print("============使用决策树的分类函数==============") myDat, labels = trees.createDataSet() print(labels) mytree = treePlotter.retrieveTree(0) print(mytree) result = trees.classify(mytree, labels, [1, 0]) print(result) result = trees.classify(mytree, labels, [1, 1]) print(result)
# 存储树,也即序列化树 def storeTree(inputTree, filename): # python模块 pickle可以序列化对象 import pickle fw = open(filename, 'w') # dump 倾倒,卸下 pickle.dump(inputTree, fw) fw.close() # 获取树,也即反序列化, grab:抢夺,抢先 def grabTree(filename): import pickle fr = open(filename) return pickle.load(fr) myTree = treePlt.retrieveTree(0) # storeTree(myTree, 'classifierStorage1.txt') # print grabTree('classifierStorage1.txt') # 隐形眼镜决策树 fr = open('lenses.txt') # 对于每行数据,根据 \t 分隔字符 lenses = [inst.strip().split('\t') for inst in fr.readlines()] lensesLabels = ['age', 'prescript', 'astigmatic', 'tearRate'] lensesTree = createTree(lenses, lensesLabels) print lensesTree treePlt.createPlot(lensesTree)
def test_treePlotter3(self): print('============test3=============') mytree = treePlotter.retrieveTree(0) mytree['no surfacing'][3] = 'maybe' treePlotter.createPlot(mytree)
def test_treePlotter2(self): print('============test2=============') mytree = treePlotter.retrieveTree(0) treePlotter.createPlot(mytree)