mercredi 11 juillet 2018

Sampling unique column indexes for each row of a numpy array

I want to generate a fixed number of random column indexes (without replacement) for each row of a numpy array.

A = np.array([[3, 5, 2, 3, 3],
       [1, 3, 3, 4, 5],
       [3, 5, 4, 2, 1],
       [1, 2, 3, 5, 3]])

If I fixed the required column number to 2, I want something like

np.array([[1,3],
          [0,4],
          [1,4],
          [2,3]])

I am looking for a non-loop Numpy based solution. I tried with choice, but with the replacement=False I get error

ValueError: Cannot take a larger sample than population when 'replace=False'




Aucun commentaire:

Enregistrer un commentaire