Numpy 的 array() 和 asarray() 函数有什么区别? [英] What is the difference between Numpy's array() and asarray() functions?
问题描述
Numpy的array()<有什么区别/code>
和 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.
推荐答案
因为其他问题被重定向到这个询问 asanyarray
或 其他数组创建例程,可能值得简要总结一下每个例程的作用.
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
提供了多种选项(大多数其他功能都是围绕它的薄包装),包括用于确定何时复制的标志.完整的解释与文档一样长(参见 数组创建,但简单地说,这里有一些例子:
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:
假设a
是一个ndarray
,而m
是一个矩阵
,它们都有一个float32
的 >dtype:
Assume a
is an ndarray
, and m
is a matrix
, and they both have a dtype
of float32
:
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)
不会复制,因为m
是一个matrix
,它是ndarray
的子类.np.array(a, dtype=int, copy=False, subok=True)
将复制两者,因为dtype
不兼容.
np.array(a)
andnp.array(m)
will copy both, because that's the default behavior.np.array(a, copy=False)
andnp.array(m, copy=False)
will copym
but nota
, becausem
is not anndarray
.np.array(a, copy=False, subok=True)
andnp.array(m, copy=False, subok=True)
will copy neither, becausem
is amatrix
, which is a subclass ofndarray
.np.array(a, dtype=int, copy=False, subok=True)
will copy both, because thedtype
is not compatible.
大多数其他函数都是围绕 array
的薄包装器,用于控制复制发生的时间:
Most of the other functions are thin wrappers around array
that control when copying happens:
asarray
:如果输入是兼容的ndarray
(copy=False
),则输入将被未复制地返回.asanyarray
:如果输入是兼容的ndarray
或子类,如matrix
(copy=False
,subok=True
).ascontiguousarray
:如果输入是连续 C 顺序中的兼容ndarray
(copy=False
,order='C')
,则输入将返回未复制.asfortranarray
:如果输入是连续的 Fortran 顺序(copy=False
、order='F'
)中的兼容ndarray
,则输入将被未复制地返回.require
:如果输入与指定的要求字符串兼容,则将返回未复制的输入.copy
:输入总是被复制.fromiter
:输入被视为可迭代的(例如,您可以从迭代器的元素构造一个数组,而不是带有迭代器的object
数组);总是复制.
asarray
: The input will be returned uncopied iff it's a compatiblendarray
(copy=False
).asanyarray
: The input will be returned uncopied iff it's a compatiblendarray
or subclass likematrix
(copy=False
,subok=True
).ascontiguousarray
: The input will be returned uncopied iff it's a compatiblendarray
in contiguous C order (copy=False
,order='C')
.asfortranarray
: The input will be returned uncopied iff it's a compatiblendarray
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 anobject
array with the iterator); always copied.
还有一些方便的函数,比如asarray_chkfinite
(与asarray
相同的复制规则,但如果有任何nan
或inf<,则引发
ValueError
/code> 值),以及子类(如 matrix
)或特殊情况(如记录数组)的构造函数,当然还有实际的 ndarray
构造函数(它允许您直接跨缓冲区创建数组).
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).
这篇关于Numpy 的 array() 和 asarray() 函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!