Skip to main content

KScorer: Auto-select optimal K-means clusters with advanced scoring

Basic Usage

LIVE demo-notebook is here

Load Modules

In [1]: import numpy as np
   ...: import pandas as pd
   ...: from sklearn import datasets
   ...: from sklearn.metrics import balanced_accuracy_score
   ...: from sklearn.model_selection import train_test_split
   ...: from kscorer.kscorer import KScorer

Init KScorer

In [2]: ks = KScorer()

Get Data

In [3]: X, y = datasets.load_digits(return_X_y=True)
   ...: X.shape
Out[3]: (1797, 64)

Train/Test Split

In [4]: X_train, X_test, y_train, y_test = train_test_split(
   ...:     X, y, test_size=0.2, random_state=1234)

Fit KScorer (i.e. Perform Unsupervised Clustering)

In [5]: labels, centroids, _ = ks.fit_predict(X_train, retall=True)
100%|██████████| 13/13 [00:09<00:00,  1.39it/s]

Optimal Clusters

In [6]: ks.show()

image

In [7]: ks.optimal_
Out[7]: 10

Confusion Matrix

In [8]: labels_mtx = (pd.Series(y_train)
   ...:               .groupby([labels, y_train])
   ...:               .count()
   ...:               .unstack()
   ...:               .fillna(0))
   ...: # match arbitrary labels to ground-truth labels
   ...: order = []
   ...: 
   ...: for i, r in labels_mtx.iterrows():
   ...:     left = [x for x in np.unique(y_train) if x not in order]
   ...:     order.append(r.iloc[left].idxmax())
   ...: 
   ...: confusion_mtx = labels_mtx[order]
   ...: confusion_mtx
Out[8]:
5 9 4 2 0 6 1 7 8 3
0 124.0 5.0 1.0 0.0 0.0 0.0 2.0 7.0 4.0 2.0
1 12.0 95.0 0.0 0.0 0.0 0.0 0.0 0.0 9.0 90.0
2 2.0 0.0 122.0 0.0 1.0 2.0 0.0 1.0 0.0 0.0
3 0.0 0.0 0.0 108.0 0.0 0.0 22.0 0.0 1.0 20.0
4 1.0 2.0 1.0 0.0 147.0 0.0 0.0 0.0 0.0 0.0
5 2.0 1.0 0.0 0.0 2.0 145.0 3.0 0.0 4.0 0.0
6 0.0 1.0 2.0 22.0 0.0 0.0 67.0 7.0 57.0 6.0
7 0.0 5.0 8.0 0.0 0.0 0.0 0.0 130.0 4.0 6.0
8 0.0 15.0 0.0 9.0 0.0 0.0 0.0 0.0 57.0 21.0
9 0.0 22.0 3.0 0.0 0.0 1.0 48.0 0.0 6.0 2.0

Cluster Unseen Data (you would prefer to build classifier instead)

In [9]: labels_unseen = ks.predict(X_test, init=centroids)

Evaluate Accuracy

In [10]: y_clustd = pd.Series(labels).replace(dict(enumerate(order)))
    ...: y_unseen = pd.Series(labels_unseen).replace(dict(enumerate(order)))
In [11]: balanced_accuracy_score(y_train, y_clustd)  # train data
Out[11]: 0.6940733254455871
In [12]: balanced_accuracy_score(y_test, y_unseen)  # unseen data
Out[12]: 0.646615365026082

ToDo:

Metadata

Release files for kscorer 2.2.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 kscorer 2.2.0
File Size Uploaded
kscorer-2.2.0.tar.gz 46.1 kB Details

Built distribution (wheel)

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

Total release size: 78.8 kB

Release files / kscorer-2.2.0.tar.gz

Download URL kscorer-2.2.0.tar.gz
Size 46.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f831fd3502b02e48f73b79be6e62f7bcb44482e57ea32d69bb0a2175ead08be7
BLAKE2b-256 checksum
How to use checksums
ef1ef5090f265c209121b2b4df8551efa218668b74bdf9a8d150250bfd207bbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.11.9

Release files / kscorer-2.2.0-py3-none-any.whl

Download URL kscorer-2.2.0-py3-none-any.whl
Size 32.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fbcb55bb887e4261e908c696d3e5a3a1d04c39ce437b7c79f071fc9e63b25a11
BLAKE2b-256 checksum
How to use checksums
2f645338696d1b42ed9a88abde63c5d2293f636b963bf9c9e2ff6daf33a26d7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 release files

2.1.0

2 release files

2.0.1

2 release files

1.2.5

2 release files

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