metric_learn.base_metric._TripletsClassifierMixin

class metric_learn.base_metric._TripletsClassifierMixin(preprocessor=None)[source]

Base class for triplets learners.

Methods

decision_function(triplets)

Predicts differences between sample distances in input triplets.

get_metadata_routing()

Get metadata routing of this object.

get_metric()

Returns a function that takes as input two 1D arrays and outputs the value of the learned metric on these two points.

get_params([deep])

Get parameters for this estimator.

pair_distance(pairs)

New in version 0.7.0: Compute the distance between pairs

pair_score(pairs)

New in version 0.7.0: Compute the similarity score between pairs

predict(triplets)

Predicts the ordering between sample distances in input triplets.

score(triplets)

Computes score on input triplets.

score_pairs(pairs)

Returns the score between pairs (can be a similarity, or a distance/metric depending on the algorithm)

set_decision_function_request(*[, triplets])

Request metadata passed to the decision_function method.

set_params(**params)

Set the parameters of this estimator.

set_predict_request(*[, triplets])

Request metadata passed to the predict method.

set_score_request(*[, triplets])

Request metadata passed to the score method.

__init__(preprocessor=None)
classes_ = array([0, 1])
decision_function(triplets)[source]

Predicts differences between sample distances in input triplets.

For each triplet (X_a, X_b, X_c) in the samples, computes the difference between the learned distance of the second pair (X_a, X_c) minus the learned distance of the first pair (X_a, X_b). The higher it is, the more probable it is that the pairs in the triplets are presented in the right order, i.e. that the label of the triplet is 1. The lower it is, the more probable it is that the label of the triplet is -1.

Parameters:
tripletarray-like, shape=(n_triplets, 3, n_features) or (n_triplets, 3)

3D array of triplets to predict, with each row corresponding to three points, or 2D array of indices of triplets if the metric learner uses a preprocessor.

Returns:
decision_functionnumpy.ndarray of floats, shape=(n_constraints,)

Metric differences.

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

abstract get_metric()

Returns a function that takes as input two 1D arrays and outputs the value of the learned metric on these two points. Depending on the algorithm, it can return a distance or a similarity function between pairs.

This function will be independent from the metric learner that learned it (it will not be modified if the initial metric learner is modified), and it can be directly plugged into the metric argument of scikit-learn’s estimators.

Returns:
metric_funfunction

The function described above.

See also

pair_distance

a method that returns the distance between several pairs of points. Unlike get_metric, this is a method of the metric learner and therefore can change if the metric learner changes. Besides, it can use the metric learner’s preprocessor, and works on concatenated arrays.

pair_score

a method that returns the similarity score between several pairs of points. Unlike get_metric, this is a method of the metric learner and therefore can change if the metric learner changes. Besides, it can use the metric learner’s preprocessor, and works on concatenated arrays.

Examples

>>> from metric_learn import NCA
>>> from sklearn.datasets import make_classification
>>> from sklearn.neighbors import KNeighborsClassifier
>>> nca = NCA()
>>> X, y = make_classification()
>>> nca.fit(X, y)
>>> knn = KNeighborsClassifier(metric=nca.get_metric())
>>> knn.fit(X, y) 
KNeighborsClassifier(algorithm='auto', leaf_size=30,
  metric=<function MahalanobisMixin.get_metric.<locals>.metric_fun
          at 0x...>,
  metric_params=None, n_jobs=None, n_neighbors=5, p=2,
  weights='uniform')
get_params(deep=True)

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

abstract pair_distance(pairs)

New in version 0.7.0: Compute the distance between pairs

Returns the (pseudo) distance between pairs, when available. For metric learners that do not learn a (pseudo) distance, an error is thrown instead.

Parameters:
pairsarray-like, shape=(n_pairs, 2, n_features) or (n_pairs, 2)

