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'
You can use
random.choice()
as following:Demo:
As an experimental approach here is a way that in 99% of the times will give unique indices:
This function will return
IndexError
at linereturn ind[index][:4]
if there's no 4 unique items in that case you can repeat the function to make sure you'll get the desire result.Here's one vectorized approach inspired by
this post
-Sample run -
Like this?