MCPcopy Create free account
hub / github.com/modAL-python/modAL / weighted_random

Function weighted_random

modAL/utils/selection.py:86–103  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

84
85
86def 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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…