如何将numpy直方图反转回强度
我想知道是否有一种将直方图反转回强度信号的方法 例如:如何将numpy直方图反转回强度,numpy,Numpy,我想知道是否有一种将直方图反转回强度信号的方法 例如: >>> A = np.array([7, 2, 1, 4, 0, 7, 8, 10]) >>> H, edge = np.histogram(A, bins=10, range=(0,10)) >>> np.sort(A) [ 0 1 2 4 7 7 8 10] >>> H [1 1 1 0 1 0 0 2 1 1] >>> edge
>>> A = np.array([7, 2, 1, 4, 0, 7, 8, 10])
>>> H, edge = np.histogram(A, bins=10, range=(0,10))
>>> np.sort(A)
[ 0 1 2 4 7 7 8 10]
>>> H
[1 1 1 0 1 0 0 2 1 1]
>>> edge
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
是否有方法使用H
和边缘
重建原始a
强度?当然,位置信息将丢失,但我只想恢复强度和出现的相对数量
我有这样一种迂回的方式:
>>> reco = []
>>> for i, h in enumerate(H):
... for _ in range(h):
... reco.append(edge[i])
...
>>> reco
[0.0, 1.0, 2.0, 4.0, 7.0, 7.0, 8.0, 9.0]
# I've done something wrong with the right-most histogram bin, but we can ignore that for now
对于较大的直方图,循环方式是低效的。是否有一个向量化的等价物,我在循环中做了什么?(我的直觉告诉我将涉及到numpy.digitalize
。)当然,您可以使用:
import numpy as np
A = np.array([7, 2, 1, 4, 0, 7, 8, 10])
counts, edges = np.histogram(A, bins=10, range=(0,10))
print(np.repeat(edges[:-1], counts))
# [ 0. 1. 2. 4. 7. 7. 8. 9.]
显然,不可能恢复某个值在一个容器中的确切位置,因为在生成直方图的过程中会丢失该信息。您可以使用下部或上部料仓边缘(如上例所示),也可以使用中心值,例如:
print(np.repeat((edges[:-1] + edges[1:]) / 2., counts))
# [ 0.5 1.5 2.5 4.5 7.5 7.5 8.5 9.5]