python 数据分箱_用scipy / numpy在python中分箱数据

用scipy / numpy在python中分箱数据

是否有更有效的方法在预先指定的箱中取平均数组? 例如,我有一个数字数组和一个对应于该数组中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

6个解决方案

146 votes

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

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

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

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

numpy.histogram(data, bins)[0])

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

Sven Marnach answered 2019-09-03T18:52:57Z

33 votes

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

对于与之前答案中相同的示例,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]

divenex answered 2019-09-03T18:54:23Z

14 votes

不知道为什么这个线程被恶化了; 但这是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

Eelco Hoogendoorn answered 2019-09-03T18:54:47Z

4 votes

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

import numpy_indexed as npi

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

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

Eelco Hoogendoorn answered 2019-09-03T18:55:16Z

1 votes

我想添加,并回答问题使用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

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

Chmeul answered 2019-09-03T18:55:47Z

0 votes

另一种方法是使用ufunc.at。 该方法适用于指定索引处的所需操作。我们可以使用searchsorted方法获取每个数据点的bin位置。然后我们可以使用at在bin_indexes给出的索引处将直方图的位置递增1,每次我们在bin_indexes处遇到索引。

np.random.seed(1)

data = np.random.random(100) * 100

bins = np.linspace(0, 100, 10)

histogram = np.zeros_like(bins)

bin_indexes = np.searchsorted(bins, data)

np.add.at(histogram, bin_indexes, 1)

kostas answered 2019-09-03T18:56:11Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值