>>>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<
module
>
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
be
Python中reshape函式引數 1的使用
個人認為這是為了偷懶而設計的,為什這麼說呢,假設你有a 1 2 3 4 5 6 7 8 9 10 這樣的一維陣列,你只想讓a變成2列的二維陣列。按照常規的reshape,你得計算他的行數,經計算reshape的引數為reshape 5,2 而使用reshape 1,2 他會利用a和2列,計算出行數為...
python中full函式 Python函式混亂
我正在學習 python.我有乙個函式readwrite filename,list filename的型別為string.list是乙個包含要在檔案中重寫的字串的列表.我有乙個簡單的函式呼叫,如下所示 fname hello.txt readwrite xx fname,datalist 我面臨的...
Python中的defaultdict函式
用於產生乙個帶有預設值的dict。主要針對key不存在的情況下,也希望有返回值的情況。a dict a k1 1 a a k2 traceback most recent call last file line 1,in keyerror k2 對於普通的dict,key不存在就報錯。但是對於def...