rank#

Methods to rank examples in standard (multi-class) classification datasets by cleanlab’s label quality score. Except for order_label_issues, which operates only on the subset of the data identified as potential label issues/errors, the methods in this module can be used on whichever subset of the dataset you choose (including the entire dataset) and provide a label quality score for every example. You can then do something like: np.argsort(label_quality_score) to obtain ranked indices of individual datapoints based on their quality.

Note: multi-label classification is not supported by most methods in this module, each example must be labeled as belonging to a single class, e.g. format: labels = np.ndarray([1,0,2,1,1,0...]).

Note: Label quality scores are most accurate when they are computed based on out-of-sample pred_probs from your model. To obtain out-of-sample predicted probabilities for every datapoint in your dataset, you can use cross-validation. This is encouraged to get better results.

Functions:

find_top_issues(quality_scores, *[, top])

Returns the sorted indices of the top issues in quality_scores, ordered from smallest to largest quality score (i.e., from most to least likely to be an issue).

get_confidence_weighted_entropy_for_each_label(...)

Returns the "confidence weighted entropy" label-quality score for each datapoint.

get_label_quality_ensemble_scores(labels, ...)

Returns label quality scores based on predictions from an ensemble of models.

get_label_quality_scores(labels, pred_probs, *)

Returns a label quality score for each datapoint.

get_normalized_margin_for_each_label(labels, ...)

Returns the "normalized margin" label-quality score for each datapoint.

get_self_confidence_for_each_label(labels, ...)

Returns the self-confidence label-quality score for each datapoint.

order_label_issues(label_issues_mask, ...[, ...])

Sorts label issues by label quality score.

cleanlab.rank.find_top_issues(quality_scores, *, top=10)[source]#

Returns the sorted indices of the top issues in quality_scores, ordered from smallest to largest quality score (i.e., from most to least likely to be an issue). For example, the first value returned is the index corresponding to the smallest value in quality_scores (most likely to be an issue). The second value in the returned array is the index corresponding to the second smallest value in quality-scores (second-most likely to be an issue), and so forth.

This method assumes that quality_scores shares an index with some dataset such that the indices returned by this method map to the examples in that dataset.

Parameters:
  • quality_scores (ndarray) – Array of shape (N,), where N is the number of examples, containing one quality score for each example in the dataset.

  • top (int) – The number of indices to return.

Return type:

ndarray

Returns:

top_issue_indices – Indices of top examples most likely to suffer from an issue (ranked by issue severity).

cleanlab.rank.get_confidence_weighted_entropy_for_each_label(labels, pred_probs)[source]#

Returns the “confidence weighted entropy” label-quality score for each datapoint.

This is a function to compute label-quality scores for classification datasets, where lower scores indicate labels less likely to be correct.

“confidence weighted entropy” is the normalized entropy divided by “self-confidence”.

Parameters:
Return type:

ndarray

Returns:

label_quality_scores (np.ndarray) – Contains one score (between 0 and 1) per example. Lower scores indicate more likely mislabeled examples.

cleanlab.rank.get_label_quality_ensemble_scores(labels, pred_probs_list, *, method='self_confidence', adjust_pred_probs=False, weight_ensemble_members_by='accuracy', custom_weights=None, log_loss_search_T_values=[0.0001, 0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 200.0], verbose=True)[source]#

Returns label quality scores based on predictions from an ensemble of models.

This is a function to compute label-quality scores for classification datasets, where lower scores indicate labels less likely to be correct.

Ensemble scoring requires a list of pred_probs from each model in the ensemble.

For each pred_probs in list, compute label quality score. Take the average of the scores with the chosen weighting scheme determined by weight_ensemble_members_by.

Score is between 0 and 1:

  • 1 — clean label (given label is likely correct).

  • 0 — dirty label (given label is likely incorrect).

Parameters:
  • labels (np.ndarray) – Labels in the same format expected by the get_label_quality_scores function.

  • pred_probs_list (List[np.ndarray]) – Each element in this list should be an array of pred_probs in the same format expected by the get_label_quality_scores function. Each element of pred_probs_list corresponds to the predictions from one model for all examples.

  • method ({"self_confidence", "normalized_margin", "confidence_weighted_entropy"}, default "self_confidence") – Label quality scoring method. See get_label_quality_scores for scenarios on when to use each method.

  • adjust_pred_probs (bool, optional) – adjust_pred_probs in the same format expected by the get_label_quality_scores function.

  • weight_ensemble_members_by ({"uniform", "accuracy", "log_loss_search", "custom"}, default "accuracy") –

    Weighting scheme used to aggregate scores from each model:

    • ”uniform”: Take the simple average of scores.

    • ”accuracy”: Take weighted average of scores, weighted by model accuracy.

    • ”log_loss_search”: Take weighted average of scores, weighted by exp(t * -log_loss) where t is selected from log_loss_search_T_values parameter and log_loss is the log-loss between a model’s pred_probs and the given labels.

    • ”custom”: Take weighted average of scores using custom weights that the user passes to the custom_weights parameter.

  • custom_weights (np.ndarray, default None) – Weights used to aggregate scores from each model if weight_ensemble_members_by=”custom”. Length of this array must match the number of models: len(pred_probs_list).

  • log_loss_search_T_values (List, default [1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 2e2]) – List of t values considered if weight_ensemble_members_by=”log_loss_search”. We will choose the value of t that leads to weights which produce the best log-loss when used to form a weighted average of pred_probs from the models.

  • verbose (bool, default True) – Set to False to suppress all print statements.

