mojolearn.LabelPropagation#

class mojolearn.LabelPropagation(kernel='rbf', *, gamma=20, n_neighbors=7, max_iter=1000, tol=0.001, n_jobs=None)#

Semi-supervised classification by label propagation.

Spreads the known labels over a similarity graph of all samples, keeping the labeled samples fixed (hard clamping), following scikit-learn’s LabelPropagation. Mark unlabeled samples with -1 in y. The graph is a dense RBF affinity matrix or a k-nearest-neighbor connectivity 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.

  • max_iter (int, default 1000) – 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’, ‘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.LabelPropagation(kernel="knn").fit(X, y)
>>> labels = model.transduction_

Methods

fit(X, y)

Fit the model and propagate 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)#

Fit the model and propagate 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 – The fitted estimator.

numeric_mode_used()#

The numeric mode this estimator runs in.

Returns:

str – 'identical' or 'fast', read from the compiled binary the estimator uses, not from the argument it was given.

predict(X)#

Predict the class of new samples.

Parameters:

X (array-like of shape (n_queries, n_features)) – Samples.

Returns:

labels (Array or list of shape (n_queries,)) – The most probable class of each sample.

predict_proba(X)#

Class probabilities of new samples from the fitted label distributions.

Parameters:

X (array-like of shape (n_queries, n_features)) – Samples.

Returns:

proba (Array of shape (n_queries, n_classes), float32) – Columns in the order of classes_.

score(X, y, sample_weight=None)#

Mean accuracy of predict() on the given data.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,)) – True labels.

  • sample_weight (array-like of shape (n_samples,) or None, default None) – Sample weights.

Returns:

score (float) – (Weighted) fraction of correct predictions.

set_params(**params)#

Set constructor parameters. The estimator is rebuilt through its constructor, so every constructor check runs again and any fitted state is dropped, as a new setting invalidates it.

vendor_used()#

The GPU backend this estimator runs on.

Returns:

str – 'metal', 'cuda' or 'hip', read from the compiled binary the estimator uses.