非常基本的 Numpy 数组维度可视化 [英] Very Basic Numpy array dimension visualization
问题描述
我是 numpy 的初学者,没有矩阵方面的经验.我了解基本的 1d 和 2d 数组,但我无法可视化如下所示的 3d numpy 数组.以下python列表如何形成具有高度、长度和宽度的3d数组?哪些是行和列?
b = np.array([[[1, 2, 3],[4, 5, 6]],[[7, 8, 9],[10, 11, 12]]])
NumPy 中 ndarray
的解剖结构如下图所示:(来源:
一旦您离开 2D 空间并进入 3D 或更高维度的空间,行和列的概念就不再有意义了.但是您仍然可以直观地理解 3D 数组.例如,考虑您的示例:
在[41]中:b出[41]:数组([[[ 1, 2, 3],[ 4, 5, 6]],[[ 7, 8, 9],[10, 11, 12]]])在 [42] 中:b.shape输出 [42]: (2, 2, 3)
这里b
的形状是(2, 2, 3)
.你可以这样想,我们有两个 (2x3)
矩阵堆叠形成一个 3D 数组.要访问您索引到数组 b
中的第一个矩阵,如 b[0]
并访问第二个矩阵,您索引到数组 b
像b[1]
.
# 为您提供位置0"处的二维数组(即矩阵)在 [43] 中:b[0]出[43]:数组([[1, 2, 3],[4, 5, 6]])# 为您提供位置 1 处的二维数组(即矩阵)在 [44] 中:b[1]出[44]:数组([[ 7, 8, 9],[10, 11, 12]])
但是,如果您进入 4D 或更高的空间,将很难从数组本身中获得任何意义,因为我们人类很难可视化 4D 和更多维度.因此,人们宁愿只考虑 ndarray.shape
属性并使用它.
有关我们如何使用(嵌套)列表构建高维数组的更多信息:
对于一维数组,数组构造器需要一个序列(tuple, list
等),但通常使用list
.
在[51]中:oneD = np.array([1, 2, 3,])在 [52]: oneD.shape出[52]:(3,)
对于二维数组,它是列表列表
,但也可以是列表元组
或元组元组
等:
在 [53]: twoD = np.array([[1, 2, 3], [4, 5, 6]])在 [54]: twoD.shape输出[54]:(2, 3)
对于 3D 数组,它是 list of lists 的 list
:
在[55]中:threeD = np.array([[[1, 2, 3], [2, 3, 4]], [[5, 6, 7], [6, 7,8]]])在 [56]:threeD.shape输出 [56]: (2, 2, 3)
<小时>
P.S. 在内部,ndarray
存储在内存块中,如下图所示.(来源:深思)
I'm a beginner to numpy with no experience in matrices. I understand basic 1d and 2d arrays but I'm having trouble visualizing a 3d numpy array like the one below. How do the following python lists form a 3d array with height, length and width? Which are the rows and columns?
b = np.array([[[1, 2, 3],[4, 5, 6]],
[[7, 8, 9],[10, 11, 12]]])
The anatomy of an ndarray
in NumPy looks like this red cube below: (source: Physics Dept, Cornell Uni)
Once you leave the 2D space and enter 3D or higher dimensional spaces, the concept of rows and columns doesn't make much sense anymore. But still you can intuitively understand 3D arrays. For instance, considering your example:
In [41]: b
Out[41]:
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
In [42]: b.shape
Out[42]: (2, 2, 3)
Here the shape of b
is (2, 2, 3)
. You can think about it like, we've two (2x3)
matrices stacked to form a 3D array. To access the first matrix you index into the array b
like b[0]
and to access the second matrix, you index into the array b
like b[1]
.
# gives you the 2D array (i.e. matrix) at position `0`
In [43]: b[0]
Out[43]:
array([[1, 2, 3],
[4, 5, 6]])
# gives you the 2D array (i.e. matrix) at position 1
In [44]: b[1]
Out[44]:
array([[ 7, 8, 9],
[10, 11, 12]])
However, if you enter 4D space or higher, it will be very hard to make any sense out of the arrays itself since we humans have hard time visualizing 4D and more dimensions. So, one would rather just consider the ndarray.shape
attribute and work with it.
More information about how we build higher dimensional arrays using (nested) lists:
For 1D arrays, the array constructor needs a sequence (tuple, list
, etc) but conventionally list
is used.
In [51]: oneD = np.array([1, 2, 3,])
In [52]: oneD.shape
Out[52]: (3,)
For 2D arrays, it's list of lists
but can also be tuple of lists
or tuple of tuples
etc:
In [53]: twoD = np.array([[1, 2, 3], [4, 5, 6]])
In [54]: twoD.shape
Out[54]: (2, 3)
For 3D arrays, it's list of lists of lists
:
In [55]: threeD = np.array([[[1, 2, 3], [2, 3, 4]], [[5, 6, 7], [6, 7, 8]]])
In [56]: threeD.shape
Out[56]: (2, 2, 3)
P.S. Internally, the ndarray
is stored in a memory block as shown in the below picture. (source: Enthought)
这篇关于非常基本的 Numpy 数组维度可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!