def condition_entropy(x, cond): assert x.shape == cond.shape, 'input is invalid.' nSize = len(x) fd = FreqDict(list(cond)) fd = {k: float(v) / nSize for k, v in fd} result = 0. for k, v in fd.iteritems(): result += v * entropy(x[cond == k]) return result