Skip to main content

A comprehensive toolkit for cluster analysis with full pipeline support

Project description

ClusterTK

PyPI version Python 3.8+ License: MIT

A comprehensive Python toolkit for cluster analysis with full pipeline support.

ClusterTK provides a complete, sklearn-style pipeline for clustering: from raw data preprocessing to cluster interpretation and export. Perfect for data analysts who want powerful clustering without writing hundreds of lines of code.

Features

  • 🔄 Complete Pipeline - One-line solution from raw data to insights
  • 📊 Multiple Algorithms - K-Means, GMM, Hierarchical, DBSCAN
  • 🎯 Auto-Optimization - Automatic optimal cluster number selection
  • 🎨 Rich Visualization - Beautiful plots (optional dependency)
  • 📁 Export & Reports - CSV, JSON, HTML reports with embedded plots
  • 💾 Save/Load - Persist and reload fitted pipelines
  • 🔍 Interpretation - Automatic profiling and cluster naming

Quick Start

Installation

# Core functionality
pip install clustertk

# With visualization
pip install clustertk[viz]

Basic Usage

import pandas as pd
from clustertk import ClusterAnalysisPipeline

# Load data
df = pd.read_csv('your_data.csv')

# Create and fit pipeline
pipeline = ClusterAnalysisPipeline(
    handle_missing='median',
    correlation_threshold=0.85,
    n_clusters=None,  # Auto-detect optimal number
    verbose=True
)

pipeline.fit(df, feature_columns=['feature1', 'feature2', 'feature3'])

# Get results
labels = pipeline.labels_
profiles = pipeline.cluster_profiles_
metrics = pipeline.metrics_

print(f"Found {pipeline.n_clusters_} clusters")
print(f"Silhouette score: {metrics['silhouette']:.3f}")

# Export
pipeline.export_results('results.csv')
pipeline.export_report('report.html')

# Visualize (requires clustertk[viz])
pipeline.plot_clusters_2d()
pipeline.plot_cluster_heatmap()

Documentation

Pipeline Workflow

Raw Data → Preprocessing → Feature Selection → Dimensionality Reduction
→ Clustering → Evaluation → Interpretation → Export

Each step is configurable through pipeline parameters or can be run independently.

Key Capabilities

Preprocessing

  • Missing value handling (median/mean/drop)
  • Outlier detection and treatment
  • Automatic scaling (robust/standard/minmax)
  • Skewness transformation

Clustering Algorithms

  • K-Means - Fast, spherical clusters
  • GMM - Probabilistic, elliptical clusters
  • Hierarchical - Dendrograms, hierarchical structure
  • DBSCAN - Density-based, arbitrary shapes

Evaluation

  • Silhouette score
  • Calinski-Harabasz index
  • Davies-Bouldin index
  • Automatic optimal k selection

Export & Reports

  • CSV export (data + labels)
  • JSON export (metadata + profiles)
  • HTML reports with embedded visualizations
  • Pipeline serialization (save/load)

Examples

Algorithm Comparison

# Compare multiple algorithms automatically
results = pipeline.compare_algorithms(
    X=df,
    feature_columns=['feature1', 'feature2', 'feature3'],
    algorithms=['kmeans', 'gmm', 'hierarchical', 'dbscan'],
    n_clusters_range=(2, 8)
)

print(results['comparison'])  # DataFrame with metrics
print(f"Best algorithm: {results['best_algorithm']}")

# Visualize comparison
pipeline.plot_algorithm_comparison(results)

Algorithm Comparison

# Compare multiple algorithms automatically
results = pipeline.compare_algorithms(
    X=df,
    feature_columns=['feature1', 'feature2', 'feature3'],
    algorithms=['kmeans', 'gmm', 'hierarchical', 'dbscan'],
    n_clusters_range=(2, 8)
)

print(results['comparison'])  # DataFrame with metrics
print(f"Best algorithm: {results['best_algorithm']}")

# Visualize comparison
pipeline.plot_algorithm_comparison(results)

Customer Segmentation

pipeline = ClusterAnalysisPipeline(
    n_clusters=None,  # Auto-detect
    auto_name_clusters=True
)

pipeline.fit(customers_df,
            feature_columns=['age', 'income', 'purchases'],
            category_mapping={
                'demographics': ['age', 'income'],
                'behavior': ['purchases']
            })

pipeline.export_report('customer_segments.html')

Anomaly Detection

pipeline = ClusterAnalysisPipeline(
    clustering_algorithm='dbscan'
)

pipeline.fit(transactions_df)
anomalies = transactions_df[pipeline.labels_ == -1]

More examples: docs/examples.md

Requirements

  • Python 3.8+
  • numpy >= 1.20.0
  • pandas >= 1.3.0
  • scikit-learn >= 1.0.0
  • scipy >= 1.7.0
  • joblib >= 1.0.0

Optional (for visualization):

  • matplotlib >= 3.4.0
  • seaborn >= 0.11.0

Contributing

Contributions are welcome! Please check:

License

MIT License - see LICENSE file for details.

Citation

If you use ClusterTK in your research, please cite:

@software{clustertk2024,
  author = {Veselov, Aleksey},
  title = {ClusterTK: A Comprehensive Python Toolkit for Cluster Analysis},
  year = {2024},
  url = {https://github.com/alexeiveselov92/clustertk}
}

Links


Made with ❤️ for the data science community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clustertk-0.7.0.tar.gz (96.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clustertk-0.7.0-py3-none-any.whl (78.1 kB view details)

Uploaded Python 3

File details

Details for the file clustertk-0.7.0.tar.gz.

File metadata

  • Download URL: clustertk-0.7.0.tar.gz
  • Upload date:
  • Size: 96.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for clustertk-0.7.0.tar.gz
Algorithm Hash digest
SHA256 4c9e7447e3369e1741b88d8012f84bb78bf721cc3250ffcf0e2a2bb51ef3533a
MD5 33727104a1fc03c957ae98aa039231b0
BLAKE2b-256 90a3edc27d73fc5b83ce7ff1fe588b9c2ea08ebb031986f33c17197d7bce8a13

See more details on using hashes here.

File details

Details for the file clustertk-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: clustertk-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 78.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for clustertk-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 55a12352a0e9bd4ba55ba752cfc44c68226555a6f727cea28ebabb47e8ef7305
MD5 e98fce4eed32182deb1cf8e87c78798f
BLAKE2b-256 dd1ba9921901f82c00ead637064b28d0177ad2eecba2071d4f82ef6e51e496ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page