SKLearner Home | About | Contact | Examples

Scikit-Learn v_measure_score() Metric

V-measure is an entropy-based clustering evaluation metric that measures the homogeneity and completeness of clusters. It provides a single score that indicates how well the clusters created by an algorithm match the true cluster assignments.

The v_measure_score() function in scikit-learn calculates V-measure by computing the harmonic mean of homogeneity and completeness scores. Homogeneity ensures that each cluster contains only members of a single class, while completeness ensures that all members of a given class are assigned to the same cluster. The score ranges from 0 to 1, with 1 indicating perfect clustering.

V-measure is useful in clustering problems where you need to evaluate the quality of the clusters produced by an algorithm. However, it does not account for the actual data distribution and should not be used for determining the number of clusters.

from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
from sklearn.cluster import KMeans
from sklearn.metrics import v_measure_score

# Generate synthetic dataset
X, y = make_blobs(n_samples=1000, centers=3, random_state=42)

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a KMeans clustering model
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X_train)

# Predict on test set
y_pred = kmeans.predict(X_test)

# Calculate V-measure score
v_measure = v_measure_score(y_test, y_pred)
print(f"V-measure: {v_measure:.2f}")

Running the example gives an output like:

V-measure: 1.00

The steps are as follows:

  1. Generate a synthetic clustering dataset using make_blobs().
  2. Split the dataset into training and test sets with train_test_split().
  3. Train a KMeans clustering model on the training set.
  4. Predict cluster labels for the test set using predict().
  5. Evaluate the clustering performance using v_measure_score() by comparing true labels to predicted labels.

First, we generate a synthetic dataset with 1000 samples and 3 clusters using the make_blobs() function from scikit-learn. This allows us to simulate a clustering problem without relying on real-world data.

Next, we split the dataset into training and test sets using the train_test_split() function, reserving 20% of the data for testing. This step ensures we can evaluate our clustering model on unseen data.

We then train a KMeans clustering model using the KMeans class from scikit-learn, specifying 3 clusters and a random state for reproducibility. The fit() method is called with the training features (X_train) to create the clusters.

After training, we use the trained KMeans model to predict cluster labels for the test set by calling the predict() method with X_test. This generates the predicted cluster labels.

Finally, we calculate the V-measure score using the v_measure_score() function. This function takes the true labels (y_test) and the predicted labels (y_pred) as input and computes the V-measure score. The resulting score is printed, providing a quantitative measure of the clustering model’s performance.

This example shows how to use the v_measure_score() function from scikit-learn to evaluate the performance of a clustering model.



See Also