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

Unpacking numpy array inside

I have the following numpy array:

a=[['Sb' array([4.24035696, 2.44817292, 7.41858935])]
 ['I' array([2.08076032, 3.69501392, 5.37518666])]
 ['I' array([6.35173963, 1.22916823, 8.9947238 ])]
 ['I' array([ 4.24036048, -0.04551256,  5.37518684])]
 ['I' array([4.24035383, 4.88618543, 8.99472472])]
 ['I' array([6.3999514 , 3.69501663, 5.37518685])]
 ['I' array([2.12897688, 1.22916548, 8.99472467])]]

I’d like to unpack the inner array to have:

a=[['Sb' 4.24035696 2.44817292 7.41858935]
 ...
 ['I' 2.12897688 1.22916548 8.99472467]]

I can’t figure this one out in a simple way.

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

>Solution :

See my comment for explanation:

In [142]: array=np.array
     ...: a=np.array([['Sb', array([4.24035696, 2.44817292, 7.41858935])],
     ...:  ['I', array([2.08076032, 3.69501392, 5.37518666])],
     ...:  ['I', array([6.35173963, 1.22916823, 8.9947238 ])],
     ...:  ['I', array([ 4.24036048, -0.04551256,  5.37518684])],
     ...:  ['I', array([4.24035383, 4.88618543, 8.99472472])],
     ...:  ['I', array([6.3999514 , 3.69501663, 5.37518685])],
     ...:  ['I', array([2.12897688, 1.22916548, 8.99472467])]], dtype=object)

In [143]: a
Out[143]: 
array([['Sb', array([4.24035696, 2.44817292, 7.41858935])],
       ['I', array([2.08076032, 3.69501392, 5.37518666])],
       ['I', array([6.35173963, 1.22916823, 8.9947238 ])],
       ['I', array([ 4.24036048, -0.04551256,  5.37518684])],
       ['I', array([4.24035383, 4.88618543, 8.99472472])],
       ['I', array([6.3999514 , 3.69501663, 5.37518685])],
       ['I', array([2.12897688, 1.22916548, 8.99472467])]], dtype=object)

In [144]: a[:,0]
Out[144]: array(['Sb', 'I', 'I', 'I', 'I', 'I', 'I'], dtype=object)

In [145]: np.stack(a[:,1])
Out[145]: 
array([[ 4.24035696,  2.44817292,  7.41858935],
       [ 2.08076032,  3.69501392,  5.37518666],
       [ 6.35173963,  1.22916823,  8.9947238 ],
       [ 4.24036048, -0.04551256,  5.37518684],
       [ 4.24035383,  4.88618543,  8.99472472],
       [ 6.3999514 ,  3.69501663,  5.37518685],
       [ 2.12897688,  1.22916548,  8.99472467]])

Combine them into an object dtype array:

In [148]: res = np.concatenate((a[:,[0]],np.stack(a[:,1])),axis=1,dtype=object)

In [149]: res
Out[149]: 
array([['Sb', 4.24035696, 2.44817292, 7.41858935],
       ['I', 2.08076032, 3.69501392, 5.37518666],
       ['I', 6.35173963, 1.22916823, 8.9947238],
       ['I', 4.24036048, -0.04551256, 5.37518684],
       ['I', 4.24035383, 4.88618543, 8.99472472],
       ['I', 6.3999514, 3.69501663, 5.37518685],
       ['I', 2.12897688, 1.22916548, 8.99472467]], dtype=object)
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