Sometimes, we want to bin data in Python with scipy or numpy.
In this article, we’ll look at how to bin data in Python with scipy or numpy.
How to bin data in Python with scipy or numpy?
To bin data in Python with scipy or numpy, we can use the linspace
method to create the bins.
And then we call digitize
to put the data into the bins`.
For instance, we write
import numpy
data = numpy.random.random(100)
bins = numpy.linspace(0, 1, 10)
digitized = numpy.digitize(data, bins)
bin_means = [data[digitized == i].mean() for i in range(1, len(bins))]
to create the bins
with
bins = numpy.linspace(0, 1, 10)
We call linspace
to creates with intervals of 0.1 between 0 and 1.
Then we put the data
items into the bins
with
digitized = numpy.digitize(data, bins)
And we get the means of the values in each bin with
[data[digitized == i].mean() for i in range(1, len(bins))]
Conclusion
To bin data in Python with scipy or numpy, we can use the linspace
method to create the bins.
And then we call digitize
to put the data into the bins`.