Numpy的array()和asarray()函数有什么区别? [英] What is the difference between Numpy's array() and asarray() functions?

查看:518
本文介绍了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.

推荐答案

由于其他问题已被重定向到询问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:

假设andarray,而mmatrix,并且它们的dtype都是float32:

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)都不会复制,因为mmatrix,它是ndarray的子类.
  • np.array(a, dtype=int, copy=False, subok=True)将同时复制这两个副本,因为dtype不兼容.
  • 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.

大多数其他功能是array周围的薄包装器,可控制何时进行复制:

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

  • asarray :输入将为如果兼容的ndarray(copy=False),则返回未复制的内容.
  • asanyarray :输入将为如果它是兼容的ndarray或类似matrix(copy=Falsesubok=True)的子类,则返回未复制.
  • ascontiguousarray :输入将为如果它是连续的C顺序兼容的ndarray(copy=Falseorder='C').
  • ),则返回未复制
  • asfortranarray :输入将为如果它是连续的Fortran顺序(copy=Falseorder='F')中兼容的ndarray,则返回未复制.
  • require :输入将为如果未与指定的要求字符串兼容,则返回未复制的内容.
  • copy :输入始终为复制.
  • fromiter :输入被处理作为一个可迭代的(因此,例如,您可以从迭代器的元素构造一个数组,而不是使用迭代器的object数组);始终复制.
  • 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.

还有便捷功能,例如 asarray_chkfinite (与asarray相同的复制规则,但如果有任何naninf值,则会引发ValueError),以及子类(如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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