Search code examples
pythonnumpyprobability

How to select points from matrix with given probability?


I have a numpy.ndarray points representing a set of coordinate points:

[[0 0]
 [0 1]
 [0 2]
 [1 0]
 [1 1]
 [1 2]
 [2 0]
 [2 1]
 [2 2]]

I want to get a subset of this points some_points where each point is independently taken with probability prob. How can I do this using only numpy?

P.S. For instance, if prob=0.5 some_points will contain about a half of original points.


Solution

  • n = points.size
    some_points = points[np.random.rand(n * n) < p]