Guest User

Untitled

a guest
Jan 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. a[~(a==0).all(1)]
  2.  
  3. In [1]: from numpy import *
  4.  
  5. In [2]: a = array([[4, 1, 1, 2, 0, 4],
  6. [3, 4, 3, 1, 4, 4],
  7. [1, 4, 3, 1, 0, 0],
  8. [0, 4, 4, 0, 4, 3],
  9. [0, 0, 0, 0, 0, 0]])
  10.  
  11. In [3]: print a==0
  12. [[False False False False True False]
  13. [False False False False False False]
  14. [False False False False True True]
  15. [ True False False True False False]
  16. [ True True True True True True]]
  17.  
  18. In [6]: print (a==0).all(1)
  19. [False False False False True]
  20.  
  21. In [7]: print ~(a==0).all(1)
  22. [ True True True True False]
  23.  
  24. In [8]: print a[~(a==0).all(1)]
  25. [[4 1 1 2 0 4]
  26. [3 4 3 1 4 4]
  27. [1 4 3 1 0 0]
  28. [0 4 4 0 4 3]]
  29.  
  30. In [1]: from numpy import *
  31.  
  32. In [2]: a = matrix([[4, 1, 1, 2, 0, 4],
  33. [3, 4, 3, 1, 4, 4],
  34. [1, 4, 3, 1, 0, 0],
  35. [0, 4, 4, 0, 4, 3],
  36. [0, 0, 0, 0, 0, 0]])
  37.  
  38. In [3]: print a==0
  39. [[False False False False True False]
  40. [False False False False False False]
  41. [False False False False True True]
  42. [ True False False True False False]
  43. [ True True True True True True]]
  44.  
  45.  
  46. In [5]: print (a==0).all(1)
  47. [[False]
  48. [False]
  49. [False]
  50. [False]
  51. [ True]]
  52.  
  53. In [6]: print (a==0).all(1).A1
  54. [False False False False True]
  55.  
  56. In [7]: print ~(a==0).all(1).A1
  57. [ True True True True False]
  58.  
  59. In [8]: print a[~(a==0).all(1).A1]
  60. [[4 1 1 2 0 4]
  61. [3 4 3 1 4 4]
  62. [1 4 3 1 0 0]
  63. [0 4 4 0 4 3]]
  64.  
  65. import numpy as np
  66. m=np.matrix([[1,2,3],[0,0,0], [4,5,6]])
  67. m_nonzero_rows = m[[i for i, x in enumerate(m) if x.any()]]
Add Comment
Please, Sign In to add comment