binning data in python with scipy/numpy binning data in python with scipy/numpy python python

binning data in python with scipy/numpy


It's probably faster and easier to use numpy.digitize():

import numpydata = 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))]

An alternative to this is to use numpy.histogram():

bin_means = (numpy.histogram(data, bins, weights=data)[0] /             numpy.histogram(data, bins)[0])

Try for yourself which one is faster... :)


The Scipy (>=0.11) function scipy.stats.binned_statistic specifically addresses the above question.

For the same example as in the previous answers, the Scipy solution would be

import numpy as npfrom scipy.stats import binned_statisticdata = np.random.rand(100)bin_means = binned_statistic(data, data, bins=10, range=(0, 1))[0]


Not sure why this thread got necroed; but here is a 2014 approved answer, which should be far faster:

import numpy as npdata = np.random.rand(100)bins = 10slices = np.linspace(0, 100, bins+1, True).astype(np.int)counts = np.diff(slices)mean = np.add.reduceat(data, slices[:-1]) / countsprint mean