3D Array of pairs for which to compute the distance, with each row corresponding to two points, for 2D array of indices of pairs if the metric learner uses a preprocessor.

Returns:
scoresnumpy.ndarray of shape=(n_pairs,)

The distance between every pair.

See also

get_metric

a method that returns a function to compute the metric between two points. The difference with pair_distance is that it works on two 1D arrays and cannot use a preprocessor. Besides, the returned function is independent of the metric learner and hence is not modified if the metric learner is.

abstract pair_score(pairs)

New in version 0.7.0: Compute the similarity score between pairs

Returns the similarity score between pairs of points (the larger the score, the more similar the pair). For metric learners that learn a distance, the score is simply the opposite of the distance between pairs. All learners have access to this method.

Parameters:
pairsarray-like, shape=(n_pairs, 2, n_features) or (n_pairs, 2)

3D Array of pairs to score, with each row corresponding to two points, for 2D array of indices of pairs if the metric learner uses a preprocessor.

Returns:
scoresnumpy.ndarray of shape=(n_pairs,)

The score of every pair.

See also

get_metric

a method that returns a function to compute the metric between two points. The difference with pair_score is that it works on two 1D arrays and cannot use a preprocessor. Besides, the returned function is independent of the metric learner and hence is not modified if the metric learner is.

predict(triplets)[source]

Predicts the ordering between sample distances in input triplets.

For each triplets, returns 1 if the first element is closer to the second than to the last and -1 if not.

Parameters:
tripletsarray-like, shape=(n_triplets, 3, n_features) or (n_triplets, 3)

3D array of triplets to predict, with each row corresponding to three points, or 2D array of indices of triplets if the metric learner uses a preprocessor.

Returns:
predictionnumpy.ndarray of floats, shape=(n_constraints,)

Predictions of the ordering of pairs, for each triplet.

score(triplets)[source]

Computes score on input triplets.

Returns the accuracy score of the following classification task: a triplet (X_a, X_b, X_c) is correctly classified if the predicted similarity between the first pair (X_a, X_b) is higher than that of the second pair (X_a, X_c)

Parameters:
tripletsarray-like, shape=(n_triplets, 3, n_features) or (n_triplets, 3)

3D array of triplets to score, with each row corresponding to three points, or 2D array of indices of triplets if the metric learner uses a preprocessor.

Returns:
scorefloat

The triplets score.

abstract score_pairs(pairs)

Returns the score between pairs (can be a similarity, or a distance/metric depending on the algorithm)

Deprecated since version 0.7.0: Refer to pair_distance and pair_score.

Warning

This method will be removed in 0.8.0. Please refer to pair_distance or pair_score. This change will occur in order to add learners that don’t necessarily learn a Mahalanobis distance.

Parameters:
pairsarray-like, shape=(n_pairs, 2, n_features) or (n_pairs, 2)

3D Array of pairs to score, with each row corresponding to two points, for 2D array of indices of pairs if the metric learner uses a preprocessor.

Returns:
scoresnumpy.ndarray of shape=(n_pairs,)

The score of every pair.

See also

get_metric

a method that returns a function to compute the metric between two points. The difference between score_pairs is that it works on two 1D arrays and cannot use a preprocessor. Besides, the returned function is independent of the metric learner and hence is not modified if the metric learner is.

set_decision_function_request(*, triplets: bool | None | str = '$UNCHANGED$') _TripletsClassifierMixin

Request metadata passed to the decision_function method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to decision_function if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to decision_function.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
tripletsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for triplets parameter in decision_function.

Returns:
selfobject

The updated object.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_predict_request(*, triplets: bool | None | str = '$UNCHANGED$') _TripletsClassifierMixin

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
tripletsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for triplets parameter in predict.

Returns:
selfobject

The updated object.

set_score_request(*, triplets: bool | None | str = '$UNCHANGED$') _TripletsClassifierMixin

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
tripletsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for triplets parameter in score.

Returns:
selfobject

The updated object.