sklearn.utils.extmath
.weighted_mode¶
- sklearn.utils.extmath.weighted_mode(a, w, *, axis=0)[source]¶
Returns an array of the weighted modal (most common) value in a.
If there is more than one such value, only the first is returned. The bin-count for the modal bins is also returned.
This is an extension of the algorithm in scipy.stats.mode.
- Parameters
- aarray-like
n-dimensional array of which to find mode(s).
- warray-like
n-dimensional array of weights for each value.
- axisint, default=0
Axis along which to operate. Default is 0, i.e. the first axis.
- Returns
- valsndarray
Array of modal values.
- scorendarray
Array of weighted counts for each mode.
See also
Examples
>>> from sklearn.utils.extmath import weighted_mode >>> x = [4, 1, 4, 2, 4, 2] >>> weights = [1, 1, 1, 1, 1, 1] >>> weighted_mode(x, weights) (array([4.]), array([3.]))
The value 4 appears three times: with uniform weights, the result is simply the mode of the distribution.
>>> weights = [1, 3, 0.5, 1.5, 1, 2] # deweight the 4's >>> weighted_mode(x, weights) (array([2.]), array([3.5]))
The value 2 has the highest score: it appears twice with weights of 1.5 and 2: the sum of these is 3.5.