Last active
August 22, 2018 16:35
-
-
Save piti118/6869e860d0279f3ec9bff5e74fb1d954 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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