Numpy沿轴线应用并得到行索引

时间:2017-03-01 20:21:22

标签: python numpy

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

x = np.array([[0, 1, 2],
          [1, 2, 3],
          [2, 3, 4],
          [3, 4, 5]]
        )

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

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.apply_along_axis函数的一些方法,以便我可以访问行索引(对于正在处理的行),然后能够使用此函数处理每一行:< / p>

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

3 个答案:

答案 0 :(得分:0)

对于轴= 1的二维数组,import urllib import os import csv cwd = os.getcwd() myFolder=cwd+"\In" url="http://biznes.pap.pl/pl/reports/espi/all,0,0,0,1" page = urllib.request.urlopen(url).read() with open('listing.csv', 'w') as f: writer = csv.writer(f) for tr in page.find_all('tr'): tds = tr.find_all('td') row = [str(elem.text.encode('utf-8'))[1:] for elem in tds[:12]] writer.writerow(row) print("Creating listing.csv file... ") print("Done... ") 与数组行的迭代相同

apply_along_axis

对于axis = 0,我们可以迭代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]]) 。当数组为3d时,x.T更有用,我们希望迭代除一个之外的所有维度。然后它节省了一些单调乏味。但它不是速度解决方案。

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

enumerate

或使用简单的范围迭代:

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

答案 1 :(得分:0)

这是另一种解决方案,同时等待真正的功能得到实施。 这有点不整洁。但也许足以解决你的问题。 :)

# 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 

希望,可以为你提供一些帮助:)

答案 2 :(得分:0)

我在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)