Advertisement
furas

Python - numpy - reshape

May 18th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. import numpy as np
  2.  
  3. # sample data
  4. a = np.array(range(75))
  5. a = a.reshape([1,3,5,5])
  6. print(a)
  7.  
  8. # convert --- version 3
  9.  
  10. result = np.dstack(a[0])
  11.  
  12. print(result)
  13. print(result.shape)
  14.  
  15. # convert --- version 1
  16.  
  17. result = []
  18. for x1, x2, x3 in zip(a[0,0], a[0,1], a[0,2]):
  19.     q = []
  20.     for z1, z2, z3 in zip(x1, x2, x3):
  21.         q.append([z1, z2, z3])
  22.     result.append(q)
  23.  
  24. # ---
  25. result = np.array(result)
  26. print(result)
  27. print(result.shape)
  28.  
  29. # convert --- version 2
  30.  
  31. result = []
  32. for x in zip(*a[0]):
  33.     q = []
  34.     for z in zip(*x):
  35.         q.append(z)
  36.     result.append(q)
  37.  
  38. # ---
  39. result = np.array(result)
  40. print(result)
  41. print(result.shape)
  42.  
  43. # convert --- version 3
  44.  
  45. result = [[z for z in zip(*x)] for x in zip(*a[0])]
  46.  
  47. # ---
  48. result = np.array(result)
  49. print(result)
  50. print(result.shape)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement