mojolearn.MeanShift#

class mojolearn.MeanShift(*, bandwidth=None, seeds=None, bin_seeding=False, min_bin_freq=1, cluster_all=True, n_jobs=None, max_iter=300)[source]#

Bases: _XCluster

Mean shift clustering with a flat kernel.

Follows scikit-learn’s MeanShift: each seed is shifted to the mean of the rows within bandwidth until it stops moving, centers are merged by intensity (a center within the bandwidth of a stronger one is dropped), and each row is labeled with its nearest center (the lowest index on a tie). Input is converted to float32.

Parameters:
  • bandwidth (float or None, default None) – Radius of the flat kernel; must be positive. None estimates it as scikit-learn’s estimate_bandwidth does (quantile 0.3 over all rows).

  • seeds (array-like of shape (n_seeds, n_features) or None, default None) – Starting points. None uses every row, or the bins when bin_seeding is True.

  • bin_seeding (bool, default False) – Seed from the rows binned on a grid of size bandwidth (rounding half to even), as in scikit-learn.

  • min_bin_freq (int, default 1) – With bin_seeding, only bins with at least this many rows become seeds.

  • cluster_all (bool, default True) – If True every row is labeled with its nearest center. If False, rows farther than bandwidth from every center get label -1.

  • n_jobs (None) – Accepted for scikit-learn compatibility; ignored.

  • max_iter (int, default 300) – Maximum shifts per seed.

  • numeric_mode (str or None, default None) – Numeric mode for this estimator. None uses the process default (see mojolearn.set_numeric_mode()).

Variables:
  • cluster_centers_ (Array of shape (n_clusters, n_features), float32) – Cluster centers.

  • labels_ (Array of shape (n_samples,), int32) – Cluster of each training row.

  • bandwidth_ (float) – Bandwidth used, given or estimated.

  • n_iter_ (int) – Largest number of shifts over the seeds.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> model = mojolearn.MeanShift(bin_seeding=True).fit(X)
>>> labels = model.labels_
__init__(*, bandwidth=None, seeds=None, bin_seeding=False, min_bin_freq=1, cluster_all=True, n_jobs=None, max_iter=300)[source]#

Methods

fit(X[, y])

Find the cluster centers of X.

fit_predict(X[, y])

Fit the model on X and return the training labels.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Assign each row of X to its nearest cluster center.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y=None)[source]#

Find the cluster centers of X.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training rows, converted to float32. NaN or infinity raises ValueError.

  • y (ignored) – Present for scikit-learn compatibility.

Returns:

self (MeanShift) – The fitted estimator.

Raises:

ValueError – If bandwidth is not positive, seeds has a different number of features than X, or X contains NaN or infinity.

predict(X)[source]#

Assign each row of X to its nearest cluster center.

A tie goes to the lowest center index.

Parameters:

X (array-like of shape (n_samples, n_features)) – Rows to label, converted to float32.

Returns:

labels (Array of shape (n_samples,), int32) – Index of the nearest center for each row.

Raises:

RuntimeError – If the model is not fitted.

fit_predict(X, y=None)[source]#

Fit the model on X and return the training labels.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training rows, converted to float32.

  • y (ignored) – Present for scikit-learn compatibility.

Returns:

labels (Array of shape (n_samples,), int32) – The fitted labels_.