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.
CodePudding user response:
You could for example create a vector with n samples from the uniform distribution, with n being the number of points and compare those with your desired probability. For all samples smaller than the probability you take the corresponding point. In code
import numpy as np
prob = 0.5
some_points = points[np.random.rand(points.shape[0]) < prob, :]
CodePudding user response:
n = points.size
some_points = points[np.random.rand(n * n) < p]