In: Computer Science
I don't know why I keep getting the following error:
AttributeError: 'tuple' object has no attribute 'size'
I am using python in Anaconda.
import numpy as np
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))
import numpy as np
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))
#Your code is correct. Please check the types of outputs and labels. I think, here labels is passed as a tuple instead of a tensor.So, you could return labels in your __getitem__ as:
torch.tensor(self.labels[idx])
#NOTE: I used google colab. I don't have jupyter or Anaconda. All the best I am sure this will help. Below I am writing the new code so use that one.
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
torch.tensor(self.labels[idx])
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))