本文整理汇总了Python中sklearn.tree.DecisionTreeClassifier.decision_path方法的典型用法代码示例。如果您正苦于以下问题:Python DecisionTreeClassifier.decision_path方法的具体用法?Python DecisionTreeClassifier.decision_path怎么用?Python DecisionTreeClassifier.decision_path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.tree.DecisionTreeClassifier
的用法示例。
在下文中一共展示了DecisionTreeClassifier.decision_path方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: range
# 需要导入模块: from sklearn.tree import DecisionTreeClassifier [as 别名]
# 或者: from sklearn.tree.DecisionTreeClassifier import decision_path [as 别名]
for i in range(n_nodes):
if is_leaves[i]:
print("%snode=%s leaf node." % (node_depth[i] * "\t", i))
else:
print(
"%snode=%s test node: go to node %s if X[:, %s] <= %ss else to "
"node %s." % (node_depth[i] * "\t", i, children_left[i], feature[i], threshold[i], children_right[i])
)
print()
# First let's retrieve the decision path of each sample. The decision_path
# method allows to retrieve the node indicator functions. A non zero element of
# indicator matrix at the position (i, j) indicates that the sample i goes
# through the node j.
node_indicator = estimator.decision_path(X_test)
# Similarly, we can also have the leaves ids reached by each sample.
leave_id = estimator.apply(X_test)
# Now, it's possible to get the tests that were used to predict a sample or
# a group of samples. First, let's make it for the sample.
sample_id = 0
node_index = node_indicator.indices[node_indicator.indptr[sample_id] : node_indicator.indptr[sample_id + 1]]
print("Rules used to predict sample %s: " % sample_id)
for node_id in node_index:
if leave_id[sample_id] != node_id:
continue
示例2: test_decision_path_hardcoded
# 需要导入模块: from sklearn.tree import DecisionTreeClassifier [as 别名]
# 或者: from sklearn.tree.DecisionTreeClassifier import decision_path [as 别名]
def test_decision_path_hardcoded():
X = iris.data
y = iris.target
est = DecisionTreeClassifier(random_state=0, max_depth=1).fit(X, y)
node_indicator = est.decision_path(X[:2]).toarray()
assert_array_equal(node_indicator, [[1, 1, 0], [1, 0, 1]])