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?
You could use
repeat:gives
[update]: