Skip to content

Instantly share code, notes, and snippets.

@piti118
Last active August 22, 2018 16:35
Show Gist options
  • Save piti118/6869e860d0279f3ec9bff5e74fb1d954 to your computer and use it in GitHub Desktop.
Save piti118/6869e860d0279f3ec9bff5e74fb1d954 to your computer and use it in GitHub Desktop.
import numpy as np
def random_mask(a, n_choose):
ret = a.copy()
indices = np.random.choice(np.nonzero(a)[0], n_choose, replace=False)
ret[:] = 0
ret[indices] = a[indices]
return ret
def random_flip(a, n_flip):
ret = a.copy()
indices = np.random.choice(np.nonzero(a)[0], n_flip, replace=False)
ret[indices] = -a[indices]
return ret
def magic(a, n_choose, n_flip):
return random_flip(random_mask(a, n_choose), n_flip)
#n = 100
#a = np.random.randint(-1, 2, n)
#magic(a, 20, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment