cluster.KMeans

K-Means clustering

Usage

import { KMeans } from 'machinelearn/cluster';

const kmean = new KMeans({ k: 2 });
const clusters = kmean.fit([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]]);

const result = kmean.predict([[0, 0], [4, 4]]);
// results in: [0, 1]

Constructors

Methods

Constructors


constructor

new KMeans(__namedParameters: `object`)

Defined in cluster/k_means.ts:33

Parameters:

ParamTypeDefaultDescription
options.distance"euclidean"Choice of distance method. Defaulting to euclidean
options.knumber3Number of clusters
options.maxIterationnumber300Relative tolerance with regards to inertia to declare convergence
options.randomStatenumber0Random state value for sorting centroids during the getInitialCentroid phase

Returns: KMeans

Methods


λ fit

Compute k-means clustering.

Defined in cluster/k_means.ts:73

Parameters:

ParamTypeDefaultDescription
Xnumber[][]nullarray-like or sparse matrix of shape = [n_samples, n_features]

Returns:

void

λ fromJSON

Restores the model from checkpoints

Defined in cluster/k_means.ts:161

Parameters:

ParamTypeDefaultDescription
options.centroidsnumber[][]null
options.clustersnumber[]null
options.knumbernull

Returns:

void

λ predict

Predicts the cluster index with the given X

Defined in cluster/k_means.ts:132

Parameters:

ParamTypeDefaultDescription
Xnumber[][]nullarray-like or sparse matrix of shape = [n_samples, n_features]

Returns:

number[]

λ toJSON

Get the model details in JSON format

Defined in cluster/k_means.ts:143

Returns:

ParamTypeDescription
knumberundefined