Return type:

ndarray

Returns:

label_quality_scores (np.ndarray) – Contains one score (between 0 and 1) per example. Lower scores indicate more likely mislabeled examples.

cleanlab.rank.get_label_quality_scores(labels, pred_probs, *, method='self_confidence', adjust_pred_probs=False)[source]#

Returns a label quality score for each datapoint.

This is a function to compute label quality scores for standard (multi-class) classification datasets, where lower scores indicate labels less likely to be correct.

Score is between 0 and 1.

1 - clean label (given label is likely correct). 0 - dirty label (given label is likely incorrect).

Parameters:
  • labels (np.ndarray) – A discrete vector of noisy labels, i.e. some labels may be erroneous. Format requirements: for dataset with K classes, labels must be in 0, 1, …, K-1. Note: multi-label classification is not supported by this method, each example must belong to a single class, e.g. format: labels = np.ndarray([1,0,2,1,1,0...]).

  • pred_probs (np.ndarray, optional) –

    An array of shape (N, K) of model-predicted probabilities, P(label=k|x). Each row of this matrix corresponds to an example x and contains the model-predicted probabilities that x belongs to each possible class, for each of the K classes. The columns must be ordered such that these probabilities correspond to class 0, 1, …, K-1.

    Note: Returned label issues are most accurate when they are computed based on out-of-sample pred_probs from your model. To obtain out-of-sample predicted probabilities for every datapoint in your dataset, you can use cross-validation. This is encouraged to get better results.

  • method ({"self_confidence", "normalized_margin", "confidence_weighted_entropy"}, default "self_confidence") –

    Label quality scoring method.

    Letting k = labels[i] and P = pred_probs[i] denote the given label and predicted class-probabilities for datapoint i, its score can either be:

    • 'normalized_margin': P[k] - max_{k' != k}[ P[k'] ]

    • 'self_confidence': P[k]

    • 'confidence_weighted_entropy': entropy(P) / self_confidence

    Let C = {0, 1, ..., K-1} denote the specified set of classes for our classification task.

    The normalized_margin score works better for identifying class conditional label errors, i.e. examples for which another label in C is appropriate but the given label is not.

    The self_confidence score works better for identifying alternative label issues corresponding to bad examples that are: not from any of the classes in C, well-described by 2 or more labels in C, or generally just out-of-distribution (ie. anomalous outliers).

  • adjust_pred_probs (bool, optional) – Account for class imbalance in the label-quality scoring by adjusting predicted probabilities via subtraction of class confident thresholds and renormalization. Set this to True if you prefer to account for class-imbalance. See Northcutt et al., 2021.

Return type:

ndarray

Returns:

label_quality_scores (np.ndarray) – Contains one score (between 0 and 1) per example. Lower scores indicate more likely mislabeled examples.

cleanlab.rank.get_normalized_margin_for_each_label(labels, pred_probs)[source]#

Returns the “normalized margin” label-quality score for each datapoint.

This is a function to compute label-quality scores for classification datasets, where lower scores indicate labels less likely to be correct.

Letting k denote the given label for a datapoint, the normalized margin is (p(label = k) - max(p(label != k))), i.e. the probability of the given label minus the probability of the argmax label that is not the given label (normalized_margin = prob_label - max_prob_not_label). This gives you an idea of how likely an example is BOTH its given label AND not another label, and therefore, scores its likelihood of being a good label or a label error.

Normalized margin works better for finding class conditional label errors where there is another label in the set of classes that is clearly better than the given label.

Parameters:
Return type:

ndarray

Returns:

label_quality_scores (np.ndarray) – Contains one score (between 0 and 1) per example. Lower scores indicate more likely mislabeled examples.

cleanlab.rank.get_self_confidence_for_each_label(labels, pred_probs)[source]#

Returns the self-confidence label-quality score for each datapoint.

This is a function to compute label-quality scores for classification datasets, where lower scores indicate labels less likely to be correct.

The self-confidence is the classifier’s predicted probability that an example belongs to its given class label.

Self-confidence can work better than normalized-margin for detecting label errors due to out-of-distribution (OOD) or weird examples vs. label errors in which labels for random examples have been replaced by other classes.

Parameters:
Return type:

ndarray

Returns:

label_quality_scores (np.ndarray) – Contains one score (between 0 and 1) per example. Lower scores indicate more likely mislabeled examples.

cleanlab.rank.order_label_issues(label_issues_mask, labels, pred_probs, *, rank_by='self_confidence', rank_by_kwargs={})[source]#

Sorts label issues by label quality score.

Default label quality score is “self_confidence”.

Parameters:
  • label_issues_mask (np.ndarray) – A boolean mask for the entire dataset where True represents a label issue and False represents an example that is accurately labeled with high confidence.

  • labels (np.ndarray) – Labels in the same format expected by the get_label_quality_scores function.

  • pred_probs (np.ndarray (shape (N, K))) – Predicted-probabilities in the same format expected by the get_label_quality_scores function.

  • rank_by (str, optional) – Score by which to order label error indices (in increasing order). See the method argument of get_label_quality_scores.

  • rank_by_kwargs (dict, optional) – Optional keyword arguments to pass into get_label_quality_scores function. Accepted args include adjust_pred_probs.

Return type:

ndarray

Returns:

label_issues_idx (np.ndarray) – Return an array of the indices of the examples with label issues, ordered by the label-quality scoring method passed to rank_by.