Numpy的array()和asarray()函数有什么区别?

问题:Numpy的array()和asarray()函数有什么区别?

Numpy array()asarray()函数之间有什么区别?什么时候应该使用一个而不是另一个?他们似乎为我能想到的所有输入生成相同的输出。

What is the difference between Numpy’s array() and asarray() functions? When should you use one rather than the other? They seem to generate identical output for all the inputs I can think of.


回答 0

由于将其他问题重定向到这个询问问题asanyarray其他数组创建例程的问题,因此可能有必要简要概述每个问题的作法。

区别主要在于何时返回不变的输入,而不是将新数组作为副本。

array提供了多种选择(大多数其他功能都是围绕它的薄包装器),包括用于确定何时复制的标志。完整的解释将和文档一样长(请参阅Array Creation,但是简要地,这里有一些示例:

假设andarray,并且mmatrix,并且它们都具有dtypefloat32

  • np.array(a)并且np.array(m)将复制两个,因为这是默认行为。
  • np.array(a, copy=False)并且np.array(m, copy=False)将复制m但不复制a,因为m不是ndarray
  • np.array(a, copy=False, subok=True),并且np.array(m, copy=False, subok=True)不会复制任何内容,因为mmatrix,这是的子类ndarray
  • np.array(a, dtype=int, copy=False, subok=True)将同时复制两者,因为与dtype不兼容。

其他大多数功能都是array在复制发生时围绕该控件的薄包装器:

  • asarray:如果兼容ndarraycopy=False),则输入将返回未复制的状态。
  • asanyarray:如果输入是兼容的ndarray或子类matrix(如copy=Falsesubok=True),则输入将不被复制。
  • ascontiguousarray:如果输入是兼容ndarray的连续C顺序(copy=False,,则将返回未复制的输入order='C')
  • asfortranarray:如果输入与ndarray连续的Fortran顺序(copy=Falseorder='F')兼容,则将返回未复制的输入。
  • require:如果输入与指定的需求字符串兼容,则输入将不复制而返回。
  • copy:总是复制输入。
  • fromiter:输入被视为可迭代的(例如,您可以从迭代器的元素构造数组,而不是object使用迭代器的数组);始终复制。

还有一些便利功能,例如asarray_chkfinite(与复制规则相同asarray,但复制规则与相同,但是ValueError如果有naninf值,则会提高),以及子类的构造函数(例如matrix或特殊情况下的记录数组),当然还有实际的ndarray构造函数(可让您直接创建数组)超出缓冲区)。

Since other questions are being redirected to this one which ask about asanyarray or other array creation routines, it’s probably worth having a brief summary of what each of them does.

The differences are mainly about when to return the input unchanged, as opposed to making a new array as a copy.

