Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- # Data size
- N = 5
- M = 5
- # How many entries to replace
- REPLACE_COUNT = 4
- # What to replace with
- REPLACE_WITH = 10
- # Just for clarity of display
- np.set_printoptions(precision=2, suppress=True)
- # Geherate some data.
- data = np.random.rand(N, M)
- print('Original: ')
- print(data)
- # Replacement is done by replacing randon items in flattened view.
- # This boils down to selecting numbers from 0 to N * M.
- # Notice replace=False, so we don't select the same number two times.
- # Instead of using N * M one could use np.prod(data.shape)
- data.flat[np.random.choice(N * M, REPLACE_COUNT, replace=False)] = REPLACE_WITH
- print('After replacement: ')
- print(data)
Add Comment
Please, Sign In to add comment