nystrom-ncut
PyTorch implementations of Normalized Cut (spectral clustering) for very large graphs, with two complementary approximations:
- Nyström extension — subsample anchor points, decompose the small anchor-anchor affinity block, and extrapolate to all remaining points (Fowlkes, Belongie, Chung, Malik 2004, Spectral Grouping Using the Nyström Method).
- Random Fourier features — replace the kernel with an explicit finite-dimensional feature map so the affinity is never materialized (Rahimi & Recht 2007, Random Features for Large-Scale Kernel Machines).
Both are built on the original normalized-cut formulation (Shi & Malik 2000, Normalized Cuts and Image Segmentation).
Installation
pip install nystrom-ncut
Requires Python >=3.10 and a working torch + pytorch3d installation.
For a CUDA build of PyTorch, install it explicitly before installing this
package, e.g.:
pip install torch --index-url https://download.pytorch.org/whl/cu121
pip install nystrom-ncut
For development:
pip install -e ".[dev]"
Quick start
Nyström Normalized Cut
import torch
from nystrom_ncut import NystromNCut, SampleConfig
# (B, H, W, C) features from some backbone
model_features = torch.rand(20, 64, 64, 768)
inp = model_features.reshape(-1, 768)
ncut = NystromNCut(
n_components=100,
affinity_type="rbf",
sample_config=SampleConfig(method="fps", num_sample=10000),
)
eigvectors = ncut.fit_transform(inp) # (20*64*64, 100)
eigvalues = ncut.eigenvalues_ # (100,)
eigvectors = eigvectors.reshape(20, 64, 64, 100)
Random-feature Kernel Normalized Cut
import torch
from nystrom_ncut import KernelNCut
inp = torch.rand(20 * 64 * 64, 768)
kn = KernelNCut(
n_components=100,
kernel_dim=1024,
affinity_type="rbf",
)
eigvectors = kn.fit_transform(inp)
Distance Realization (MDS-style embedding)
import torch
from nystrom_ncut import DistanceRealization
inp = torch.rand(10000, 768)
dr = DistanceRealization(n_components=64, distance_type="euclidean")
embedding = dr.fit_transform(inp) # (10000, 64)
Discretizing eigenvectors into clusters
AxisAlign implements the Yu & Shi 2003 multiclass discretization on top of
the spectral embedding:
from nystrom_ncut import AxisAlign
aligner = AxisAlign(sort_method="norm")
hard_labels = aligner.fit_transform(eigvectors, hard=True) # (N,) int cluster ids
Sampling
SampleConfig controls how anchor points are picked for the Nyström and
kernel methods. Available methods are:
"full"— use every point (no subsampling)."random"— uniform random subset."fps"— farthest-point sampling on a low-rank PCA of the features (default for accuracy)."fps_recursive"— iteratively refine FPS anchors using the current spectral embedding.
from nystrom_ncut import SampleConfig
cfg = SampleConfig(method="fps", num_sample=10000, fps_dim=12)
Repository layout
src/nystrom_ncut/
├── kernel/ # Random-feature NCut
├── nystrom/ # Nystrom NCut + distance realization
├── transformer/ # OnlineTransformerSubsampleFit, AxisAlign, mixins
├── common.py
├── distance_utils.py
├── extrapolation.py # KNN extrapolation of anchors to new points
├── coloring.py # RGB visualization helpers
└── sampling_utils.py
Citation
If you find this useful, please cite the upstream NCUT writeup:
AlignedCut: Visual Concepts Discovery on Brain-Guided Universal Feature Space. Huzheng Yang, James Gee, Jianbo Shi, 2024.
License
MIT.
Metadata
Release files for nystrom-ncut 0.4.2
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 |
|---|---|---|---|---|
| nystrom_ncut-0.4.2.tar.gz | 32.8 kB | Without provenance | Details for nystrom_ncut-0.4.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nystrom_ncut-0.4.2-py3-none-any.whl | Python 3 | none | any | Details for nystrom_ncut-0.4.2-py3-none-any.whl |
Total release size: 65.8 kB
Release files / nystrom_ncut-0.4.2.tar.gz
| Download URL | nystrom_ncut-0.4.2.tar.gz |
|---|---|
| Size | 32.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
93e41328826035c0fb6b2d105be088f6321b622fbd5a3fdd10a07ba599045752
|
|
BLAKE2b-256 checksum How to use checksums |
143d597887d761a17dc13f284f0aee7644ac6875a9f91f6773af67a70c91928e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.8
|
Release files / nystrom_ncut-0.4.2-py3-none-any.whl
| Download URL | nystrom_ncut-0.4.2-py3-none-any.whl |
|---|---|
| Size | 33.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d257ac47fa83aa95023c09b6836d712747f3ab1e84fee96368a3f35df96338d
|
|
BLAKE2b-256 checksum How to use checksums |
ec738883f4eb1c8b806c8b20ccb52e399cede771ea7b162f55ed82cb5c79d18e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.8
|
Security for nystrom-ncut 0.4.2
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.