NexusHero
NexusHero

Reputation: 69

Values of each bin

i got following problem:

hist, edges = np.histogram(data, bins=50)

How can i access the values of each bin? I wanted to calculate the avg of each bin.

Thanks

Upvotes: 0

Views: 227

Answers (1)

javidcf
javidcf

Reputation: 59701

I think this function does what you want:

import numpy as np

def binned_mean(values, edges):
    values = np.asarray(values)
    # Classify values into bins
    dig = np.digitize(values, edges)
    # Mask values out of bins
    m = (dig > 0) & (dig < len(edges))
    values = values[m]
    dig = dig[m] - 1
    # Binned sum of values
    nbins = len(edges) - 1
    s = np.zeros(nbins, dtype=values.dtype)
    np.add.at(s, dig, values)
    # Binned count of values
    count = np.zeros(nbins, dtype=np.int32)
    np.add.at(count, dig, 1)
    # Means
    return s / count.clip(min=1)

Example:

print(binned_mean([1.2, 1.8, 2.1, 2.4, 2.7], [1, 2, 3]))
# [1.5 2.4]

There is a slight difference with the histogram in this function though, as np.digitize considers all bins to be half-closed (either right or left), unlike np.histogram which considers the last edge to be closed.

Upvotes: 2

Related Questions