class sklearn.semi_supervised.LabelSpreading(kernel='rbf', gamma=20, n_neighbors=7, alpha=0.2, max_iter=30, tol=0.001)[source]

LabelSpreading model for semi-supervised learning

This model is similar to the basic Label Propgation algorithm, but uses affinity matrix based on the normalized graph Laplacian and soft clamping across the labels.


kernel : {‘knn’, ‘rbf’}

String identifier for kernel function to use. Only ‘rbf’ and ‘knn’ kernels are currently supported.

gamma : float

parameter for rbf kernel

n_neighbors : integer > 0

parameter for knn kernel

alpha : float

clamping factor

max_iter : float

maximum number of iterations allowed

tol : float

Convergence tolerance: threshold to consider the system at steady state


X_ : array, shape = [n_samples, n_features]

Input array.

classes_ : array, shape = [n_classes]

The distinct labels used in classifying instances.

label_distributions_ : array, shape = [n_samples, n_classes]

Categorical distribution for each item.

transduction_ : array, shape = [n_samples]

Label assigned to each item via the transduction.

n_iter_ : int

Number of iterations run.

See also

Unregularized graph based semi-supervised learning


Dengyong Zhou, Olivier Bousquet, Thomas Navin Lal, Jason Weston, Bernhard Schoelkopf. Learning with local and global consistency (2004)


>>> from sklearn import datasets
>>> from sklearn.semi_supervised import LabelSpreading
>>> label_prop_model = LabelSpreading()
>>> iris = datasets.load_iris()
>>> random_unlabeled_points = np.where(np.random.random_integers(0, 1,
...    size=len(
>>> labels = np.copy(
>>> labels[random_unlabeled_points] = -1
>>>, labels)


__init__(kernel='rbf', gamma=20, n_neighbors=7, alpha=0.2, max_iter=30, tol=0.001)[source]
fit(X, y)[source]

Fit a semi-supervised label propagation model based

All the input data is provided matrix X (labeled and unlabeled) and corresponding label matrix y with a dedicated marker value for unlabeled samples.


X : array-like, shape = [n_samples, n_features]

A {n_samples by n_samples} size matrix will be created from this

y : array_like, shape = [n_samples]

n_labeled_samples (unlabeled points are marked as -1) All unlabeled samples will be transductively assigned labels


self : returns an instance of self.


Get parameters for this estimator.


deep: boolean, optional :

If True, will return the parameters for this estimator and contained subobjects that are estimators.


params : mapping of string to any

Parameter names mapped to their values.


Performs inductive inference across the model.


X : array_like, shape = [n_samples, n_features]


y : array_like, shape = [n_samples]

Predictions for input data


Predict probability for each possible outcome.

Compute the probability estimates for each single sample in X and each possible outcome seen during training (categorical distribution).


X : array_like, shape = [n_samples, n_features]


probabilities : array, shape = [n_samples, n_classes]

Normalized probability distributions across class labels

score(X, y, sample_weight=None)[source]

Returns the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.


X : array-like, shape = (n_samples, n_features)

Test samples.

y : array-like, shape = (n_samples) or (n_samples, n_outputs)

True labels for X.

sample_weight : array-like, shape = [n_samples], optional

Sample weights.


score : float

Mean accuracy of self.predict(X) wrt. y.


Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns:self :