



上面是知乎博主的回答,講得很清晰易懂。
下面是自己的一點理解:
1.當(dāng)原始數(shù)組A[4,6]為二維數(shù)組,代表4行6列。
A.reshape(-1,8):表示將數(shù)組轉(zhuǎn)換成8列的數(shù)組,具體多少行我們不知道,所以參數(shù)設(shè)為-1。用我們的數(shù)學(xué)可以計算出是3行8列
2當(dāng)原始數(shù)組A[4,6]為二維數(shù)組,代表4行6列。
A.reshape(3,-1):表示將數(shù)組轉(zhuǎn)換成3行的數(shù)組,具體多少列我們不知道,所以參數(shù)設(shè)為-1。用我們的數(shù)學(xué)可以計算出是3行8列
下面是代碼進(jìn)行驗證:
C:\Users\K>python
Python 2.7.13 |Anaconda, Inc.| (default, Sep 19 2017, 08:25:59) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> A = np.arange(24).reshape(4,6)
>>> A
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
>>> B = A.reshape(-1,8)
>>> B
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23]])
>>> A
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
>>> C = A.reshape(3,-1)
>>> C
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23]])
>>>

image.png