Toggle Menu
Prev Up Next

scikit-learn 1.3.2
Other versions

Please cite us if you use the software.

# `sklearn.semi_supervised`.LabelPropagation

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

  Label Propagation classifier.

  Read more in the User Guide.

  Parameters:
    kernel{‘knn’, ‘rbf’} or callable, default=’rbf’

      String identifier for kernel function to use or the kernel function itself. Only ‘rbf’ and ‘knn’ strings are valid inputs. The function passed should take two inputs, each of shape (n_samples, n_features), and return a (n_samples, n_samples) shaped weight matrix.

    gamma float, default=20

      Parameter for rbf kernel.

    n_neighbors int, default=7

      Parameter for knn kernel which need to be strictly positive.

    max_iter int, default=1000

      Change maximum number of iterations allowed.

    tol float, 1e-3

      Convergence tolerance: threshold to consider the system at steady state.

    n_jobs int, default=None

      The number of parallel jobs to run. `None` means 1 unless in a `joblib.parallel_backend` context. `-1` means using all processors. See Glossary for more details.

  Attributes:
    X_{array-like, sparse matrix} of shape (n_samples, n_features)

      Input array.

    classes_ndarray of shape (n_classes,)

      The distinct labels used in classifying instances.

    label_distributions_ndarray of shape (n_samples, n_classes)

      Categorical distribution for each item.

    transduction_ndarray of shape (n_samples)

      Label assigned to each item during fit.

    n_features_in_int

      Number of features seen during fit.

      New in version 0.24.

    feature_names_in_ndarray of shape (`n_features_in_`,)

      Names of features seen during fit. Defined only when `X` has feature names that are all strings.

      New in version 1.0.

    n_iter_int

      Number of iterations run.

  See also

  `LabelSpreading`

    Alternate label propagation strategy more robust to noise.

  References

  Xiaojin Zhu and Zoubin Ghahramani. Learning from labeled and unlabeled data with label propagation. Technical Report CMU-CALD-02-107, Carnegie Mellon University, 2002 http://pages.cs.wisc.edu/~jerryzhu/pub/CMU-CALD-02-107.pdf

  Examples

      >>> import numpy as np
      >>> from sklearn import datasets
      >>> from sklearn.semi_supervised import LabelPropagation
      >>> label_prop_model = LabelPropagation()
      >>> iris = datasets.load_iris()
      >>> rng = np.random.RandomState(42)
      >>> random_unlabeled_points = rng.rand(len(iris.target)) < 0.3
      >>> labels = np.copy(iris.target)
      >>> labels[random_unlabeled_points] = -1
      >>> label_prop_model.fit(iris.data, labels)
      LabelPropagation(...)

  Methods

  `fit`(X, y) | Fit a semi-supervised label propagation model to X.
  `get_metadata_routing`() | Get metadata routing of this object.
  `get_params`([deep]) | Get parameters for this estimator.
  `predict`(X) | Perform inductive inference across the model.
  `predict_proba`(X) | Predict probability for each possible outcome.
  `score`(X, y[, sample_weight]) | Return the mean accuracy on the given test data and labels.
  `set_params`(**params) | Set the parameters of this estimator.
  `set_score_request`(*[, sample_weight]) | Request metadata passed to the `score` method.

  fit(X, y)

    Fit a semi-supervised label propagation model to X.

    Parameters:
      X{array-like, sparse matrix} of shape (n_samples, n_features)

        Training data, where `n_samples` is the number of samples and `n_features` is the number of features.

      y array-like of shape (n_samples,)

        Target class values with unlabeled points marked as -1. All unlabeled samples will be transductively assigned labels internally, which are stored in `transduction_`.

    Returns:
      self object

        Returns the instance itself.

  get_metadata_routing()

    Get metadata routing of this object.

    Please check User Guide on how the routing mechanism works.

    Returns:
      routing MetadataRequest

        A `MetadataRequest` encapsulating routing information.

  get_params(deep=True)

    Get parameters for this estimator.

    Parameters:
      deep bool, default=True

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

    Returns:
      params dict

        Parameter names mapped to their values.

  predict(X)

    Perform inductive inference across the model.

    Parameters:
      X array-like of shape (n_samples, n_features)

        The data matrix.

    Returns:
      y ndarray of shape (n_samples,)

        Predictions for input data.

  predict_proba(X)

    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).

    Parameters:
      X array-like of shape (n_samples, n_features)

        The data matrix.

    Returns:
      probabilities ndarray of shape (n_samples, n_classes)

        Normalized probability distributions across class labels.

  score(X, y, sample_weight=None)

    Return 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.

    Parameters:
      X array-like of shape (n_samples, n_features)

        Test samples.

      y array-like of shape (n_samples,) or (n_samples, n_outputs)

        True labels for `X`.

      sample_weight array-like of shape (n_samples,), default=None

        Sample weights.

    Returns:
      score float

        Mean accuracy of `self.predict(X)` w.r.t. `y`.

  set_params(**params)

    Set the parameters of this estimator.

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

    Parameters:
      **params dict

        Estimator parameters.

    Returns:
      self estimator instance

        Estimator instance.

  set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') → LabelPropagation

    Request metadata passed to the `score` method.

    Note that this method is only relevant if `enable_metadata_routing=True` (see `sklearn.set_config`). Please see User Guide on how the routing mechanism works.

    The options for each parameter are:

    - `True`: metadata is requested, and passed to `score` if provided. The request is ignored if metadata is not provided.

    - `False`: metadata is not requested and the meta-estimator will not pass it to `score`.

    - `None`: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

    - `str`: metadata should be passed to the meta-estimator with this given alias instead of the original name.

    The default (`sklearn.utils.metadata_routing.UNCHANGED`) retains the existing request. This allows you to change the request for some parameters and not others.

    New in version 1.3.

    Note

    This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a `Pipeline`. Otherwise it has no effect.

    Parameters:
      sample_weight str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

        Metadata routing for `sample_weight` parameter in `score`.

    Returns:
      self object

        The updated object.