关于 reshape
的普通用法这里就不提了,记录一下代码中遇到的其他用法:
reshape
还可以给数据扩充维度,如:
a = np.arange(0,12).reshape((2,2,3))
''' array([[[ 0, 1, 2],[ 3, 4, 5]],[[ 6, 7, 8],[ 9, 10, 11]]]) '''b = a
b = b.reshape((1,) + b.shape)
b.shape
''' (1, 2, 2, 3) '''
b
''' array([[[[ 0, 1, 2],[ 3, 4, 5]],[[ 6, 7, 8],[ 9, 10, 11]]]]) '''
或者:
b = a
b = b.reshape(b.shape + (1,))
b.shape
''' (2, 2, 3, 1) '''
b
''' array([[[[ 0],[ 1],[ 2]],[[ 3],[ 4],[ 5]]],[[[ 6],[ 7],[ 8]],[[ 9],[10],[11]]]]) '''