Guest User

Untitled

a guest
Nov 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. import numpy as np
  2.  
  3. # Data size
  4. N = 5
  5. M = 5
  6.  
  7. # How many entries to replace
  8. REPLACE_COUNT = 4
  9.  
  10. # What to replace with
  11. REPLACE_WITH = 10
  12.  
  13. # Just for clarity of display
  14. np.set_printoptions(precision=2, suppress=True)
  15.  
  16. # Geherate some data.
  17. data = np.random.rand(N, M)
  18.  
  19. print('Original: ')
  20. print(data)
  21.  
  22. # Replacement is done by replacing randon items in flattened view.
  23. # This boils down to selecting numbers from 0 to N * M.
  24. # Notice replace=False, so we don't select the same number two times.
  25. # Instead of using N * M one could use np.prod(data.shape)
  26. data.flat[np.random.choice(N * M, REPLACE_COUNT, replace=False)] = REPLACE_WITH
  27. print('After replacement: ')
  28. print(data)
Add Comment
Please, Sign In to add comment