我是 Python 世界的新手,所以请原谅我的愚蠢问题。
在许多情况下,我实现了一个适用于类似数组的数字输入的函数,使用 NumPy 实用程序对序列进行基本操作通常是有利的。为此,我会写这样的东西:
import numpy as np
def f(x):
if not isinstance(x, np.ndarray):
x = np.asarray(x)
# and from now on we know that x is a NumPy array, with all standard methods
(请注意,我不想依赖调用者始终传递 NumPy 数组。)
我想知道如果通过删除 if
来简化代码,额外的开销会是多少?即,有类似的东西
def f(x):
x = np.asarray(x)
# and from now on we know that x is a NumPy array, with all standard methods
基本上,两种情况的区别在于第二个代码更紧凑,但会不必要地调用 np.asarray
,即使 x
已经是一个 NumPy 数组。
最佳答案
简短回答:因为您正在使用 isinstance()
检查,您可以使用 numpy.asanyarray()
它将通过任何 ndarray
及其子类没有开销。
根据 numpy.asarray() 的文档,当输入已经是 ndarray
类型时,当输入已经是数组时没有开销:没有复制发生,它们“通过”。虽然,值得注意的是 ndarray
的子类没有通过。
由于在您的原始代码中您使用的是 isinstance(x, numpy.ndarray)
,因此您很可能需要 numpy.asanyarray()
它也通过 ndarray
的子类,这对于您的用例来说会更有效。 (因为 isinstance()
也为子类返回 true)
Returns: out : ndarray Array interpretation of a. No copy is performed if the input is already an ndarray with matching dtype and order. If a is a subclass of ndarray, a base class ndarray is returned.
文档中的这个示例(加上我自己的评论)解释了差异以及为什么 asanyarray()
更适合您的用例:
>>> issubclass(np.recarray, np.ndarray)
True # This is to show that recarray is a subclass of ndarray
>>> a = np.array([(1.0, 2), (3.0, 4)], dtype='f4,i4').view(np.recarray)
>>> np.asarray(a) is a
False # Here a copy happens which is an overhead you do not want,
# because the input type recarray is only a subclass of ndarray
>>> np.asanyarray(a) is a
True # Here no copying happens, your subclass of ndarray passes through.
关于python - 在 NumPy 数组上调用 `np.asarray' 是否有很大的开销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56805126/