简体   繁体   English

关于numpy数组的维数问题

[英]Question about dimensionality of numpy array

I am studying physics and numpy simultaneously.我正在同时学习物理和 numpy。 Numpy says my 3x3 matrix has 2-dimensions, but in my physics book or 3blue1brown 'Essence of linear Algebra' a 3x3 matrix is 3-D Numpy 说我的 3x3 矩阵有 2 维,但在我的物理书或 3blue1brown '线性代数的本质'中,3x3 矩阵是 3-D

#a '2d' array, created using identity
i2d = np.identity(3)  

print(i2d)

print('this is a %s-D array, shape is %s with %s elements'%(i2d.ndim, i2d.shape, i2d.size))


YIELDS:

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
this is a 2-D array, shape is (3, 3) with 9 elements

In linear algebra, this defines a 3-D space with 3 perpendicular basis vectors.在线性代数中,这定义了一个具有 3 个垂直基向量的 3-D 空间。 Anyone know what I am missing.任何人都知道我错过了什么。

In numpy, the shape attribute gives you (3,3) and represents the number of rows and columns - in a physical context, your 3 basis vectors of length three form the basis for 3D-space.在 numpy 中, shape属性为您提供(3,3)并表示行数和列数 - 在物理环境中,您的 3 个长度为 3 的基向量构成 3D 空间的基础。

Numpy's ndim attribute for arrays references how many "nestings" of arrays there are. Numpy 的 arrays 的ndim属性引用了 arrays 的“嵌套”数量。 You have an array of arrays - so you have two dimensions.你有一个arrays 数组- 所以你有两个维度。

In general, if the elements of an array are arrays themselves, your dimensionality is 2. To access any element in an array like this, you need 2 indices, one for each dimension.一般来说,如果数组的元素本身是 arrays,那么您的维度是 2。要访问这样的数组中的任何元素,您需要 2 个索引,每个索引一个。 ie arr[i][j]arr[i][j]

If the elements of an array are arrays, and the elements of those arrays are also arrays, your dimensionality is 3 and you need 3 indices to access any element ie arr[i][j][k] .如果数组的元素是 arrays,并且那些 arrays 的元素也是 arrays,那么你的维度是 3,你需要 3 个索引来访问 a[jrrk arr[i][j][k]任何元素。 You have a nested array structure like this:你有一个这样的嵌套数组结构:

[
  [ 
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
  ],
  [ 
    [ 1, 0, 0 ],
    [ 0, 1, 0 ],
    [ 0, 0, 1 ]
  ],
  ...
]

In a physical sense, the shape attribute should be what you pay attention to.在物理意义上, shape属性应该是你所关注的。

For the basis of 3-space, 3x3 matrix.对于 3 空间、3x3 矩阵的基础。 For the basis of 4-space, 4x4 matrix, and so on.对于 4 空间、4x4 矩阵等的基础。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM