为什么numpy对许多ndarray方法都有相应的功能?

时间:2022-11-14 23:59:58

A few examples:

几个例子:

numpy.sum()
ndarray.sum()
numpy.amax()
ndarray.max()
numpy.dot()
ndarray.dot()

... and quite a few more. Is it to support some legacy code, or is there a better reason for that? And, do I choose only on the basis of how my code 'looks', or is one of the two ways better than the other?

......还有更多。它是支持一些遗留代码,还是有更好的理由?而且,我是根据我的代码“看起来”的方式选择的,还是两种方式中的一种比另一种更好?

I can imagine that one might want numpy.dot() to use reduce (e.g., reduce(numpy.dot, A, B, C, D)) but I don't think that would be as useful for something like numpy.sum().

我可以想象一个人可能希望numpy.dot()使用reduce(例如,reduce(numpy.dot,A,B,C,D))但我不认为这对numpy.sum这样的东西有用。 ()。

3 个解决方案

#1


7  

As others have noted, the identically-named NumPy functions and array methods are often equivalent (they end up calling the same underlying code). One might be preferred over the other if it makes for easier reading.

正如其他人所指出的那样,同名的NumPy函数和数组方法通常是等价的(它们最终会调用相同的底层代码)。如果它更容易阅读,那么一个可能比另一个更受欢迎。

However, in some instances the two behave different slightly differently. In particular, using the ndarray method sometimes emphasises the fact that the method is modifying the array in-place.

但是,在某些情况下,两者的行为略有不同。特别是,使用ndarray方法有时会强调该方法正在修改数组的事实。

For example, np.resize returns a new array with the specified shape. On the other hand, ndarray.resize changes the shape of the array in-place. The fill values used in each case are also different.

例如,np.resize返回具有指定形状的新数组。另一方面,ndarray.resize就地更改了数组的形状。每种情况下使用的填充值也不同。

Similarly, a.sort() sorts the array a in-place, while np.sort(a) returns a sorted copy.

类似地,a.sort()就地对数组进行排序,而np.sort(a)返回一个已排序的副本。

#2


4  

In most cases the method is the basic compiled version. The function uses that method when available, but also has some sort of backup when the argument(s) is not an array. It helps to look at the code and/or docs of the function or method.

在大多数情况下,该方法是基本的编译版本。该函数在可用时使用该方法,但在参数不是数组时也具有某种备份。它有助于查看函数或方法的代码和/或文档。

For example if in Ipython I ask to look at the code for the sum method, I see that it is compiled code

例如,如果在Ipython中我要求查看sum方法的代码,我看到它是编译代码

In [711]: x.sum??
Type:        builtin_function_or_method
String form: <built-in method sum of numpy.ndarray object at 0xac1bce0>
...
Refer to `numpy.sum` for full documentation.

Do the same on np.sum I get many lines of documentation plus some Python code:

在np.sum上做同样的事情我得到了很多文档和一些Python代码:

   if isinstance(a, _gentype):
        res = _sum_(a)
        if out is not None:
            out[...] = res
            return out
        return res
    elif type(a) is not mu.ndarray:
        try:
            sum = a.sum
        except AttributeError:
            return _methods._sum(a, axis=axis, dtype=dtype,
                                out=out, keepdims=keepdims)
        # NOTE: Dropping the keepdims parameters here...
        return sum(axis=axis, dtype=dtype, out=out)
    else:
        return _methods._sum(a, axis=axis, dtype=dtype,
                            out=out, keepdims=keepdims)

If I call np.sum(x) where x is an array, it ends up calling x.sum():

如果我调用np.sum(x),其中x是一个数组,它最终调用x.sum():

    sum = a.sum
    return sum(axis=axis, dtype=dtype, out=out)

np.amax similar (but simpler). Note that the np. form can handle a an object that isn't an array (that doesn't have the method), e.g. a list: np.amax([1,2,3]).

np.amax相似(但更简单)。注意到np。 form可以处理一个不是数组的对象(没有该方法),例如列表:np.amax([1,2,3])。

np.dot and x.dot both show as 'built-in' function, so we can't say anything about priority. They probably both end up calling some underlying C function.

