20

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.

3
  • 2
    Please post some sample data. Commented Jan 10, 2014 at 14:19
  • 3
    What is A? A python list, or a numpy array? Seems numpy.array(A) is all you need to do. What exactly is the problem? Commented Jan 10, 2014 at 14:26
  • 1
    The comments and answers pointing to numpy.array(A) are all correct, with one caveat: the inner elements (whether they're tuples, lists, or np.arrays themselves) must have the same length. If they don't, you'll still get A.shape = (3,) and A will have dtype=object. I've definitely been snagged by this, when elements unexpectedly had different lengths. Commented Oct 6, 2016 at 21:12

4 Answers 4

22

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)
Sign up to request clarification or add additional context in comments.

Comments

8

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.

Comments

4

I think like:

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

1 Comment

Shorter: map(list, A)
3

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())

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.