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

查看:28
本文介绍了Numpy 的 array() 和 asarray() 函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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) 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=False, subok=True).
  • ascontiguousarray:如果输入是连续 C 顺序中的兼容 ndarray (copy=False, order='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/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屋!

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