defcreatTree(dataset,lables): classList = [example[-1] for example in dataset] if classList.count(classList[0])==len(classList): return classList[0] if len(dataset[0])==1: return majorityCnt(classList) bestFeature = chooseBestFeatureToSplit(dataset) bestFeatureLable = lables[bestFeature] myTree = {bestFeatureLable: {}} del(lables[bestFeature]) featValue = [example[bestFeature] for example in dataset] values = set(featValue) for value in values: sublables = lables[:] myTree[bestFeatureLable][value]=creatTree(splitDataSet(dataset,bestFeature,value),sublables) return myTree
if __name__ == '__main__': myDat,labels= creatDataSet() print(creatTree(myDat,labels))