python - Numpy apply_along_axis 不返回 ndarray 子类

标签 python numpy multidimensional-array

我有一个 ndarray 子类,正确实现了 __array_wrap__np.apply_along_axis 没有返回我的子类的实例,而是 ndarrays。下面的代码复制了我的问题:

import numpy as np

class MySubClass(np.ndarray):

    def __new__(cls, input_array, info=None):
        obj = np.asarray(input_array).view(cls)
        obj.info = info
        return obj

    def __array_finalize__(self, obj):
        if obj is None: return
        self.info = getattr(obj, 'info', None)

    def __array_wrap__(self, out_arr, context=None):
        return np.ndarray.__array_wrap__(self, out_arr, context)

sample_ndarray = np.array([[0,5],[2.1,0]]) 
sample_subclass = MySubClass(sample_ndarray, info="Hi Stack Overflow")

# Find the smallest positive (>0) number along the first axis
min_positive = np.apply_along_axis(lambda x: np.min(np.extract(x>0,x)),
                                   0, sample_subclass)

# No more info
print hasattr(min_positive, 'info')
# Not a subclass
print isinstance(min_positive, MySubClass)
# Is an ndarray
print isinstance(min_positive, np.ndarray)

我能找到的最相关的问题是 this one但那里的共识似乎是 __array_wrap__ 需要实现,我已经完成了。此外,np.extractnp.min 都按预期返回子类,只是在使用 apply_along_axis 时我看到了这种行为。

有什么方法可以让我的代码返回我的子类吗? 我正在使用 numpy 版本 1.11.0

最佳答案

查看 apply_along_axis 代码(通过 Ipython ??)

Type:        function
String form: <function apply_along_axis at 0xb5a73b6c>
File:        /usr/lib/python3/dist-packages/numpy/lib/shape_base.py
Definition:  np.apply_along_axis(func1d, axis, arr, *args, **kwargs)
Source:
def apply_along_axis(func1d, axis, arr, *args, **kwargs):
...
    outarr = zeros(outshape, asarray(res).dtype)
    outarr[tuple(ind)] = res
....
    return outarr

我跳过了很多细节,但基本上它使用了 np.zerosshapedtype,但没有努力调整数组子类。

很多numpy函数将 Action 委托(delegate)给数组的方法,或者使用_wrapit (_wrapit(a, 'take', indices, axis, out, mode) ).

你真的需要使用apply_along_axis吗?这没什么神奇的。您可以在自己的代码中执行相同的迭代,而且速度一样快。

===================

这里有 2 个 apply_along_axis 示例和替代实现。它们对于有意义的计时来说太小了,我相信它们同样快,甚至更快:

In [3]: def my_func(a):
    return (a[0]+a[-1]*0.5)    
In [4]: b=np.arange(1,10).reshape(3,3)

In [5]: np.apply_along_axis(my_func,0,b)
Out[5]: array([ 4.5,  6. ,  7.5])

In [6]: np.apply_along_axis(my_func,1,b)
Out[6]: array([  2.5,   7. ,  11.5])

直接数组实现:

In [8]: b[0,:]+b[-1,:]*0.5
Out[8]: array([ 4.5,  6. ,  7.5])

In [9]: b[:,0]+b[:,-1]*0.5
Out[9]: array([  2.5,   7. ,  11.5])

第二个:

In [10]: c=np.array([[8,1,7],[4,3,9],[5,2,6]])

In [11]: np.apply_along_axis(sorted, 1, c)
Out[11]: 
array([[1, 7, 8],
       [3, 4, 9],
       [2, 5, 6]])

In [12]: d=np.zeros_like(c)
In [13]: for i in range(c.shape[0]):
   ....:     d[i,:] = sorted(c[i,:]) 

In [14]: d
Out[14]: 
array([[1, 7, 8],
       [3, 4, 9],
       [2, 5, 6]])

首先,我完全跳过了迭代;在第二个中,我使用相同的分配和迭代,开销更少。

查看 np.matrixnp.ma 以了解如何实现 ndarray 子类的示例。


np.core.fromnumeric.py 作为 _wrapit 函数,由 np.take 等函数使用:

# functions that are now methods
def _wrapit(obj, method, *args, **kwds):
    try:
        wrap = obj.__array_wrap__
    except AttributeError:
        wrap = None
    result = getattr(asarray(obj), method)(*args, **kwds)
    if wrap:
        if not isinstance(result, mu.ndarray):
            result = asarray(result)
        result = wrap(result)
    return result

所以看起来如果 obj 有一个 __array_wrap__ 方法,它将把它应用到数组结果。因此,您可以将其用作包装 apply_along_axis 的模型,以取回您自己的类。

关于python - Numpy apply_along_axis 不返回 ndarray 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38132483/

相关文章:

python - 为 anaconda 导入 vpython 的问题

python - 直接体积图 3D 阵列

c - 求二维数组中每一列的平均值

c - float arrayName[][] 和 float (* arrayNamePointer)[] 有什么区别

python - 使用 Entrez 解析来自 PubMed 的出版数据的问题

python - 如何从 PNG 图像中删除第 4 个 channel

python - 通过列表 Python 进行搜索和查找

python - 计算矩阵中不同列的数量

python-3.x - 如何计算数据帧列中 >=3 个连续 1 值的出现次数

python - 没有for循环的行,列分配