I have a Numpy array that looks like
>>> a
array([[ 3. , 2. , -1. ],
[-1. , 0.1, 3. ],
[-1. , 2. , 3.5]])
I would like to select a value from each row at random, but I would like to exclude the -1 values from the random sampling.
What I do currently is:
x=[]
for i in range(a.shape[0]):
idx=numpy.where(a[i,:]>0)[0]
idxr=random.sample(idx,1)[0]
xi=a[i,idxr]
x.append(xi)
and get
>>> x
[3.0, 3.0, 2.0]
This is becoming a bit slow for large arrays and I would like to know if there is a way to conditionally select random values from the original a
matrix without dealing with each row individually.