0

during data processing I create an array looking like this:

[array([ 0.08606408]) array([ 0.26071976])
 array([ 0.181566  ,  0.94154611]) array([ 0.1734347 ,  0.94160601])
 array([ 0.17859844,  0.94167483]) array([ 0.16880761,  0.94156277])
 array([ 0.17624151,  0.94149038]) array([ 0.18770433,  0.94181287])
 array([ 0.16707977,  0.94227733]) array([ 0.94162233])
 array([ 0.9426902,  0.9615621]) array([ 0.94195127,  0.96174422])
 array([ 0.94237795,  0.96195226,  0.98059446])
 array([ 0.94249657,  0.96219391,  0.98095329])
 array([ 0.94280697,  0.96286183,  0.98109352])
 array([ 0.94267473,  0.96304417,  0.98252799])]

created in the following manner:

peakpositions = []
peakpositions.append(stuff)

How do I extract the float values into a single 1D numpy array? What I want is this:

[0.08606408, 0.26071976, 0.181566 ... 0.98252799]

Thanks in advance!

1
  • 1
    Use np.concatenate(peakpositions). Commented Jul 9, 2015 at 15:07

2 Answers 2

4

You can concatenate your inner arrays :

peakpositions=np.concatenate(peakpositions)

Demo :

>>> l= [[array([ 0.08606408]), array([ 0.26071976]),
       array([ 0.181566  ,  0.94154611]),
       array([ 0.1734347 ,  0.94160601]),
       array([ 0.17859844,  0.94167483]),
       array([ 0.16880761,  0.94156277]),
       array([ 0.17624151,  0.94149038]),
       array([ 0.18770433,  0.94181287]),
       array([ 0.16707977,  0.94227733]), array([ 0.94162233]),
       array([ 0.9426902,  0.9615621]), array([ 0.94195127,  0.96174422]),
       array([ 0.94237795,  0.96195226,  0.98059446]),
       array([ 0.94249657,  0.96219391,  0.98095329]),
       array([ 0.94280697,  0.96286183,  0.98109352]),
       array([ 0.94267473,  0.96304417,  0.98252799])]
>>> np.concatenate(l)
array([ 0.08606408,  0.26071976,  0.181566  ,  0.94154611,  0.1734347 ,
        0.94160601,  0.17859844,  0.94167483,  0.16880761,  0.94156277,
        0.17624151,  0.94149038,  0.18770433,  0.94181287,  0.16707977,
        0.94227733,  0.94162233,  0.9426902 ,  0.9615621 ,  0.94195127,
        0.96174422,  0.94237795,  0.96195226,  0.98059446,  0.94249657,
        0.96219391,  0.98095329,  0.94280697,  0.96286183,  0.98109352,
        0.94267473,  0.96304417,  0.98252799])
>>> 
Sign up to request clarification or add additional context in comments.

3 Comments

For the first option, you need to convert peakpositions to numpy.array before: numpy.array(peakpositions).flatten().
Actually, the first one does not work (at least with my version of numpy and python) because numpy is able to flatten a 2d numpy.array but not a numpy.array of numpy.array. Anyway, I though the second method would be really slow but it runs in less than .3s on my (poor) computer with a list of 1000000 arrays of random size (between 1 and 10).
@Holt Yeah flatten doesn't works on this case sine it is a list contain numpy arrays!
0

Create an empty list/array and extend it.

array_1d = []
for array in arrays:
    array_1d.extend(array)

That should do the magic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.