zython
zython

Reputation: 1288

Cant seem to flatten numpy array

I have a numpy array which when print-ed looks like this:

print(a.shape)
(21,)
print(a)
[array([8.55570588e+03, 4.23078573e+05, 2.81254715e+07, 2.10356201e+09,
       4.24558286e+05, 2.10032147e+07, 1.39638949e+09, 1.04453957e+11,
       2.81593475e+07, 1.39354786e+09, 9.26480296e+10, 6.92992796e+12,
       2.10047682e+09, 1.03982525e+11, 6.91296507e+12, 5.17021191e+14])
 array([8.55404706e+03, 4.23328400e+05, 2.80891690e+07, 2.09651453e+09,
       4.23874124e+05, 2.09628073e+07, 1.39044370e+09, 1.03745119e+11,
       2.81060928e+07, 1.38935279e+09, 9.21288996e+10, 6.87207671e+12,
       2.09626303e+09, 1.03584989e+11, 6.86712650e+12, 5.12107449e+14])
 array([6.71569608e+03, 3.32364057e+05, 2.20526342e+07, 1.64564735e+09,
       3.32826578e+05, 1.64539763e+07, 1.09116635e+09, 8.13888141e+10,
       2.20612069e+07, 1.08976996e+09, 7.22409501e+10, 5.38629510e+12,
       1.64474898e+09, 8.11907944e+10, 5.38026989e+12, 4.01021156e+14])
 array([  97,  120,  147,  106,  115,  151,  300,  268,  326,  454,  684,
       1594, 2202, 2229, 1205,    2])
 array([   1,    0,    0,    0,    0,    1,    0,    1,    0,    2,    1,
         11,  359, 1355, 3921, 4348])
 array([   1,    0,    0,    1,    0,    0,    6,   11,   31,  644, 2312,
       3046, 3618,  321,    7,    2])
 625.0 625.0 625.0 537178.875 1874648.75 1373895.875 1.275734191674592
 2.066594119913508 1.6749058704798478 0.11276410212887233 2.55304393588347
 1.1167704949278905 2.177796835501801 1.1323869527951895
 1.3940068452456151]

Ideally I would want all of these values in one big array of length (3*16 + 3*16 + 15)

np.concatenate did not work, flatten also did bring the desired result.

Upvotes: 1

Views: 921

Answers (1)

titipata
titipata

Reputation: 5389

One simple way is to use np.hstack in order to flatten list of array and float. Example usage is as follows:

import numpy as np
a = [np.array([1, 2, 3]), np.array([4, 5, 6]), 7, 8, 9]
np.hstack(a)

>> array([1, 2, 3, 4, 5, 6, 7, 8, 9])

Upvotes: 2

Related Questions