Skip to main content

Dbscan

Struct Dbscan 

Source
#[non_exhaustive]
pub struct Dbscan { /* private fields */ }
Expand description

DBSCAN (Density-Based Spatial Clustering of Applications with Noise).

Points are classified as core, border, or noise based on neighborhood density. Supports configurable distance metrics and KD-tree acceleration.

§Example

use scry_learn::cluster::Dbscan;
use scry_learn::dataset::Dataset;

let data = Dataset::new(
    vec![vec![0.0, 0.0, 10.0, 10.0], vec![0.0, 0.0, 10.0, 10.0]],
    vec![0.0; 4],
    vec!["x".into(), "y".into()],
    "label",
);

let mut db = Dbscan::new(5.0, 2);
db.fit(&data).unwrap();
assert_eq!(db.n_clusters(), 2);

Implementations§

Source§

impl Dbscan

Source

pub fn new(eps: f64, min_samples: usize) -> Self

Create a new DBSCAN model.

§Arguments
  • eps — maximum distance for two points to be considered neighbors.
  • min_samples — minimum number of neighbors for a point to be a core point.
Source

pub fn metric(self, m: DistanceMetric) -> Self

Set the distance metric.

Default is DistanceMetric::Euclidean. KD-tree acceleration is only used with Euclidean distance and ≤ 20 features; other metrics always use brute-force.

Source

pub fn fit(&mut self, data: &Dataset) -> Result<()>

Fit the model on a dataset.

Uses KD-tree for Euclidean distance with ≤ 20 features, brute-force otherwise.

Source

pub fn predict(&self, features: &[Vec<f64>]) -> Result<Vec<i32>>

Predict cluster labels for new points.

Each new point is assigned to the cluster of its nearest core point if that core point is within eps. Otherwise the point is labeled noise (-1).

§Example
use scry_learn::cluster::Dbscan;
use scry_learn::dataset::Dataset;

let data = Dataset::new(
    vec![vec![0.0, 0.0, 0.0, 10.0, 10.0, 10.0],
         vec![0.0, 0.0, 0.0, 10.0, 10.0, 10.0]],
    vec![0.0; 6],
    vec!["x".into(), "y".into()],
    "label",
);

let mut db = Dbscan::new(5.0, 2);
db.fit(&data).unwrap();

let preds = db.predict(&[vec![0.5, 0.5]]).unwrap();
assert!(preds[0] >= 0, "Should be assigned to a cluster");
Source

pub fn labels(&self) -> &[i32]

Get cluster labels (-1 = noise).

Source

pub fn n_clusters(&self) -> usize

Number of clusters found (excluding noise).

Source

pub fn n_noise(&self) -> usize

Number of noise points.

Source

pub fn n_core_points(&self) -> usize

Number of core points identified during fitting.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.