Python中reshape函式引數 1的意思?

時間 2021-05-05 20:00:47

1樓:西紅柿雞蛋湯

首先要明白shape的含義,shape代表了乙個矩陣的行和列。比如這個矩陣,m=

np.array

([[1,2

,3],[

5,6,

7]])m.

shape

它的shape是(3,2)

reshape就是對矩陣的shape重新排列。

比如m.reshape(3, 2)

[[1 2]

[3 5]

[6 7]]

我們就將矩陣重新排列成了兩行三列的新矩陣。

那麼我們可以把矩陣重新排列成(3,3)的矩陣嗎?答案是不行的。(3,3)需要9個元素,而原來的矩陣有6個元素。這就說明前面的行定義好了,後面的列是固定的,就是m.size//3

而-1實際上就是m.size // 3了,代表的就是重新排成三行,列根據原來的size自動計算。

m.reshape(3, 2)

m.reshape(3, m.size // 3)

m.reshape(3, -1)

上面三個操作是等價的。

你再想想這個矩陣有可能重排成5行嗎?答案是不行的,因為6不能被5整除,無論你寫m.size//5或者(5,-1)都會報錯。

另外reshape是返回乙個新的陣列,不改變原來傳入的陣列,這一點需要注意。

2樓:cuicuicui

舉個簡單的例子,要記住,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]

一串是啥意思?一串就是秩rank()為0的矩陣~

3樓:H4ck3r L1

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

根據Numpy文件(https://

docs.scipy.org/doc/nump

y/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也可以自動計算出新陣列的列數。

4樓:BiggerHao

可以參考 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"",

line1,

in

>File

"/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py"

,line

224,

inreshape

return

reshape

(newshape

,order

=order

)ValueError

:total

size

ofnew

array

must

beunchanged

如何在Python高階閉包中得到最外層函式的環境變數?

已登出 你不shadow不行嗎 shadowing的用意就是隱藏外層作用域的變數,你這 我要用同名變數隱藏掉外層,還要能有辦法訪問到外層 這邏輯真的太矛盾了。def fn1 a 11 def fn2 b 22 def fn3 print a return fn3 return fn2 a 0 f2 ...

python中enumerate object究竟是一種怎樣的存在形式呢?

l Ass We Can type of l is tuplee enumerate l type of e is enumerate objectforv inenumerate e type of enumerate e is enumerate object print v type of v...

Python中ASCII,Unicode,UTF 8,encode,decode這些有什麼關係?

NoOffense ASCII,UTF 8是常用的字元編碼型別,Unicode是字符集,它們跟具體某一門語言 比如Python 無關,是計算機通行的標準。字元編碼型別規定了位元組 bytes 和字元 character 是如何對應的。例如ASCII中,10進製65代字元 A UTF 8中,16進製制...