Python Numpy沿轴应用并获取行索引

Python Numpy沿轴应用并获取行索引,python,numpy,Python,Numpy,我有一个2D阵列(它实际上非常大,并且是另一个阵列的视图): 我有一个函数处理数组的每一行: def some_func(a): """ Some function that does something funky with a row of numbers """ return [a[2], a[0]] # This is not so funky np.apply_along_axis(some_func, 1, x) 我正在寻找的是调用np的某种方法

我有一个2D阵列(它实际上非常大,并且是另一个阵列的视图):

我有一个函数处理数组的每一行:

def some_func(a):
    """
    Some function that does something funky with a row of numbers
    """
    return [a[2], a[0]]  # This is not so funky

np.apply_along_axis(some_func, 1, x)
我正在寻找的是调用
np的某种方法。沿_轴应用_
函数,这样我就可以访问行索引(用于正在处理的行),然后能够使用此函数处理每一行:

def some_func(a, idx):
    """
    I plan to use the index for some logic on which columns to
    return. This is only an example
    """
    return [idx, a[2], a[0]]  # This is not so funky

对于轴=1的二维阵列,
沿_轴应用
与阵列行的迭代相同

In [149]: np.apply_along_axis(some_func, 1, x)
Out[149]: 
array([[2, 0],
       [3, 1],
       [4, 2],
       [5, 3]])
In [151]: np.array([some_func(i) for i in x])
Out[151]: 
array([[2, 0],
       [3, 1],
       [4, 2],
       [5, 3]])
对于axis=0,我们可以在
x.T
上迭代<当数组为3d时,代码>沿_轴应用_更有用,我们希望迭代除一个维度之外的所有维度。这样就省去了一些繁琐的工作。但这不是一个速度解决方案

通过修改后的函数,我们可以使用标准的
枚举
来获取行和索引:

In [153]: np.array([some_func(v,i) for i,v in enumerate(x)])
Out[153]: 
array([[0, 2, 0],
       [1, 3, 1],
       [2, 4, 2],
       [3, 5, 3]])
或者通过简单的范围迭代:

In [157]: np.array([some_func(x[i],i) for i in range(x.shape[0])])
Out[157]: 
array([[0, 2, 0],
       [1, 3, 1],
       [2, 4, 2],
       [3, 5, 3]])
有各种工具可用于获取更高维度的索引,例如
ndenumerate
ndindex

快速解决方案-一次处理所有行:

In [158]: np.column_stack((np.arange(4), x[:,2], x[:,0]))
Out[158]: 
array([[0, 2, 0],
       [1, 3, 1],
       [2, 4, 2],
       [3, 5, 3]])

在等待真正的功能实现时,这里有一个替代解决方案。 这会有点乱。但目前可能足以解决您的问题。:)


希望能对您有所帮助:)

我有一个关于三维张量的问题,所以我认为有必要发布一个通用的解决方案,即使用
np.ndenumerate

    f = lambda indices: #(whatever you'd like to do)

    output = np.empty(M.shape)
    for i, x in np.ndenumerate(M):
        output(i) = f(i)

用范围数组压缩它?@Divakar你能举个例子吗?您可以假设2D数组是一个视图,非常大,因此副本不是解决方案。您好,您解决了这个问题吗?@slaw这仍然是一个非初学者!?很高兴知道。我有义务问,但是有没有一种方法可以快速解决问题?我在
np中添加了一个完整的数组解决方案。沿着轴应用(一些函数,1,x)
什么是
一些函数
@见问题帖。真的没有办法按照OP想要的方式访问行/列索引吗!?
# create global variable
In [247]: global counter  

# Initialize it to your need
In [248]: counter = 0 

# create your function callback, lambda also could be used
In [252]: def funct(row): 
     ...:     # reference to global variable created before hand 
     ...:     global counter   
     ...:     counter += 1 # increment the counter
     ...:     # return something, or else 
     ...:     # will raise a 'NoneType' has no len() exception
     ...:     return counter

In [260]: np.apply_along_axis(funct, 1, np.array([[0],[0],[0]]))
Out[260]: array([1, 2, 3])

# revert counter to initial state or the counter will keep raising afterward
In [261]: counter = 0 

# or you could just delete it if you had no use for it anymore
In [262]: del counter 
    f = lambda indices: #(whatever you'd like to do)

    output = np.empty(M.shape)
    for i, x in np.ndenumerate(M):
        output(i) = f(i)