Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

utils.py 1.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  1. import numpy as np
  2. import pandas as pd
  3. from sklearn.metrics import adjusted_mutual_info_score
  4. import yaml
  5. def read_config(
  6. config_path: str
  7. ):
  8. with open(config_path) as file:
  9. config_file = yaml.safe_load(file)
  10. return config_file
  11. def show_mutial_info(
  12. df: pd.DataFrame
  13. ) -> None:
  14. scores = uniform_labelings_scores(score_func, n_samples, n_clusters_range)
  15. print("done in %0.3fs" % (time() - t0))
  16. plots.append(
  17. plt.errorbar(n_clusters_range, np.median(scores, axis=1), scores.std(axis=1))[0]
  18. )
  19. names.append(score_func.__name__)
  20. def uniform_labelings_scores(
  21. score_func, n_samples, n_clusters_range, fixed_n_classes=None, n_runs=5, seed=42
  22. ):
  23. """Compute score for 2 random uniform cluster labelings.
  24. Both random labelings have the same number of clusters for each value
  25. possible value in ``n_clusters_range``.
  26. When fixed_n_classes is not None the first labeling is considered a ground
  27. truth class assignment with fixed number of classes.
  28. """
  29. random_labels = np.random.RandomState(seed).randint
  30. scores = np.zeros((len(n_clusters_range), n_runs))
  31. if fixed_n_classes is not None:
  32. labels_a = random_labels(low=0, high=fixed_n_classes, size=n_samples)
  33. for i, k in enumerate(n_clusters_range):
  34. for j in range(n_runs):
  35. if fixed_n_classes is None:
  36. labels_a = random_labels(low=0, high=k, size=n_samples)
  37. labels_b = random_labels(low=0, high=k, size=n_samples)
  38. scores[i, j] = score_func(labels_a, labels_b)
  39. return scores
  40. def ami_score(U, V):
  41. return adjusted_mutual_info_score(U, V)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...