package sklearn

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type t
val of_pyobject : Py.Object.t -> t
val to_pyobject : t -> Py.Object.t
val create : ?memory:[ `S of string | `JoblibMemory of Py.Object.t ] -> ?verbose:bool -> steps:(string * Py.Object.t) list -> unit -> t

Pipeline of transforms with a final estimator.

Sequentially apply a list of transforms and a final estimator. Intermediate steps of the pipeline must be 'transforms', that is, they must implement fit and transform methods. The final estimator only needs to implement fit. The transformers in the pipeline can be cached using ``memory`` argument.

The purpose of the pipeline is to assemble several steps that can be cross-validated together while setting different parameters. For this, it enables setting parameters of the various steps using their names and the parameter name separated by a '__', as in the example below. A step's estimator may be replaced entirely by setting the parameter with its name to another estimator, or a transformer removed by setting it to 'passthrough' or ``None``.

Read more in the :ref:`User Guide <pipeline>`.

.. versionadded:: 0.5

Parameters ---------- steps : list List of (name, transform) tuples (implementing fit/transform) that are chained, in the order in which they are chained, with the last object an estimator.

memory : None, str or object with the joblib.Memory interface, optional Used to cache the fitted transformers of the pipeline. By default, no caching is performed. If a string is given, it is the path to the caching directory. Enabling caching triggers a clone of the transformers before fitting. Therefore, the transformer instance given to the pipeline cannot be inspected directly. Use the attribute ``named_steps`` or ``steps`` to inspect estimators within the pipeline. Caching the transformers is advantageous when fitting is time consuming.

verbose : bool, default=False If True, the time elapsed while fitting each step will be printed as it is completed.

Attributes ---------- named_steps : bunch object, a dictionary with attribute access Read-only attribute to access any step parameter by user given name. Keys are step names and values are steps parameters.

See Also -------- sklearn.pipeline.make_pipeline : Convenience function for simplified pipeline construction.

Examples -------- >>> from sklearn import svm >>> from sklearn.datasets import make_classification >>> from sklearn.feature_selection import SelectKBest >>> from sklearn.feature_selection import f_regression >>> from sklearn.pipeline import Pipeline >>> # generate some data to play with >>> X, y = make_classification( ... n_informative=5, n_redundant=0, random_state=42) >>> # ANOVA SVM-C >>> anova_filter = SelectKBest(f_regression, k=5) >>> clf = svm.SVC(kernel='linear') >>> anova_svm = Pipeline(('anova', anova_filter), ('svc', clf)) >>> # You can set the parameters using the names issued >>> # For instance, fit using a k of 10 in the SelectKBest >>> # and a parameter 'C' of the svm >>> anova_svm.set_params(anova__k=10, svc__C=.1).fit(X, y) Pipeline(steps=('anova', SelectKBest(...)), ('svc', SVC(...))) >>> prediction = anova_svm.predict(X) >>> anova_svm.score(X, y) 0.83 >>> # getting the selected features chosen by anova_filter >>> anova_svm'anova'.get_support() array(False, False, True, True, False, False, True, True, False, True, False, True, True, False, True, False, True, True, False, False) >>> # Another way to get selected features chosen by anova_filter >>> anova_svm.named_steps.anova.get_support() array(False, False, True, True, False, False, True, True, False, True, False, True, True, False, True, False, True, True, False, False) >>> # Indexing can also be used to extract a sub-pipeline. >>> sub_pipeline = anova_svm:1 >>> sub_pipeline Pipeline(steps=('anova', SelectKBest(...))) >>> coef = anova_svm-1.coef_ >>> anova_svm'svc' is anova_svm-1 True >>> coef.shape (1, 10) >>> sub_pipeline.inverse_transform(coef).shape (1, 20)

val get_item : ind: [ `I of int | `S of string | `Slice of [ `None | `I of int ] * [ `None | `I of int ] * [ `None | `I of int ] ] -> t -> Py.Object.t

Returns a sub-pipeline or a single esimtator in the pipeline

Indexing with an integer will return an estimator; using a slice returns another Pipeline instance which copies a slice of this Pipeline. This copy is shallow: modifying (or fitting) estimators in the sub-pipeline will affect the larger pipeline and vice-versa. However, replacing a value in `step` will not affect a copy.

val decision_function : x:Arr.t -> t -> Arr.t

Apply transforms, and decision_function of the final estimator

Parameters ---------- X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline.

Returns ------- y_score : array-like of shape (n_samples, n_classes)

val fit : ?y:Arr.t -> ?fit_params:(string * Py.Object.t) list -> x:Arr.t -> t -> t

Fit the model

Fit all the transforms one after the other and transform the data, then fit the transformed data using the final estimator.

Parameters ---------- X : iterable Training data. Must fulfill input requirements of first step of the pipeline.

y : iterable, default=None Training targets. Must fulfill label requirements for all steps of the pipeline.

**fit_params : dict of string -> object Parameters passed to the ``fit`` method of each step, where each parameter name is prefixed such that parameter ``p`` for step ``s`` has key ``s__p``.

Returns ------- self : Pipeline This estimator

val fit_predict : ?y:Arr.t -> ?fit_params:(string * Py.Object.t) list -> x:Arr.t -> t -> Arr.t

Applies fit_predict of last step in pipeline after transforms.

Applies fit_transforms of a pipeline to the data, followed by the fit_predict method of the final estimator in the pipeline. Valid only if the final estimator implements fit_predict.

Parameters ---------- X : iterable Training data. Must fulfill input requirements of first step of the pipeline.

y : iterable, default=None Training targets. Must fulfill label requirements for all steps of the pipeline.

**fit_params : dict of string -> object Parameters passed to the ``fit`` method of each step, where each parameter name is prefixed such that parameter ``p`` for step ``s`` has key ``s__p``.

Returns ------- y_pred : array-like

val fit_transform : ?y:Arr.t -> ?fit_params:(string * Py.Object.t) list -> x:Arr.t -> t -> Arr.t

Fit the model and transform with the final estimator

Fits all the transforms one after the other and transforms the data, then uses fit_transform on transformed data with the final estimator.

Parameters ---------- X : iterable Training data. Must fulfill input requirements of first step of the pipeline.

y : iterable, default=None Training targets. Must fulfill label requirements for all steps of the pipeline.

**fit_params : dict of string -> object Parameters passed to the ``fit`` method of each step, where each parameter name is prefixed such that parameter ``p`` for step ``s`` has key ``s__p``.

Returns ------- Xt : array-like of shape (n_samples, n_transformed_features) Transformed samples

val get_params : ?deep:bool -> t -> Dict.t

Get parameters for this estimator.

Parameters ---------- deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns ------- params : mapping of string to any Parameter names mapped to their values.

val inverse_transform : ?x:Arr.t -> ?y:Arr.t -> t -> Arr.t

Apply inverse transformations in reverse order

All estimators in the pipeline must support ``inverse_transform``.

Parameters ---------- Xt : array-like of shape (n_samples, n_transformed_features) Data samples, where ``n_samples`` is the number of samples and ``n_features`` is the number of features. Must fulfill input requirements of last step of pipeline's ``inverse_transform`` method.

Returns ------- Xt : array-like of shape (n_samples, n_features)

val predict : ?predict_params:(string * Py.Object.t) list -> x:Arr.t -> t -> Arr.t

Apply transforms to the data, and predict with the final estimator

Parameters ---------- X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline.

**predict_params : dict of string -> object Parameters to the ``predict`` called at the end of all transformations in the pipeline. Note that while this may be used to return uncertainties from some models with return_std or return_cov, uncertainties that are generated by the transformations in the pipeline are not propagated to the final estimator.

Returns ------- y_pred : array-like

val predict_log_proba : x:Arr.t -> t -> Arr.t

Apply transforms, and predict_log_proba of the final estimator

Parameters ---------- X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline.

Returns ------- y_score : array-like of shape (n_samples, n_classes)

val predict_proba : x:Arr.t -> t -> Arr.t

Apply transforms, and predict_proba of the final estimator

Parameters ---------- X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline.

Returns ------- y_proba : array-like of shape (n_samples, n_classes)

val score : ?y:Arr.t -> ?sample_weight:Arr.t -> x:Arr.t -> t -> float

Apply transforms, and score with the final estimator

Parameters ---------- X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline.

y : iterable, default=None Targets used for scoring. Must fulfill label requirements for all steps of the pipeline.

sample_weight : array-like, default=None If not None, this argument is passed as ``sample_weight`` keyword argument to the ``score`` method of the final estimator.

Returns ------- score : float

val score_samples : x:Arr.t -> t -> Arr.t

Apply transforms, and score_samples of the final estimator.

Parameters ---------- X : iterable Data to predict on. Must fulfill input requirements of first step of the pipeline.

Returns ------- y_score : ndarray, shape (n_samples,)

val set_params : ?kwargs:(string * Py.Object.t) list -> t -> t

Set the parameters of this estimator.

Valid parameter keys can be listed with ``get_params()``.

Returns ------- self

val named_steps : t -> Dict.t

Attribute named_steps: get value or raise Not_found if None.

val named_steps_opt : t -> Dict.t option

Attribute named_steps: get value as an option.

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Format.formatter -> t -> unit

Pretty-print the object to a formatter.

OCaml

Innovation. Community. Security.