José Ángel González commited on
Commit
509822c
·
1 Parent(s): 671ff9d

first commit

Browse files
Files changed (4) hide show
  1. app.py +1 -2
  2. clustering_evaluator.py +71 -71
  3. requirements.txt +4 -1
  4. tests.py +0 -17
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import evaluate
2
  from evaluate.utils import launch_gradio_widget
3
 
4
-
5
  module = evaluate.load("symanto/clustering_evaluator")
6
- launch_gradio_widget(module)
 
1
  import evaluate
2
  from evaluate.utils import launch_gradio_widget
3
 
 
4
  module = evaluate.load("symanto/clustering_evaluator")
5
+ launch_gradio_widget(module)
clustering_evaluator.py CHANGED
@@ -1,95 +1,95 @@
1
- # Copyright 2020 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
- """TODO: Add a description here."""
15
-
16
- import evaluate
17
  import datasets
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
-
20
- # TODO: Add BibTeX citation
21
- _CITATION = """\
22
- @InProceedings{huggingface:module,
23
- title = {A great new module},
24
- authors={huggingface, Inc.},
25
- year={2020}
 
 
 
 
26
  }
27
  """
28
 
29
- # TODO: Add description of the module here
30
  _DESCRIPTION = """\
31
- This new module is designed to solve this great ML task and is crafted with a lot of care.
32
  """
33
 
34
 
35
- # TODO: Add description of the arguments of the module here
36
  _KWARGS_DESCRIPTION = """
37
- Calculates how good are predictions given some references, using certain scores
38
  Args:
39
- predictions: list of predictions to score. Each predictions
40
- should be a string with tokens separated by spaces.
41
- references: list of reference for each prediction. Each
42
- reference should be a string with tokens separated by spaces.
43
  Returns:
44
- accuracy: description of the first score,
45
- another_score: description of the second score,
46
- Examples:
47
- Examples should be written in doctest format, and should illustrate how
48
- to use the function.
49
-
50
- >>> my_new_module = evaluate.load("my_new_module")
51
- >>> results = my_new_module.compute(references=[0, 1], predictions=[0, 1])
52
- >>> print(results)
53
- {'accuracy': 1.0}
54
  """
55
 
56
- # TODO: Define external resources urls if needed
57
- BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
58
-
59
-
60
- @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
61
- class Clusteringevaluator(evaluate.Metric):
62
- """TODO: Short description of my evaluation module."""
63
 
 
 
 
 
64
  def _info(self):
65
- # TODO: Specifies the evaluate.EvaluationModuleInfo object
66
  return evaluate.MetricInfo(
67
- # This is the description that will appear on the modules page.
68
  module_type="metric",
69
  description=_DESCRIPTION,
70
  citation=_CITATION,
71
  inputs_description=_KWARGS_DESCRIPTION,
72
- # This defines the format of each prediction and reference
73
- features=datasets.Features({
74
- 'predictions': datasets.Value('int64'),
75
- 'references': datasets.Value('int64'),
76
- }),
77
- # Homepage of the module for documentation
78
- homepage="http://module.homepage",
79
- # Additional links to the codebase or references
80
- codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
81
- reference_urls=["http://path.to.reference.url/new_module"]
82
  )
83
 
