用scipy / numpy在python中分箱数据

时间:2021-07-09 21:19:09

is there a more efficient way to take an average of an array in prespecified bins? for example, i have an array of numbers and an array corresponding to bin start and end positions in that array, and I want to just take the mean in those bins? I have code that does it below but i am wondering how it can be cut down and improved. thanks.

是否有更有效的方法在预先指定的箱中取平均数组?例如,我有一个数字数组和一个数组对应于该数组中的bin开始和结束位置,我想在这些数据库中取平均值?我有下面的代码,但我想知道如何减少和改进它。谢谢。

from scipy import *
from numpy import *

def get_bin_mean(a, b_start, b_end):
    ind_upper = nonzero(a >= b_start)[0]
    a_upper = a[ind_upper]
    a_range = a_upper[nonzero(a_upper < b_end)[0]]
    mean_val = mean(a_range)
    return mean_val


data = rand(100)
bins = linspace(0, 1, 10)
binned_data = []

n = 0
for n in range(0, len(bins)-1):
    b_start = bins[n]
    b_end = bins[n+1]
    binned_data.append(get_bin_mean(data, b_start, b_end))

print binned_data

5 个解决方案

#1


130  

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

使用numpy.digitize()可能更快更容易:

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))]

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

另一种方法是使用numpy.histogram():

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

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

试试自己哪一个更快...... :)

#2


28  

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

Scipy(> = 0.11)函数scipy.stats.binned_statistic专门解决了上述问题。

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

对于与之前答案中相同的示例,Scipy解决方案将是

import numpy as np
from scipy.stats import binned_statistic

data = np.random.rand(100)
bin_means = binned_statistic(data, data, bins=10, range=(0, 1))[0]

#3


14  

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

不知道为什么这个线程被恶化了;但这是2014年批准的答案,应该快得多:

import numpy as np

data = np.random.rand(100)
bins = 10
slices = np.linspace(0, 100, bins+1, True).astype(np.int)
counts = np.diff(slices)

mean = np.add.reduceat(data, slices[:-1]) / counts
print mean

#4


4  

The numpy_indexed package (disclaimer: I am its author) contains functionality to efficiently perform operations of this type:

numpy_indexed包(免责声明:我是它的作者)包含有效执行此类操作的功能:

import numpy_indexed as npi
print(npi.group_by(np.digitize(data, bins)).mean(data))

This is essentially the same solution as the one I posted earlier; but now wrapped in a nice interface, with tests and all :)

这与我之前发布的解决方案基本相同;但现在包装在一个漂亮的界面,测试和所有:)

#5


2  

I would add, and also to answer the question find mean bin values using histogram2d python that the scipy also have a function specially designed to compute a bidimensional binned statistic for one or more sets of data

我想添加,并回答问题使用histogram2d python找到平均bin值,scipy还有一个专门设计用于计算一组或多组数据的二维分级统计数据的函数

import numpy as np
from scipy.stats import binned_statistic_2d

x = np.random.rand(100)
y = np.random.rand(100)
values = np.random.rand(100)
bin_means = binned_statistic_2d(x, y, values, bins=10).statistic

the function scipy.stats.binned_statistic_dd is a generalization of this funcion for higher dimensions datasets

函数scipy.stats.binned_statistic_dd是更高维度数据集的此函数的推广

#1


130  

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

使用numpy.digitize()可能更快更容易:

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))]

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

另一种方法是使用numpy.histogram():

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

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

试试自己哪一个更快...... :)

#2


28  

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

Scipy(> = 0.11)函数scipy.stats.binned_statistic专门解决了上述问题。

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

对于与之前答案中相同的示例,Scipy解决方案将是

import numpy as np
from scipy.stats import binned_statistic

data = np.random.rand(100)
bin_means = binned_statistic(data, data, bins=10, range=(0, 1))[0]

#3


14  

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

不知道为什么这个线程被恶化了;但这是2014年批准的答案,应该快得多:

import numpy as np

data = np.random.rand(100)
bins = 10
slices = np.linspace(0, 100, bins+1, True).astype(np.int)
counts = np.diff(slices)

mean = np.add.reduceat(data, slices[:-1]) / counts
print mean

#4


4  

The numpy_indexed package (disclaimer: I am its author) contains functionality to efficiently perform operations of this type:

numpy_indexed包(免责声明:我是它的作者)包含有效执行此类操作的功能:

import numpy_indexed as npi
print(npi.group_by(np.digitize(data, bins)).mean(data))

This is essentially the same solution as the one I posted earlier; but now wrapped in a nice interface, with tests and all :)

这与我之前发布的解决方案基本相同;但现在包装在一个漂亮的界面,测试和所有:)

#5


2  

I would add, and also to answer the question find mean bin values using histogram2d python that the scipy also have a function specially designed to compute a bidimensional binned statistic for one or more sets of data

我想添加,并回答问题使用histogram2d python找到平均bin值,scipy还有一个专门设计用于计算一组或多组数据的二维分级统计数据的函数

import numpy as np
from scipy.stats import binned_statistic_2d

x = np.random.rand(100)
y = np.random.rand(100)
values = np.random.rand(100)
bin_means = binned_statistic_2d(x, y, values, bins=10).statistic

the function scipy.stats.binned_statistic_dd is a generalization of this funcion for higher dimensions datasets

函数scipy.stats.binned_statistic_dd是更高维度数据集的此函数的推广