我使用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
。在
当您意识到
sklearn
约定是在numpy
矩阵中放入尽可能多的内容时,理解RandomForestClassifier.decision_path
的输出就更容易了。在decision_path
返回每个决策树的decision_path
的水平连接,第二个返回值通知您每个子矩阵的边界。因此,在RandomForestClassifier
上使用decision_path
等同于在RandomForestClassifier.estimators_
上使用decision_path
。对于单行示例,可以按如下方式遍历结果:树实例没有将每个节点视为单独的对象,而是具有以下属性:
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}$请注意,这会告诉您测试的特性,而不了解它们的价值或它们如何影响结果。在
相关问题 更多 >
编程相关推荐