决策树的预测误差

时间:2018-01-20 11:32:14

标签: algorithm python-2.7 decision-tree supervised-learning

我正在使用决策树学习器algorithm来构建我的决策树并在数据集上对其进行测试。

我也在尝试计算我的树的预测错误率,所以我可以用我的测试和训练集的学习曲线绘制图表。我做了一个循环,我的算法被应用n次(任意)。我的变量internal_nodes存储生成的内部节点的数量(这将是我在学习曲线图中的横坐标),我在每次调用时返回它。

我创建了count_error()来衡量预测值和期望值之间的差异。

def count_errors(examples, target, tree):
    counter = 0
    for ex in examples: # examples is a list that contains list of example
        desired = ex[target]
        predicted = tree(ex) # use __call__(self,example) to obtain leaf value
        if desired != predicted:
            counter += 1
    return float(counter / len(examples)) * float(100)`

def __call__(self, example):
    """Given an example, classify it using the attribute and the branches."""
    attrvalue = example[self.attr] #attr is a list of integers that index into an example
    return self.branches[attrvalue](example)

发生的错误始终为0.我已经用一次迭代测试了我的算法并且看起来它有效。我认为错误在于我如何计算错误。 完整的存储库位于我的github

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在计算错误率时,看起来你有整数除法。试试这个:

return float(counter) / len(examples) * float(100)
相关问题