【发布时间】:2020-10-08 04:34:20
【问题描述】:
我不断收到以下错误:AttributeError: 'tuple' object has no attribute 'size'。还添加了拆分功能
import numpy as np
def split(array):
N = {}
uniqe_array = np.unique(array)
for i in uniqe_array:
N[i] = np.where(array==i)
return N
def information_gain(x_array, y_array):
parent_entropy = entropy(x_array)
split_dict = split(y_array)
for val in split_dict.values():
freq = val.size / x_array.size
child_entropy = entropy([x_array[i] for i in val])
parent_entropy -= child_entropy* freq
return parent_entropy
x = np.array([0, 1, 0, 1, 0, 1])
y = np.array([0, 1, 0, 1, 1, 1])
print(round(information_gain(x, y), 4))
x = np.array([0, 0, 1, 1, 2, 2])
y = np.array([0, 1, 0, 1, 1, 1])
print(round(information_gain(x, y), 4))
【问题讨论】:
-
附加回溯
-
你在哪里定义“熵”?这会导致某人收到 NameError,因为您没有在您提供的代码中定义它
-
能否提供函数
split的代码? -
在某个地方,您在实际上是元组的东西上使用 .size 方法..
标签: python