shape属性可以获取矩阵(ndarray)的行列信息,获取的结果是一个元组,下面以二维数组为例
import numpy as np
x = np.array([[1,2,3,4,5],[4,5,6,7,8],[7,8,9,10,11],[10,11,12,13,14]])
print(x.shape) #输出数组的行和列数
print(x.shape[0]) #只输出行数
print(x.shape[1]) #只输出列数
'''
输出
(4, 5)
4
5
'''
shape属性可以获取矩阵(ndarray)的行列信息,获取的结果是一个元组,下面以二维数组为例
import numpy as np
x = np.array([[1,2,3,4,5],[4,5,6,7,8],[7,8,9,10,11],[10,11,12,13,14]])
print(x.shape) #输出数组的行和列数
print(x.shape[0]) #只输出行数
print(x.shape[1]) #只输出列数
'''
输出
(4, 5)
4
5
'''