Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. >>> a = np.array([[1,2,3,4],
  2. [2,4,6,8]])
  3. >>> b = np.array(zip(a.T,a.T))
  4. >>> b.shape = (2*len(a[0]), 2)
  5. >>> b.T
  6. array([[1, 1, 2, 2, 3, 3, 4, 4],
  7. [2, 2, 4, 4, 6, 6, 8, 8]])
  8.  
  9. import numpy as np
  10.  
  11. def slow(a):
  12. b = np.array(zip(a.T,a.T))
  13. b.shape = (2*len(a[0]), 2)
  14. return b.T
  15.  
  16. def fast(a):
  17. return a.repeat(2).reshape(2, 2*len(a[0]))
  18.  
  19. def faster(a):
  20. # compliments of WW
  21. return a.repeat(2, axis=1)
  22.  
  23. In [42]: a = np.array([[1,2,3,4],[2,4,6,8]])
  24.  
  25. In [43]: timeit slow(a)
  26. 10000 loops, best of 3: 59.4 us per loop
  27.  
  28. In [44]: timeit fast(a)
  29. 100000 loops, best of 3: 4.94 us per loop
  30.  
  31. In [45]: a = np.arange(100).reshape(2, 50)
  32.  
  33. In [46]: timeit slow(a)
  34. 1000 loops, best of 3: 489 us per loop
  35.  
  36. In [47]: timeit fast(a)
  37. 100000 loops, best of 3: 6.7 us per loop
  38.  
  39. In [101]: timeit faster(a)
  40. 100000 loops, best of 3: 4.4 us per loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement