Skip to main content

Unsupervised instance classification via improved silhouette scoring, with optional ensemble-metric refinement.

Project description

silhouetteclassifier

Unsupervised instance classification via an improved silhouette score, with an optional ensemble-metric refinement stage.

Instead of training on labels, SilhouetteClassifier ranks every instance by a weighted combination of local cohesion (closeness to nearest neighbors) and global separation (distance to the rest of the data), then partitions the ranked list according to the desired class ratios. This makes it useful for quick, label-light baselines on tabular data — including imbalanced binary problems — and for studying how internal geometry alone separates classes.

Import name is silclass (the distribution name on PyPI is silhouetteclassifier).

Installation

pip install silhouetteclassifier

For the benchmark comparison script (KMeans / UMAP / DBSCAN / MeanShift / OPTICS):

pip install "silhouetteclassifier[benchmark]"

Quick start

import pandas as pd
from silclass import SilhouetteClassifier

df = pd.read_csv("reduced.csv")
y = df["vital.status"]
X = df.drop(columns=["vital.status"])

clf = SilhouetteClassifier(n_neighbors=7)

# Supervised ratios: pass y so the class proportions are derived from labels
y_pred = clf.fit_predict(X, y=y)

# Or specify the major-class ratio directly (no labels needed)
# y_pred = clf.fit_predict(X, major_ratio=0.7, major_class=0)

print(clf.calculate_f1_scores(y))

Multi-class

y_pred = clf.fit_predict(
    X,
    class_ratios=[0.5, 0.3, 0.2],   # must sum to 1.0
    class_labels=[0, 1, 2],
)

Optional refinement

EnsembleRefinement takes initial binary labels and iteratively flips points to improve an ensemble of internal metrics (silhouette, Davies–Bouldin, Calinski–Harabasz, connectivity, density ratio). It is fully unsupervised; any true labels passed in are used only for reporting.

from silclass import EnsembleRefinement

initial = clf.fit_predict(X, y=y)
refiner = EnsembleRefinement(max_iterations=30, early_stopping=5)
refined = refiner.fit_refine(X, initial_labels=initial, y_true=y)  # y_true optional

API summary

SilhouetteClassifier(n_neighbors=15, scale_data=True)

  • fit_predict(X, y=None, major_ratio=0.5, major_class=0, class_ratios=None, class_labels=None) → labels
  • calculate_scores(X)(cohesion, separation, silhouette)
  • calculate_f1_scores(true_labels) → dict with per-class F1, F1_major, F1_minor, accuracy
  • get_instance_data() / get_sorted_instances() → per-instance scores and labels

EnsembleRefinement(...)

  • fit_refine(X, initial_labels=None, y_true=None, major_ratio=0.5) → refined labels
  • get_history() → DataFrame of per-iteration metrics

License

MIT — see LICENSE.

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

silhouetteclassifier-0.1.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

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

silhouetteclassifier-0.1.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file silhouetteclassifier-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for silhouetteclassifier-0.1.0.tar.gz
Algorithm Hash digest
SHA256 381c27075764388e3340a3249e62e22af37e14baffb2de777a86195d8f3fd76e
MD5 2d8de0e023526948049d53e42f2e245e
BLAKE2b-256 25bcd9d0c7b26a23ab37fd9f1933190c28afb0807213976d342e0d5bdbc30a9e

See more details on using hashes here.

File details

Details for the file silhouetteclassifier-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for silhouetteclassifier-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52a7a6cd46e94efc801906952a0163b1b52686ced2031e710540bfd629799497
MD5 9904b4b124d215940c1f3d747fd7ffaa
BLAKE2b-256 7a60a302e61da81a099529b9cd25a0b474665b687df5e5da88eee8a8df0efdae

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