.nonzero() will only skip the 0 s, for any value that you want to skip this is a solution:
In [281]:
x = np.array([1.0,0.0,1.5,0.0,6.0])
non_zero_idx=np.argwhere(x!=0).flatten()
np.random.shuffle(non_zero_idx)
random_pick=x[non_zero_idx]
random_pick[0]
Out[281]:
1.5
In [282]:
%%timeit
x = np.array([1.0,0.0,1.5,0.0,6.0])
non_zero_idx=np.argwhere(x!=0).flatten()
np.random.shuffle(non_zero_idx)
random_pick=x[non_zero_idx]
random_pick[0]
10000 loops, best of 3: 104 µs per loop
If you are just interested in getting one random pick (rather then an array of random picks) , x[np.random.choice(non_zero_idx,1)] will be just enough. But that is actually slower:
In [286]:
%%timeit
x = np.array([1.0,0.0,1.5,0.0,6.0])
non_zero_idx=np.argwhere(x!=0).flatten()
x[np.random.choice(non_zero_idx,1)]
10000 loops, best of 3: 173 µs per loop
There are three way to get random picks, as @AndyHayden pointed out, behaving differently different array sizes:
In [299]:
X=np.random.random(100000)
In [300]:
%timeit np.random.choice(X, 1)
%timeit X[np.random.randint(0,len(X),1)]
10000 loops, best of 3: 70.3 µs per loop
100000 loops, best of 3: 12.6 µs per loop
In [301]:
%%timeit
np.random.shuffle(X)
X[0]
10 loops, best of 3: 130 ms per loop
In [302]:
X=np.random.random(10000)
In [303]:
%timeit np.random.choice(X, 1)
%timeit X[np.random.randint(0,len(X),1)]
10000 loops, best of 3: 70.1 µs per loop
100000 loops, best of 3: 12.6 µs per loop
In [304]:
%%timeit
np.random.shuffle(X)
X[0]
100 loops, best of 3: 12.6 ms per loop
Appears X[np.random.randint(0,len(X),1)] is the best.
.nonzero()to get the indices of the nonzero elements and then choose one of those. Does that make sense?