reshape是对array重新塑型,但是今天看别人代码出现了np.reshape(z,[-1])这种参数,仔细看下。
按照引用的例子:https://blog.youkuaiyun.com/weixin_39449570/article/details/78619196
>>> z = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]])
>>> print(z)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
>>> print(z.shape)
(4, 4)
>>> print(z.reshape(-1))
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
>>> print(z.reshape(-1,1)) #我们不知道z的shape属性是多少,
#但是想让z变成只有一列,行数不知道多少,
#通过`z.reshape(-1,1)`,Numpy自动计算出有16行,
#新的数组shape属性为(16, 1),与原来的(4, 4)配套。
[[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]
[11]
[12]
[13]
[14]
[15]
[16]]
>>> print(z.reshape(2,-1))
[[ 1 2