Input X: A numpy array whose size gives the number of instances. X contains each instance's attribute value. Y: A numpy array which contains each instance's corresponding target label.
Output : Conditional Entropy
Can you please help me code the conditional entropy calculation dynamically which will further be subracted from total entropy of the given population to find the information gain.
I tried something like the below code example. But the only input data I have are the two numpy arrays. can you please help me correct this ? [code]
def gain(data, attr, target_attr):
val_freq = {}
subset_entropy = 0.0
for record in data:
if (val_freq.has_key(record[attr])):
val_freq[record[attr]] += 1.0
else:
val_freq[record[attr]] = 1.0
for val in val_freq.keys():
val_prob = val_freq[val] / sum(val_freq.values())
data_subset = [record for record in data if record[attr] == val]
conditional_entropy += val_prob * entropy(data_subset, target_attr)