CSAIEvaluator: A Cluster Stability Assesment Index for clustering validation
Project description
CSAIEvaluator: Clustering Stability Assessment Index (CSAI)
CSAIEvaluator is a Python library for evaluating the stability and validity of clustering algorithms across multiple data partitions. It is based on the Clustering Stability Assessment Index (CSAI), which leverages feature-based information to measure the consistency and robustness clustering solutions.
Installation
pip install cluster-validity-csai
or the development version from GitHub:
pip install git+https://github.com/AdaneNT/cluster-validity-csai.git
Example Usage
Dataset
Let's use the 20 Newsgroups (20NG) dataset — a collection of approximately 20,000 newsgroup posts organized into various categories.
It is widely used for benchmarking text clustering and classification algorithms.
Load libraries
import torch
from sentence_transformers import SentenceTransformer
from sklearn.datasets import fetch_20newsgroups
from sklearn.cluster import KMeans
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import re
import umap
from csai import CSAIEvaluator
Step 1: Text embedding (e.g. SentenceTransformer)
def get_sbert_embeddings(texts):
model = SentenceTransformer("all-MiniLM-L6-v2")
return model.encode(texts, convert_to_tensor=False)
Step 2: Dimensionality reduction (e.g. UMAP)
def reduce_with_umap(df, emb_col="Embedding", output_col="key_umap", n_components=10):
reducer = umap.UMAP(n_components=n_components, random_state=42)
emb_array = np.array(df[emb_col].tolist())
reduced = reducer.fit_transform(emb_array)
df[output_col] = reduced.tolist()
return df
Step 3: Load and preprocess data (20 Newsgroups dataset)
def run_pipeline():
newsgroups = fetch_20newsgroups(subset='all')
data = newsgroups.data
df = pd.DataFrame(data, columns=["text"])
df = df.sample(n=5000, random_state=42).reset_index(drop=True)
texts = df["text"].fillna("").apply(lambda x: re.sub(r"\d+|[^\w\s]|\s+@", " ", x.lower()).strip()).tolist()
embeddings = get_sbert_embeddings(texts)
df["Embeddings"] = embeddings.tolist()
df = reduce_with_umap(df, emb_col="Embeddings", output_col="key_umap", n_components=10)
return df
df_result = run_pipeline()
Step 4: Train/test split
X_train, X_test = train_test_split(df_result, test_size=0.30, random_state=42)
Step 5: Define clustering function (e.g. K-means)
def kmeans_label_func(embeddings, n_clusters=7):
model = KMeans(n_clusters=n_clusters, random_state=42)
labels = model.fit_predict(embeddings)
return labels, model
Step 6: Apply CSAI Evaluation
csai = CSAIEvaluator()
score = csai.run_csai_evaluation(
X_train,
X_test,
key_col="key_umap",
label_func=kmeans_label_func,
n_splits=5)
print("CSAIEvaluator Score:", score)
📄 Citation
If you use this package in your work, please cite:
Tarekegn, A. N., Tessem, B., & Rabbi, F. (2025).
A New Cluster Validation Index Based on Stability Analysis.
In Proceedings of the 14th International Conference on Pattern Recognition Applications and Methods (ICPRAM),
SciTePress, pp. 377–384.
DOI: 10.5220/0013309100003905
License
This software is provided under a custom academic, non-commercial license.
See LICENSE.txt for full terms.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cluster_validity_csai-2.1.1.tar.gz.
File metadata
- Download URL: cluster_validity_csai-2.1.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b94f4f6c710c7142dd3f0278ee732b2b7cb05aae250632c9d9ff8eb60b823d8b
|
|
| MD5 |
3e6806c068b92c86bf01f051ecea65f9
|
|
| BLAKE2b-256 |
33818dc508e4770b6b3d7cad5402fe21b5309f01495e12011cd86234529cb445
|
File details
Details for the file cluster_validity_csai-2.1.1-py3-none-any.whl.
File metadata
- Download URL: cluster_validity_csai-2.1.1-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7907c5225cc109a08931df8fb688fb5b6379621b81dd2a4e46a32df2f316453b
|
|
| MD5 |
bdb32b7ee542b7bc4bc026de284f3441
|
|
| BLAKE2b-256 |
7ef8a64f3de55f13b65cb11cf635ce4680394e924522cc79d2a90a376ef92670
|