opensw
opensw

Reputation: 425

Python buggy histogram?

I have a strange behavior with this very simple code

import numpy as np
[y, binEdges] = np.histogram(x, xout)

where x and xout are numpy arrays (xout describes the edges of the bins that are equally spaced).

If I do

np.sum(y)

the value is not equal to the number of elements in x (x.shape), this value is a lot lesser then x.shape and I cannot figure out why. Is it a bug of np.histogram? If you need I can upload the x and xout numpy arrays but they are very long (x.shape is 19133 float64 and xout.shape is 1360 float64). Let me know if I did something wrong in the above code.

Upvotes: 1

Views: 160

Answers (2)

Amir
Amir

Reputation: 1179

Use this to compute the xout correctly, n is the number of bins.

xout = np.linspace(floor(x.min()), ceil(x.max(), n)

and then call the histogram function:

[y, binEdges] = histogram(x, xout)

Upvotes: 1

Bi Rico
Bi Rico

Reputation: 25833

Try this:

y.sum() + (x < xout[0]).sum() + (x > xout[-1]).sum()

Upvotes: 1

Related Questions