Python 直方图:检索每个箱子中权重平方的总和

Python 直方图:检索每个箱子中权重平方的总和,python,numpy,scipy,Python,Numpy,Scipy,在numpy(或scipy)中,是否可以检索直方图每个格中的权重平方和?我想在我的直方图中找出每个箱子高度的误差。对于未调平的数据,每个料仓高度的统计误差应为sqrt(N),其中N为料仓高度。。但对于加权数据,我需要加权平方和numpy.histogram不能这样做,但是numpy或scipy中是否有其他功能可以基于不同的数组(例如我正在进行历史编程的值数组)存储数组(例如权重数组)?我仔细阅读了文档,但没有找到任何内容。正如Alex所建议的,这就是您想要的。该函数返回x数组的条目所属的存储箱。

在numpy(或scipy)中,是否可以检索直方图每个格中的权重平方和?我想在我的直方图中找出每个箱子高度的误差。对于未调平的数据,每个料仓高度的统计误差应为sqrt(N),其中N为料仓高度。。但对于加权数据,我需要加权平方和
numpy.histogram
不能这样做,但是numpy或scipy中是否有其他功能可以基于不同的数组(例如我正在进行历史编程的值数组)存储数组(例如权重数组)?我仔细阅读了文档,但没有找到任何内容。

正如Alex所建议的,这就是您想要的。该函数返回
x
数组的条目所属的存储箱。然后,您可以使用此信息访问
w
的正确元素:

x = np.array([2,9,4,8])
w = np.array([0.1,0.2,0.3,0.4])

bins = np.digitize(x, [0,5,10])

# access elements for first bin
first_bin_ws = w[np.where(bins==1)[0]]

# error of fist bin
error = np.sqrt(np.sum(first_bin_ws**2.))

最后一行计算第一个箱子的误差。请注意,
np.digitalize
从1开始计数。

如果我可以为@obachtos的答案添加一个补充,我已经将其扩展为一个函数,然后演示完整的直方图:

def hist_bin_uncertainty(data, weights, bin_edges):
    """
    The statistical uncertainity per bin of the binned data.
    If there are weights then the uncertainity will be the root of the
    sum of the weights squared.
    If there are no weights (weights = 1) this reduces to the root of
    the number of events.

    Args:
        data: `array`, the data being histogrammed.
        weights: `array`, the associated weights of the `data`.
        bin_edges: `array`, the edges of the bins of the histogram.

    Returns:
        bin_uncertainties: `array`, the statistical uncertainity on the bins.

    Example:
    >>> x = np.array([2,9,4,8])
    >>> w = np.array([0.1,0.2,0.3,0.4])
    >>> edges = [0,5,10]
    >>> hist_bin_uncertainty(x, w, edges)
    array([ 0.31622777,  0.4472136 ])
    >>> hist_bin_uncertainty(x, None, edges)
    array([ 1.41421356,  1.41421356])
    >>> hist_bin_uncertainty(x, np.ones(len(x)), edges)
    array([ 1.41421356,  1.41421356])
    """
    import numpy as np
    # Bound the data and weights to be within the bin edges
    in_range_index = [idx for idx in range(len(data))
                      if data[idx] > min(bin_edges) and data[idx] < max(bin_edges)]
    in_range_data = np.asarray([data[idx] for idx in in_range_index])

    if weights is None or np.array_equal(weights, np.ones(len(weights))):
        # Default to weights of 1 and thus uncertainty = sqrt(N)
        in_range_weights = np.ones(len(in_range_data))
    else:
        in_range_weights = np.asarray([weights[idx] for idx in in_range_index])

    # Bin the weights with the same binning as the data
    bin_index = np.digitize(in_range_data, bin_edges)
    # N.B.: range(1, bin_edges.size) is used instead of set(bin_index) as if
    # there is a gap in the data such that a bin is skipped no index would appear
    # for it in the set
    binned_weights = np.asarray(
        [in_range_weights[np.where(bin_index == idx)[0]] for idx in range(1, len(bin_edges))])
    bin_uncertainties = np.asarray(
        [np.sqrt(np.sum(np.square(w))) for w in binned_weights])
    return bin_uncertainties
def hist_bin_不确定性(数据、权重、bin_边):
"""
分格数据的每个分格的统计不确定性。
如果存在权重,则不确定性将是问题的根源
权重的平方和。
如果没有权重(权重=1),则会减少到
事件的数量。
Args:
data:`array`,正在进行历史编程的数据。
权重:`array`,`data`的关联权重。
bin_edges:`array`,直方图中各个bin的边。
返回:
bin_不确定性:`array`,bin上的统计不确定性。
例子:
>>>x=np.数组([2,9,4,8])
>>>w=np.数组([0.1,0.2,0.3,0.4])
>>>边=[0,5,10]
>>>历史不确定性(x、w、边)
阵列([0.31622777,0.4472136])
>>>历史不确定性(x,无,边)
数组([1.41421356,1.41421356])
>>>历史不确定性(x,np.ONE(len(x)),边)
数组([1.41421356,1.41421356])
"""
将numpy作为np导入
#将数据和权重绑定到箱子边缘内
in_range_index=[idx代表范围内的idx(len(数据))
如果数据[idx]>min(bin_边)和数据[idx]
我会从我不明白开始。你能用更多的数学术语来表达吗?@obachtos让我们假设我有一个数组
x=[2,9,4,8]
和一个权重数组
w=[0.1,0.2,0.3,0.4]。
我将创建一个包含两个容器的直方图,其中包含
numpy.histogram(x,weights=w,容器=[0,5,10])
。在第0个箱子中,我将得到2和4,但是由于重量的原因,箱子的总高度为0.1+0.3=0.4。在第一个箱子中,我将得到箱子高度为0.2+0.4=0.6的9和8。我还想得到每个箱子的重量平方和。第0个存储箱的值为.1^2+.3^2。此料仓高度的统计误差为sqrt(总和(.1^2+.3^2))=0.316。。。与未加权数据不同的是sqrt(箱子高度)。@Alex我看了一下
numpy。数字化了
,它将输入数据装箱,我看不出它如何能够将重量w.r.t.数据装箱。你能举个例子吗,也许只是没有点击我的建议解决方案对我来说非常缓慢。看看增强Historgams:with the weight storage.perfect-带有
np的示例。其中
是我需要看到的,以便单击它。