的形狀B是(1,7,1)。我試圖B1在特定位置插入,B但如何保持新陣列的形狀與額外元素的形狀B2相同,即?附加了當前和所需的輸出。B(1,8,1)
import numpy as np
B = np.array([[[0.678731133],
[1.244425627],
[0.767884084],
[2.006154222],
[3.073758392],
[1.037728999],
[5.032947535]]])
B1=np.array([[10]])
B2=np.insert(B, 2, B1)
print("B2=",[B2])
print("B2 shape=",[B2.shape])
當前輸出為
B2= [array([ 0.678731133, 1.244425627, 10.000000000, 0.767884084,
2.006154222, 3.073758392, 1.037728999, 5.032947535])]
B2 shape= [(8,)]
所需的輸出是
B2=np.array([[[0.678731133],
[1.244425627],
[0.767884084],
[2.006154222],
[3.073758392],
[1.037728999],
[5.032947535]]])
B2 shape=[(1, 8, 1)]
uj5u.com熱心網友回復:
檔案說: ,這If axis is None then arr is flattened first解釋了您所看到的結果。嘗試告訴它要插入哪個軸:B2=np.insert(B, 2, B1, axis=1)。
import numpy as np
B = np.array([[[0.678731133],
[1.244425627],
[0.767884084],
[2.006154222],
[3.073758392],
[1.037728999],
[5.032947535]]])
B1=np.array([[10]])
B2=np.insert(B, 2, B1, axis=1)
print("B2=",B2)
print("B2 shape=",B2.shape)
這列印:
B2= [[[ 0.67873113]
[ 1.24442563]
[10. ]
[ 0.76788408]
[ 2.00615422]
[ 3.07375839]
[ 1.037729 ]
[ 5.03294753]]]
B2 shape= (1, 8, 1)
[另外,您想要的輸出沒有顯示插入的元素。我認為這是一個疏忽]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494399.html
上一篇:一步將多個元素插入陣列
下一篇:查詢其他兩個陣列對齊的陣列
