numpy ndarrays:行和列操作。

时间:2022-07-02 21:42:38

If I wanted to apply a function row-wise (or column-wise) to an ndarray, do I look to ufuncs (doesn't seem like it) or some type of array broadcasting (not what I'm looking for either?) ?

如果我想在ndarray中应用一个函数行(或列式),那么我是否注意到ufuncs(看起来不像它)或者某种类型的数组广播(不是我想要的?)

Edit

编辑

I am looking for something like R's apply function. For instance,

我在寻找像R的应用函数。例如,

apply(X,1,function(x) x*2)

would multiply 2 to each row of X through an anonymously defined function, but could also be a named function. (This is of course a silly, contrived example in which apply is not actually needed). There is no generic way to apply a function across an NumPy array's "axis", ?

通过一个匿名定义的函数将2乘以X的每一行,但也可以是一个命名函数。(当然,这是一个愚蠢的、做作的例子,实际上并不需要应用。)在NumPy数组的“axis”中,没有通用的方法来应用函数。

1 个解决方案

#1


13  

First off, many numpy functions take an axis argument. It's probably possible (and better) to do what you want with that sort of approach.

首先,许多numpy函数采用axis参数。用这种方法做你想做的事情可能(而且更好)。

However, a generic "apply this function row-wise" approach would look something like this:

然而,一个通用的“应用这个函数行行”的方法看起来是这样的:

import numpy as np

def rowwise(func):
    def new_func(array2d, **kwargs):
        # Run the function once to determine the size of the output
        val = func(array2d[0], **kwargs)
        output_array = np.zeros((array2d.shape[0], val.size), dtype=val.dtype)
        output_array[0] = val
        for i,row in enumerate(array2d[1:], start=1):
            output_array[i] = func(row, **kwargs)
        return output_array
    return new_func

@rowwise
def test(data):
    return np.cumsum(data)

x = np.arange(20).reshape((4,5))
print test(x)

Keep in mind that we can do exactly the same thing with just:

请记住,我们可以做同样的事情:

np.cumsum(x, axis=1)

There's often a better way that the generic approach, especially with numpy.

通常会有更好的方法来使用通用方法,特别是在numpy中。

Edit:

编辑:

I completely forgot about it, but the above is essentially equivalent to numpy.apply_along_axis.

我完全忘记了,但上面的内容基本上相当于numpy.apply_along_axis。

So, we could re-write that as:

我们可以把它改写为:

import numpy as np

def test(row):
    return np.cumsum(row)

x = np.arange(20).reshape((4,5))
print np.apply_along_axis(test, 1, x)

#1


13  

First off, many numpy functions take an axis argument. It's probably possible (and better) to do what you want with that sort of approach.

首先,许多numpy函数采用axis参数。用这种方法做你想做的事情可能(而且更好)。

However, a generic "apply this function row-wise" approach would look something like this:

然而,一个通用的“应用这个函数行行”的方法看起来是这样的:

import numpy as np

def rowwise(func):
    def new_func(array2d, **kwargs):
        # Run the function once to determine the size of the output
        val = func(array2d[0], **kwargs)
        output_array = np.zeros((array2d.shape[0], val.size), dtype=val.dtype)
        output_array[0] = val
        for i,row in enumerate(array2d[1:], start=1):
            output_array[i] = func(row, **kwargs)
        return output_array
    return new_func

@rowwise
def test(data):
    return np.cumsum(data)

x = np.arange(20).reshape((4,5))
print test(x)

Keep in mind that we can do exactly the same thing with just:

请记住,我们可以做同样的事情:

np.cumsum(x, axis=1)

There's often a better way that the generic approach, especially with numpy.

通常会有更好的方法来使用通用方法,特别是在numpy中。

Edit:

编辑:

I completely forgot about it, but the above is essentially equivalent to numpy.apply_along_axis.

我完全忘记了,但上面的内容基本上相当于numpy.apply_along_axis。

So, we could re-write that as:

我们可以把它改写为:

import numpy as np

def test(row):
    return np.cumsum(row)

x = np.arange(20).reshape((4,5))
print np.apply_along_axis(test, 1, x)