Python numpy:“array_like”对象的正式定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40378427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:27:56  来源:igfitidea点击:

numpy: formal definition of "array_like" objects?

pythonnumpy

提问by blue_note

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 中,许多对象的构造函数都接受“array_like”作为第一个参数。是否有这样的对象的定义,无论是作为抽象元类,还是应该包含方法的文档?

采纳答案by unutbu

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.

事实证明,从技术上讲,几乎任何东西都类似于数组。“类数组”更多地是对如何解释输入的声明,而不是对输入内容的限制;如果参数被记录为类似数组,NumPy 将尝试将其解释为数组。

There is no formal definition of array-like beyond the nearly tautological one-- an array-like is any Python object that np.arraycan convert to an ndarray. To go beyond this, you'd need to study the source code.

除了几乎同义反复的定义之外没有正式的类数组定义——类数组是任何np.array可以转换为ndarray. 要超越这一点,您需要研究源代码

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;
    }
    ...

Particularly interesting is PyArray_GetArrayParamsFromObject, whose comments enumerate the types of objects np.arrayexpects:

特别有趣的是PyArray_GetArrayParamsFromObject,它的注释列举了np.array期望的对象类型:

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

所以通过研究源代码,我们可以得出一个类似数组的结论

回答by Moinuddin Quadri

The term "array-like"is used in NumPy, referring to anything that can be passed as first parameter to numpy.array()to create an array ().

NumPy 中使用了术语“类数组”,指的是任何可以作为第一个参数传递给以numpy.array()创建数组 () 的东西。

As per the Numpy document:

根据Numpy 文档

In general, numerical data arranged in an array-like structure in Python can be converted to arrays through the use of the array() function. The most obvious examples are lists and tuples. See the documentation for array() for details for its use. Some objects may support the array-protocol and allow conversion to arrays this way. A simple way to find out if the object can be converted to a numpy array using array() is simply to try it interactively and see if it works! (The Python Way).

一般情况下,Python 中以类数组结构排列的数值数据可以通过使用 array() 函数转换为数组。最明显的例子是列表和元组。有关使用的详细信息,请参阅 array() 的文档。某些对象可能支持数组协议并允许以这种方式转换为数组。确定对象是否可以使用 array() 转换为 numpy 数组的一种简单方法就是以交互方式尝试它并查看它是否有效!(Python 方式)。

For more information, read:

有关更多信息,请阅读:

回答by YaOzI

It's just a concept, and there is an official statement (in Numpy Glossary)about it besides the explanation in User Guide partmentioned in other answers:

这只是一个概念,除了其他答案中提到的用户指南部分的解释外,还有一个官方声明(在 Numpy Glossary 中)

array_like

Any sequence that can be interpreted as an ndarray. This includes nested lists, tuples, scalars and existing arrays.

array_like

任何可以解释为 ndarray 的序列。这包括嵌套列表、元组、标量和现有数组。

so even scalars can be taken into account, just like np.array(1024).

所以甚至可以考虑标量,就像np.array(1024).