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.1.tar.gz (16.1 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.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: silhouetteclassifier-0.1.1.tar.gz
  • Upload date:
  • Size: 16.1 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.1.tar.gz
Algorithm Hash digest
SHA256 136c548446e5e416f1e8a83548b37fe422329e31d81ccb6266cbd72a1ad1d965
MD5 0452538a38a3b3284280ceadb0d4ed0d
BLAKE2b-256 b707263ec1435509cf45f70b40e11b6db35c4aeb8d1b849be407763500cc9c06

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for silhouetteclassifier-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9a5011a88e6aaa0f3ea35754c77bcb6e4a1b31a134fef4724f183600d02b42cd
MD5 f58e682f9d24095c368cf03eb461faa4
BLAKE2b-256 dd4ee27e667dd0cfd9becbc8906e6aa66036d00d1cabdafd9c206f92e7e02775

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