Expected Information

Info(D)=i=1mpilog2(pi)\operatorname{Info}(D)=-\sum_{i=1}^{m} p_{i} \log _{2}\left(p_{i}\right)

where pip_i is the probability that tuple in DD belongs to class CiC_i which can be estimated by CiD\frac{|C_i|}{|D|}. The Expectation of Surprisal over every possible outome.

It is used to calculate Information Gain and Perplexity.

Python Implementation

def information(dataset: pd.DataFrame, target_attribute: str) -> float:
	p = dataset.value_counts(target_attribute) / dataset.shape[0]
	return -sum([pi * log(pi, 2) for pi in p])