Update Space (evaluate main: 4adef25e)
Browse files- README.md +88 -5
- app.py +6 -0
- label_distribution.py +93 -0
- requirements.txt +3 -0
README.md
CHANGED
@@ -1,12 +1,95 @@
|
|
1 |
---
|
2 |
title: Label Distribution
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
title: Label Distribution
|
3 |
+
emoji: 🤗
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.0.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
tags:
|
11 |
+
- evaluate
|
12 |
+
- measurement
|
13 |
+
description: >-
|
14 |
+
Returns the label distribution and skew of the input data.
|
15 |
---
|
16 |
|
17 |
+
# Measurement Card for Label Distribution
|
18 |
+
|
19 |
+
## Measurement Description
|
20 |
+
The label distribution measurements returns the fraction of each label represented in the dataset.
|
21 |
+
|
22 |
+
## Intended Uses
|
23 |
+
|
24 |
+
Calculating the distribution of labels in a dataset allows to see how balanced the labels in your dataset are, which
|
25 |
+
can help choosing a relevant metric (e.g. accuracy when the dataset is balanced, versus F1 score when there is an
|
26 |
+
imbalance).
|
27 |
+
|
28 |
+
## How to Use
|
29 |
+
|
30 |
+
The measurement takes a list of labels as input:
|
31 |
+
|
32 |
+
```python
|
33 |
+
from evaluate import load
|
34 |
+
>>> distribution = evaluate.load("label_distribution")
|
35 |
+
>>> data = [1, 0, 2, 2, 0, 0, 0, 0, 0, 2]
|
36 |
+
>>> results = distribution.compute(data=data)
|
37 |
+
```
|
38 |
+
|
39 |
+
### Inputs
|
40 |
+
- **data** (`list`): a list of integers or strings containing the data labels.
|
41 |
+
|
42 |
+
### Output Values
|
43 |
+
By default, this metric outputs a dictionary that contains :
|
44 |
+
-**label_distribution** (`dict`) : a dictionary containing two sets of keys and values: `labels`, which includes the list of labels contained in the dataset, and `fractions`, which includes the fraction of each label.
|
45 |
+
-**label_skew** (`scalar`) : the asymmetry of the label distribution.
|
46 |
+
|
47 |
+
```python
|
48 |
+
{'label_distribution': {'labels': [1, 0, 2], 'fractions': [0.1, 0.6, 0.3]}, 'label_skew': 0.7417688338666573}
|
49 |
+
```
|
50 |
+
|
51 |
+
If skewness is 0, the dataset is perfectly balanced; if it is less than -1 or greater than 1, the distribution is highly skewed; anything in between can be considered moderately skewed.
|
52 |
+
|
53 |
+
#### Values from Popular Papers
|
54 |
+
|
55 |
+
|
56 |
+
### Examples
|
57 |
+
Calculating the label distribution of a dataset with binary labels:
|
58 |
+
|
59 |
+
```python
|
60 |
+
>>> data = [1, 0, 1, 1, 0, 1, 0]
|
61 |
+
>>> distribution = evaluate.load("label_distribution")
|
62 |
+
>>> results = distribution.compute(data=data)
|
63 |
+
>>> print(results)
|
64 |
+
{'label_distribution': {'labels': [1, 0], 'fractions': [0.5714285714285714, 0.42857142857142855]}}
|
65 |
+
```
|
66 |
+
|
67 |
+
Calculating the label distribution of the test subset of the [IMDb dataset](https://huggingface.co/datasets/imdb):
|
68 |
+
```python
|
69 |
+
>>> from datasets import load_dataset
|
70 |
+
>>> imdb = load_dataset('imdb', split = 'test')
|
71 |
+
>>> distribution = evaluate.load("label_distribution")
|
72 |
+
>>> results = distribution.compute(data=imdb['label'])
|
73 |
+
>>> print(results)
|
74 |
+
{'label_distribution': {'labels': [0, 1], 'fractions': [0.5, 0.5]}, 'label_skew': 0.0}
|
75 |
+
```
|
76 |
+
N.B. The IMDb dataset is perfectly balanced.
|
77 |
+
|
78 |
+
The output of the measurement can easily be passed to matplotlib to plot a histogram of each label:
|
79 |
+
|
80 |
+
```python
|
81 |
+
>>> data = [1, 0, 2, 2, 0, 0, 0, 0, 0, 2]
|
82 |
+
>>> distribution = evaluate.load("label_distribution")
|
83 |
+
>>> results = distribution.compute(data=data)
|
84 |
+
>>> plt.bar(results['label_distribution']['labels'], results['label_distribution']['fractions'])
|
85 |
+
>>> plt.show()
|
86 |
+
```
|
87 |
+
|
88 |
+
## Limitations and Bias
|
89 |
+
While label distribution can be a useful signal for analyzing datasets and choosing metrics for measuring model performance, it can be useful to accompany it with additional data exploration to better understand each subset of the dataset and how they differ.
|
90 |
+
|
91 |
+
## Citation
|
92 |
+
|
93 |
+
## Further References
|
94 |
+
- [Facing Imbalanced Data Recommendations for the Use of Performance Metrics](https://sites.pitt.edu/~jeffcohn/skew/PID2829477.pdf)
|
95 |
+
- [Scipy Stats Skew Documentation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.skew.html#scipy-stats-skew)
|
app.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import evaluate
|
2 |
+
from evaluate.utils import launch_gradio_widget
|
3 |
+
|
4 |
+
|
5 |
+
module = evaluate.load("label_distribution", module_type="measurement")
|
6 |
+
launch_gradio_widget(module)
|
label_distribution.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2022 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 |
+
"""Label Distribution Measurement."""
|
15 |
+
|
16 |
+
from collections import Counter
|
17 |
+
|
18 |
+
import datasets
|
19 |
+
import pandas as pd
|
20 |
+
from scipy import stats
|
21 |
+
|
22 |
+
import evaluate
|
23 |
+
|
24 |
+
|
25 |
+
_DESCRIPTION = """
|
26 |
+
Returns the label ratios of the dataset labels, as well as a scalar for skewness.
|
27 |
+
"""
|
28 |
+
|
29 |
+
_KWARGS_DESCRIPTION = """
|
30 |
+
Args:
|
31 |
+
`data`: a list containing the data labels
|
32 |
+
|
33 |
+
Returns:
|
34 |
+
`label_distribution` (`dict`) : a dictionary containing two sets of keys and values: `labels`, which includes the list of labels contained in the dataset, and `fractions`, which includes the fraction of each label.
|
35 |
+
`label_skew` (`scalar`) : the asymmetry of the label distribution.
|
36 |
+
Examples:
|
37 |
+
>>> data = [1, 0, 1, 1, 0, 1, 0]
|
38 |
+
>>> distribution = evaluate.load("label_distribution")
|
39 |
+
>>> results = distribution.compute(data=data)
|
40 |
+
>>> print(results)
|
41 |
+
{'label_distribution': {'labels': [1, 0], 'fractions': [0.5714285714285714, 0.42857142857142855]}, 'label_skew': -0.2886751345948127}
|
42 |
+
"""
|
43 |
+
|
44 |
+
_CITATION = """\
|
45 |
+
@ARTICLE{2020SciPy-NMeth,
|
46 |
+
author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and
|
47 |
+
Haberland, Matt and Reddy, Tyler and Cournapeau, David and
|
48 |
+
Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and
|
49 |
+
Bright, Jonathan and {van der Walt}, St{\'e}fan J. and
|
50 |
+
Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and
|
51 |
+
Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and
|
52 |
+
Kern, Robert and Larson, Eric and Carey, C J and
|
53 |
+
Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and
|
54 |
+
{VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and
|
55 |
+
Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and
|
56 |
+
Harris, Charles R. and Archibald, Anne M. and
|
57 |
+
Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and
|
58 |
+
{van Mulbregt}, Paul and {SciPy 1.0 Contributors}},
|
59 |
+
title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific
|
60 |
+
Computing in Python}},
|
61 |
+
journal = {Nature Methods},
|
62 |
+
year = {2020},
|
63 |
+
volume = {17},
|
64 |
+
pages = {261--272},
|
65 |
+
adsurl = {https://rdcu.be/b08Wh},
|
66 |
+
doi = {10.1038/s41592-019-0686-2},
|
67 |
+
}
|
68 |
+
"""
|
69 |
+
|
70 |
+
|
71 |
+
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
72 |
+
class LabelDistribution(evaluate.Measurement):
|
73 |
+
def _info(self):
|
74 |
+
return evaluate.MeasurementInfo(
|
75 |
+
module_type="measurement",
|
76 |
+
description=_DESCRIPTION,
|
77 |
+
citation=_CITATION,
|
78 |
+
inputs_description=_KWARGS_DESCRIPTION,
|
79 |
+
features=[
|
80 |
+
datasets.Features({"data": datasets.Value("int32")}),
|
81 |
+
datasets.Features({"data": datasets.Value("string")}),
|
82 |
+
],
|
83 |
+
)
|
84 |
+
|
85 |
+
def _compute(self, data):
|
86 |
+
"""Returns the fraction of each label present in the data"""
|
87 |
+
c = Counter(data)
|
88 |
+
label_distribution = {"labels": [k for k in c.keys()], "fractions": [f / len(data) for f in c.values()]}
|
89 |
+
if isinstance(data[0], str):
|
90 |
+
label2id = {label: id for id, label in enumerate(label_distribution["labels"])}
|
91 |
+
data = [label2id[d] for d in data]
|
92 |
+
skew = stats.skew(data)
|
93 |
+
return {"label_distribution": label_distribution, "label_skew": skew}
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/evaluate@a45df1eb9996eec64ec3282ebe554061cb366388
|
2 |
+
datasets~=2.0
|
3 |
+
scipy
|