datasets
98 строк · 3.5 Кб
1# Copyright 2021 The HuggingFace Datasets Authors and the current dataset script contributor.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Mahalanobis metric."""
15
16import numpy as np17
18import datasets19
20
21_DESCRIPTION = """22Compute the Mahalanobis Distance
23
24Mahalonobis distance is the distance between a point and a distribution.
25And not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.
26It was introduced by Prof. P. C. Mahalanobis in 1936
27and has been used in various statistical applications ever since
28[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]
29"""
30
31_CITATION = """\32@article{de2000mahalanobis,
33title={The mahalanobis distance},
34author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},
35journal={Chemometrics and intelligent laboratory systems},
36volume={50},
37number={1},
38pages={1--18},
39year={2000},
40publisher={Elsevier}
41}
42"""
43
44_KWARGS_DESCRIPTION = """45Args:
46X: List of datapoints to be compared with the `reference_distribution`.
47reference_distribution: List of datapoints from the reference distribution we want to compare to.
48Returns:
49mahalanobis: The Mahalonobis distance for each datapoint in `X`.
50Examples:
51
52>>> mahalanobis_metric = datasets.load_metric("mahalanobis")
53>>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])
54>>> print(results)
55{'mahalanobis': array([0.5])}
56"""
57
58
59@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)60class Mahalanobis(datasets.Metric):61def _info(self):62return datasets.MetricInfo(63description=_DESCRIPTION,64citation=_CITATION,65inputs_description=_KWARGS_DESCRIPTION,66features=datasets.Features(67{68"X": datasets.Sequence(datasets.Value("float", id="sequence"), id="X"),69}70),71)72
73def _compute(self, X, reference_distribution):74# convert to numpy arrays75X = np.array(X)76reference_distribution = np.array(reference_distribution)77
78# Assert that arrays are 2D79if len(X.shape) != 2:80raise ValueError("Expected `X` to be a 2D vector")81if len(reference_distribution.shape) != 2:82raise ValueError("Expected `reference_distribution` to be a 2D vector")83if reference_distribution.shape[0] < 2:84raise ValueError(85"Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension"86)87
88# Get mahalanobis distance for each prediction89X_minus_mu = X - np.mean(reference_distribution)90cov = np.cov(reference_distribution.T)91try:92inv_covmat = np.linalg.inv(cov)93except np.linalg.LinAlgError:94inv_covmat = np.linalg.pinv(cov)95left_term = np.dot(X_minus_mu, inv_covmat)96mahal_dist = np.dot(left_term, X_minus_mu.T).diagonal()97
98return {"mahalanobis": mahal_dist}99