Python中reshape函數參數-1的意思?

在Python的numpy庫中,經常出現reshape(x,[-1,28,28,1])之類的表達,請問新shape中-1是什麼含義?我在網上查不到詳細的解釋,官方解釋看的不是太明白,希望大神幫助!


numpy.reshape(a, newshape, order=C)[source],參數`newshape`是啥意思?

根據Numpy文檔(https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html#numpy-reshape)的解釋:

newshape : int or tuple of ints


The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, **the value is inferred from the length of the array and remaining dimensions**.

大意是說,數組新的shape屬性應該要與原來的配套,如果等於-1的話,那麼Numpy會根據剩下的維度計算出數組的另外一個shape屬性值。

舉幾個例子或許就清楚了,有一個數組z,它的shape屬性是(4, 4)

z = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
z.shape
(4, 4)

z.reshape(-1)

z.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])

z.reshape(-1, 1)

也就是說,先前我們不知道z的shape屬性是多少,但是想讓z變成只有一列,行數不知道多少,通過`z.reshape(-1,1)`,Numpy自動計算出有12行,新的數組shape屬性為(16, 1),與原來的(4, 4)配套。

z.reshape(-1,1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14],
[15],
[16]])

z.reshape(-1, 2)

newshape等於-1,列數等於2,行數未知,reshape後的shape等於(8, 2)

z.reshape(-1, 2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12],
[13, 14],
[15, 16]])

同理,只給定行數,newshape等於-1,Numpy也可以自動計算出新數組的列數。

----------

參考

1.http://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape


官方文檔:numpy.reshape - NumPy v1.11 Manual

&>&>&> 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]])


題主是在用tensorflow做卷積神經網路入門的手寫數字識別吧,

源碼:x_image = tf.reshape(x, [-1, 28, 28, 1])

這裡是將一組圖像矩陣x重建為新的矩陣,該新矩陣的維數為(a,28,28,1),其中-1表示a由實際情況來定。例如,x是一組圖像的矩陣(假設是50張,大小為56×56),則執行

x_image = tf.reshape(x, [-1, 28, 28, 1])

可以計算a=50×56×56/28/28/1=200。即x_image的維數為(200,28,28,1)。

首次回答,如有錯誤,望各位大蝦指出!


可以參考 numpy.reshape - NumPy v1.12.dev0 Manual 最後給的 example。

&>&>&> 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]])

因為 a 總共有 6 個元素,reshape 成一個二維數組,指定第一維的長度是3(即 3 行),numpy 可以自動推斷出第二維的長度是 2 (6 除以 3 等於 2)。

我們可以再試一下如果有多個維度沒有指定長度的話會怎樣。

&>&>&> np.reshape(a, (-1,-1))
Traceback (most recent call last):
File "&", line 1, in &
File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 224, in reshape
return reshape(newshape, order=order)
ValueError: can only specify one unknown dimension

如果出現了無法整除的情況呢?

np.reshape(a, (4,-1))
Traceback (most recent call last):
File "&", line 1, in &
File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 224, in reshape
return reshape(newshape, order=order)
ValueError: total size of new array must be unchanged


-1是模糊控制的意思 比如人reshape(-1,2)固定2列 多少行不知道


舉個簡單的例子,要記住,python默認是按行取元素

c = np.array([[1,2,3],[4,5,6]])

輸出:

[[1 2 3]

[4 5 6]]

我們看看不同的reshape

print 改成2行3列:
print c.reshape(2,3)
print 改成3行2列:
print c.reshape(3,2)
print 我也不知道幾行,反正是1列:
print c.reshape(-1,1)
print 我也不知道幾列,反正是1行:
print c.reshape(1,-1)
print 不分行列,改成1串
print c.reshape(-1)

輸出為:

改成2行3列:

[[1 2 3]

[4 5 6]]

改成3行2列:

[[1 2]

[3 4]

[5 6]]

我也不知道幾行,反正是1列:

[[1]

[2]

[3]

[4]

[5]

[6]]

我也不知道幾列,反正是1行:

[[1 2 3 4 5 6]]

不分行列,改成1串

[1 2 3 4 5 6]


推薦閱讀:

Python項目挑戰賽(樓賽 第17期 )

TAG:Python | 編程 | 代碼 |