当前位置: 首页>>代码示例>>Python>>正文


Python DecisionTreeClassifier.decision_path方法代码示例

本文整理汇总了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
开发者ID:HimankAiron,项目名称:scikit-learn,代码行数:33,代码来源:unveil_tree_structure.py

示例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]])
开发者ID:arjoly,项目名称:scikit-learn,代码行数:8,代码来源:test_tree.py


注:本文中的sklearn.tree.DecisionTreeClassifier.decision_path方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。