mojolearn.LabelSpreading#
- class mojolearn.LabelSpreading(kernel='rbf', *, gamma=20, n_neighbors=7, alpha=0.2, max_iter=30, tol=0.001, n_jobs=None)#
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’sLabelSpreading. Mark unlabeled samples with -1 iny. 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 belowtolor aftermax_itersteps. 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’, ‘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_
Methods
fit(X, y)Fit the model and spread labels to the unlabeled samples.
get_params([deep])The numeric mode this estimator runs in.
predict(X)Predict the class of new samples.
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.
The GPU backend this estimator runs on.
- fit(X, y)#
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
alphais not in (0, 1).
- 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.