Return an array of the weighted modal (most common) value in the passed array.
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.
Array of which values to find mode(s).
Array of weights for each value.
Axis along which to operate. Default is 0, i.e. the first axis.
Array of modal values.
Array of weighted counts for each mode.
See also
scipy.stats.modeCalculates the Modal (most common) value of array elements along specified axis.
>>> 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.
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.utils.extmath.weighted_mode.html