奇葩问题ValueError: math domain error,已解决!!!
在实现决策树的ID3算法时,编写了计算信息熵的函数,运行后遇到了如下的问题:
对应处的代码为:
@staticmethod def calc_ent(datasets): data_length = len(datasets) label_count = {} for i in range(data_length): label = datasets[i][-1] if label not in label_count: label_count[label] = 0 else: label_count[label] += 1 ent = -sum([(p / data_length) * log(p / data_length, 2) for p in label_count.values()]) return ent
经过不断地试错与百度,最后解决了该问题,竟然是把else:去掉:
@staticmethod def calc_ent(datasets): data_length = len(datasets) label_count = {} for i in range(data_length): label = datasets[i][-1] if label not in label_count: label_count[label] = 0 label_count[label] += 1 ent = -sum([(p / data_length) * log(p / data_length, 2) for p in label_count.values()]) return ent
运行成功:
根据报错提示,原因应该是数学域错误,不知道为什么删掉else:就成功了......