python实现决策树的保存和调⽤总⽬录:
本⽂数据以及⼤部分代码来⾃《机器学习实战》
姜维决策树的保存和调⽤
决策树的保存和调⽤
前⾯讲到将训练的决策树绘制成简单易懂的图⽚,
trees.py下的代码
def classify(inputTree, featLabels, testVec):
print(featLabels)
firstStr =list(inputTree.keys())[0]
condDict = inputTree[firstStr]
print(firstStr)
print(featLabels)
featIndex = featLabels.index(firstStr)
key = testVec[featIndex]
valueOfFeat = condDict[key]
if isinstance(valueOfFeat,dict):
毕业论文范本classLabel = classify(valueOfFeat, featLabels, testVec)
猴面小龙兰el:
classLabel = valueOfFeat
return classLabel
def dumpTree(inputTree, filename):
import pickle
结束与开始
fw =open(filename,'wb')
pickle.dump(inputTree, fw)
fw.clo()
def loadTree(filename):
import pickle
fr =open(filename,'rb')
return pickle.load(fr)
测试代码如下:
import pandas as pd
import numpy as np
import trees
孕妇可以吃甘蔗
from math import log
data_file = pd.read_csv('', p='\t')
data_file = data_file.iloc[:,1:]
a = data_file.values
b = a.tolist()
the_label1 =list(data_file.keys()[:-1])
the_label2 =list(data_file.keys()[:-1])
mytree = ateTree(b, the_label1)笔记本麦克风
trees.dumpTree(mytree,'mytree')
new_trees = trees.loadTree('mytree')
print(new_trees)
沈德潜dd = trees.classify(new_trees, the_label2,['L1','R1'])
天气之子观后感print(dd)
之前的数据集和模型的训练在上⼀张,不过有个问题,我也不知道the_label1为啥经过函数mytree = ateTree(b, the_label1)后值变了,我并没有重新赋值给the_label1啊,没有办法,只好新建了个变量the_label2解决问题。