我遇到了這種奇怪的行為,np.transpose其中當用于numpy陣列和從串列構造的陣列時,它的作業方式不同。作為 MWE,提供了以下代碼。
import numpy as np
a = np.random.randint(0, 5, (1, 2, 3))
print(a.shape)
# prints (1, 2, 3)
b = np.transpose(a, (0, 2, 1))
print(b.shape)
# prints (1, 3, 2), which is expected
# Constructing array from list of arrays
c = np.array([np.random.randint(0, 5, (2, 3)), np.random.randint(0, 5, (2, 3)), np.random.randint(0, 5, (2, 3)), np.random.randint(0, 5, (2, 3))])
print(c.shape)
# prints (4, 2, 3)
d = np.transpose(c, (2, 0, 1))
print(d.shape)
# prints (3, 4, 2), whereas I expect it to be (2, 3, 4)
我不明白這種行為。為什么從串列構造的陣列的維度混淆了?任何幫助表示贊賞。
uj5u.com熱心網友回復:
np.transpose() 按照您指定的順序選取您指定的尺寸。
在您的第一種情況下,您的陣列形狀是(1,2,3)iedimension->value格式,它是0 -> 1,1 -> 2和2 -> 3. 在np.transpose()中,您請求的訂單0,2,1是1,3,2。
在第二種情況下,您的陣列形狀是(4,2,3)iedimension->value格式,它是0 -> 4,1 -> 2和2 -> 3。在np.transpose()中,您請求的訂單2,0,1是3,4,2。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/413804.html
標籤:
