DBSCAN Calculator
GitHub Link to Code.
DBSCAN calculator implementation.
This module provides the DBSCANCalculator class that performs the actual DBSCAN clustering computation using scikit-learn.
- class mdxplain.clustering.cluster_type.dbscan.dbscan_calculator.DBSCANCalculator(cache_path: str = './cache', max_memory_gb: float = 2.0, chunk_size: int = 1000, use_memmap: bool = False, max_blas_threads: int | None = 1, auto_limit_blas: bool = True)
Bases:
CalculatorBaseCalculator for DBSCAN clustering.
This class implements the actual DBSCAN clustering computation using scikit-learn’s DBSCAN implementation and computes clustering quality metrics.
Examples
>>> # Create calculator and compute clustering >>> calc = DBSCANCalculator() >>> data = np.random.rand(100, 10) >>> labels, metadata = calc.compute(data, eps=0.5, min_samples=5) >>> print(f"Found {metadata['n_clusters']} clusters")
- __init__(cache_path: str = './cache', max_memory_gb: float = 2.0, chunk_size: int = 1000, use_memmap: bool = False, max_blas_threads: int | None = 1, auto_limit_blas: bool = True) None
Initialize DBSCAN calculator.
Parameters
- cache_pathstr, optional
Path for cache files. Default is ‘./cache’.
- max_memory_gbfloat, optional
Maximum memory threshold in GB. Default is 2.0.
- chunk_sizeint, optional
Chunk size for processing large datasets in sampling methods. Used for chunked k-NN prediction and precomputed distance matrix. Default is 1000.
- use_memmapbool, optional
Whether to use memory mapping for large datasets. Default is False.
- max_blas_threadsint or None, default=1
Preferred BLAS/OpenMP thread limit; set auto_limit_blas=False to disable thread limiting, or None to fall back to a safe default
- auto_limit_blasbool, default=True
Apply a safe thread policy: use BLAS=1 when n_jobs != 1, otherwise use max_blas_threads (fallback 2 when None)
- compute(data: ndarray, center_method: str = 'centroid', **kwargs) Tuple[ndarray, Dict[str, Any]]
Compute DBSCAN clustering.
Parameters
- datanumpy.ndarray
Input data matrix to cluster, shape (n_samples, n_features)
- center_methodstr, optional
Method for calculating cluster centers, default=”centroid”:
“centroid”: Representative point (medoid - closest to mean)
“mean”: Average of cluster members
“median”: Coordinate-wise median (robust to outliers)
“density_peak”: Point with highest local density
“median_centroid”: Medoid from median (more robust to outliers)
“rmsd_centroid”: Centroid using RMSD metric (better for structural comparisons)
- kwargsdict
DBSCAN parameters including:
eps : float, maximum distance between samples
min_samples : int, minimum samples in neighborhood
method : str, clustering method (‘standard’, ‘precomputed’, ‘knn_sampling’)
sample_fraction : float, fraction of data to sample
n_jobs : int, number of parallel jobs (-1 uses all processors)
force : bool, override memory and dimensionality checks
Returns
- Tuple[numpy.ndarray, Dict]
Tuple containing:
cluster_labels: Cluster labels for each sample (-1 for noise)
metadata: Dictionary with clustering information
Raises
- ValueError
If input data is invalid or required parameters are missing