UbuKit
Fuzzy, rough and classical clustering, self-organizing maps, evaluation metrics and TPE search on NumPy and SciPy.
pip install ubukit
It needs Python 3.12 or newer.
import numpy as np
import ubukit as ub
X = np.random.default_rng(0).normal(size=(500, 2))
r = ub.fcm(X, 3, m=2.0, seed=0) # also kmeans, efcm, rcm, rmcm
r.centers, r.membership, r.labels, r.n_iter, r.stop_reason, r.history
m = ub.som_olp(X, (8, 8), lam=0.5, gamma=1.0) # also som, batch_som
m.embedding # latent positions
y = ub.kmeans(X, 3, seed=1).labels
ub.ari(y, r.labels), ub.ami(y, r.labels)
ub.trustworthiness(X, m.embedding, k=5), ub.continuity(X, m.embedding, k=5)
best = ub.minimize(
lambda p: -ub.ari(y, ub.fcm(X, 3, m=p["m"], seed=0).labels),
{"m": ub.uniform(1.1, 3.0)},
n_trials=20,
)
Every fit returns a Result: centers (K, D), labels (N,), membership
(N, K; None for kmeans, som and batch_som), for the maps embedding
(N, Q) and unit_coordinates (K, Q), n_iter, stop_reason ("tol",
"schedule" or "limit") and history, the objective at each assignment
(empty for rcm, rmcm, som and batch_som, which have none). A map's
unit j sits at unit_coordinates[j], (row, col) on a grid with
j = row * cols + col, and has the prototype centers[j].
A Result is the state of its last iteration: labels and membership come
from the last assignment and centers from the update after it, so
assigning the data to centers again, as ub.apply does, can change them,
most after an early stop ("limit"). som and batch_som instead give the
best-matching units of their final prototypes.
The stop reason says how a run stopped, not that its result is good: from
about five dimensions on, FCM with the default m = 2 can put every
prototype at the mean of the data and still stop with "tol". Use m
closer to 1 there (1.2, for example) and check that the centers are apart.
Inputs must be finite numbers of ordinary scale; standardize features first.
A pickled Result or TPE is promised to load only in the version that
wrote it. The equations, the stopping rules and when FCM and EFCM collapse are in
docs/algorithms.md.
Trained prototypes apply to new data, fixed: ub.apply has a function for
every method, which takes the data, the prototypes and the parameters they
were trained with, and returns the Result of the new data (ub.steps.apply
has the same functions as generators). The parameters have no defaults,
since only the trained ones are right: keep them in one dictionary for
both calls.
X_new = np.random.default_rng(1).normal(size=(20, 2))
new = ub.apply.som_olp(X_new, m.centers, m.unit_coordinates, lam=0.5, gamma=1.0)
new.embedding # the new points on the trained map
ub.apply.fcm(X_new, r.centers, m=2.0).membership # the m that r was trained with
Each method assigns once, except som_olp, whose memberships iterate with
the map fixed until they settle, so for the points the map was trained on
they need not end at the trained embedding; rmcm's memberships depend on
the points applied together
(applying trained prototypes).
A search that scores many embeddings of one X computes its distances once
with Neighborhood(X) (8 N^2 bytes), which both metrics take in place of
X; a list of k gives one value per k:
reference = ub.Neighborhood(X)
def layout_loss(p):
embedding = ub.som_olp(X, (8, 8), lam=p["lam"], gamma=1.0).embedding
return -ub.trustworthiness(reference, embedding, k=10)
best = ub.minimize(layout_loss, {"lam": ub.loguniform(0.05, 5.0)}, n_trials=20)
ub.continuity(reference, m.embedding, k=[5, 10, 20]) # an array of three values
ub.steps has every fitting function as a generator that yields after each
iteration (each epoch for maps). p.result() is the Result the run would
return had it stopped there, so the learning itself can be plotted or
recorded:
frames = [p.result().embedding for p in ub.steps.som_olp(X, (8, 8), lam=0.5, gamma=1.0)]
The data are an input of every iteration: run.send(X_new) runs the next
iteration on new rows and keeps what the run has learned (prototypes,
schedules, and SOM-OLP's memberships while row i stays the same point).
With max_iter=None a run goes on for as long as data keep coming:
run = ub.steps.som_olp(X, (8, 8), lam=0.5, gamma=1.0, max_iter=None)
p = next(run)
for t in range(1, 50):
p = run.send(X + 0.05 * t) # the data drift; the map follows from where it is
kmeans, fcm, efcm, rcm, som, som_olp, trustworthiness,
continuity and ami also take engine="numba", which runs their loops
compiled, with the same results (for kmeans, the same labels; for it,
the fuzzy methods and som_olp, centers and memberships equal to
rounding):
pip install "ubukit[numba]"
m = ub.som(X, (8, 8), engine="numba")
ub.trustworthiness(X, m.embedding, k=5, engine="numba")
Each kernel compiles on its first call in a process. The parallel ones
(kmeans, trustworthiness, continuity, ami) use every core unless
NUMBA_NUM_THREADS limits them; their results do not depend on it.
Worked uses: https://github.com/subukata/ubukit/blob/v0.4.0/docs/recipes.md.
A JavaScript package with the same API in camelCase is available as ubukit on npm.
Metadata
Release files for ubukit 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ubukit-0.4.0.tar.gz | 57.2 kB | With provenance | Details for ubukit-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ubukit-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for ubukit-0.4.0-py3-none-any.whl |
Total release size: 98.3 kB
Release files / ubukit-0.4.0.tar.gz
| Download URL | ubukit-0.4.0.tar.gz |
|---|---|
| Size | 57.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
346829b6563e7c0c03d15ec763be7de02768d787cbda276af639b9593661d263
|
|
BLAKE2b-256 checksum How to use checksums |
37603b34bd31d8021b53efe871310ba4d8e9a4d31fa93f486c0edf03bf825aa8
|
| 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 Dec 1, 2026.
Transparency logRelease files / ubukit-0.4.0-py3-none-any.whl
| Download URL | ubukit-0.4.0-py3-none-any.whl |
|---|---|
| Size | 41.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0a547b7803c97b361925a72b55ded275896b912d11d35fa382779d8099dc90d3
|
|
BLAKE2b-256 checksum How to use checksums |
2b08340645cd80ddb6c66994b6b7e753c994e82819ddefec8e9839d1dbeaa99b
|
| 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 Dec 1, 2026.
Transparency logSecurity for ubukit 0.4.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.
Attestation for ubukit-0.4.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 Dec 1, 2026.
Transparency logA 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.