决策树最后的存储 检测

def storeTree(inputTree,filename):
    import pickle;
    fw=open(filename,'w');
    pickle.dump(inputTree,fw);
    fw.close();

def grabTree(filename):
    import pickle;
    fr = open(filename);
    return pickle.load(fr);

def classify(inputTree,featLabels,testVec):
    firstStr=inputTree.keys()[0];
    secondDict=inputTree[firstStr];
    featIndex=featLabels.index(firstStr);
    key=testVec[featIndex];
    valueOfFeat=secondDict[key];
    if isinstance(valueOfFeat,dict):
        classLabel=classify(valueOfFeat,featLabels,testVec);
    else:
        classLabel=valueOfFeat;
    return classLabel;
View Code

 

posted on 2018-03-09 18:56  HelloWorld!--By-MJY  阅读(96)  评论(0编辑  收藏  举报

导航