np.dot和x.dot都显示为'内置'功能,所以我们不能说优先级。他们可能最终都会调用一些底层的C函数。

np.reshape is another that deligates if possible:

如果可能的话,np.reshape是另一个分离的:

try:
    reshape = a.reshape
except AttributeError:
    return _wrapit(a, 'reshape', newshape, order=order)
return reshape(newshape, order=order)

So np.reshape(x,(2,3)) is identical in functionality to x.reshape((2,3)). But the _wrapit expression enables np.reshape([1,2,3,4],(2,2)).

所以np.reshape(x,(2,3))在功能上与x.reshape((2,3))相同。但是_wrapit表达式启用了np.reshape([1,2,3,4],(2,2))。

np.sort returns a copy by doing an inplace sort on a copy:

np.sort通过对副本执行就地排序来返回副本:

a = asanyarray(a).copy()
a.sort(axis, kind, order)
return a

x.resize is built-in, while np.resize ends up doing a np.concatenate and reshape.

x.resize是内置的,而np.resize最终执行np.concatenate和reshape。

If your array is a subclass, like matrix or masked, it may have its own variant. The action of a matrix .sum is:

如果您的数组是子类,如矩阵或蒙版,它可能有自己的变体。矩阵.sum的动作是:

return N.ndarray.sum(self, axis, dtype, out, keepdims=True)._collapse(axis)

#3


0  

Elaborating on Peter's comment for visibility:

阐述彼得对可见度的评论:

We could make it more consistent by removing methods from ndarray and sticking to just functions. But this is impossible because it would break everyone's existing code that uses methods.

我们可以通过从ndarray中删除方法并坚持使用函数来使其更加一致。但这是不可能的,因为它会打破每个人使用方法的现有代码。

Or, we could move all functions to also be methods. But this is impossible because new users and packages are constantly defining new functions. Plus continuing to multiply these duplicate methods violates "there should be one obvious way to do it".

或者,我们可以将所有函数也移动到方法中。但这是不可能的,因为新用户和软件包不断定义新功能。加上继续增加这些重复方法违反了“应该有一种明显的方法来实现它”。

If we could go back in time then I'd probably argue for not having these methods on ndarray at all, and using functions exclusively. ... So this all argues for using functions exclusively

如果我们可以回到过去,那么我可能会争辩说根本没有在ndarray上使用这些方法,并且只使用函数。 ...所以这一切都主张只使用函数

numpy issue: More consistency with array-methods #7452

numpy问题:与数组方法#7452更加一致

#1


7  

As others have noted, the identically-named NumPy functions and array methods are often equivalent (they end up calling the same underlying code). One might be preferred over the other if it makes for easier reading.

正如其他人所指出的那样,同名的NumPy函数和数组方法通常是等价的(它们最终会调用相同的底层代码)。如果它更容易阅读,那么一个可能比另一个更受欢迎。

However, in some instances the two behave different slightly differently. In particular, using the ndarray method sometimes emphasises the fact that the method is modifying the array in-place.

但是,在某些情况下,两者的行为略有不同。特别是,使用ndarray方法有时会强调该方法正在修改数组的事实。

For example, np.resize returns a new array with the specified shape. On the other hand, ndarray.resize changes the shape of the array in-place. The fill values used in each case are also different.

例如,np.resize返回具有指定形状的新数组。另一方面,ndarray.resize就地更改了数组的形状。每种情况下使用的填充值也不同。

Similarly, a.sort() sorts the array a in-place, while np.sort(a) returns a sorted copy.

类似地,a.sort()就地对数组进行排序,而np.sort(a)返回一个已排序的副本。

#2


4  

In most cases the method is the basic compiled version. The function uses that method when available, but also has some sort of backup when the argument(s) is not an array. It helps to look at the code and/or docs of the function or method.

在大多数情况下,该方法是基本的编译版本。该函数在可用时使用该方法,但在参数不是数组时也具有某种备份。它有助于查看函数或方法的代码和/或文档。

For example if in Ipython I ask to look at the code for the sum method, I see that it is compiled code

