# Fix Python – Numpy: Get random set of rows from 2D array

## Question

I have a very large 2D array which looks something like this:

``````a=
[[a1, b1, c1],
[a2, b2, c2],
...,
[an, bn, cn]]
``````

Using numpy, is there an easy way to get a new 2D array with, e.g., 2 random rows from the initial array `a` (without replacement)?

e.g.

``````b=
[[a4,  b4,  c4],
[a99, b99, c99]]
``````

Now we will see solution for issue: Numpy: Get random set of rows from 2D array

``````>>> A = np.random.randint(5, size=(10,3))
>>> A
array([[1, 3, 0],
[3, 2, 0],
[0, 2, 1],
[1, 1, 4],
[3, 2, 2],
[0, 1, 0],
[1, 3, 1],
[0, 4, 1],
[2, 4, 2],
[3, 3, 1]])
>>> idx = np.random.randint(10, size=2)
>>> idx
array([7, 6])
>>> A[idx,:]
array([[0, 4, 1],
[1, 3, 1]])
``````

Putting it together for a general case:

``````A[np.random.randint(A.shape[0], size=2), :]
``````

For non replacement (numpy 1.7.0+):

``````A[np.random.choice(A.shape[0], 2, replace=False), :]
``````

I do not believe there is a good way to generate random list without replacement before 1.7. Perhaps you can setup a small definition that ensures the two values are not the same.

This question is answered By – Daniel