import { ClusterBase } from '../base/cluster'; import { Params } from '../base/estimator'; /** * MiniBatchKMeans, following sklearn.cluster.MiniBatchKMeans (the Sculley * 2010 web-scale k-means algorithm as refined by sklearn): * * 1. `nInit` candidate initializations (k-means++ or random pick) are each * computed on a random subsample of size init_size = min(3 * batchSize, * nSamples) (at least 3 * nClusters), evaluated by one mini-batch step on * a fixed validation subsample; the candidate with the lowest inertia * wins. * 2. Main loop of (maxIter * nSamples / batchSize) steps. Each step draws * `batchSize` samples uniformly at random *with replacement* (sklearn's * `random_state.randint`), assigns them to the nearest centers, and moves * each center with the count-based learning rate: * c_new = (c_old * count + sum(batch members)) / (count + members) * which is the batched form of Sculley's per-sample c += (x - c)/count. * 3. Centers whose accumulated weight falls below * reassignmentRatio * max(weights) are periodically (every ~10*nClusters * processed samples) reassigned to random samples from the current batch. * 4. Early stopping (sklearn's _mini_batch_convergence): stop when the * normalized squared center change is <= tol (variance-scaled, only when * tol > 0), or when the exponentially weighted average of the batch * inertia has not improved for `maxNoImprovement` consecutive steps. * 5. Final labels and inertia are computed on the full dataset. */ export interface MiniBatchKMeansProps { /** number of clusters */ nClusters?: number; /** initialization strategy */ initParams?: 'k-means++' | 'random'; /** maximum number of passes (epoch-equivalents) over the data */ maxIter?: number; /** mini-batch size (clamped to the number of samples) */ batchSize?: number; /** * relative tolerance on the center change for convergence, scaled by * the mean feature variance; 0 (default) disables this criterion */ tol?: number; /** * stop when the smoothed batch inertia has not improved for this many * consecutive steps; null/0 disables early stopping on inertia */ maxNoImprovement?: number | null; /** number of random initializations evaluated */ nInit?: number; /** * centers with accumulated weight below this fraction of the maximum * center weight are periodically reassigned; 0 disables reassignment */ reassignmentRatio?: number; /** seed for reproducible sampling and initialization */ randomState?: number; } export declare class MiniBatchKMeans extends ClusterBase { private nClusters; private initParams; private maxIter; private batchSize; private tol; private maxNoImprovement; private nInit; private reassignmentRatio; private randomState?; private centers; private counts; private labels; private inertia; constructor(props?: MiniBatchKMeansProps); getParams(): Params; fit(samplesX: number[][], sampleWeights?: number[]): this; fitPredict(samplesX: number[][], sampleWeights?: number[]): number[]; /** Assign each (new) sample to its nearest fitted center. */ predict(samplesX: number[][]): number[]; getCentroids(): number[][] | null; getInertia(): number; getLabels(): number[]; /** One candidate initialization on a random subsample of size initSize. */ private initCenters; /** * One mini-batch update: assign the batch to the nearest centers * (returning the pre-update inertia), then move each touched center with * its count-based learning rate. Optionally random-reassign low-weight * centers to samples from this batch. Mutates `centers` and `counts`. */ private miniBatchStep; }