gpt4 book ai didi

python - 使用 RandomForestClassifier.decision_path,我如何判断分类器用于做出决定的样本?

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:41 25 4
gpt4 key购买 nike

我正在使用 RandomForestClassifier 对具有二元结果(“没有东西”与“有东西”)的样本进行分类。根据 RandomForestClassifier.decision_path 的结果,我如何确定哪些样本有助于分类决策?

documentation说:

Returns:

indicator : sparse csr array, shape = [n_samples, n_nodes]

Return a node indicator matrix where non zero elements indicates that the samples goes through the nodes.

n_nodes_ptr : array of size (n_estimators + 1, )

The columns from indicator[n_nodes_ptr[i]:n_nodes_ptr[i+1]] gives the indicator value for the i-th estimator.

不幸的是,这些术语对我来说是不透明的。 indicator[x:y] 在维度为 [n_samples, n_nodes] 的矩阵上似乎是个错误(不应该是 indicator[sample, n_nodes_ptr [i]:n_nodes_ptr[i+1]]?),但即便如此,我也不确定如何获取“节点指示器”并找到节点所指的功能。我可以找到将 decision_path 用于 DecisionTreeClassifier 的示例,但不能用于 RandomForestClassifier

最佳答案

当您意识到 sklearn 约定是将尽可能多的内容放入 numpy 时,理解 RandomForestClassifier.decision_path 的输出会更容易矩阵。

decision_path 返回每个决策树的 decision_path 的水平串联,第二个返回值告诉您每个子矩阵的边界。因此,在 RandomForestClassifier 上使用 decision_path 等同于在每个 RandomForestClassifier.estimators_ 上使用 decision_path。对于单行样本,您可以像这样遍历结果:

indicators, index_by_tree = classifier.decision_path(data_row)
indices = zip(index_by_tree, index_by_tree[1:])
for tree_classifier, (begin, end) in zip(classifier.estimators_, indices):
tree = tree_classifier.tree_
node_indices = indicators[0, begin:end].indices

树实例具有以下属性,而不是将每个节点视为一个单独的对象:

  • 特征
  • children_left
  • children_right

每个都是数组或矩阵,记录由其索引标识的树节点的特征。例如,tree.feature[3] 告诉您节点 3 测试的是哪个特征; tree.value 以 3D 数组的形式告诉您树的值,第一个维度是节点编号,最后一个维度包含分类值和阈值。 (我不知道第二维是什么。在我的例子中它只有一个元素。)tree.children_left[5] 告诉你节点 5 的节点号左 child ,正如您猜到的那样,tree.children_right[6] 告诉您节点 6 的右 child 的节点编号。

除了这些数组,DecisionTreeClassifier.decision_path也是一个数组,其中decision_path[N]如果节点#不为零决策过程中访问了N。

要返回已测试的功能,您可以执行以下操作:

for index in node_indices:
feature = tree.feature[index]
if feature >= 0:
features.add(feature) # where `features` is a set()

请注意,这会告诉您有关测试的功能,但不会告诉您它们的值(value)或它们如何影响结果。

关于python - 使用 RandomForestClassifier.decision_path,我如何判断分类器用于做出决定的样本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49991677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com