I have a N x 2 dimensional numpy array. I would like to make a (2*N) x 2, where each column is repeated. I'm curious if there is a more efficient way than what I've written below to accomplish this task.
>>> a = np.array([[1,2,3,4],
[2,4,6,8]])
>>> b = np.array(zip(a.T,a.T))
>>> b.shape = (2*len(a[0]), 2)
>>> b.T
array([[1, 1, 2, 2, 3, 3, 4, 4],
[2, 2, 4, 4, 6, 6, 8, 8]])
The code above is slow by numpy standards, most likely because of the zip
. Is there a numpy
function that I can replace zip
with? Or a better way to do this altogether?
The repeat() function is used to repeat elements of an array. Input array. The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.
In Python, if you want to repeat the elements multiple times in the NumPy array then you can use the numpy. repeat() function. In Python, this method is available in the NumPy module and this function is used to return the numpy array of the repeated items along with axis such as 0 and 1.
transpose() to repeat an array as a column. Call numpy. transpose(a) with a in the form list * n with list as a list whose sole element is a one-dimensional array and n as the number of times to repeat the array .
To stack masked arrays in sequence depth wise (along third axis), use the ma. dstack() method in Python Numpy. This is equivalent to concatenation along the third axis after 2-D arrays of shape (M,N) have been reshaped to (M,N,1) and 1-D arrays of shape (N,) have been reshaped to (1,N,1).
You could use repeat
:
import numpy as np
def slow(a):
b = np.array(zip(a.T,a.T))
b.shape = (2*len(a[0]), 2)
return b.T
def fast(a):
return a.repeat(2).reshape(2, 2*len(a[0]))
def faster(a):
# compliments of WW
return a.repeat(2, axis=1)
gives
In [42]: a = np.array([[1,2,3,4],[2,4,6,8]])
In [43]: timeit slow(a)
10000 loops, best of 3: 59.4 us per loop
In [44]: timeit fast(a)
100000 loops, best of 3: 4.94 us per loop
In [45]: a = np.arange(100).reshape(2, 50)
In [46]: timeit slow(a)
1000 loops, best of 3: 489 us per loop
In [47]: timeit fast(a)
100000 loops, best of 3: 6.7 us per loop
[update]:
In [101]: timeit faster(a)
100000 loops, best of 3: 4.4 us per loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With