沿numpy的一个轴进行数据分箱 [英] Binning of data along one axis in numpy
本文介绍了沿numpy的一个轴进行数据分箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个很大的二维数组arr
,我想使用numpy在第二个轴上进行装箱.因为np.histogram
展平了我当前使用的for循环数组:
I have a large two dimensional array arr
which I would like to bin over the second axis using numpy. Because np.histogram
flattens the array I'm currently using a for loop:
import numpy as np
arr = np.random.randn(100, 100)
nbins = 10
binned = np.empty((arr.shape[0], nbins))
for i in range(arr.shape[0]):
binned[i,:] = np.histogram(arr[i,:], bins=nbins)[0]
我觉得在numpy中应该有一种更直接,更有效的方法来做到这一点,但我没有找到一个.
I feel like there should be a more direct and more efficient way to do that within numpy but I failed to find one.
推荐答案
您可以使用 np.apply_along_axis
:
You could use np.apply_along_axis
:
x = np.array([range(20), range(1, 21), range(2, 22)])
nbins = 2
>>> np.apply_along_axis(lambda a: np.histogram(a, bins=nbins)[0], 1, x)
array([[10, 10],
[10, 10],
[10, 10]])
主要优点(如果有的话)是稍微短一点,但是我不希望性能有太大提高.组装每行结果的效率可能略高.
The main advantage (if any) is that it's slightly shorter, but I wouldn't expect much of a performance gain. It's possibly marginally more efficient in the assembly of the per-row results.
这篇关于沿numpy的一个轴进行数据分箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文