日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2025/3/20 python 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python决策树生成规则_如何从scikit-learn决策树中提取决策规则? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我創(chuàng)建了自己的函數(shù)來從sklearn創(chuàng)建的決策樹中提取規(guī)則:

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)

此函數(shù)首先從節(jié)點(diǎn)(在子數(shù)組中由-1標(biāo)識(shí))開始,然后以遞歸方式查找父節(jié)點(diǎn)。我將此稱為節(jié)點(diǎn)的“譜系”。一路上,我抓住了我需要?jiǎng)?chuàng)建的值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

下面的元組包含創(chuàng)建SAS if / then / else語句所需的一切。我不喜歡do在SAS中使用塊,這就是我創(chuàng)建描述節(jié)點(diǎn)整個(gè)路徑的邏輯的原因。元組之后的單個(gè)整數(shù)是路徑中終端節(jié)點(diǎn)的ID。所有前面的元組組合起來創(chuàng)建該節(jié)點(diǎn)。

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

總結(jié)

以上是生活随笔為你收集整理的python决策树生成规则_如何从scikit-learn决策树中提取决策规则?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。