Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inserting multiple elements in one step

I have an array B with shape (1,7,1). I am trying to insert multiple elements (here, 2 elements) at specific positions in one step. The shape of the new array B3 should be (1,9,1). However, I am getting an error. The desired output is attached.

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])

The error is

<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'

The desired output is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

B3 = np.array([[[0.678731133],
        [1.244425627],
        [10],
        [0.767884084],
        [2.006154222],
        [3.073758392],
        [1.037728999],
        [5.032947535],
        [20]]])
B3 shape= (1,9,1)

>Solution :

B3=np.insert(B, [2,7],[B1,B2],axis=1) works. You can check np.insert documentation

print(B3)
print(B3.shape)
>>>[[[ 0.67873113]
  [ 1.24442563]
  [10.        ]
  [ 0.76788408]
  [ 2.00615422]
  [ 3.07375839]
  [ 1.037729  ]
  [ 5.03294753]
  [20.        ]]]
>>>(1, 9, 1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading