Skip to main content

📊 Spectral Bridges

codecov

sbcluster is a Python package that implements a novel clustering algorithm combining k-means and spectral clustering techniques, called Spectral Bridges. It leverages efficient affinity matrix computation and merges clusters based on a connectivity measure inspired by SVM's margin concept. This package is designed to provide robust clustering solutions, particularly suited for large datasets.


✨ Features

  • Spectral Bridges Clustering Algorithm: Integrates k-means and spectral clustering with efficient affinity matrix calculation for improved clustering results.
  • Scalability: Designed to handle large datasets by optimizing cluster formation through advanced affinity matrix computations.
  • Customizable: Parameters such as number of clusters, iterations, and random state allow flexibility in clustering configurations.
  • Model selection: Automatic model selection for number of nodes (m) according to a normalized eigengap metric.
  • scikit-learn integration: Native BaseEstimator integration with fit, predict, fit_predict, get_params, and model-selection scorers.

⚡ Speed

Spectral Bridges utilizes fastkmeanspp's efficient implementation for KMeans, which makes it remarkably fast even with large scale datasets.


🚀 Installation

pip install sbcluster

🔧 Usage

Example

from time import time

import matplotlib.pyplot as plt
import numpy as np
from sbcluster import SpectralBridges, ngap_scorer
from sklearn.cluster import SpectralClustering
from sklearn.metrics import adjusted_rand_score
from sklearn.model_selection import GridSearchCV

# Load some synthetic data
data = np.genfromtxt("data/impossible.csv", delimiter=",")
X, y = data[:, :-1], data[:, -1]

# Define the parameter grid
param_grid = {"n_clusters": [2, 3, 4, 5, 6, 7, 8, 9, 10]}
cv = [(np.arange(X.shape[0]), np.arange(X.shape[0]))] * 5

# Perform grid search for optimal parameters
grid_search = GridSearchCV(
    estimator=SpectralBridges(n_clusters=2, n_nodes=250),
    param_grid=param_grid,
    scoring=ngap_scorer,
    cv=cv,
    verbose=1,
)

# Fit the grid search
grid_search.fit(X)

# Print the results
print(grid_search.cv_results_["mean_test_score"])
print(grid_search.best_params_)

# Make predictions with the best model
guess = grid_search.best_estimator_.predict(X)
ari = adjusted_rand_score(y, guess)

# Print the ARI
print(f"Adjusted Rand Index: {ari}")

# Visualize the clustering results
plt.scatter(X[:, 0], X[:, 1], c=guess, alpha=0.1)
plt.scatter(
    grid_search.best_estimator_.subcluster_centers_[:, 0],
    grid_search.best_estimator_.subcluster_centers_[:, 1],
    c=grid_search.best_estimator_.subcluster_labels_,
    marker="X",
)
plt.title("Clustered data and centroids with best SpectralBridges fit")
plt.show()

# Compare with sklearn's SpectralClustering
sc_low = SpectralClustering(n_clusters=7).fit(X)

plt.scatter(X[:, 0], X[:, 1], c=sc_low.labels_)
plt.title("Spectral Clustering of the original dataset, gamma=1.0")
plt.show()

sc_high = SpectralClustering(n_clusters=7, gamma=5).fit(X)

plt.scatter(X[:, 0], X[:, 1], c=sc_high.labels_)
plt.title("Spectral Clustering of the original dataset, gmma=5.0")
plt.show()

# Comapre times
start = time()
grid_search.best_estimator_.fit(X)
end = time()
print("SpectralBridges fit time:", end - start)

start = time()
sc_low.fit(X)
end = time()
print("SpectralClustering fit time:", end - start)

At prediction time, SpectralBridges first assigns each sample to the nearest learned subcluster center, then maps that subcluster to its final spectral cluster label.

Results Comparison

Spectral Bridges result Spectral Clustering result Spectral Clustering result


📖 Learn More

For tutorials, API reference, visit the official site:
👉 sbcluster's documentation

Metadata

Release files for sbcluster 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sbcluster 0.5.2
File Size Uploaded
sbcluster-0.5.2.tar.gz 561.9 kB Details

Built distribution (wheel)

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

Total release size: 582.9 kB

Release files / sbcluster-0.5.2.tar.gz

Download URL sbcluster-0.5.2.tar.gz
Size 561.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1d8633c3a1a9b4d241f0300411989af028e20bd2c396dab12dfed9a35b694f47
BLAKE2b-256 checksum
How to use checksums
51e0dc1f2f8580883260252b0b67056ebd76d7449d7ce4749d1923a137b82df7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.14

Release files / sbcluster-0.5.2-py3-none-any.whl

Download URL sbcluster-0.5.2-py3-none-any.whl
Size 21.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
839d5918be958a06bc066f8bd3a3324ad1f2f50a8322b54c4330439336c49599
BLAKE2b-256 checksum
How to use checksums
57531ffdd81c79875b7a94fe38895eb1a4f86becd4e0280ec8110d6df1270b3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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