How to select random rows from a NumPy array in Python

Selecting random rows from a NumPy array results in a new array with a specified number of rows from the original. All rows are equally likely to be selected. For example, randomly selecting 2 rows from [[a, a, a], [b, b, b],…, [z, z, z]] could result in [[r, r, r], [b, b, b]].

Solution for How to select random rows from a NumPy array in Python : You can use numpy.random.choice() to select random rows from a NumPy array Use numpy.random.choice(a, size=k, replace=False) to generate a list of k random indices without repetition from a NumPy array with a rows. Subset the array with this list to select k random rows.

If repeated rows are desirable, omit the replace keyword argument.


how-to-select-random-rows-from-a-numpy-array-in-python