例如,如果在Ipython中我要求查看sum方法的代码,我看到它是编译代码

In [711]: x.sum??
Type:        builtin_function_or_method
String form: <built-in method sum of numpy.ndarray object at 0xac1bce0>
...
Refer to `numpy.sum` for full documentation.

Do the same on np.sum I get many lines of documentation plus some Python code:

在np.sum上做同样的事情我得到了很多文档和一些Python代码:

   if isinstance(a, _gentype):
        res = _sum_(a)
        if out is not None:
            out[...] = res
            return out
        return res
    elif type(a) is not mu.ndarray:
        try:
            sum = a.sum
        except AttributeError:
            return _methods._sum(a, axis=axis, dtype=dtype,
                                out=out, keepdims=keepdims)
        # NOTE: Dropping the keepdims parameters here...
        return sum(axis=axis, dtype=dtype, out=out)
    else:
        return _methods._sum(a, axis=axis, dtype=dtype,
                            out=out, keepdims=keepdims)

If I call np.sum(x) where x is an array, it ends up calling x.sum():

如果我调用np.sum(x),其中x是一个数组,它最终调用x.sum():

    sum = a.sum
    return sum(axis=axis, dtype=dtype, out=out)

np.amax similar (but simpler). Note that the np. form can handle a an object that isn't an array (that doesn't have the method), e.g. a list: np.amax([1,2,3]).

np.amax相似(但更简单)。注意到np。 form可以处理一个不是数组的对象(没有该方法),例如列表:np.amax([1,2,3])。

np.dot and x.dot both show as 'built-in' function, so we can't say anything about priority. They probably both end up calling some underlying C function.

np.dot和x.dot都显示为'内置'功能,所以我们不能说优先级。他们可能最终都会调用一些底层的C函数。

np.reshape is another that deligates if possible:

如果可能的话,np.reshape是另一个分离的:

try:
    reshape = a.reshape
except AttributeError:
    return _wrapit(a, 'reshape', newshape, order=order)
return reshape(newshape, order=order)

So np.reshape(x,(2,3)) is identical in functionality to x.reshape((2,3)). But the _wrapit expression enables np.reshape([1,2,3,4],(2,2)).

所以np.reshape(x,(2,3))在功能上与x.reshape((2,3))相同。但是_wrapit表达式启用了np.reshape([1,2,3,4],(2,2))。

np.sort returns a copy by doing an inplace sort on a copy:

np.sort通过对副本执行就地排序来返回副本:

a = asanyarray(a).copy()
a.sort(axis, kind, order)
return a

x.resize is built-in, while np.resize ends up doing a np.concatenate and reshape.

x.resize是内置的,而np.resize最终执行np.concatenate和reshape。

If your array is a subclass, like matrix or masked, it may have its own variant. The action of a matrix .sum is:

如果您的数组是子类,如矩阵或蒙版,它可能有自己的变体。矩阵.sum的动作是:

return N.ndarray.sum(self, axis, dtype, out, keepdims=True)._collapse(axis)

#3


0  

Elaborating on Peter's comment for visibility:

阐述彼得对可见度的评论:

We could make it more consistent by removing methods from ndarray and sticking to just functions. But this is impossible because it would break everyone's existing code that uses methods.

我们可以通过从ndarray中删除方法并坚持使用函数来使其更加一致。但这是不可能的,因为它会打破每个人使用方法的现有代码。

Or, we could move all functions to also be methods. But this is impossible because new users and packages are constantly defining new functions. Plus continuing to multiply these duplicate methods violates "there should be one obvious way to do it".

或者,我们可以将所有函数也移动到方法中。但这是不可能的,因为新用户和软件包不断定义新功能。加上继续增加这些重复方法违反了“应该有一种明显的方法来实现它”。

If we could go back in time then I'd probably argue for not having these methods on ndarray at all, and using functions exclusively. ... So this all argues for using functions exclusively

如果我们可以回到过去,那么我可能会争辩说根本没有在ndarray上使用这些方法,并且只使用函数。 ...所以这一切都主张只使用函数

numpy issue: More consistency with array-methods #7452

numpy问题:与数组方法#7452更加一致