84
- def _download_and_prepare(self, dl_manager):
85
- """Optional: download external resources useful to compute the scores"""
86
- # TODO: Download external resources if needed
87
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
- def _compute(self, predictions, references):
90
- """Returns the scores"""
91
- # TODO: Compute the different scores of the module
92
- accuracy = sum(i == j for i, j in zip(predictions, references)) / len(predictions)
93
- return {
94
- "accuracy": accuracy,
95
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import datasets
2
+ import evaluate
3
+ from sklearn.metrics import (
4
+ adjusted_mutual_info_score,
5
+ adjusted_rand_score,
6
+ calinski_harabasz_score,
7
+ completeness_score,
8
+ davies_bouldin_score,
9
+ fowlkes_mallows_score,
10
+ homogeneity_score,
11
+ silhouette_score,
12
+ )
13
+ from sklearn.metrics.cluster import contingency_matrix, pair_confusion_matrix
14
 
15
+ _CITATION = """
16
+ @article{scikit-learn,
17
+ title={Scikit-learn: Machine Learning in {P}ython},
18
+ author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
19
+ and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
20
+ and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
21
+ Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
22
+ journal={Journal of Machine Learning Research},
23
+ volume={12},
24
+ pages={2825--2830},
25
+ year={2011}
26
  }
27
  """
28
 
29
+
30
  _DESCRIPTION = """\
31
+ This evaluator computes multiple clustering metrics to assess the quality of a clustering.
32
  """
33
 
34
 
 
35
  _KWARGS_DESCRIPTION = """
36
+ Computes the quality of clustering results.
37
  Args:
38
+ samples' vector representations
39
+ y: computed cluster labels
 
 
40
  Returns:
41
+ silhouete_score (float): cohesiveness and separation between clusters
42
+ davies_bouldin_score (float): average similarity measure of each cluster with its most similar cluster
43
+ calinski_harabasz_score (float): ratio of the sum of between-cluster dispersion and of within-cluster dispersion
 
 
 
 
 
 
 
44
  """
45
 
 
 
 
 
 
 
 
46
 
47
+ @evaluate.utils.file_utils.add_start_docstrings(
48
+ _DESCRIPTION, _KWARGS_DESCRIPTION
49
+ )
50
+ class ClusteringEvaluator(evaluate.Metric):
51
  def _info(self):
 
52
  return evaluate.MetricInfo(
 
53
  module_type="metric",
54
  description=_DESCRIPTION,
55
  citation=_CITATION,
56
  inputs_description=_KWARGS_DESCRIPTION,
57
+ features=datasets.Features(
58
+ {
59
+ "samples": datasets.Sequence(datasets.Value("float32")),
60
+ "predictions": datasets.Value("int64"),
61
+ }
62
+ ),
 
 
 
 
63
  )
64
 
65
+ def _compute(self, samples, predictions, truth_labels=None):
66
+ unsupervised_metrics = [
67
+ silhouette_score,
68
+ davies_bouldin_score,
69
+ calinski_harabasz_score,
70
+ ]
71
+ supervised_metrics = [
72
+ adjusted_rand_score,
73
+ adjusted_mutual_info_score,
74
+ homogeneity_score,
75
+ completeness_score,
76
+ fowlkes_mallows_score,
77
+ contingency_matrix,
78
+ pair_confusion_matrix,
79
+ ]
80
+ results = {}
81
+
82
+ # Compute unsupervised metrics always
83
+ for fn in unsupervised_metrics:
84
+ results[fn.__name__] = float(fn(samples, predictions))
85
 
86
+ # Compute supervised metrics if reference labels are passed
87
+ if truth_labels is not None:
88
+ for fn in supervised_metrics:
89
+ score = fn(truth_labels, predictions)
90
+ try:
91
+ score = float(score)
92
+ except (AttributeError, TypeError):
93
+ pass
94
+ results[fn.__name__] = score
95
+ return results
requirements.txt CHANGED
@@ -1 +1,4 @@
1
- git+https://github.com/huggingface/evaluate@main
 
 
 
 
1
+ evaluate
2
+ datasets
3
+ scikit-learn
4
+ gradio
tests.py DELETED
@@ -1,17 +0,0 @@
1
- test_cases = [
2
- {
3
- "predictions": [0, 0],
4
- "references": [1, 1],
5
- "result": {"metric_score": 0}
6
- },
7
- {
8
- "predictions": [1, 1],
9
- "references": [1, 1],
10
- "result": {"metric_score": 1}
11
- },
12
- {
13
- "predictions": [1, 0],
14
- "references": [1, 1],
15
- "result": {"metric_score": 0.5}
16
- }
17
- ]