2

I have an array in which all of the elements in the array are also arrays:

a = [[array([ 4.15167139]) array([ 2.80260218]) array([ 3.34189103])
  array([ 3.73434426]) array([ 3.76504973]) array([ 3.91946708])
  array([ 2.850741]) array([ 1.9985034]) array([ 4.05191836])
  array([ 3.46145848]) array([ 2.99489689]) array([ 2.60462582])
  array([ 1.91031189]) array([ 2.90006744]) array([ 3.69799451])
  array([ 3.83314665]) array([ 4.42917628]) array([ 5.17647658])
  array([ 4.63462677]) array([ 4.69085313]) array([ 4.84746095])
  array([ 5.04396694]) array([ 5.10152712]) array([ 3.33442499])
  array([ 4.87380637]) ...]]

I want this to be the same shape array that it is now (20,96) but instead of all of the elements within this array being arrays I would just like them to be floats:

 a = [[ 4.15167139  2.80260218  3.34189103...4.87380637 ...]]

What is the best way to iterate over this array and change all of the elements from individual arrays to floats?

5
  • Try a mix of indexing to remove extra [], and concatenate (or stack) to join arrays. Commented Jul 5, 2017 at 5:40
  • 2
    how was this array created? I think modifying its creation directly might be easier... and probably cleaner. Commented Jul 5, 2017 at 5:42
  • how about a=np.array(a).astype(np.float)? Commented Jul 5, 2017 at 5:43
  • However you made this array, you did something wrong. You should address the problem at the source. Commented Jul 5, 2017 at 5:52
  • 1
    Sometimes lists of arrays, or object arrays of arrays are produced by other modules, and you have to make the best of it. Commented Jul 5, 2017 at 5:54

2 Answers 2

1

Use indexing to remove a top layer of []

In [159]: array=np.array
In [161]: a = [[array([ 4.15167139]), array([ 2.80260218]), array([ 3.34189103]),
     ...:   array([ 3.73434426]), array([ 3.76504973]), array([ 3.91946708])]]
In [162]: len(a)
Out[162]: 1
In [163]: len(a[0])
Out[163]: 6

Use concatenate or stack to join a list of arrays.

In [164]: np.stack(a[0])
Out[164]: 
array([[ 4.15167139],
       [ 2.80260218],
       [ 3.34189103],
       [ 3.73434426],
       [ 3.76504973],
       [ 3.91946708]])
In [165]: np.concatenate(a[0])
Out[165]: 
array([ 4.15167139,  2.80260218,  3.34189103,  3.73434426,  3.76504973,
        3.91946708])

Reshape as needed.

In this case np.array also works, but I found that the concatenate approach is more reliable. Sometimes np.array just produces an object array.

In [166]: np.array(a[0])
Out[166]: 
array([[ 4.15167139],
       [ 2.80260218],
       [ 3.34189103],
       [ 3.73434426],
       [ 3.76504973],
       [ 3.91946708]])
Sign up to request clarification or add additional context in comments.

2 Comments

In[161] for ipython 3.5 (windows) gives error, TypeError: array() argument 1 must be a unicode character, not list. How to create such array?
I had to define an alias; array = np.array. Notice I also added commas.
1

It may be that np.concatenate is more reliable as said in hpaulj's answer, but in the current case np.array makes for a simpler solution, as it retains the dimensions. Applying it directly at the current array, will add a third dimension, however. This can be removed by slicing as:

b = np.array(a)[:,:,0]

It does not have to be more complicated than that.

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.