Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interleave a numpy array with itself

Tags:

python

numpy

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?

like image 310
Wilduck Avatar asked Sep 12 '12 21:09

Wilduck


People also ask

How do you repeat an array?

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.

How do you repeat an array and time?

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.

How do you repeat a column in a NumPy array?

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 .

How do you stack an array depth wise?

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


1 Answers

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
like image 150
DSM Avatar answered Oct 12 '22 22:10

DSM