title: Choosing a random sample from each row of Numpy array, excluding negative numbers
body: <p>I have a Numpy array that looks like</p>
  
  <pre><code>&gt;&gt;&gt; a
  array([[ 3. ,  2. , -1. ],
         [-1. ,  0.1,  3. ],
         [-1. ,  2. ,  3.5]])
  </code></pre>
  
  <p>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.</p>
  
  <p>What I do currently is:</p>
  
  <pre><code>x=[]
  for i in range(a.shape[0]):
      idx=numpy.where(a[i,:]&gt;0)[0]
      idxr=random.sample(idx,1)[0]
      xi=a[i,idxr]
      x.append(xi)
  </code></pre>
  
  <p>and get</p>
  
  <pre><code>&gt;&gt;&gt; x
  [3.0, 3.0, 2.0]
  </code></pre>
  
  <p>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 <code>a</code> matrix without dealing with each row individually.</p>