Corse
Corse

Reputation: 205

What is the most straightforward way to convert a list of numpy arrays into a single numpy array?

enter image description here

What is the most straightforward way to convert the list to the numpy array as shown?

I tried to to numpy.asarray() but it resulted in a 3 axis, with the data in the resulting array being transposed, although the values are combined.

UPDATED:

So asarray().reshape(1,3) works with single row. But the element in my list is of size (2,1) or greater, it seems the reshape does not transpose properly. How can i do it?

enter image description here

Updated:

Managed to do it with 2 steps: reshape(3,2) then np.transpose(). I wonder if there is a single step approach?

Upvotes: 0

Views: 337

Answers (3)

MB-F
MB-F

Reputation: 23637

  • Case A: combine a list of three arrays with shape (1, 1) into an array of shape (1, 3)

  • Case B: combine a list of three arrays with shape (2, 1) into an array of shape (2, 3)

Constraint: do it in a single step. (Presumably, a single "step" is a single function call.)

General answer: concatenate them! The function numpy.concatenate() takes a list of arrays and sticks them together along an arbitrary axis. The only constraint is that the shape of the arrays must be the same along all other dimensions.

The following example shows how to combine a list of N arrays with shape (K, 1) into an array of shape (K, N).

import numpy as np

list_of_arrays = [np.array([[1], [2]]), np.array([[10], [20]]), np.array([[100], [200]])]

assert list_of_arrays[0].shape == (2, 1)
assert list_of_arrays[1].shape == (2, 1)
assert list_of_arrays[2].shape == (2, 1)

result = np.concatenate(list_of_arrays, axis=1)  # single step: list -> array

assert result.shape == (2, 3)

Upvotes: 0

Tarifazo
Tarifazo

Reputation: 4343

If you want a 2-D (line or column) array, you can use np.atleast_2d with np.flatten as answered above:

x = np.arange(10).reshape((2,5))
print(x)
>>array([[0, 1, 2, 3, 4],
   [5, 6, 7, 8, 9]])

x_new = np.atleast_2d(x.flatten())
print(x_new) 
>> array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

x_new = np.atleast_2d(x.flatten()).T
print(x_new) 
>> array([[0],
   [1],
   [2],
   [3],
   [4],
   [5],
   [6],
   [7],
   [8],
   [9]])    

Upvotes: 0

kabanus
kabanus

Reputation: 25895

You are lists all have two dimensions. It seems you are looking to flatten that:

>>> x=[[1]]
>>> y=[[2]]
>>> z=[[3]]
>>> np.array((x,y,z)).flatten()
array([1, 2, 3])

If you want 3 columns and one row, you can reshape specifically:

>>> np.array((x,y,z)).reshape(1,3)
array([[1, 2, 3]])

Note this is two dimensional. The flat array is one dimensional, so there is no sense of row and column to it - you can reshape(3,1) to see the difference to a 3x1 array.

To answer your edit, I do not think any other way would be much more elegant than

>>> x=[[1],[2]]
>>> y=[[3],[4]]
>>> z=[[5],[6]]
>>> np.array((x,y,z)).reshape(3,2).T
array([[1, 3, 5],
       [2, 4, 6]])

and it is quite optimal as well. Using .T will save you an explicit call to np.transpose.

Upvotes: 1

Related Questions