python实现决策树的保存和调用

  • Post author:
  • Post category:python



总目录:

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('file.txt', 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 = trees.createTree(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 = trees.createTree(b, the_label1)后值变了,我并没有重新赋值给the_label1啊,没有办法,只好新建了个变量the_label2解决问题。



版权声明:本文为weixin_44255182原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。