'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 1:[1]
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, :]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Simon Hawe |
