Python numpy 如何查看数组的维度?
>>> import numpy as np
>>> a=np.array([[1,2,3],[3,5,6]])
>>> b=np.array([1,2,3,4,5],ndmin=2)
>>> print(b)
[[1 2 3 4 5]]
>>> b.shape
(1, 5)
>>> com=np.array([a,b,c])
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘c’ is not defined
>>> com=np.array([a,b,c])
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘c’ is not defined
>>> c=np.array([[3,2,3],[3,5,6]])
>>> com=np.array([a,b,c])
>>> com
array([array([[1, 2, 3],
[3, 5, 6]]), array([[1, 2, 3, 4, 5]]),
array([[3, 2, 3],
[3, 5, 6]])], dtype=object)
>>> com.shape
(3,)
>>> b.shape
(1, 5)
>>> c.shape
(2, 3)
>>>
比如这个怎么看维度啊?(1, 5) 这个是二维的图怎么画出来啊
Python numpy 如何查看数组的维度?
这个很简单啊
用 ndarray.shape 属性就行,它会返回一个元组,告诉你数组在每个维度上的大小。
比如:
import numpy as np
# 创建一个二维数组
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d.shape) # 输出: (2, 3) 表示2行3列
# 创建一个三维数组
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d.shape) # 输出: (2, 2, 2) 表示2个2x2的矩阵
shape 元组的长度就是数组的维度数,可以用 ndim 属性直接获取:
print(arr_2d.ndim) # 输出: 2
print(arr_3d.ndim) # 输出: 3
总结:用 .shape 看具体维度大小,用 .ndim 看维度数量。
不看文档的么。。。python<br>a.dim<br>
a.shape, 好好看看文档吧
正解

