2

How to covert this array (different dimensions numpy),

l= (array([0.08]), array([[ 0.56, -0.01, 0.46]), array([[ 0.60], [0.07], [0.03]]), array([[0., 0., 0., 0.]]), array([[0.]]))

into a 1D array,

l= array([0.08, 0.56, -0.01, 0.60, 0.07, 0.03, 0., 0., 0., 0., 0.])
1
  • Numpy has a few related stacking functions. You can read about them in the docs Commented May 2, 2018 at 10:16

1 Answer 1

1

One way is to use numpy.hstack with ravel to flatten the various dimensions.

import numpy as np

l = (np.array([0.08]), np.array([ 0.56, -0.01, 0.46]),
     np.array([[ 0.60], [0.07], [0.03]]), np.array([[0., 0., 0., 0.]]),
     np.array([[0.]]))

res = np.hstack(i.ravel() for i in l)

array([ 0.08,  0.56, -0.01,  0.46,  0.6 ,  0.07,  0.03,  0.  ,  0.  ,
        0.  ,  0.  ,  0.  ])

Or if you want a functional approach:

from operator import methodcaller

res = np.hstack(list(map(methodcaller('ravel'), l)))
Sign up to request clarification or add additional context in comments.

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.