numpy尺寸 [英] numpy dimensions
问题描述
我是Numpy的新手,并试图了解什么是维度的基本问题,
Im a newbie to Numpy and trying to understand the basic question of what is dimension,
我尝试了以下命令,并试图理解为什么最后两个数组的ndim相同?
I tried the following commands and trying to understand why the ndim for last 2 arrays are same?
>>> a= array([1,2,3])
>>> a.ndim
1
>>> a= array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
[4, 5, 6]])
>>> a.ndim
2
>>> a=arange(15).reshape(3,5)
>>> a.ndim
2
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
我的理解.
Case 1:
array([[1, 2, 3],
[4, 5, 6]])
2 elements are present in main lists, so ndim is-2
Case 2:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
3个元素出现在主列表中,ndim为-3
3 elements are present in the main lists, do ndim is-3
推荐答案
数组的shape
是其尺寸的元组.一维数组的形状为(n,).二维数组的形状为(n,m)(类似于您的案例2和3),而三维数组的形状为(n,m,k),依此类推.
The shape
of an array is a tuple of its dimensions. An array with one dimension has a shape of (n,). A two dimension array has a shape of (n,m) (like your case 2 and 3) and a three dimension array has a shape of (n,m,k) and so on.
因此,尽管您的第二个和第三个示例的形状不同,但没有.在这两种情况下,尺寸都是两个:
Therefore, whilst the shape of your second and third example are different, the no. dimensions is two in both cases:
>>> a= np.array([[1,2,3],[4,5,6]])
>>> a.shape
(2, 3)
>>> b=np.arange(15).reshape(3,5)
>>> b.shape
(3, 5)
如果您想在示例中添加另一个维度,则必须执行以下操作:
If you wanted to add another dimension to your examples you would have to do something like this:
a= np.array([[[1,2,3]],[[4,5,6]]])
或
np.arange(15).reshape(3,5,1)
您可以继续以这种方式添加尺寸:
You can keep adding dimensions in this way:
一个维度:
>>> a = np.zeros((2))
array([ 0., 0.])
>>> a.shape
(2,)
>>> a.ndim
1
二维:
>>> b = np.zeros((2,2))
array([[ 0., 0.],
[ 0., 0.]])
>>> b.shape
(2,2)
>>> b.ndim
2
三个维度:
>>> c = np.zeros((2,2,2))
array([[[ 0., 0.],
[ 0., 0.]],
[[ 0., 0.],
[ 0., 0.]]])
>>> c.shape
(2,2,2)
>>> c.ndim
3
四个维度:
>>> d = np.zeros((2,2,2,2))
array([[[[ 0., 0.],
[ 0., 0.]],
[[ 0., 0.],
[ 0., 0.]]],
[[[ 0., 0.],
[ 0., 0.]],
[[ 0., 0.],
[ 0., 0.]]]])
>>> d.shape
(2,2,2,2)
>>> d.ndim
4
这篇关于numpy尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!