local_density_factor

Function local_density_factor 

Source
pub fn local_density_factor<F, S1, S2, D>(
    x: &ArrayBase<S1, Ix2>,
    labels: &ArrayBase<S2, D>,
    k: Option<usize>,
) -> Result<HashMap<usize, F>>
where F: Float + NumCast + Debug + ScalarOperand + AddAssign + DivAssign, S1: Data<Elem = F>, S2: Data<Elem = usize>, D: Dimension,
Expand description

Calculate the Local Density Factor (LDF) for all clusters

The Local Density Factor measures how dense clusters are compared to their surrounding space. Higher values indicate better clustering of dense regions.

§Arguments

  • x - Array of shape (n_samples, n_features) - The data
  • labels - Array of shape (n_samples,) - Predicted labels for each sample
  • k - Number of neighbors to consider for density calculation (default: 5)

§Returns

  • HashMap mapping each cluster label to its local density factor

§Examples

use scirs2_core::ndarray::{array, Array2};
use scirs2_metrics::clustering::density::local_density_factor;

// Create a simple dataset with 2 clusters
let x = Array2::from_shape_vec((6, 2), vec![
    1.0, 2.0, 1.5, 1.8, 1.2, 2.2,
    5.0, 6.0, 5.2, 5.8, 5.5, 6.2,
]).unwrap();

let labels = array![0, 0, 0, 1, 1, 1];

let factors = local_density_factor(&x, &labels, None).unwrap();