Skip to main content

clustereasy

A beginner-friendly, consistent Python API that combines K-Means and Agglomerative (Hierarchical) Clustering on pandas DataFrames into one class: Cluster.

Instead of re-learning different scikit-learn/SciPy calling conventions for each algorithm, you get one object with one set of methods for fitting, visualizing, evaluating, and exporting results — for both algorithms.

1. Why clustereasy

  • One consistent interface for two different clustering algorithms.
  • Keeps your original DataFrame untouched.
  • Clear, explicit errors instead of silently doing the wrong thing.
  • Advanced users can still pass through scikit-learn parameters.

2. Installation

Local development install (editable):

git clone https://github.com/shashikant-ds636/easycluster.git
cd easycluster
pip install -e ".[dev]"

Once published, it will be installable with:

pip install clustereasy

3. Requirements

  • Python >= 3.9
  • pandas >= 1.5
  • numpy >= 1.23
  • scikit-learn >= 1.2
  • scipy >= 1.9
  • matplotlib >= 3.6

4. Quick start

import pandas as pd
from clustereasy import Cluster

df = pd.read_csv("customers.csv")

cluster = Cluster(
    data=df,
    features=["Annual Income", "Spending Score"],
    scale=True,
    missing="median",
)

cluster.elbow_method(max_clusters=10)
cluster.fit_kmeans(n_clusters=4, random_state=42)
cluster.plot_clusters(x="Annual Income", y="Spending Score")

print(cluster.evaluate())
result_df = cluster.get_clustered_data(column_name="Segment")

5. DataFrame input requirements

Cluster(data=df, features=[...], scale=..., missing=...):

  • data must be a non-empty pandas.DataFrame. It is never modified.
  • features: optional list of column names to cluster on. Defaults to all columns. Columns must exist, be unique, and be numeric — clustereasy will not silently convert text/categorical values into numbers. Encode categorical columns yourself first (e.g. pd.get_dummies) if you want to include them.
  • missing: one of "raise" (default), "drop", "mean", "median". Controls how missing values in the selected features are handled.
  • Infinite values always raise an explicit error — they are never silently dropped or converted.
  • scale: False (default), True (same as "standard"), or one of "standard", "minmax", "robust".

6. K-Means tutorial

cluster = Cluster(data=df, features=["Income", "Score"], scale=True)

# Explore how many clusters might make sense
elbow_table = cluster.elbow_method(max_clusters=10, random_state=42)

# Fit
cluster.fit_kmeans(n_clusters=4, init="k-means++", n_init=10, random_state=42)

cluster.cluster_centers()      # DataFrame, original feature scale by default
cluster.inertia()              # WCSS for the fitted model
cluster.predict(new_df)        # assign new rows to existing clusters

Supported fit_kmeans parameters: n_clusters, init, n_init, max_iter, tol, random_state, algorithm — all pass straight through to scikit-learn's KMeans.

7. Elbow Method

elbow_method(max_clusters=10, random_state=42, show=True) fits K-Means for k = 1..max_clusters, plots k vs. WCSS (inertia), and returns a DataFrame of the results so you can reuse them without refitting.

The elbow is a heuristic, not a guaranteed optimum — use it together with silhouette_score() and your domain knowledge, not in isolation.

8. Hierarchical Clustering tutorial

Z = cluster.plot_dendrogram(method="ward", metric="euclidean")
cluster.fit_agglomerative(n_clusters=3, linkage="ward")

Supported linkage methods: ward, complete, average, single. ward linkage requires metric="euclidean" — using any other metric with ward raises a clear error instead of silently producing wrong results.

You can also cluster by distance instead of a fixed cluster count:

cluster.fit_agglomerative(n_clusters=None, distance_threshold=5.0)

distance_threshold and n_clusters are mutually exclusive, matching scikit-learn's own requirement.

Important: AgglomerativeClustering has no native predict() method in scikit-learn, and Cluster.predict() will raise an explicit error if called on an agglomerative model rather than faking support for it.

9. Dendrogram interpretation

  • Each leaf is one observation.
  • Each join (inverted "U") is a merge of two clusters; its height is the distance at which they were merged.
  • Cutting the dendrogram horizontally at a given height determines how many clusters you get at that distance.
  • For large datasets, pass truncate_mode="lastp" with a small p to avoid an unreadable plot — clustereasy will warn you above ~2000 samples if you don't.

10. Evaluation metrics and their limitations

All metrics below work without ground-truth labels:

Method Notes
silhouette_score() Needs 2..n-1 distinct clusters.
silhouette_samples() Per-sample values, same constraints.
davies_bouldin_score() Lower is better. Needs >= 2 clusters.
calinski_harabasz_score() Higher is better. Needs >= 2 clusters.
inertia() K-Means only — not available for Agglomerative models.
evaluate() One DataFrame with every metric applicable to the fitted model.

These are diagnostic tools, not proof of a "correct" clustering — always sanity-check results against domain knowledge. If a metric can't be computed (e.g. only 1 distinct cluster), evaluate() shows NaN with a note instead of crashing; the individual metric methods raise a ParameterError.

11. Feature scaling and missing-value handling

  • Distances (and therefore both K-Means and Agglomerative results) are scale -sensitive. If your features are on different scales (e.g. income in thousands vs. a 1-100 score), set scale=True.
  • K-Means cluster centers are available both in the transformed (scaled) space (cluster._model.cluster_centers_) and inverse-transformed back to the original feature scale (cluster.cluster_centers(), the default).
  • Missing values are never silently dropped by default — you must choose missing="drop"/"mean"/"median" explicitly, or clean the data yourself.

12. Visualization examples

cluster.plot_clusters(x="Income", y="Score", show_centers=True)
cluster.plot_cluster_sizes()
cluster.plot_silhouette()
cluster.plot_pca()   # for > 2 features; clearly labeled as a 2D projection

plot_clusters requires exactly two selected feature columns to display; for higher-dimensional data use plot_pca() instead, which is explicitly labeled as a projection, not the original feature space.

13. Cluster labels and DataFrame output

labels = cluster.get_labels()                       # pd.Series, original index
clustered_df = cluster.get_clustered_data("Segment") # original df + label column
summary = cluster.cluster_summary()                  # size, proportion, per-feature mean

Cluster IDs (0, 1, 2, ...) are arbitrary identifiers — they carry no inherent ranking or meaning.

14. Common errors and solutions

Error Likely cause Fix
InputValidationError: not numeric Selected a text/categorical column Encode it first, or exclude it from features
InputValidationError: missing values NaNs present, missing="raise" (default) Pass missing="drop"/"mean"/"median"
ParameterError: Ward linkage requires metric='euclidean' linkage="ward" with another metric Use metric="euclidean" or a different linkage
NotFittedError Called an evaluation/plot/output method before fit_kmeans/fit_agglomerative Fit a model first
ParameterError on predict() Called predict() on an Agglomerative model Not supported — only K-Means supports predict()

15. Computational limitations

  • Dendrograms require O(n²) memory/time for the distance matrix and are impractical much past a few thousand samples.
  • AgglomerativeClustering scales worse than KMeans for large n.
  • No GPU acceleration; this wraps scikit-learn/SciPy CPU implementations.

16. API reference (summary)

Construction: Cluster(data, features=None, scale=False, missing="raise")

K-Means: elbow_method, fit_kmeans, cluster_centers, inertia, predict

Agglomerative: plot_dendrogram, get_linkage_matrix, fit_agglomerative

Shared: plot_clusters, plot_cluster_sizes, plot_silhouette, plot_pca

Evaluation: silhouette_score, silhouette_samples, davies_bouldin_score, calinski_harabasz_score, evaluate

Output: get_labels, get_clustered_data, cluster_summary, elbow_results

17. Testing

pip install -e ".[dev]"
pytest -q

Current suite: 59 tests, all passing, covering validation, both algorithms, visualization, evaluation edge cases, and DataFrame-output guarantees (index preservation, immutability, reproducibility).

18. Contribution guidelines

  1. Fork, branch from main.
  2. Add/update tests for any behavior change.
  3. Run pytest -q before opening a PR.
  4. Keep public methods documented with docstrings.

19. Roadmap

  • DBSCAN, OPTICS, GaussianMixture support behind the same API.
  • Automatic comparison across multiple k / linkage configurations.
  • Model save/load (joblib).
  • Export results directly to CSV.
  • Optional connectivity-constrained agglomerative clustering helpers.

20. Preparing a PyPI release

pip install build twine
python -m build                 # creates dist/*.whl and dist/*.tar.gz
twine check dist/*
pip install dist/clustereasy-0.1.0-py3-none-any.whl  # test locally
twine upload --repository testpypi dist/*             # TestPyPI first
twine upload dist/*                                    # then real PyPI

Bump the version in pyproject.toml before each release and update CHANGELOG.md.

License

MIT — see LICENSE.

Metadata

Release files for clustereasy 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clustereasy 0.1.0
File Size Uploaded Provenance Actions
clustereasy-0.1.0.tar.gz 20.7 kB With provenance Details for clustereasy-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for clustereasy 0.1.0
File Interpreter ABI Platform Actions
clustereasy-0.1.0-py3-none-any.whl Python 3 none any Details for clustereasy-0.1.0-py3-none-any.whl

Total release size: 37.1 kB

Release files / clustereasy-0.1.0.tar.gz

Download URL clustereasy-0.1.0.tar.gz
Size 20.7 kB
Tags Source
SHA-256 checksum
How to use checksums
53f0264c8b60a7641398ea3fb5e5939ebeb506fad7b641379af3b2e4ae47801a
BLAKE2b-256 checksum
How to use checksums
06def7ec1379b4a3be43cdad912a8de7a18a93c35e801c45980cb4e8272d30ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Release files / clustereasy-0.1.0-py3-none-any.whl

Download URL clustereasy-0.1.0-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
81a2905df441cd49c1a28f2c3294cf2e7000d0b9fdf74bfab8e1400da3531a6d
BLAKE2b-256 checksum
How to use checksums
6447c86dea1bbc4160f8bf432cbd97f53c99ac03a454920c904f96a06b02072d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Security for clustereasy 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for clustereasy-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page