Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 281
user3182080 Avatar asked Jan 10 '14 14:01

user3182080


People also ask

How do you convert a 1D array to a 2D array in Python?

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping. To modify the layout of a NumPy ndarray, we will be using the reshape() method.

Can you convert between Python lists and arrays?

Lists can be converted to arrays using the built-in functions in the Python numpy library. numpy provides us with two functions to use when converting a list into an array: numpy. array()


2 Answers

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)
like image 78
YS-L Avatar answered Sep 22 '22 17:09

YS-L


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.

like image 32
Sergey Antopolskiy Avatar answered Sep 20 '22 17:09

Sergey Antopolskiy