Skip to content

Instantly share code, notes, and snippets.

@davidechicco
Last active November 16, 2024 23:32
Show Gist options
  • Save davidechicco/cf91c1679c819687b87ca0843d2869a2 to your computer and use it in GitHub Desktop.
Save davidechicco/cf91c1679c819687b87ca0843d2869a2 to your computer and use it in GitHub Desktop.
# Piece of Python code developed by Davide Chicco on 17th November 2024
# developed for Python 3.11 on Linux Xubuntu 22
# released with creative commons license
# contact: [email protected]
import numpy as np
from permetrics import ClusteringMetric
from dbcv import dbcv
print("\n : : : : permetrics example : : : : ")
# data matrix
data = np.array([[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10]])
# labels array
labels = np.array([0, 0, 1, 1, 1])
# Calculate DBCV score
result = dbcv(X=data, y=labels)
print("FelSiq/DBCV =", result)
cm = ClusteringMetric(X=data, y_pred=labels)
print("permetrics dbcvi =", cm.DBCVI())
print("\n : : : : Stack Overflow question example : : : : ")
# https://stackoverflow.com/a/79188273
dataSO = np.array([[1, 1], # Cluster 0
[1.2, 1.1],
[0.8, 1.2],
[4, 4], # Cluster 1
[4.2, 4.1],
[3.8, 4.2]])
labelsSO = np.array([0, 0, 0, 1, 1, 1])
resultSO = dbcv(X=dataSO, y=labelsSO)
print("FelSiq/DBCV =", resultSO)
cmSO = ClusteringMetric(X=dataSO, y_pred=labelsSO)
print("permetrics dbcvi =", cmSO.DBCVI())
print("\n : : : : other example : : : : ")
# Generate random data for each cluster
cluster1 = np.random.normal(loc=0, scale=0.3, size=(25, 2))
cluster2 = np.random.normal(loc=3, scale=0.3, size=(25, 2))
cluster3 = np.random.normal(loc=6, scale=0.3, size=(25, 2))
# Combine the clusters into one dataset
example_data_user = np.vstack((cluster1, cluster2, cluster3))
# Create cluster labels
cluster_labels_user = np.array([1]*25 + [2]*25 + [3]*25)
resultUser = dbcv(X=example_data_user, y=cluster_labels_user)
print("FelSiq/DBCV =", resultUser)
cmUser = ClusteringMetric(X=example_data_user, y_pred=cluster_labels_user)
print("permetrics dbcvi =", cmUser.DBCVI(), "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment