简体   繁体   中英

Python - Conversion of list of arrays to 2D array

I have a dataset that is formatted like this:

A=[(Num1,Num2,Num3), (Num4,Num5,Num6), (Num7,Num8,Num9)]

with

A.shape = (3,)

and I would like to convert this into a 2D numpy array:

A=[[Num1,Num2,Num3],[Num4,Num5,Num6],[Num7,Num8,Num9]]

with

A.shape = (3,3)

How do I do this, preferably without loops? Thanks.

Not sure if I understood the question correctly, but does this work for you?

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

If A is a list of numpy array, how about this:

Ah = np.vstack(A)
Av = np.hstack(A)

If I understood correctly what you're asking, you have a case where numpy did not convert array of arrays into 2d array. This can happen when your arrays are not of the same size. Example:

Automatic conversion to 2d array:

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

Output:

>>>[[1 2 3]
    [2 3 4]
    [6 7 8]]

No automatic conversion (look for the change in the second subarray):

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

Output:

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

I found a couple of ways of converting an array of arrays to 2d array. In any case you need to get rid of subarrays which have different size. So you will need a mask to select only "good" subarrays. Then you can use this mask with list comprehensions to recreate array, like this:

import numpy as np

a = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
mask = np.array([True, False, True])

c = np.array([element for (i,element) in enumerate(a) if mask[i]])

print a
print c

Output:

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

Or you can delete "bad" subarrays and use vstack(), like this:

import numpy as np

a = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
mask = np.array([True, False, True])

d = np.delete(a,np.where(mask==False))
e = np.vstack(d)

print a
print e

Output:

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

I believe second method would be faster for large arrays, but I haven't tested the timing.

我想像:

A = map(lambda t: list(t), A)

If what you have is a array of tupples. A (3,) array with dtype=object.
There is no way, that i am aware of, to elegantly unpack them into a (3,3) array through broadcasting. Converting back to a list, and then creating a new array seems the easiest.

In [314]: data
Out[314]: array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=object)
In [315]: data.shape
Out[315]: (3L,)

data2 = np.empty((3,3), dtype=int)

#Neither of these work.
data2[:] = data[:]
data2[:] = data[:, None]

#This will work, but requires looping
data2[0,:] = data[0]
data2[1,:] = data[1]
data2[2,:] = data[2]

#This is the easies way i could find
data2 = np.array(data.tolist())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM