A versatile two-phase clustering algorithm designed for datasets with both known and exploratory components.
Project description
CLustering In Multiphase Boundaries (CLiMB)
A versatile two-phase clustering algorithm designed for datasets with both known and exploratory components.
Features
- Two-Phase Clustering: Combines constrained clustering with exploratory clustering to identify both known and novel patterns.
- Density-Aware: Uses local density estimation to intelligently filter and assign points.
- Flexible Exploratory Phase: Supports multiple clustering algorithms (DBSCAN, HDBSCAN, OPTICS) through a strategy pattern.
- Visualization Tools: Built-in 2D and 3D visualization capabilities for cluster analysis.
- Parameter Tuning: Builder pattern for flexible parameter adjustment.
- Customizable Distance Metrics: Now supports various distance metrics such as Euclidean, Mahalanobis, and custom metrics, offering greater flexibility in distance calculation.
- Advanced Seed Points: Ability to initialize clustering with known seed points provided in a dictionary structure, allowing for more precise control over centroid initialization.
Installation
pip install climb-astro
Or install from source:
git clone https://github.com/LorenzoMonti/CLiMB.git
cd CLiMB
pip install -e .
Quick Start
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
from CLiMB.core.CLiMB import CLiMB
from CLiMB.exploratory.DBSCANExploratory import DBSCANExploratory
# The number of centers to generate
centers = 4
# Generate synthetic data with 5 dimensions
X, y = make_blobs(n_samples=500, centers=centers, n_features=5, random_state=42)
# Scale the data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Create seed points (optional)
seed_points = np.array([
X[y == i].mean(axis=0) for i in range(centers)
])
seed_points_scaled = scaler.transform(seed_points)
# Example of seed points as a dictionary for more precise control
seed_dict_scaled = {
tuple(seed_points_scaled[0]): [tuple(X_scaled[y == 0][0]), tuple(X_scaled[y == 0][1])], # Centroid 1 and associated seed points
tuple(seed_points_scaled[1]): [tuple(X_scaled[y == 1][0])], # Centroid 2 and associated seed points
tuple(seed_points_scaled[2]): [], # Centroid 3 with no specific seed points
tuple(seed_points_scaled[3]): [tuple(X_scaled[y == 3][0]), tuple(X_scaled[y == 3][1]), tuple(X_scaled[y == 3][2])] # Centroid 4 and seed points
}
# Initialize and fit CLiMB with Mahalanobis metric and dictionary seed points
climb = CLiMB(
constrained_clusters=4,
seed_points=seed_dict_scaled, # Use the dictionary of seed points
density_threshold=0.15,
distance_threshold=2.5,
radial_threshold=1.2,
convergence_tolerance=0.05,
distance_metric='euclidean',
metric_params=None,
exploratory_algorithm=DBSCANExploratory(0.5)
)
climb.fit(X_scaled)
# Get cluster labels
labels = climb.get_labels()
# Visualize results (only possible in lower dimensions)
climb.inverse_transform(scaler)
fig = climb.plot_comprehensive_3d(save_path="./3d")
fig2 = climb.plot_comprehensive_2d(save_path="./2d")
Examples
See the examples/ directory for detailed usage examples:
simple_example.py: Basic usage with well-defined clusterscomplex_mixed.py: Handling mixed data with both convex and non-convex clustersmixed_seeds.py: Handling mixed data with both convex and non-convex clusters with seedscompare_methods.py: Comparing different exploratory clustering methods
How It Works
CLiMB operates in two phases:
-
Constrained Phase (KBound): A modified K-means that:
- Uses seed points to guide initial clustering
- Applies density and distance constraints
- Prevents centroids from drifting too far using radial thresholds
- Supports customizable distance metrics through the distance_metric and metric_params parameters.
- Handles advanced seed points via a dictionary structure for more controlled initialization.
-
Exploratory Phase: Uses density-based clustering methods to discover patterns in points not assigned during the first phase.
Use Cases
CLiMB is particularly useful for:
- Datasets with partially known structure
- Astronomical data analysis
- Particle physics clustering
- Pattern discovery in scientific datasets
- Data exploration with prior knowledge
Advanced Usage
Using Different Exploratory Algorithms
from CLiMB.core.CLiMB import CLiMB
from CLiMB.exploratory.HDBSCANExploratory import HDBSCANExploratory
# Create HDBSCAN exploratory algorithm
hdbscan = HDBSCANExploratory(min_cluster_size=5, min_samples=3)
# Use it with CLIMB
climb = CLiMB(
constrained_clusters=3,
exploratory_algorithm=hdbscan
)
Parameter Tuning with Builder Pattern
climb = CLiMB()
climb.set_density(0.3) \
.set_distance(2.5) \
.set_radial(1.0) \
.set_convergence(0.1)
Using Custom Distance Metrics
To use distance metrics other than Euclidean, you can use the distance_metric and metric_params parameters in the KBound class.
Example with Mahalanobis Metric:
import numpy as np
from CLiMB.core.KBound import KBound
# ... (Load or generate your data X) ...
# Calculate the inverse covariance matrix (VI) for Mahalanobis
covariance_matrix = np.cov(X.T)
inv_covariance_matrix = np.linalg.inv(covariance_matrix)
# Initialize KBound with the Mahalanobis metric and parameters
kbound = KBound(
n_clusters=3,
distance_metric='mahalanobis',
metric_params={'VI': inv_covariance_matrix}
)
kbound.fit(X)
If you do not provide metric_params for the Mahalanobis metric, the inverse covariance matrix will be automatically calculated on the input data X during fit.
Example with Custom Metric:
import numpy as np
from CLiMB.core.KBound import KBound
from scipy.spatial.distance import euclidean
# Define your custom distance metric
def custom_distance(u, v):
# Example: weighted Euclidean distance, where the first dimension counts double
weight = np.array([2, 1, 1]) # Weights for each dimension
return euclidean(u * weight, v * weight)
# Initialize KBound with the custom metric
kbound = KBound(
n_clusters=3,
distance_metric='custom',
metric_params={'func': custom_distance}
)
kbound.fit(X)
Advanced Usage of Seed Points with Dictionary
In addition to the list of seed points, you can now provide a dictionary to initialize centroids and associate specific seed points with each centroid. This offers more granular control over the initialization of constrained clustering.
Example of Seed Point Dictionary:
import numpy as np
from CLiMB.core.KBound import KBound
# ... (Load or generate your data X and define initial centroids and seed points) ...
# Define a dictionary of seed points
# Keys are initial centroids (points), values are lists of seed points associated with that centroid
seed_dict = {
tuple(initial_centroids[0]): [tuple(seed_point_cluster1_1), tuple(seed_point_cluster1_2)],
tuple(initial_centroids[1]): [tuple(seed_point_cluster2_1)],
tuple(initial_centroids[2]): [], # No specific seed points for this centroid
# ... and so on for all centroids ...
}
# Initialize KBound with the seed point dictionary
kbound = KBound(
n_clusters=len(initial_centroids),
seeds=seed_dict
# ... and so on for the other parameters
)
kbound.fit(X)
License
MIT
Tests Status
Citation
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 climb_astro-0.2.1.tar.gz.
File metadata
- Download URL: climb_astro-0.2.1.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5db28e1906c6b486d2288a9ef8eb11fd53b703ae2b4a0961b4de9444bebbd9c
|
|
| MD5 |
b58b023b05e3ef899b64744b3715dc8f
|
|
| BLAKE2b-256 |
5dda843f06db81333e61cb7c9081ddaf361f52af53e78b4d751d863ab5b4e597
|
File details
Details for the file climb_astro-0.2.1-py3-none-any.whl.
File metadata
- Download URL: climb_astro-0.2.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c39269318838babe1f1b60990771a4377214579644dc244ae17047b35e40156b
|
|
| MD5 |
0a45ae02a2d924be64c26312b12aaeba
|
|
| BLAKE2b-256 |
bcc7184245597e7cdd87ad67cc5c84b94850b2e2a9fa2649ed0e8535fc78d53f
|