Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate all rows of a numpy matrix in python

Tags:

python

numpy

I have a numpy matrix and would like to concatenate all of the rows together so I end up with one long array.

#example

input:
[[1 2 3]
 [4 5 6}
 [7 8 9]]

output:
[[1 2 3 4 5 6 7 8 9]]

The way I am doing it now doe not seem pythonic. I'm sure there is a better way.

combined_x = x[0] 
for index, row in enumerate(x):
    if index!= 0:
        combined_x = np.concatenate((combined_x,x[index]),axis=1)

Thank you for the help.

like image 689
user1764386 Avatar asked Nov 06 '12 14:11

user1764386


People also ask

Is NumPy concatenate faster than append?

concat function is 50 times faster than using the DataFrame. append version. With multiple append , a new DataFrame is created at each iteration, and the underlying data is copied each time.

Does += work with NumPy arrays?

Numpy arrays are mutable objects that have clearly defined in place operations. If a and b are arrays of the same shape, a += b adds the two arrays together, using a as an output buffer.


1 Answers

I would suggest the ravel or flatten method of ndarray.

>>> a = numpy.arange(9).reshape(3, 3)
>>> a.ravel()
array([0, 1, 2, 3, 4, 5, 6, 7, 8])

ravel is faster than concatenate and flatten because it doesn't return a copy unless it has to:

>>> a.ravel()[5] = 99
>>> a
array([[ 0,  1,  2],
       [ 3,  4, 99],
       [ 6,  7,  8]])
>>> a.flatten()[5] = 77
>>> a
array([[ 0,  1,  2],
       [ 3,  4, 99],
       [ 6,  7,  8]])

But if you need a copy to avoid the memory sharing illustrated above, you're better off using flatten than concatenate, as you can see from these timings:

>>> %timeit a.ravel()
1000000 loops, best of 3: 468 ns per loop
>>> %timeit a.flatten()
1000000 loops, best of 3: 1.42 us per loop
>>> %timeit numpy.concatenate(a)
100000 loops, best of 3: 2.26 us per loop

Note also that you can achieve the exact result that your output illustrates (a one-row 2-d array) with reshape (thanks Pierre GM!):

>>> a = numpy.arange(9).reshape(3, 3)
>>> a.reshape(1, -1)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8]])
>>> %timeit a.reshape(1, -1)
1000000 loops, best of 3: 736 ns per loop
like image 116
senderle Avatar answered Sep 25 '22 11:09

senderle