为什么Numpy数组中的第二维为空? [英] Why the second dimension in a Numpy array is empty?
问题描述
为什么在这里输出
array = np.arange(3)
array.shape
是
(3,)
而不是
(1,3)
缺少维度是什么意思或等于?
What does the missing dimension means or equals?
推荐答案
如果出现混乱,(3,)
不会表示缺少尺寸。逗号是单个元素元组的标准Python表示法的一部分。形状(1,3),(3,)和(3,1)
是不同的,
In case there's confusion, (3,)
doesn't mean there's a missing dimension. The comma is part of the standard Python notation for a single element tuple. Shapes (1,3), (3,), and (3,1)
are distinct,
它们可以包含相同的3个元素,它们在计算中的使用(广播
)不同,它们的打印格式不同,并且等效列表也不同:
While they can contain the same 3 elements, their use in calculations (broadcasting
) is different, their print format is different, and their list equivalent is different:
In [21]: np.array([1,2,3])
Out[21]: array([1, 2, 3])
In [22]: np.array([1,2,3]).tolist()
Out[22]: [1, 2, 3]
In [23]: np.array([1,2,3]).reshape(1,3).tolist()
Out[23]: [[1, 2, 3]]
In [24]: np.array([1,2,3]).reshape(3,1).tolist()
Out[24]: [[1], [2], [3]]
我们不必停止添加一个单例尺寸:
And we don't have to stop at adding just one singleton dimension:
In [25]: np.array([1,2,3]).reshape(1,3,1).tolist()
Out[25]: [[[1], [2], [3]]]
In [26]: np.array([1,2,3]).reshape(1,3,1,1).tolist()
Out[26]: [[[[1]], [[2]], [[3]]]]
在 numpy
中,数组可以有0、1 2个或更多尺寸。 1维和2维一样逻辑。
In numpy
an array can have 0, 1, 2 or more dimensions. 1 dimension is just as logical as 2.
在MATLAB中,矩阵始终具有2个dim(或更多),但是不必那样。严格来说,MATLAB甚至没有标量。仅当以MATLAB为标准时,形状为(3,)的数组才缺少维。
In MATLAB a matrix always has 2 dim (or more), but it doesn't have to be that way. Strictly speaking MATLAB doesn't even have scalars. An array with shape (3,) is missing a dimension only if MATLAB is taken as the standard.
numpy
是建立在标量和列表(可以嵌套)的Python上的。 Python列表有多少个维度?
numpy
is built on Python which as scalars, and lists (which can nest). How many dimensions does a Python list have?
如果想了解历史,MATLAB是作为Fortran线性代数例程集的前端开发的。考虑到这些问题,这些例程解决了二维矩阵的概念,并且行向量和列向量很有意义。直到版本3为止,在1990年代后期,MATLAB普遍适用于2个以上的维度。
If you want to get into history, MATLAB was developed as a front end to a set of Fortran linear algebra routines. Given the problems those routines solved the concept of matrix with 2 dimensions, and row vs column vectors made sense. It wasn't until version 3.something that MATLAB was generalized to allow more than 2 dimensions (in the late 1990s).
numpy
是基于多次尝试向Python提供数组的尝试(例如数字
)。这些开发人员对数组采取了更通用的方法,其中2d是人为约束。在计算机语言和数学(以及物理学)中,这是优先的。 APL于1960年代开发,最初是作为一种数学符号,然后是一种计算机语言。像 numpy
一样,其数组
可以为0d或更高。 (由于在使用MATLAB之前先使用APL,所以 numpy
的方法感觉很自然。)
numpy
is based on several attempts to provide arrays to Python (e.g. numeric
). Those developers took a more general approach to arrays, one where 2d was an artificial constraint. That has precedence in computer languages and mathematics (and physics). APL was developed in the 1960s, first as a mathematical notation, and then as a computer language. Like numpy
its arrays
can be 0d or higher. (Since I used APL before I used MATLAB, the numpy
approach feels quite natural.)
在 APL
中没有单独的列表或元组。因此,数组
, rho A
的形状本身就是数组,而 rho rho A
是A的维数,也称为 rank
。
In APL
there aren't separate lists or tuples. So the shape of an array
, rho A
is itself an array, and rho rho A
is the number of dimensions of A, also called the rank
.
http://docs.dyalog.com/14.0/Dyalog%20APL%20Idioms.pdf
这篇关于为什么Numpy数组中的第二维为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!