mojolearn.LabelSpreading#

class mojolearn.LabelSpreading(kernel='rbf', *, gamma=20, n_neighbors=7, alpha=0.2, max_iter=30, tol=0.001, n_jobs=None)[source]#

Bases: _LabelPropagationBase

Semi-supervised classification by label spreading.

Spreads the known labels over the normalized graph Laplacian of a similarity graph, letting labeled samples change by a fraction alpha (soft clamping), following scikit-learn’s LabelSpreading. Mark unlabeled samples with -1 in y. The graph is a dense RBF affinity matrix or a k-nearest-neighbor graph (each sample is its own first neighbor; distance ties go to the lower index). Iteration stops when the summed absolute change of the label distributions falls below tol or after max_iter steps. Computation is float32 (scikit-learn uses float64).

Parameters:
  • kernel ({‘rbf’, ‘knn’}, default ‘rbf’) – Graph kernel. Other values, including callables, raise NotImplementedError.

  • gamma (float, default 20) – Coefficient of the 'rbf' kernel.

  • n_neighbors (int, default 7) – Number of neighbors of the 'knn' kernel.

  • alpha (float, default 0.2) – Clamping factor in (0, 1): the fraction of a sample’s label taken from its neighbors each step. Other values raise ValueError.

  • max_iter (int, default 30) – Maximum number of iterations.

  • tol (float, default 1e-3) – Convergence tolerance.

  • n_jobs (int or None, default None) – Accepted for scikit-learn compatibility; unused.

  • numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this estimator ('fast' is Apple only). None uses the process default.

Variables:
  • X_ (Array of shape (n_samples, n_features)) – Training data.

  • classes_ (list) – Class labels (without the -1 marker).

  • label_distributions_ (Array of shape (n_samples, n_classes)) – Label distribution of each training sample.

  • transduction_ (Array or list of shape (n_samples,)) – Label assigned to each training sample.

  • n_iter_ (int) – Number of iterations run.

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

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = (X[:, 0] > 0.5).astype(np.int64)
>>> y[100:] = -1
>>> model = mojolearn.LabelSpreading(kernel="knn", alpha=0.2).fit(X, y)
>>> labels = model.transduction_
__init__(kernel='rbf', *, gamma=20, n_neighbors=7, alpha=0.2, max_iter=30, tol=0.001, n_jobs=None)[source]#

Methods

fit(X, y)

Fit the model and spread labels to the unlabeled samples.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict the class of new samples.

predict_proba(X)

Class probabilities of new samples from the fitted label distributions.

score(X, y[, sample_weight])

Mean accuracy of predict() on the given data.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y)[source]#

Fit the model and spread labels to the unlabeled samples.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Training data. Sparse input is densified.

  • y (array-like of shape (n_samples,)) – Labels; -1 marks an unlabeled sample.

Returns:

self (LabelSpreading) – The fitted estimator.

Raises:

ValueError – If alpha is not in (0, 1).