Random-partition similarity clustering for mixed-type tabular data
Project description
forest-clustering
Random-partition similarity clustering for mixed-type tabular data.
forest-clustering builds a compact binary embedding from random feature partitions and then applies any sklearn-compatible clustering algorithm to that embedding. It handles numerical, categorical, and mixed data natively, with built-in correlation-based feature weighting and outlier-robust cut-point generation.
How it works
Each of the L iterations:
- Samples m features (weighted by inverse correlation-group size).
- Draws K−1 random cut-points per numerical feature (uniform or quantile-based).
- Assigns each sample a mixed-radix cell ID based on which bin it falls in for each selected feature.
The result is an n × L integer embedding. Two samples that consistently land in the same cell are similar; Hamming distance on this embedding approximates the true similarity.
Hamming(i, j) = (1/L) · Σ_l [ E[i,l] ≠ E[j,l] ] ∈ [0, 1]
See ALGORITHM.md for a detailed description with diagrams.
Installation
pip install forest-clustering
Python ≥ 3.10 required.
Quick start
from forest_clustering import ForestClusterer
from sklearn.cluster import KMeans
fc = ForestClusterer(
n_iterations=200,
n_bins=3,
quantile_cuts=True, # robust to outliers
corr_threshold=0.9, # down-weight correlated features
clusterer=KMeans(n_clusters=5, n_init="auto", random_state=0),
random_state=42,
)
labels = fc.fit_predict(df) # works with DataFrame or ndarray
Parameters
| Parameter | Default | Description |
|---|---|---|
n_iterations |
200 | Number of random partitioning iterations L. More → more stable. |
n_bins |
3 | Number of bins per feature per iteration K. |
n_features |
"sqrt" |
Features selected per iteration: int, float fraction, "sqrt", "log2". |
quantile_cuts |
False |
Sample cut-points from empirical quantiles (robust to outliers). |
corr_threshold |
0.7 | Spearman |r| threshold for grouping correlated features. None disables. |
corr_sample_size |
10 000 | Rows used to estimate feature correlations. |
clusterer |
DBSCAN(metric="hamming") |
Any sklearn-compatible fit_predict estimator. |
feature_types |
None |
Override type detection: {col: "numerical"|"categorical"}. |
cat_threshold |
10 | Numerical columns with ≤ this many unique values → treated as categorical. |
n_jobs |
−1 | Parallelism for embedding computation (joblib). |
random_state |
None |
Seed for reproducibility. |
Downstream clustering algorithms
| Algorithm | When to use |
|---|---|
KMeans(n_clusters=K) on embedding |
Known K, any n — fast and stable |
AgglomerativeClustering(metric="precomputed") |
n ≤ 50 K, non-spherical clusters |
DBSCAN(metric="hamming") on embedding |
Unknown K, need outlier detection |
HDBSCAN(metric="precomputed") |
Variable-density clusters (pass .astype(float64)) |
MiniBatchKMeans on embedding |
n > 100 K |
from sklearn.cluster import AgglomerativeClustering
fc = ForestClusterer(
n_iterations=300,
clusterer=AgglomerativeClustering(n_clusters=3, metric="precomputed", linkage="average"),
)
labels = fc.fit_predict(X)
Utilities
# Get the raw n×L embedding
E = fc.get_embedding()
# Pairwise Hamming distance matrix (chunked for large n)
D = fc.pairwise_distance()
# Transform new data using fitted partition specs
E_new = fc.transform(X_new)
Hyperparameter guidelines
| Goal | Recommendation |
|---|---|
| Fast prototype | n_iterations=50, n_bins=3 |
| Balanced quality/speed | n_iterations=200, n_bins=3 (default) |
| High stability | n_iterations=500, n_bins=4 |
| Outlier-heavy data | quantile_cuts=True |
| Many correlated features | corr_threshold=0.8–0.9 |
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file forest_clustering-0.4.0.tar.gz.
File metadata
- Download URL: forest_clustering-0.4.0.tar.gz
- Upload date:
- Size: 129.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
634628207e395ce80067d86ed6a908ff749ca617bb118ab636ad8767abbd9ab4
|
|
| MD5 |
3e98a48841a6de9ff468e7a4510bd6db
|
|
| BLAKE2b-256 |
11f8ccc3f71a962ad29cb65fee2ae5a9f56f959edd60d145215d12544420c071
|
File details
Details for the file forest_clustering-0.4.0-py3-none-any.whl.
File metadata
- Download URL: forest_clustering-0.4.0-py3-none-any.whl
- Upload date:
- Size: 67.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e32ebd751e0b9537dfe37b389e9afe7d72b076e022a4a1af367f6b08c3a55ce
|
|
| MD5 |
bdd111c395feb272bf71c40feb5a057d
|
|
| BLAKE2b-256 |
a85f6c0f10d368b85ebd244dc46ade46bddeef1909a3113931306c7d455befbc
|