Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> 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]])
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement