Returns n_instances indices based on the weights. Args: weights: Contains the weights of the sampling. n_instances: Specifies how many indices to return. Returns: n_instances random indices based on the weights.
(weights: np.ndarray, n_instances: int = 1)
| 84 | |
| 85 | |
| 86 | def weighted_random(weights: np.ndarray, n_instances: int = 1) -> np.ndarray: |
| 87 | """ |
| 88 | Returns n_instances indices based on the weights. |
| 89 | |
| 90 | Args: |
| 91 | weights: Contains the weights of the sampling. |
| 92 | n_instances: Specifies how many indices to return. |
| 93 | |
| 94 | Returns: |
| 95 | n_instances random indices based on the weights. |
| 96 | """ |
| 97 | assert n_instances <= weights.shape[0], 'n_instances must be less or equal than the size of utility' |
| 98 | weight_sum = np.sum(weights) |
| 99 | assert weight_sum > 0, 'the sum of weights must be larger than zero' |
| 100 | |
| 101 | random_idx = np.random.choice( |
| 102 | range(len(weights)), size=n_instances, p=weights/weight_sum, replace=False) |
| 103 | return random_idx |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…