export.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:decision-tree-id3 作者: svaante 项目源码 文件源码
def export_text(decision_tree, feature_names=None):
    """Export a decision tree in WEKA like string format.
    Parameters
    ----------
    decision_tree : decision tree classifier
    feature_names : list of strings, optional (default=None)
        Names of each of the features.
    Returns
    -------
    ret : string
    """
    max_depth = 500

    def build_string(node, indent, depth):
        ret = ''
        if node is None or depth > max_depth:
            return ''
        if node.is_feature:
            ret += '\n'
            template = '|   ' * indent
            if feature_names is None:
                template += str(node.details.feature_idx)
            else:
                template += feature_names[node.details.feature_idx]
            template += ' {}'
            for child in node.children:
                edge_value = _extract_edge_value(decision_tree, child[1])
                ret += template.format(edge_value)
                ret += build_string(child[0], indent + 1, depth + 1)
        else:
            value = decision_tree.y_encoder.single_inv_transform(node.value)
            if isinstance(value, np.bytes_):
                value = value.decode('UTF-8')
            ret += ': {} {} \n'.format(value, _extract_class_count(node))
        return ret
    return build_string(decision_tree.root, 0, 0)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号