Metadata-Version: 2.4
Name: silhouetteclassifier
Version: 0.1.0
Summary: Unsupervised instance classification via improved silhouette scoring, with optional ensemble-metric refinement.
Author-email: Yoshiyasu Takefuji <takefuji@keio.jp>
Maintainer-email: Yoshiyasu Takefuji <takefuji@keio.jp>
License: MIT License
        
        Copyright (c) 2025 y-takefuji
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/y-takefuji/silhouette
Project-URL: Repository, https://github.com/y-takefuji/silhouette
Keywords: silhouette,unsupervised-learning,classification,clustering,machine-learning,imbalanced-data
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.21
Requires-Dist: pandas>=1.3
Requires-Dist: scikit-learn>=1.0
Requires-Dist: scipy>=1.7
Provides-Extra: benchmark
Requires-Dist: umap-learn>=0.5; extra == "benchmark"
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Dynamic: license-file

# 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

```bash
pip install silhouetteclassifier
```

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

```bash
pip install "silhouetteclassifier[benchmark]"
```

## Quick start

```python
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

```python
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.

```python
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](LICENSE).
