import numpy as np
from sklearn.utils import check_array, check_consistent_length
from ._label import is_labeled, is_unlabeled
from ._label_encoder import ExtLabelEncoder
from ._label_dtype import _as_label_array
from ._selection import rand_argmax
[docs]
def compute_vote_vectors(y, w=None, classes=None, missing_label=np.nan):
"""Counts number of votes per class label for each sample.
Parameters
----------
y : array-like, shape (n_samples) or (n_samples, n_annotators)
Class labels, which may contain also missing labels.
w : array-like, shape (n_samples) or (n_samples, n_annotators) or None,\
default=None
Class label weights. One-dimensional weights apply to every annotation
of the corresponding sample. Two-dimensional weights specify one weight
per annotation. If None, every annotation has unit weight.
classes : array-like of shape (n_classes,), default=None
Holds the label for each class.
missing_label : scalar or string or np.nan orNone, default=np.nan
Value to represent a missing label.
Returns
-------
v : numpy.ndarray of shape (n_samples, n_classes)
`V[i,j]` counts number of votes per class `j` for sample `i`.
"""
# check input parameters
le = ExtLabelEncoder(classes=classes, missing_label=missing_label)
y = le.fit_transform(y)
n_classes = len(le.classes_)
y = y if y.ndim == 2 else y.reshape((-1, 1))
is_unlabeled_y = is_unlabeled(y, missing_label=-1)
y[is_unlabeled_y] = 0
y = y.astype(int)
w = _check_vote_weights(y, w)
# count class labels per class and weight by confidence scores
w[np.logical_or(np.isnan(w), is_unlabeled_y)] = 0
y_off = y + np.arange(y.shape[0])[:, None] * n_classes
v = np.bincount(
y_off.ravel(), minlength=y.shape[0] * n_classes, weights=w.ravel()
)
return v.reshape(-1, n_classes)
[docs]
def majority_vote(
y, w=None, classes=None, missing_label=np.nan, random_state=None
):
"""Assigns a label to each sample based on weighted voting.
Samples with no labels are assigned with `missing_label`.
A matrix without any observed label therefore aggregates to
`missing_label` for every sample. That result needs no class vocabulary,
so it is returned for `classes=None` as well.
Parameters
----------
y : array-like, shape (n_samples,) or (n_samples, n_annotators)
Class labels, which may contain also missing labels.
w : array-like, shape (n_samples) or (n_samples, n_annotators) or None,\
default=None
Class label weights. One-dimensional weights apply to every annotation
of the corresponding sample. Two-dimensional weights specify one weight
per annotation. If None, every annotation has unit weight.
classes : array-like of shape (n_classes,), default=None
Holds the label for each class.
missing_label : scalar or string or np.nan or None, default=np.nan
Value to represent a missing label.
random_state : int or RandomState instance or None, default=None
Determines random number generation for shuffling the data. Pass an int
for reproducible results across multiple function calls.
Returns
-------
y_aggregated : numpy.ndarray of shape (n_samples,)
Assigned labels for each sample.
"""
# check input parameters
y = check_array(
_as_label_array(y),
ensure_2d=False,
dtype=None,
ensure_all_finite=False,
)
y = y if y.ndim == 2 else y.reshape((-1, 1))
n_samples = y.shape[0]
w = _check_vote_weights(y, w)
is_labeled_y = np.any(is_labeled(y, missing_label), axis=1)
y_labeled = y[is_labeled_y]
le = ExtLabelEncoder(classes=classes, missing_label=missing_label)
if not np.any(is_labeled_y):
if classes is not None:
le.fit(y)
return y[:, 0].copy()
le.fit(y)
y_aggregated = np.full((n_samples,), missing_label, dtype=le._dtype)
y_labeled_transformed = le.transform(y_labeled)
vote_matrix = compute_vote_vectors(
y_labeled_transformed,
w=w[is_labeled_y],
missing_label=-1,
classes=np.arange(len(le.classes_)),
)
vote_vector = rand_argmax(vote_matrix, random_state, axis=1)
y_labeled_inverse_transformed = le.inverse_transform(vote_vector)
y_aggregated[is_labeled_y] = y_labeled_inverse_transformed
return y_aggregated
def _check_vote_weights(y, w):
"""Validate weights against a two-dimensional annotation matrix."""
if w is None:
return np.ones(y.shape)
w = check_array(
w, ensure_2d=False, ensure_all_finite=False, dtype=float, copy=True
)
check_consistent_length(y, w)
if w.ndim == 1:
w = np.repeat(w[:, np.newaxis], y.shape[1], axis=1)
elif w.shape != y.shape:
raise ValueError(
"`w` must have shape `(n_samples,)` or the same shape as `y`."
)
return w