决策树最后的存储 检测

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
原文地址:https://www.cnblogs.com/cherryMJY/p/8535472.html