Download utils/cluster_analysis.py from FelixOtt/gnss_class_incremental_learning: direct link, hf CLI and curl.
- Browser
- Download file 996 Bytes
-
https://huggingface.co/datasets/FelixOtt/gnss_class_incremental_learning/resolve/main/utils/cluster_analysis.py
- Command line
-
hf download hf://datasets/FelixOtt/gnss_class_incremental_learning/utils/cluster_analysis.py
-
curl -L -o cluster_analysis.py https://huggingface.co/datasets/FelixOtt/gnss_class_incremental_learning/resolve/main/utils/cluster_analysis.py
996 Bytes
| from sklearn.metrics import davies_bouldin_score as dbs | |
| from sklearn.metrics import calinski_harabasz_score as chs | |
| def cluster_analysis(embeddings, predicted_labels, task_no): | |
| """ | |
| Calculate clustering quality metrics for the provided embeddings and labels. | |
| Args: | |
| embeddings (np.ndarray): The embeddings (n_samples, n_features) to be clustered. | |
| predicted_labels (np.ndarray): The predicted cluster labels (n_samples,) for the embeddings. | |
| Returns: | |
| tuple: A tuple containing: | |
| - Davies-Bouldin Score | |
| - Calinski-Harabasz Score | |
| - Silhouette Score (if more than 1 cluster exists) | |
| """ | |
| if len(embeddings) != len(predicted_labels): | |
| raise ValueError("Embeddings and predicted labels must have the same length.") | |
| # Calculating cluster analysis scores | |
| dbs_score = dbs(X=embeddings, labels=predicted_labels) | |
| chs_score = chs(X=embeddings, labels=predicted_labels) | |
| return dbs_score, chs_score | |