python实现决策树的保存和调⽤总⽬录:
本⽂数据以及⼤部分代码来⾃《机器学习实战》
决策树的保存和调⽤
决策树的保存和调⽤
前⾯讲到将训练的决策树绘制成简单易懂的图⽚,
trees.py下的代码
def classify(inputTree, featLabels, testVec):
print(featLabels)
firstStr =list(inputTree.keys())[0]
secondDict = inputTree[firstStr]
print(firstStr)
print(featLabels)
featIndex = featLabels.index(firstStr)
key = testVec[featIndex]
valueOfFeat = secondDict[key]
if isinstance(valueOfFeat,dict):
classLabel = classify(valueOfFeat, featLabels, testVec)
else:
classLabel = valueOfFeat
return classLabel
def dumpTree(inputTree, filename):
import pickle
fw =open(filename,'wb')
pickle.dump(inputTree, fw)
fw.close()
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('', sep='\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解决问题。
import pickle
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论