python决策树生成规则_如何从scikit-learn决策树中提取决策规则?

我创建了自己的函数来从sklearn创建的决策树中提取规则:

import pandas as pd

import numpy as np

from sklearn.tree import DecisionTreeClassifier

# dummy data:

df = pd.DataFrame({'col1':[0,1,2,3],'col2':[3,4,5,6],'dv':[0,1,0,1]})

# create decision tree

dt = DecisionTreeClassifier(max_depth=5, min_samples_leaf=1)

dt.fit(df.ix[:,:2], df.dv)

此函数首先从节点(在子数组中由-1标识)开始,然后以递归方式查找父节点。我将此称为节点的“谱系”。一路上,我抓住了我需要创建的值if / then / else SAS逻辑:

def get_lineage(tree, feature_names):

left      = tree.tree_.children_left

right     = tree.tree_.children_right

threshold = tree.tree_.threshold

features  = [feature_names[i] for i in tree.tree_.feature]

# get ids of child nodes

idx = np.argwhere(left == -1)[:,0]

def recurse(left, right, child, lineage=None):

if lineage is None:

lineage = [child]

if child in left:

parent = np.where(left == child)[0].item()

split = 'l'

else:

parent = np.where(right == child)[0].item()

split = 'r'

lineage.append((parent, split, threshold[parent], features[parent]))

if parent == 0:

lineage.reverse()

return lineage

else:

return recurse(left, right, parent, lineage)

for child in idx:

for node in recurse(left, right, child):

print node

下面的元组包含创建SAS if / then / else语句所需的一切。我不喜欢do在SAS中使用块,这就是我创建描述节点整个路径的逻辑的原因。元组之后的单个整数是路径中终端节点的ID。所有前面的元组组合起来创建该节点。

In [1]: get_lineage(dt, df.columns)

(0, 'l', 0.5, 'col1')

1

(0, 'r', 0.5, 'col1')

(2, 'l', 4.5, 'col2')

3

(0, 'r', 0.5, 'col1')

(2, 'r', 4.5, 'col2')

(4, 'l', 2.5, 'col1')

5

(0, 'r', 0.5, 'col1')

(2, 'r', 4.5, 'col2')

(4, 'r', 2.5, 'col1')

6

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值