Drop from list into numeric array tasks in python

I want to convert this list to a numpy array:

var=[array([ 33.85967782]), array([ 34.07298272]), array([ 35.06835424])]

The result should be as follows:

[[ 33.85967782]
 [ 34.07298272]
 [ 35.06835424]]

but if I type var = np.array(var), the result will be the following:

[array([ 33.85967782]) array([ 34.07298272]) array([ 35.06835424])]

I have a numpy library: import numpy as np

+4
source share
2 answers

np.vstack is the canonical way to perform this operation:

>>> var=[np.array([ 33.85967782]), np.array([ 34.07298272]), np.array([ 35.06835424])]

>>> np.vstack(var)
array([[ 33.85967782],
       [ 34.07298272],
       [ 35.06835424]])

If you need an array of forms (n,1), but you have arrays with several elements, you can do the following:

>>> var=[np.array([ 33.85967782]), np.array([ 35.06835424, 39.21316439])]
>>> np.concatenate(var).reshape(-1,1)
array([[ 33.85967782],
       [ 35.06835424],
       [ 39.21316439]])
+5
source

I don't know why your approach is not working, but this worked for me:

>>> import numpy as np
>>> from numpy import array
>>> var=[array([ 33.85967782]), array([ 34.07298272]), array([ 35.06835424])]
>>> np.array(var)
array([[ 33.85967782],
       [ 34.07298272],
       [ 35.06835424]])

This also worked (fresh translator):

>>> import numpy as np
>>> var = [np.array([ 33.85967782]), np.array([ 34.07298272]), np.array([ 35.06835424])]
>>> np.array(var)
array([[ 33.85967782],
       [ 34.07298272],
       [ 35.06835424]])
+4
source

Source: https://habr.com/ru/post/1532674/


All Articles