参考文章:https://blog.csdn.net/Mr_Cat123/article/details/78841747
- shape() 查看或数组的维数
shape函数返回list,array,matrix等的一维和二维长度值。
1)list列表
列表只有一维,二维为0
import numpy as np
a = [1,2,3]
print(np.shape(a))
输出结果:(3,)
a.shape[0]
显示:AttributeError Traceback (most recent call last)
<ipython-input-2-78b9b49c6e77> in <module>()
2 a = [1,2,3]
3 print(np.shape(a))
----> 4 a.shape[0]
AttributeError: 'list' object has no attribute 'shape'
注意:列表没有shape函数
2)array数组
import numpy as np
a = np.eye(4,3)
print(a)
输出结果:[[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.],
[ 0., 0., 0.]]
print(np.shape(a))
输出结果:(4, 3)
print(a.shape[0])
输出结果:4
print(a.shape[1])
输出结果:3
上面建立了一个4x3的数组,
第一个维度是4(即第一层中括号里面的列表数量),矩阵的行数
第二个维度是3(即每二层中括号里的元素个数),矩阵的列数
3)matrix
import numpy as np
from numpy import mat
b = mat([[1,2,3],[2,3,5]])
print(b)
输出结果:[[1 2 3]
[2 3 5]]
print(np.shape(b))
输出结果:(2, 3)