我知道numpy数组有一个称为shape的方法,它返回[行号,列数],形状[0]为您提供行数,形状[1]给出你的列数。numpy数组中的行数
a = numpy.array([[1,2,3,4], [2,3,4,5]])
a.shape
>> [2,4]
a.shape[0]
>> 2
a.shape[1]
>> 4
但是,如果我的数组只有一行,那么它返回[Number of columns,]。形状[1]将不在索引中。例如
a = numpy.array([1,2,3,4])
a.shape
>> [4,]
a.shape[0]
>> 4 //this is the number of column
a.shape[1]
>> Error out of index
现在如何获得numpy数组的行数,如果数组可能只有一行?
谢谢
拯救我的生命!非常感谢 –
@YichuanWang如果你从一个一维数组开始('a_1d = numpy.array([1,2,3,4])'),你总是可以把它转换成一个二维数组例如'a_2d = a_1d [无,:]' – donkopotamus