Python Numpy中是否有和Matlab一样的histogram函数?

Python Numpy中是否有和Matlab一样的histogram函数?

1、python

y,x = np.histogram(arrCount, bins=np.linspace(0, 100, 20))

2、python

bins = 100 #设置成100bin
# px = np.histogram(x, bins=bins,range=(sigMin, sigMax))[0]/length  #bins代表划分后的最小单元个数,可以理解成信号的分辨率;range代表信号的范围

# bins代表划分后的最小单元个数,可以理解成信号的分辨率;
# 如果加上range之后,结果和matlab的pxp = histogram(x, bins);px = pxp.Values/length; 差异很大,所以先不加range了
px = np.histogram(x, bins=bins)[0] / length

hx = - np.sum(px * np.log(px+1e-8)) #随机变量X的边缘熵

# py = np.histogram(y, bins=bins,range=(sigMin, sigMax))[0]/length
py = np.histogram(y, bins=bins)[0] / length #不加range使得结果跟matlab尽可能地相近;
hy = - np.sum(py * np.log(py+1e-8))

#pxy = np.histogram2d(x,y,bins=bins,range=[[sigMin, sigMax], [sigMin, sigMax]])[0]/length
pxy = np.histogram2d(x, y, bins=bins )[0] / length
hxy = - np.sum(pxy * np.log(pxy+1e-8))

3、matlab

function ixy = calMIMatlab(x,y)

length = size(x,2);

bins = 100 ;

pxp = histogram(x, bins);

px = pxp.Values/length;

hx = - sum(px .* log(px+1e-8));

pyp = histogram(y,bins);

py1 = pyp.Values/length;

hy = - sum(py1 .* log(py1+1e-8));

pxyp = histogram2(x,y,bins);

pxy = pxyp.Values/length;

hxy = - sum(sum(pxy .* log(pxy+1e-8)));

ixy = hx + hy - hxy;

end

posted @ 2022-08-13 19:21  bH1pJ  阅读(47)  评论(0编辑  收藏  举报