легко объединить данные в набор гистограмм
#2d histogram gives you the counts, in each cell
(H,redEdges,greedEdges) = numpy.histogram2d(
red.ravel(),green.ravel(),
bins=nbins
)
#divide by the total to get the probability of
#each cell -> the joint distribution
Prg = H/H.sum()
#sum over the `green` axis to get the `red` marginal (nx1)
Pr = H2d.sum(1)[:,numpy.newaxis]
#sum over the `red` axis to get the `green` marginal (1xn)
Pg = H2d.sum(0)[numpy.newaxis,:]
Оттуда взаимная информация проста:
#calculate information contribution of each bin
dIrg = Prg*numpy.log(Prg/(Pr*Pg))
#filter nans and sum
Irg = dIrg[~numpy.isnan(dIrg)].mean()