array offers a wide variety of options (most of the other functions are thin wrappers around it), including flags to determine when to copy. A full explanation would take just as long as the docs (see Array Creation, but briefly, here are some examples:

Assume a is an ndarray, and m is a matrix, and they both have a dtype of float32:

  • np.array(a) and np.array(m) will copy both, because that’s the default behavior.
  • np.array(a, copy=False) and np.array(m, copy=False) will copy m but not a, because m is not an ndarray.
  • np.array(a, copy=False, subok=True) and np.array(m, copy=False, subok=True) will copy neither, because m is a matrix, which is a subclass of ndarray.
  • np.array(a, dtype=int, copy=False, subok=True) will copy both, because the dtype is not compatible.

Most of the other functions are thin wrappers around array that control when copying happens:

  • asarray: The input will be returned uncopied iff it’s a compatible ndarray (copy=False).
  • asanyarray: The input will be returned uncopied iff it’s a compatible ndarray or subclass like matrix (copy=False, subok=True).
  • ascontiguousarray: The input will be returned uncopied iff it’s a compatible ndarray in contiguous C order (copy=False, order='C').
  • asfortranarray: The input will be returned uncopied iff it’s a compatible ndarray in contiguous Fortran order (copy=False, order='F').
  • require: The input will be returned uncopied iff it’s compatible with the specified requirements string.
  • copy: The input is always copied.
  • fromiter: The input is treated as an iterable (so, e.g., you can construct an array from an iterator’s elements, instead of an object array with the iterator); always copied.

There are also convenience functions, like asarray_chkfinite (same copying rules as asarray, but raises ValueError if there are any nan or inf values), and constructors for subclasses like matrix or for special cases like record arrays, and of course the actual ndarray constructor (which lets you create an array directly out of strides over a buffer).


回答 1

定义asarray是:

def asarray(a, dtype=None, order=None):
    return array(a, dtype, copy=False, order=order)

就像array,除了它的选项更少,和copy=Falsearraycopy=True默认。

主要区别在于array(默认情况下)将复制对象,而asarray除非有必要,否则不会复制。

The definition of asarray is:

def asarray(a, dtype=None, order=None):
    return array(a, dtype, copy=False, order=order)

So it is like array, except it has fewer options, and copy=False. array has copy=True by default.

The main difference is that array (by default) will make a copy of the object, while asarray will not unless necessary.


回答 2

可以通过以下示例证明差异:

  1. 产生矩阵

    >>> A = numpy.matrix(numpy.ones((3,3)))
    >>> A
    matrix([[ 1.,  1.,  1.],
            [ 1.,  1.,  1.],
            [ 1.,  1.,  1.]])
  2. 用于numpy.array修改A。不起作用,因为您正在修改副本

    >>> numpy.array(A)[2]=2
    >>> A
    matrix([[ 1.,  1.,  1.],
            [ 1.,  1.,  1.],
            [ 1.,  1.,  1.]])
  3. 用于numpy.asarray修改A。之所以有效,是因为您正在修改A自己

    >>> numpy.asarray(A)[2]=2
    >>> A
    matrix([[ 1.,  1.,  1.],
            [ 1.,  1.,  1.],
            [ 2.,  2.,  2.]])

希望这可以帮助!

The difference can be demonstrated by this example:

  1. generate a matrix

    >>> A = numpy.matrix(numpy.ones((3,3)))
    >>> A
    matrix([[ 1.,  1.,  1.],
            [ 1.,  1.,  1.],
            [ 1.,  1.,  1.]])
    
  2. use numpy.array to modify A. Doesn’t work because you are modifying a copy

    >>> numpy.array(A)[2]=2
    >>> A
    matrix([[ 1.,  1.,  1.],
            [ 1.,  1.,  1.],
            [ 1.,  1.,  1.]])
    
  3. use numpy.asarray to modify A. It worked because you are modifying A itself

    >>> numpy.asarray(A)[2]=2
    >>> A
    matrix([[ 1.,  1.,  1.],
            [ 1.,  1.,  1.],
            [ 2.,  2.,  2.]])
    

Hope this helps!


回答 3

array和的文档中非常清楚地提到了差异asarray。不同之处在于参数列表,因此取决于这些参数的功能作用。

函数定义为:

numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)

numpy.asarray(a, dtype=None, order=None)

以下参数是可能传递给文档的参数,array不是 asarray文档中提到的参数:

copy:bool,可选如果为true(默认),则复制对象。否则,仅当__array__返回一个副本,obj是一个嵌套序列或需要一个副本以满足其他任何要求(dtype,order等)时,才创建副本。

subok:bool,可选如果为True,则子类将被传递,否则返回的数组将被强制为基类数组(默认)。

ndmin:int,可选指定结果数组应具有的最小维数。可以根据需要预先添加形状。

The differences are mentioned quite clearly in the documentation of array and asarray. The differences lie in the argument list and hence the action of the function depending on those parameters.

The function definitions are :

numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)

and

numpy.asarray(a, dtype=None, order=None)

The following arguments are those that may be passed to array and not asarray as mentioned in the documentation :

copy : bool, optional If true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements (dtype, order, etc.).

subok : bool, optional If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).

ndmin : int, optional Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement.


回答 4

这是一个可以证明差异的简单示例。

主要区别在于数组将复制原始数据,并且使用不同的对象,我们可以修改原始数组中的数据。

import numpy as np
a = np.arange(0.0, 10.2, 0.12)
int_cvr = np.asarray(a, dtype = np.int64)

数组(a)中的内容保持不变,但仍然可以使用另一个对象对数据执行任何操作,而无需修改原始数组中的内容。

Here’s a simple example that can demonstrate the difference.

The main difference is that array will make a copy of the original data and using different object we can modify the data in the original array.

import numpy as np
a = np.arange(0.0, 10.2, 0.12)
int_cvr = np.asarray(a, dtype = np.int64)

The contents in array (a), remain untouched, and still, we can perform any operation on the data using another object without modifying the content in original array.


回答 5

asarray(x) 就好像 array(x, copy=False)

asarray(x)当您要确保x在执行任何其他操作之前将其设为数组时使用。如果x已经是数组,则不会进行任何复制。这不会造成冗余的性能损失。

这是确保x首先转换为数组的函数示例。

def mysum(x):
    return np.asarray(x).sum()

asarray(x) is like array(x, copy=False)

Use asarray(x) when you want to ensure that x will be an array before any other operations are done. If x is already an array then no copy would be done. It would not cause a redundant performance hit.

Here is an example of a function that ensure x is converted into an array first.

def mysum(x):
    return np.asarray(x).sum()