Reputation: 6958
I have a list containing objects of type numpy.ndarray, all list elements has same .shape value.
How can I collapse this into a matrix?
Upvotes: 2
Views: 675
Reputation: 500713
Sounds like you're looking for numpy.vstack()
or numpy.hstack()
, depending on whether you want the arrays to become the rows or the columns of the matrix.
From the manual:
>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.vstack((a,b))
array([[1, 2, 3],
[2, 3, 4]])
Upvotes: 4
Reputation: 85663
I am not sure if you mean this:
>>> alist = [np.array([item, item+1]) for item in range(5)]
>>> alist
[array([0, 1]), array([1, 2]), array([2, 3]), array([3, 4]), array([4, 5])]
>>> np.array(alist)
array([[ 0, 1],
[ 1, 2],
[ 2, 3],
[ 3, 4],
[ 4, 5]])
>>>
or actually you want a matrix:
>>> np.matrix(alist)
matrix([[ 0, 1],
[ 1, 2],
[ 2, 3],
[ 3, 4],
[ 4, 5]])
Upvotes: 3