B我有一個shape陣列(1,7,1)。我正在嘗試在一個步驟中在特定位置插入多個元素(此處為 2 個元素)。新陣列的形狀B3 應該是(1,9,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.array([20])
B3=np.insert(B, 2, B1,8,B2,axis=1)
print("B3=",[B3])
print("B3 shape=",[B3.shape])
錯誤是
<module>
B3=np.insert(B, 2, B1,8,B2,axis=1)
File "<__array_function__ internals>", line 4, in insert
TypeError: _insert_dispatcher() got multiple values for argument 'axis'
所需的輸出是
B3 = np.array([[[0.678731133],
[1.244425627],
[10],
[0.767884084],
[2.006154222],
[3.073758392],
[1.037728999],
[5.032947535],
[20]]])
B3 shape= (1,9,1)
uj5u.com熱心網友回復:
B3=np.insert(B, [2,7],[B1,B2],axis=1)作品。您可以查看np.insert 檔案
print(B3)
print(B3.shape)
>>>[[[ 0.67873113]
[ 1.24442563]
[10. ]
[ 0.76788408]
[ 2.00615422]
[ 3.07375839]
[ 1.037729 ]
[ 5.03294753]
[20. ]]]
>>>(1, 9, 1)
uj5u.com熱心網友回復:
切片和水平堆疊也有效
np.hstack([B[:, :2], [[B1]], B[:, 2:], [[B2]]])
#array([[[ 0.67873113],
# [ 1.24442563],
# [10. ],
# [ 0.76788408],
# [ 2.00615422],
# [ 3.07375839],
# [ 1.037729 ],
# [ 5.03294753],
# [20. ]]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494398.html
