reshape中的-1

>>> a = np.array([[1,2,3], [4,5,6]])
>>> np.reshape(a, (3,-1))  # the unspecified value is inferred to be 2
array([[1, 2],
       [3, 4],
       [5, 6]])

 -1表示我懒得计算该填什么数字,由python通过a和其他的值3推测出来。

# 下面是两张2*3大小的照片(不知道有几张照片用-1代替),如何把所有二维照片给摊平成一维
>>> image = np.array([[[1,2,3], [4,5,6]], [[1,1,1], [1,1,1]]])
>>> image.shape
(2, 2, 3)
>>> image.reshape((-1, 6))
array([[1, 2, 3, 4, 5, 6],
       [1, 1, 1, 1, 1, 1]])

 作者:Lovecanon 链接:https://www.zhihu.com/question/52684594/answer/134039079 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

原文地址:https://www.cnblogs.com/lindaxin/p/7978573.html