使用RandomForestClassifier.decision_路径,如何判断分类器用于决策的样本?

2024-05-19 11:29:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用RandomForestClassifier对具有二进制结果的样本进行分类(“不具有”vs“具有该事物”)。根据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.

不幸的是,这些条款对我来说是不透明的。^维数为[n_samples, n_nodes]的矩阵上的{}似乎是个错误(它不应该是indicator[sample, n_nodes_ptr[i]:n_nodes_ptr[i+1]]?),但即使这样,我也不知道该怎么做才能获取“节点指示符”并找到节点所指的功能。我可以找到使用decision_path表示DecisionTreeClassifier的示例,但不适用于RandomForestClassifier。在


Tags: thepath节点二进制分类arrayindicator事物
1条回答
网友
1楼 · 发布于 2024-05-19 11:29:58

当您意识到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

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

  • feature
  • value
  • children_left
  • children_right

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

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

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

^{pr2}$

请注意,这会告诉您测试的特性,而不了解它们的价值或它们如何影响结果。在

相关问题 更多 >

    热门问题