numpy:“array_like"的正式定义;对象? [英] numpy: formal definition of "array_like" objects?
问题描述
在 numpy 中,许多对象的构造函数都接受array_like"作为第一个参数.是否有这样的对象的定义,无论是作为抽象元类,还是应该包含方法的文档??
In numpy, the constructors of many objects accept an "array_like" as first argument. Is there a definition of a such object, either as an abstract meta class, or documentation of the methods is should contain??
推荐答案
事实证明,从技术上讲,几乎任何东西都类似于数组.类阵列"更多的是关于如何解释输入的声明,而不是对输入的限制;如果参数被记录为类似数组,NumPy 将尝试将其解释为数组.
It turns out almost anything is technically an array-like. "Array-like" is more of a statement of how the input will be interpreted than a restriction on what the input can be; if a parameter is documented as array-like, NumPy will try to interpret it as an array.
除了近乎重言式的 -- 类数组是任何 Python 对象,np.array
可以转换为 ndarray代码>.要超越这一点,您需要研究 源代码.
There is no formal definition of array-like beyond the nearly tautological one -- an array-like is any Python object that np.array
can convert to an ndarray
. To go beyond this, you'd need to study the source code.
NPY_NO_EXPORT PyObject *
PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
int max_depth, int flags, PyObject *context)
{
/*
* This is the main code to make a NumPy array from a Python
* Object. It is called from many different places.
*/
PyArrayObject *arr = NULL, *ret;
PyArray_Descr *dtype = NULL;
int ndim = 0;
npy_intp dims[NPY_MAXDIMS];
/* Get either the array or its parameters if it isn't an array */
if (PyArray_GetArrayParamsFromObject(op, newtype,
0, &dtype,
&ndim, dims, &arr, context) < 0) {
Py_XDECREF(newtype);
return NULL;
}
...
特别有趣的是PyArray_GetArrayParamsFromObject
,其注释列举了np.array
期望的对象类型:
Particularly interesting is PyArray_GetArrayParamsFromObject
, whose comments enumerate the types of objects np.array
expects:
NPY_NO_EXPORT int
PyArray_GetArrayParamsFromObject(PyObject *op,
PyArray_Descr *requested_dtype,
npy_bool writeable,
PyArray_Descr **out_dtype,
int *out_ndim, npy_intp *out_dims,
PyArrayObject **out_arr, PyObject *context)
{
PyObject *tmp;
/* If op is an array */
/* If op is a NumPy scalar */
/* If op is a Python scalar */
/* If op supports the PEP 3118 buffer interface */
/* If op supports the __array_struct__ or __array_interface__ interface */
/*
* If op supplies the __array__ function.
* The documentation says this should produce a copy, so
* we skip this method if writeable is true, because the intent
* of writeable is to modify the operand.
* XXX: If the implementation is wrong, and/or if actual
* usage requires this behave differently,
* this should be changed!
*/
/* Try to treat op as a list of lists */
/* Anything can be viewed as an object, unless it needs to be writeable */
}
所以通过研究源代码我们可以得出一个类似数组的结论
So by studying the source code we can conclude an array-like is
- 一个 NumPy 数组,或
- a NumPy 标量,或
- 一个 Python 标量,或
- 任何支持 PEP 3118 缓冲区接口的对象,或莉>
- 任何支持
__array_struct__
或__array_interface__
接口,或 - 任何提供
__array__<的对象/code> 函数
,或 - 任何可以被视为列表列表的对象,或
- 什么都行!如果它不属于其他情况之一,它将被视为
object
dtype 的 0 维数组.
- a NumPy array, or
- a NumPy scalar, or
- a Python scalar, or
- any object which supports the PEP 3118 buffer interface, or
- any object that supports the
__array_struct__
or__array_interface__
interface, or - any object that supplies the
__array__
function, or - any object that can be treated as a list of lists, or
- anything! If it doesn't fall under one of the other cases, it'll be treated as a 0-dimensional array of
object
dtype.
这篇关于numpy:“array_like"的正式定义;对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!