I have a numpy array as follows, I want to take a random sample of n
rows.
([[996.924, 265.879, 191.655],
[996.924, 265.874, 191.655],
[996.925, 265.884, 191.655],
[997.294, 265.621, 192.224],
[997.294, 265.643, 192.225],
[997.304, 265.652, 192.223]], dtype=float32)
I've tried:
rows_id = random.sample(range(0,arr.shape[1]-1), 1)
row = arr[rows_id, :]
But this 9ndex mask only returns a single row, I want to return n
rows as an numpy array (without duplication).
CodePudding user response:
You have three key issues: arr.shape[1]
returns the number of columns, while you want the number of rows--arr.shape[0]
. Second, the second parameter to range is exclusive, so you don't really need the -1. Third, your last parameter to random.sample
is the number of rows, which you set to 1.
A better way to do what you're trying might be random.choices
instead.
CodePudding user response:
Try where x
is your original array:
n = 2 #number of rows
idx = np.random.choice(len(x), n, replace = False)
result = np.array([x[i] for i in idx])