sanjay555

Untitled

Sep 22nd, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. # This is my (2, 3, 4) shape np array
  2. array([[[ 0,  1,  2,  3],
  3.         [ 4,  5,  6,  7],
  4.         [ 8,  9, 10, 11]],
  5.  
  6.        [[12, 13, 14, 15],
  7.         [16, 17, 18, 19],
  8.         [20, 21, 22, 23]]])
  9.  
  10. # I want to reshape the elements 8-to-15 in new 2D array
  11. # I will convert it to 1D array
  12. d321d = d.flatten()
  13.  
  14. #This is how it looks now
  15. array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
  16.        17, 18, 19, 20, 21, 22, 23])
  17.  
  18. #Now I will grab the elements that I want
  19. d1 = d321d[8:16]
  20.  
  21. #This is how it looks now
  22. array([ 8,  9, 10, 11, 12, 13, 14, 15])
  23.  
  24. # Finally I'm able to reshape above values in 2D array
  25. fd2 = np.reshape(d1, (-1, 4))
  26. #This is how it looks now
  27. #RESULT :
  28. array([[ 8,  9, 10, 11],
  29.        [12, 13, 14, 15]])
Add Comment
Please, Sign In to add comment