Skip to main content

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), embedding ((N, Q) map positions, for the maps), unit_coordinates ((K, Q), for the maps; see below), n_iter, stop_reason and history, the objective at each iteration (empty for rcm, rmcm, som and batch_som, which have none). stop_reason is the stopping rule that holds at the result's iteration: "tol" (no prototype moved more than the tolerance), "schedule" (som and batch_som: every epoch has run) or "limit" (max_iter was reached first), and None in a per-iteration result while the run goes on; none of them says that the result is good. In rcm, membership 1 puts a point in the lower approximation of a cluster; a point shared equally among clusters (1/2, 1/3, ...) is in their boundary. A pickled Result or TPE loads in the UbuKit version that wrote it; keep results across versions as arrays. Inputs should be numbers, finite and of ordinary scale; standardize features first.

Trained prototypes apply to new data, fixed: ub.apply has a function for every method, which takes the data, the prototypes (centers) and the parameters they were trained with, and returns the Result of the new data, centers unchanged. Retraining is the fitting function's job; these never move the prototypes.

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

Each method assigns once (stop_reason == "schedule"), except som_olp, whose memberships are iterated with the map fixed until they settle ("tol", or "limit" after max_iter, 1000 by default). They start from the distances alone, so for the points a map was trained on they need not end where training left them: keep the trained embedding for those, or apply to them too for positions consistent with the final map. rmcm builds its neighborhoods within the data given, so apply it to new points together. ub.steps.apply has the same functions as generators.

A search that scores many embeddings of the same X computes the distances of X once with a Neighborhood (8 N^2 bytes), which both metrics take in place of X with the same values; 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

From about five dimensions on, FCM with the default m = 2 can collapse: every prototype goes to the mean of the data, every membership to 1/K, and the run still stops with "tol". Use m closer to 1 there (1.2, for example) and check that the centers are apart; efcm collapses likewise when tau is too large (degenerate solutions).

A map's unit_coordinates are the coordinates of its units as the run used them, not normalized: (row, col) in grid steps on a rectangular grid (unit j = row * cols + col, so a plot can take x = col and y = row), or the (K, Q) array given to som or som_olp. centers[j] is the prototype of unit j, and embedding is unit_coordinates[labels] for som and batch_som and membership @ unit_coordinates for som_olp.

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, without rerunning:

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 everything else the run has learned (prototypes, schedules, and SOM-OLP's memberships while the number of rows stays the same, row i being the same point as before). 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

som, trustworthiness, continuity and ami also take engine="numba", which runs their loops compiled, with the same results: the online SOM about 8 times faster, trustworthiness and continuity about 3 times per core (25 times on 16 cores), and ami most when the labels have many distinct cluster sizes.

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 (0.3 to 1.3 s). The parallel ones (trustworthiness, continuity, ami) use every core unless NUMBA_NUM_THREADS limits them; their results do not depend on it.

Equations, defaults and references: https://github.com/subukata/ubukit/blob/main/docs/algorithms.md. Worked uses (SOM-OLP's scales, held-out data, adding points to a map, federated RCM): https://github.com/subukata/ubukit/blob/main/docs/recipes.md. A JavaScript package with the same API in camelCase is available as ubukit on npm.

Metadata

Release files for ubukit 0.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 ubukit 0.2.0
File Size Uploaded Provenance Actions
ubukit-0.2.0.tar.gz 51.1 kB With provenance Details for ubukit-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 88.9 kB

Release files / ubukit-0.2.0.tar.gz

Download URL ubukit-0.2.0.tar.gz
Size 51.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3045a750a2d42a3449780cbe22c68f4b01f68175a70f2284a807ae402b804e7b
BLAKE2b-256 checksum
How to use checksums
60cc0b31dadc597b0be304536c9426f48abe20747fa8cd8d5f2f7c97ee7ae64a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 18, 2026.

Transparency log

Release files / ubukit-0.2.0-py3-none-any.whl

Download URL ubukit-0.2.0-py3-none-any.whl
Size 37.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e167feb2cd1bc996a4b48aec9cdae18b533f4a118a6553cbd68f8e4fdd4b3ce
BLAKE2b-256 checksum
How to use checksums
7652152491bffa3b5689f61aa6a81cd2696ded27b8c2f268779ae2d85c6b0413
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 18, 2026.

Transparency log

Security for ubukit 0.2.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.

Select from 2 files

Attestation for ubukit-0.2.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 Oct 18, 2026.

Transparency log

A 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.

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