非常基本的Numpy数组维度可视化 [英] Very Basic Numpy array dimension visualization

查看:357
本文介绍了非常基本的Numpy数组维度可视化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是numpy的初学者,没有矩阵方面的经验.我了解基本的1d和2d数组,但是在可视化3d numpy数组(如下面的数组)时遇到了麻烦.以下python列表如何形成具有高度,长度和宽度的3d数组?哪些行和列?

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]]])

推荐答案

在NumPy中ndarray的解剖结构看起来像下面的红色立方体:(来源:

The anatomy of an ndarray in NumPy looks like this red cube below: (source: Physics Dept, Cornell Uni)

一旦离开2D空间并输入3D或更高维度的空间,行和列的概念就不再有意义.但是您仍然可以直观地了解3D阵列.例如,考虑您的示例:

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)

此处b的形状为(2, 2, 3).您可以考虑一下,我们已经堆叠了两个 (2x3)个矩阵以形成3D阵列.要访问第一个矩阵,请像b[0]一样索引到数组b中,要访问第二个矩阵,请像b[1]那样索引到数组b中.

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]])

但是,如果您输入4D或更高的空间,将很难从数组本身中理解任何意义,因为我们人类很难看到4D和更多维度.因此,宁愿只考虑ndarray.shape属性并使用它.

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:

对于一维数组,数组构造函数需要一个序列(tuple, list等),但通常使用list.

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,)

对于2D数组,它是list of lists,但也可以是tuple of liststuple of tuples等:

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)

对于3D阵列,它是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.在内部,ndarray存储在存储块中,如下图所示. (来源:思考)


P.S. Internally, the ndarray is stored in a memory block as shown in the below picture. (source: Enthought)

这篇关于非常基本的Numpy数组维度可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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