决策树算法实现(二)

来源:互联网 发布:python培训机构 编辑:程序博客网 时间:2024/06/08 08:17

前一篇博客讲解了决策树,并实现其代码,构造决策树是为了对实际数据进行分类,这篇博客将介绍如何用决策树进行分类。
这里的代码继上篇博客中的代码,并使用生成的决策树mytree和标签向量labels

def classify(inputTree,featLables,testVec):    firstStr=list(inputTree.keys())[0]    secondDict=inputTree[firstStr]    featIndex=featLables.index(firstStr)    for key in secondDict.keys():        if testVec[featIndex]==key:            if type(secondDict[key]).__name__=='dict':                classLabel=classify(secondDict[key],featLables,testVec)            else:                classLabel=secondDict[key]    return classLabelprint(classify(mytree,labels,[0,0]))
0 0
原创粉丝点击