DenMune#
- class denmune_skl.DenMune(k_nearest=10, reduce_dims=True, target_dims=2, dim_reducer='tsne', metric='euclidean', metric_params=None, n_jobs=None, random_state=None)#
DenMune: A density-peak based clustering algorithm.
This implementation is a refactored and optimized version of the original algorithm published in Pattern Recognition (2021). It adheres to the scikit-learn API and offers significant performance improvements.
- Parameters:
- k_nearestint, default=10
The number of nearest neighbors to use for density estimation. This is the primary parameter of the algorithm.
- reduce_dimsbool, default=True
If True, performs dimensionality reduction to
target_dimsbefore clustering. Recommended for high-dimensional data.- target_dimsint, default=2
The number of dimensions to reduce the data to if
reduce_dimsis True.- dim_reducerstr or estimator object, default=’tsne’
The dimensionality reduction method to use. Can be ‘tsne’, ‘pca’, or a pre-initialized scikit-learn compatible estimator object.
- metricstr, default=’euclidean’
The distance metric to use for the k-nearest neighbor search. See
sklearn.neighbors.NearestNeighborsfor valid options.- metric_paramsdict, default=None
Additional keyword arguments for the metric function.
- n_jobsint, default=None
The number of parallel jobs to run for neighbors search and dimensionality reduction.
Nonemeans 1,-1means using all processors.
- Attributes:
- labels_np.ndarray of shape (n_samples,)
Cluster labels for each point in the dataset given to fit(). Noise points are labeled -1.
- core_sample_indices_np.ndarray of shape (n_core_samples,)
Indices of core samples. In the context of DenMune, these are the “Strong Points” that have an in-degree of at least
k_nearestin the k-NN graph and are used to form the initial cluster skeletons.- n_clusters_int
The estimated number of clusters found by the algorithm. This does not include the noise cluster if one exists.
- density_scores_np.ndarray of shape (n_samples,)
The density score calculated for each point. In this implementation, this corresponds to the in-degree (
|KNN_p<-|) from the k-NN graph, which is used for the “canonical ordering” step of the algorithm.- reducer_estimator object
The fitted dimensionality reduction estimator instance. This attribute is only set if the
reduce_dimsparameter is True and the number of input features is greater thantarget_dims.Noneif dimensionality reduction is not performed.- nn_NearestNeighbors
The fitted
NearestNeighborsinstance used to find the k-nearest neighbors of each point.- projected_X_np.ndarray of shape (n_samples, target_dims)
The input data
Xafter dimensionality reduction has been applied. Ifreduce_dimsis False, or if dimensionality is not possible, this is a copy of the originalX.- n_samples_int
The number of samples in the input data
X.- n_features_in_int
The number of features seen during fit. Not available if
metric == "precomputed".
References
- [1] Abbas, M., El-Zoghabi, A., & Shoukry, A. (2021). DenMune: Density peak based
clustering using mutual nearest neighbors. Pattern Recognition, 109, 107589. https://doi.org/10.1016/j.patcog.2020.107589
Examples
>>> from sklearn.datasets import make_moons >>> import numpy as np >>> # Assuming DenMune is defined in the current scope or imported >>> from denmune_skl import DenMune >>> >>> # Generate sample data >>> X, y = make_moons(n_samples=200, noise=0.05, random_state=42) >>> >>> # Compute DenMune clustering >>> model = DenMune(k_nearest=15, random_state=42) >>> model.fit(X) DenMune(k_nearest=15, random_state=42) >>> >>> # Access the results >>> labels = model.labels_ >>> n_clusters = model.n_clusters_ >>> print(f"Estimated number of clusters: {n_clusters}") Estimated number of clusters: 2
Methods
fit(X[, y])Perform DenMune clustering.
fit_predict(X[, y])Compute cluster centers and predict cluster index for each sample.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
set_params(**params)Set the parameters of this estimator.
- fit(X, y=None)#
Perform DenMune clustering.
- fit_predict(X, y=None)#
Compute cluster centers and predict cluster index for each sample.
Convenience method; equivalent to calling fit(X) followed by accessing the
labels_attribute.- Parameters:
- Xarray-like of shape (n_samples, n_features)
New data to transform.
- yIgnored
Not used, present here for API consistency by convention.
- Returns:
- labelsndarray of shape (n_samples,)
Index of the cluster each sample belongs to.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
Examples using denmune_skl.DenMune#
DenMune with a Custom Dimensionality Reducer (UMAP)
Analyzing DenMune’s Sensitivity to the k_nearest Parameter