我有一个N x 2维numpy数组。我想做一个(2 * N)x 2,其中每列重复。我很好奇是否有比我下面写的更有效的方法来完成这项任务。
>>> 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]])
上面的代码是numpy标准的慢,很可能是因为zip
。是否有numpy
函数可以替换zip
?还是一个更好的方法来完成这个?
答案 0 :(得分:6)
您可以使用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)
给出
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
[更新]:
In [101]: timeit faster(a)
100000 loops, best of 3: 4.4 us per loop