U
    2d                     @   sZ  d Z ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
mZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZm Z  ddl!m"Z" ddlm#Z# ddddgZ$dd Z%G dd de#Z&dd Z'dddddZ(d d! Z)d*d#d$Z*d+d%d&Z+G d'd dee#Z,ddd(d)dZ-dS ),z
The :mod:`sklearn.pipeline` module implements utilities to build a composite
estimator, as a chain of transforms and estimators.
    )defaultdict)isliceN)sparse   )cloneTransformerMixin)FunctionTransformer)_VisualBlock)available_if)Bunch_print_elapsed_time)
_safe_tags)check_memory)check_is_fitted)check_pandas_support)_safe_set_output_get_output_config)delayedParallel)NotFittedError)_BaseCompositionPipelineFeatureUnionmake_pipeline
make_unionc                    s    fdd}|S )z\Check that final_estimator has `attr`.

    Used together with `avaliable_if` in `Pipeline`.c                    s   t | j  dS NT)getattr_final_estimatorselfattr 4/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/pipeline.pycheck,   s    z#_final_estimator_has.<locals>.checkr"   )r!   r$   r"   r    r#   _final_estimator_has'   s    r%   c                   @   s  e Zd ZdZdgZdddddZddd	d
ZdSddZdd Zdd Z	dTddZ
dd Zdd Zedd Zedd Zedd Zdd Zd d! ZdUd"d#ZdVd$d%ZdWd&d'Zeed(d)d* Zeed+dXd,d-Zeed.d/d0 Zeed1d2d3 Zeed4d5d6 Zeed7d8d9 Zd:d; Zeed<d= Zd>d? Z ee d@dA Z!eedBdYdCdDZ"edEdF Z#dGdH Z$dZdIdJZ%edKdL Z&edMdN Z'dOdP Z(dQdR Z)dS )[r   a  
    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 of tuple
        List of (name, transform) tuples (implementing `fit`/`transform`) that
        are chained in sequential order. The last transform must be an
        estimator.

    memory : str or object with the joblib.Memory interface, default=None
        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 : :class:`~sklearn.utils.Bunch`
        Dictionary-like object, with the following attributes.
        Read-only attribute to access any step parameter by user given name.
        Keys are step names and values are steps parameters.

    classes_ : ndarray of shape (n_classes,)
        The classes labels. Only exist if the last step of the pipeline is a
        classifier.

    n_features_in_ : int
        Number of features seen during :term:`fit`. Only defined if the
        underlying first estimator in `steps` exposes such an attribute
        when fit.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Only defined if the
        underlying estimator exposes such an attribute when fit.

        .. versionadded:: 1.0

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

    Examples
    --------
    >>> from sklearn.svm import SVC
    >>> from sklearn.preprocessing import StandardScaler
    >>> from sklearn.datasets import make_classification
    >>> from sklearn.model_selection import train_test_split
    >>> from sklearn.pipeline import Pipeline
    >>> X, y = make_classification(random_state=0)
    >>> X_train, X_test, y_train, y_test = train_test_split(X, y,
    ...                                                     random_state=0)
    >>> pipe = Pipeline([('scaler', StandardScaler()), ('svc', SVC())])
    >>> # The pipeline can be used as any other estimator
    >>> # and avoids leaking the test set into the train set
    >>> pipe.fit(X_train, y_train)
    Pipeline(steps=[('scaler', StandardScaler()), ('svc', SVC())])
    >>> pipe.score(X_test, y_test)
    0.88
    stepsNFmemoryverbosec                C   s   || _ || _|| _d S N)r&   r(   r)   )r   r&   r(   r)   r"   r"   r#   __init__   s    zPipeline.__init__	transformc                C   s$   |   D ]\}}}t||d q| S )aR  Set the output container when `"transform"` and `"fit_transform"` are called.

        Calling `set_output` will set the output of all estimators in `steps`.

        Parameters
        ----------
        transform : {"default", "pandas"}, default=None
            Configure output of `transform` and `fit_transform`.

            - `"default"`: Default output format of a transformer
            - `"pandas"`: DataFrame output
            - `None`: Transform configuration is unchanged

        Returns
        -------
        self : estimator instance
            Estimator instance.
        r,   )_iterr   r   r-   _stepr"   r"   r#   
set_output   s    zPipeline.set_outputTc                 C   s   | j d|dS )a  Get parameters for this estimator.

        Returns the parameters given in the constructor as well as the
        estimators contained within the `steps` of the `Pipeline`.

        Parameters
        ----------
        deep : bool, default=True
            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.
        r&   deepZ_get_paramsr   r4   r"   r"   r#   
get_params   s    zPipeline.get_paramsc                 K   s   | j d| | S )aC  Set the parameters of this estimator.

        Valid parameter keys can be listed with ``get_params()``. Note that
        you can directly set the parameters of the estimators contained in
        `steps`.

        Parameters
        ----------
        **kwargs : dict
            Parameters of this estimator or parameters of estimators contained
            in `steps`. Parameters of the steps may be set using its name and
            the parameter name separated by a '__'.

        Returns
        -------
        self : object
            Pipeline class instance.
        r&   )r&   Z_set_paramsr   kwargsr"   r"   r#   
set_params   s    zPipeline.set_paramsc                 C   s   t | j \}}| | |d d }|d }|D ]H}|d ks0|dkrFq0t|dsZt|drdt|ds0td|t|f q0|d k	r|dkrt|dstd|t|f d S )Npassthroughfitfit_transformr-   zAll intermediate steps should be transformers and implement fit and transform or be the string 'passthrough' '%s' (type %s) doesn'tzaLast step of Pipeline should implement fit or be the string 'passthrough'. '%s' (type %s) doesn't)zipr&   _validate_nameshasattr	TypeErrortype)r   names
estimatorstransformers	estimatortr"   r"   r#   _validate_steps   s6    
 
	
zPipeline._validate_stepsc                 c   sh   t | j}|s|d8 }tt| jd|D ]:\}\}}|sF|||fV  q(|dk	r(|dkr(|||fV  q(dS )z
        Generate (idx, (name, trans)) tuples from self.steps

        When filter_passthrough is True, 'passthrough' and None transformers
        are filtered out.
        r   r   Nr=   )lenr&   	enumerater   )r   
with_finalfilter_passthroughstopidxnametransr"   r"   r#   r.      s    
zPipeline._iterc                 C   s
   t | jS )z4
        Returns the length of the Pipeline
        )rK   r&   r   r"   r"   r#   __len__
  s    zPipeline.__len__c                 C   sl   t |tr6|jdkrtd| j| j| | j| jdS z| j| \}}W n tk
rf   | j	|  Y S X |S )a  Returns a sub-pipeline or a single estimator 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.
        )r   Nz*Pipeline slicing only supports a step of 1r'   )

isinstanceslicer1   
ValueError	__class__r&   r(   r)   rC   named_steps)r   indrQ   estr"   r"   r#   __getitem__  s    	

  zPipeline.__getitem__c                 C   s   | j d d jS )Nr<   r   )r&   _estimator_typer   r"   r"   r#   r\   &  s    zPipeline._estimator_typec                 C   s   t f t| jS )zAccess the steps by name.

        Read-only attribute to access any step by given name.
        Keys are steps names and values are the steps objects.)r   dictr&   r   r"   r"   r#   rX   *  s    zPipeline.named_stepsc                 C   s   | j d d }|d krdS |S )Nr<   r   r=   )r&   )r   rH   r"   r"   r#   r   3  s    zPipeline._final_estimatorc                 C   s0   | j s
d S | j| \}}d|d t| j|f S )N(step %d of %d) Processing %sr   )r)   r&   rK   )r   step_idxrQ   r0   r"   r"   r#   _log_message8  s    zPipeline._log_messagec                 K   sX   dd | j D }| D ]:\}}d|kr6td||dd\}}||| |< q|S )Nc                 S   s   i | ]\}}|d k	r|i qS r*   r"   ).0rQ   r1   r"   r"   r#   
<dictcomp>@  s       z.Pipeline._check_fit_params.<locals>.<dictcomp>__zPipeline.fit does not accept the {} parameter. You can pass parameters to specific steps of your pipeline using the stepname__parameter format, e.g. `Pipeline.fit(X, y, logisticregression__sample_weight=sample_weight)`.r   )r&   itemsrV   formatsplit)r   
fit_paramsfit_params_stepsZpnameZpvalr1   paramr"   r"   r#   _check_fit_params?  s    zPipeline._check_fit_paramsc              
   K   s   t | j| _|   t| j}|t}| jdddD ]\}}}|d ksP|dkrxtd| 	| W 5 Q R  q6W 5 Q R X t
|dr|jd kr|}	nt|}	||	||d fd| 	|d|| \}}
||
f| j|< q6|S )NF)rM   rN   r=   r   locationmessage_clsnamemessage)listr&   rJ   r   r(   cache_fit_transform_oner.   r   r`   rB   rk   r   )r   Xyrh   r(   Zfit_transform_one_cachedr_   rQ   transformerZcloned_transformerZfitted_transformerr"   r"   r#   _fitP  s6    

 
zPipeline._fitc              	   K   st   | j f |}| j||f|}td| t| jd 4 | jdkrf|| jd d  }| jj||f| W 5 Q R X | S )a$  Fit the model.

        Fit all the transformers one after the other and transform the
        data. Finally, 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 : object
            Pipeline with fitted steps.
        r   r   r=   r<   r   )rj   ru   r   r`   rK   r&   r   r>   )r   rr   rs   rg   rh   Xtfit_params_last_stepr"   r"   r#   r>   v  s    
zPipeline.fitc              
   K   s   | j f |}| j||f|}| j}td| t| jd x |dkrV|W  5 Q R  S || jd d  }t|dr|j||f|W  5 Q R  S |j	||f|
|W  5 Q R  S W 5 Q R X dS )a  Fit the model and transform with the final estimator.

        Fits all the transformers one after the other and transform 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 : ndarray of shape (n_samples, n_transformed_features)
            Transformed samples.
        r   r   r=   r<   r   r?   N)rj   ru   r   r   r`   rK   r&   rB   r?   r>   r-   )r   rr   rs   rg   rh   rv   Z	last_steprw   r"   r"   r#   r?     s    
zPipeline.fit_transformpredictc                 K   s>   |}| j ddD ]\}}}||}q| jd d j|f|S )a#  Transform the data, and apply `predict` with the final estimator.

        Call `transform` of each transformer in the pipeline. The transformed
        data are finally passed to the final estimator that calls `predict`
        method. Only valid if the final estimator implements `predict`.

        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.

            .. versionadded:: 0.20

        Returns
        -------
        y_pred : ndarray
            Result of calling `predict` on the final estimator.
        FrM   r<   r   )r.   r-   r&   rx   )r   rr   Zpredict_paramsrv   r0   rQ   r-   r"   r"   r#   rx     s    zPipeline.predictfit_predictc              	   K   sr   | j f |}| j||f|}|| jd d  }td| t| jd   | jd d j||f|}W 5 Q R X |S )a  Transform the data, and apply `fit_predict` with the final estimator.

        Call `fit_transform` of each transformer in the pipeline. The
        transformed data are finally passed to the final estimator that calls
        `fit_predict` method. Only valid 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 : ndarray
            Result of calling `fit_predict` on the final estimator.
        r<   r   r   r   )rj   ru   r&   r   r`   rK   rz   )r   rr   rs   rg   rh   rv   rw   Zy_predr"   r"   r#   rz     s    $zPipeline.fit_predictpredict_probac                 K   s>   |}| j ddD ]\}}}||}q| jd d j|f|S )a>  Transform the data, and apply `predict_proba` with the final estimator.

        Call `transform` of each transformer in the pipeline. The transformed
        data are finally passed to the final estimator that calls
        `predict_proba` method. Only valid if the final estimator implements
        `predict_proba`.

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

        **predict_proba_params : dict of string -> object
            Parameters to the `predict_proba` called at the end of all
            transformations in the pipeline.

        Returns
        -------
        y_proba : ndarray of shape (n_samples, n_classes)
            Result of calling `predict_proba` on the final estimator.
        Fry   r<   r   )r.   r-   r&   r{   )r   rr   Zpredict_proba_paramsrv   r0   rQ   r-   r"   r"   r#   r{     s    zPipeline.predict_probadecision_functionc                 C   s:   |}| j ddD ]\}}}||}q| jd d |S )a  Transform the data, and apply `decision_function` with the final estimator.

        Call `transform` of each transformer in the pipeline. The transformed
        data are finally passed to the final estimator that calls
        `decision_function` method. Only valid if the final estimator
        implements `decision_function`.

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

        Returns
        -------
        y_score : ndarray of shape (n_samples, n_classes)
            Result of calling `decision_function` on the final estimator.
        Fry   r<   r   )r.   r-   r&   r|   )r   rr   rv   r0   rQ   r-   r"   r"   r#   r|   %  s    zPipeline.decision_functionscore_samplesc                 C   s:   |}| j ddD ]\}}}||}q| jd d |S )a  Transform the data, and apply `score_samples` with the final estimator.

        Call `transform` of each transformer in the pipeline. The transformed
        data are finally passed to the final estimator that calls
        `score_samples` method. Only valid if the final estimator implements
        `score_samples`.

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

        Returns
        -------
        y_score : ndarray of shape (n_samples,)
            Result of calling `score_samples` on the final estimator.
        Fry   r<   r   )r.   r-   r&   r}   )r   rr   rv   r0   rt   r"   r"   r#   r}   >  s    zPipeline.score_samplespredict_log_probac                 K   s>   |}| j ddD ]\}}}||}q| jd d j|f|S )a\  Transform the data, and apply `predict_log_proba` with the final estimator.

        Call `transform` of each transformer in the pipeline. The transformed
        data are finally passed to the final estimator that calls
        `predict_log_proba` method. Only valid if the final estimator
        implements `predict_log_proba`.

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

        **predict_log_proba_params : dict of string -> object
            Parameters to the ``predict_log_proba`` called at the end of all
            transformations in the pipeline.

        Returns
        -------
        y_log_proba : ndarray of shape (n_samples, n_classes)
            Result of calling `predict_log_proba` on the final estimator.
        Fry   r<   r   )r.   r-   r&   r~   )r   rr   Zpredict_log_proba_paramsrv   r0   rQ   r-   r"   r"   r#   r~   W  s    zPipeline.predict_log_probac                 C   s   | j dkpt| j dS )Nr=   r-   )r   rB   r   r"   r"   r#   _can_transformt  s     zPipeline._can_transformc                 C   s&   |}|   D ]\}}}||}q|S )a  Transform the data, and apply `transform` with the final estimator.

        Call `transform` of each transformer in the pipeline. The transformed
        data are finally passed to the final estimator that calls
        `transform` method. Only valid if the final estimator
        implements `transform`.

        This also works where final estimator is `None` in which case all prior
        transformations are applied.

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

        Returns
        -------
        Xt : ndarray of shape (n_samples, n_transformed_features)
            Transformed data.
        )r.   r-   )r   rr   rv   r0   r-   r"   r"   r#   r-   y  s    zPipeline.transformc                 C   s   t dd |  D S )Nc                 s   s   | ]\}}}t |d V  qdS )inverse_transformNrB   )ra   r0   rI   r"   r"   r#   	<genexpr>  s     z2Pipeline._can_inverse_transform.<locals>.<genexpr>)allr.   r   r"   r"   r#   _can_inverse_transform  s    zPipeline._can_inverse_transformc                 C   s.   t t|  }|D ]\}}}||}q|S )a  Apply `inverse_transform` for each step in a 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 : ndarray of shape (n_samples, n_features)
            Inverse transformed data, that is, data in the original feature
            space.
        )reversedro   r.   r   )r   rv   Zreverse_iterr0   r-   r"   r"   r#   r     s    zPipeline.inverse_transformscorec           	      C   sT   |}| j ddD ]\}}}||}qi }|dk	r:||d< | jd d j||f|S )a  Transform the data, and apply `score` with the final estimator.

        Call `transform` of each transformer in the pipeline. The transformed
        data are finally passed to the final estimator that calls
        `score` method. Only valid if the final estimator implements `score`.

        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
            Result of calling `score` on the final estimator.
        Fry   Nsample_weightr<   r   )r.   r-   r&   r   )	r   rr   rs   r   rv   r0   rQ   r-   Zscore_paramsr"   r"   r#   r     s    zPipeline.scorec                 C   s   | j d d jS )z@The classes labels. Only exist if the last step is a classifier.r<   r   )r&   classes_r   r"   r"   r#   r     s    zPipeline.classes_c                 C   s   dt | jd d diS )NZpairwiser   r   )r   r&   r   r"   r"   r#   
_more_tags  s    zPipeline._more_tagsc                 C   s>   |}|   D ],\}}}t|ds.td|||}q|S )aj  Get output feature names for transformation.

        Transform input features using the pipeline.

        Parameters
        ----------
        input_features : array-like of str or None, default=None
            Input features.

        Returns
        -------
        feature_names_out : ndarray of str objects
            Transformed feature names.
        get_feature_names_outzpEstimator {} does not provide get_feature_names_out. Did you mean to call pipeline[:-1].get_feature_names_out()?)r.   rB   AttributeErrorre   r   )r   input_featuresfeature_names_outr0   rQ   r-   r"   r"   r#   r     s    
zPipeline.get_feature_names_outc                 C   s   | j d d jS )z7Number of features seen during first step `fit` method.r   r   )r&   n_features_in_r   r"   r"   r#   r     s    zPipeline.n_features_in_c                 C   s   | j d d jS )z6Names of features seen during first step `fit` method.r   r   )r&   feature_names_in_r   r"   r"   r#   r     s    zPipeline.feature_names_in_c                 C   s4   zt | jd d  W dS  tk
r.   Y dS X dS )z'Indicate whether pipeline has been fit.r<   r   TFN)r   r&   r   r   r"   r"   r#   __sklearn_is_fitted__  s
    zPipeline.__sklearn_is_fitted__c                    sJ   t | j \}}dd   fdd| jD }dd |D }td|||ddS )	Nc                 S   s,   |d ks|dkr|  dS |  d|j j S )Nr=   z: passthroughz: )rW   __name__)rQ   rZ   r"   r"   r#   	_get_name  s    
z-Pipeline._sk_visual_block_.<locals>._get_namec                    s   g | ]\}} ||qS r"   r"   )ra   rQ   rZ   r   r"   r#   
<listcomp>  s     z.Pipeline._sk_visual_block_.<locals>.<listcomp>c                 S   s   g | ]}t |qS r"   )str)ra   rZ   r"   r"   r#   r     s     serialF)rE   name_detailsZdash_wrapped)r@   r&   r	   )r   r0   rF   rE   r   r"   r   r#   _sk_visual_block_  s    zPipeline._sk_visual_block_)T)TT)N)N)N)N)NN)N)*r   
__module____qualname____doc___required_parametersr+   r2   r7   r;   rJ   r.   rS   r[   propertyr\   rX   r   r`   rj   ru   r>   r?   r
   r%   rx   rz   r{   r|   r}   r~   r   r-   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r   4   sf   Z
#




&
#
(

!
$










"



c                 C   s   dd | D }t t}t| |D ]\}}||  d7  < q t| D ]\}}|dkrF||= qFttt| D ]<}|| }||krn||  d||  7  < ||  d8  < qntt|| S )zGenerate names for estimators.c                 S   s(   g | ] }t |tr|nt|j qS r"   )rT   r   rD   r   lower)ra   rH   r"   r"   r#   r   &  s   z$_name_estimators.<locals>.<listcomp>r   z-%d)r   intr@   ro   rd   r   rangerK   )rF   rE   Z	namecountrZ   rQ   kvir"   r"   r#   _name_estimators#  s    r   Fr'   c                 G   s   t t|| |dS )a  Construct a :class:`Pipeline` from the given estimators.

    This is a shorthand for the :class:`Pipeline` constructor; it does not
    require, and does not permit, naming the estimators. Instead, their names
    will be set to the lowercase of their types automatically.

    Parameters
    ----------
    *steps : list of Estimator objects
        List of the scikit-learn estimators that are chained together.

    memory : str or object with the joblib.Memory interface, default=None
        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.

    Returns
    -------
    p : Pipeline
        Returns a scikit-learn :class:`Pipeline` object.

    See Also
    --------
    Pipeline : Class for creating a pipeline of transforms with a final
        estimator.

    Examples
    --------
    >>> from sklearn.naive_bayes import GaussianNB
    >>> from sklearn.preprocessing import StandardScaler
    >>> from sklearn.pipeline import make_pipeline
    >>> make_pipeline(StandardScaler(), GaussianNB(priors=None))
    Pipeline(steps=[('standardscaler', StandardScaler()),
                    ('gaussiannb', GaussianNB())])
    r'   )r   r   )r(   r)   r&   r"   r"   r#   r   ;  s    -c                 K   s   |  |}|d kr|S || S r*   r,   )rt   rr   rs   weightrg   resr"   r"   r#   _transform_onek  s    
r    c              	   K   sd   t ||8 t| dr(| j||f|}n| j||f||}W 5 Q R X |dkrX|| fS || | fS )z
    Fits ``transformer`` to ``X`` and ``y``. The transformed result is returned
    with the fitted transformer. If ``weight`` is not ``None``, the result will
    be multiplied by ``weight``.
    r?   N)r   rB   r?   r>   r-   )rt   rr   rs   r   rm   rn   rg   r   r"   r"   r#   rq   s  s    
 rq   c              
   K   s2   t || | j||f|W  5 Q R  S Q R X dS )z2
    Fits ``transformer`` to ``X`` and ``y``.
    N)r   r>   )rt   rr   rs   r   rm   rn   rg   r"   r"   r#   _fit_one  s    r   c                       s   e Zd ZdZdgZddddddZdd fd	d

Zedd Zd.ddZ	dd Z
dd Zdd Zdd Zd/ddZd0ddZd1ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zed(d) Zd*d+ Zd,d- Z  ZS )2r   a  Concatenates results of multiple transformer objects.

    This estimator applies a list of transformer objects in parallel to the
    input data, then concatenates the results. This is useful to combine
    several feature extraction mechanisms into a single transformer.

    Parameters of the transformers may be set using its name and the parameter
    name separated by a '__'. A transformer may be replaced entirely by
    setting the parameter with its name to another transformer, removed by
    setting to 'drop' or disabled by setting to 'passthrough' (features are
    passed without transformation).

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

    .. versionadded:: 0.13

    Parameters
    ----------
    transformer_list : list of (str, transformer) tuples
        List of transformer objects to be applied to the data. The first
        half of each tuple is the name of the transformer. The transformer can
        be 'drop' for it to be ignored or can be 'passthrough' for features to
        be passed unchanged.

        .. versionadded:: 1.1
           Added the option `"passthrough"`.

        .. versionchanged:: 0.22
           Deprecated `None` as a transformer in favor of 'drop'.

    n_jobs : int, default=None
        Number of jobs to run in parallel.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

        .. versionchanged:: v0.20
           `n_jobs` default changed from 1 to None

    transformer_weights : dict, default=None
        Multiplicative weights for features per transformer.
        Keys are transformer names, values the weights.
        Raises ValueError if key not present in ``transformer_list``.

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

    Attributes
    ----------
    named_transformers : :class:`~sklearn.utils.Bunch`
        Dictionary-like object, with the following attributes.
        Read-only attribute to access any transformer parameter by user
        given name. Keys are transformer names and values are
        transformer parameters.

        .. versionadded:: 1.2

    n_features_in_ : int
        Number of features seen during :term:`fit`. Only defined if the
        underlying first transformer in `transformer_list` exposes such an
        attribute when fit.

        .. versionadded:: 0.24

    See Also
    --------
    make_union : Convenience function for simplified feature union
        construction.

    Examples
    --------
    >>> from sklearn.pipeline import FeatureUnion
    >>> from sklearn.decomposition import PCA, TruncatedSVD
    >>> union = FeatureUnion([("pca", PCA(n_components=1)),
    ...                       ("svd", TruncatedSVD(n_components=2))])
    >>> X = [[0., 1., 3], [2., 2., 5]]
    >>> union.fit_transform(X)
    array([[ 1.5       ,  3.0...,  0.8...],
           [-1.5       ,  5.7..., -0.4...]])
    transformer_listNF)n_jobstransformer_weightsr)   c                C   s   || _ || _|| _|| _d S r*   )r   r   r   r)   )r   r   r   r   r)   r"   r"   r#   r+     s    zFeatureUnion.__init__r,   c                   s2   t  j|d |  D ]\}}}t||d q| S )aU  Set the output container when `"transform"` and `"fit_transform"` are called.

        `set_output` will set the output of all estimators in `transformer_list`.

        Parameters
        ----------
        transform : {"default", "pandas"}, default=None
            Configure output of `transform` and `fit_transform`.

            - `"default"`: Default output format of a transformer
            - `"pandas"`: DataFrame output
            - `None`: Transform configuration is unchanged

        Returns
        -------
        self : estimator instance
            Estimator instance.
        r,   )superr2   r.   r   r/   rW   r"   r#   r2     s    zFeatureUnion.set_outputc                 C   s   t f t| jS r*   )r   r]   r   r   r"   r"   r#   named_transformers  s    zFeatureUnion.named_transformersTc                 C   s   | j d|dS )a  Get parameters for this estimator.

        Returns the parameters given in the constructor as well as the
        estimators contained within the `transformer_list` of the
        `FeatureUnion`.

        Parameters
        ----------
        deep : bool, default=True
            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.
        r   r3   r5   r6   r"   r"   r#   r7     s    zFeatureUnion.get_paramsc                 K   s   | j d| | S )ab  Set the parameters of this estimator.

        Valid parameter keys can be listed with ``get_params()``. Note that
        you can directly set the parameters of the estimators contained in
        `transformer_list`.

        Parameters
        ----------
        **kwargs : dict
            Parameters of this estimator or parameters of estimators contained
            in `transform_list`. Parameters of the transformers may be set
            using its name and the parameter name separated by a '__'.

        Returns
        -------
        self : object
            FeatureUnion class instance.
        r   )r   r8   r9   r"   r"   r#   r;     s    zFeatureUnion.set_paramsc                 C   sb   t | j \}}| | |D ]@}|dkr*qt|ds>t|drHt|dstd|t|f qd S )N)dropr=   r>   r?   r-   zIAll estimators should implement fit and transform. '%s' (type %s) doesn't)r@   r   rA   rB   rC   rD   )r   rE   rG   rI   r"   r"   r#   _validate_transformers2  s    
 
z#FeatureUnion._validate_transformersc                 C   sF   | j s
d S tdd | jD }| j D ]}||kr$td| dq$d S )Nc                 s   s   | ]\}}|V  qd S r*   r"   )ra   rQ   r0   r"   r"   r#   r   H  s     z=FeatureUnion._validate_transformer_weights.<locals>.<genexpr>z"Attempting to weight transformer "z-", but it is not present in transformer_list.)r   setr   rV   )r   Ztransformer_namesrQ   r"   r"   r#   _validate_transformer_weightsD  s    

z*FeatureUnion._validate_transformer_weightsc                 c   sL   | j pi j}| jD ]4\}}|dkr$q|dkr6tdd}||||fV  qdS )zg
        Generate (name, trans, weight) tuples excluding None and
        'drop' transformers.
        r   r=   z
one-to-one)r   N)r   getr   r   )r   Z
get_weightrQ   rR   r"   r"   r#   r.   P  s    
zFeatureUnion._iterc                    sh   g }|   D ]L\ }}t|ds:tdt t|jf | fdd||D  qtj	|t
dS )a4  Get output feature names for transformation.

        Parameters
        ----------
        input_features : array-like of str or None, default=None
            Input features.

        Returns
        -------
        feature_names_out : ndarray of str objects
            Transformed feature names.
        r   z@Transformer %s (type %s) does not provide get_feature_names_out.c                    s   g | ]}  d | qS )rc   r"   ra   frQ   r"   r#   r   t  s     z6FeatureUnion.get_feature_names_out.<locals>.<listcomp>)Zdtype)r.   rB   r   r   rD   r   extendr   npZasarrayobject)r   r   Zfeature_namesrR   r0   r"   r   r#   r   _  s    
z"FeatureUnion.get_feature_names_outc                 K   s&   |  |||t}|s| S | | | S )a  Fit all transformers using X.

        Parameters
        ----------
        X : iterable or array-like, depending on transformers
            Input data, used to fit transformers.

        y : array-like of shape (n_samples, n_outputs), default=None
            Targets for supervised learning.

        **fit_params : dict, default=None
            Parameters to pass to the fit method of the estimator.

        Returns
        -------
        self : object
            FeatureUnion class instance.
        )_parallel_funcr   _update_transformer_list)r   rr   rs   rg   rG   r"   r"   r#   r>   x  s
    
zFeatureUnion.fitc                 K   sH   |  |||t}|s(t|jd dfS t| \}}| | | |S )a  Fit all transformers, transform the data and concatenate results.

        Parameters
        ----------
        X : iterable or array-like, depending on transformers
            Input data to be transformed.

        y : array-like of shape (n_samples, n_outputs), default=None
            Targets for supervised learning.

        **fit_params : dict, default=None
            Parameters to pass to the fit method of the estimator.

        Returns
        -------
        X_t : array-like or sparse matrix of                 shape (n_samples, sum_n_components)
            The `hstack` of results of transformers. `sum_n_components` is the
            sum of `n_components` (output dimension) over transformers.
        r   )r   rq   r   zerosshaper@   r   _hstack)r   rr   rs   rg   resultsXsrG   r"   r"   r#   r?     s    
zFeatureUnion.fit_transformc                 C   s   | j s
d S d|||f S )Nr^   )r)   )r   rQ   rP   totalr"   r"   r#   r`     s    zFeatureUnion._log_messagec                    sV   t j_    t  tjd fddtdD S )z Runs func in parallel on X and yr   c              
   3   sF   | ]>\}\}}}t | |fd ||tdV  qdS )r   rl   N)r   r`   rK   )ra   rP   rQ   rt   r   rr   rg   funcr   rG   rs   r"   r#   r     s   
z.FeatureUnion._parallel_func.<locals>.<genexpr>r   )ro   r   r   r   r.   r   r   rL   )r   rr   rs   rg   r   r"   r   r#   r     s    
zFeatureUnion._parallel_funcc                    sD   t | jd fdd|  D }|s:t jd dfS | |S )a  Transform X separately by each transformer, concatenate results.

        Parameters
        ----------
        X : iterable or array-like, depending on transformers
            Input data to be transformed.

        Returns
        -------
        X_t : array-like or sparse matrix of                 shape (n_samples, sum_n_components)
            The `hstack` of results of transformers. `sum_n_components` is the
            sum of `n_components` (output dimension) over transformers.
        r   c                 3   s&   | ]\}}}t t| d |V  qd S r*   )r   r   )ra   rQ   rR   r   rr   r"   r#   r     s   z)FeatureUnion.transform.<locals>.<genexpr>r   )r   r   r.   r   r   r   r   )r   rr   r   r"   r   r#   r-     s    zFeatureUnion.transformc                 C   sn   t d| }|d dkr>tdd |D r>td}|j|ddS tdd |D r`t| }n
t|}|S )	Nr-   ZdenseZpandasc                 s   s   | ]}t |d V  qdS )ZilocNr   )ra   rr   r"   r"   r#   r     s     z'FeatureUnion._hstack.<locals>.<genexpr>r   )Zaxisc                 s   s   | ]}t |V  qd S r*   )r   issparser   r"   r"   r#   r     s     )	r   r   r   concatanyr   ZhstackZtocsrr   )r   r   configpdr"   r"   r#   r     s    

zFeatureUnion._hstackc                    s*   t    fdd| jD | jd d < d S )Nc                    s(   g | ] \}}||d kr|nt  fqS )r   )next)ra   rQ   oldrG   r"   r#   r     s   z9FeatureUnion._update_transformer_list.<locals>.<listcomp>)iterr   )r   rG   r"   r   r#   r     s    
z%FeatureUnion._update_transformer_listc                 C   s   | j d d jS )z+Number of features seen during :term:`fit`.r   r   )r   r   r   r"   r"   r#   r     s    zFeatureUnion.n_features_in_c                 C   s    |   D ]\}}}t| qdS r   )r.   r   )r   r0   rt   r"   r"   r#   r     s    
z"FeatureUnion.__sklearn_is_fitted__c                 C   s   t | j \}}td||dS )Nparallel)rE   )r@   r   r	   )r   rE   rG   r"   r"   r#   r     s    zFeatureUnion._sk_visual_block_)T)N)N)N)r   r   r   r   r   r+   r2   r   r   r7   r;   r   r   r.   r   r>   r?   r`   r   r-   r   r   r   r   r   __classcell__r"   r"   r   r#   r     s4   R  





r   r)   c                 G   s   t t|| |dS )a  Construct a FeatureUnion from the given transformers.

    This is a shorthand for the FeatureUnion constructor; it does not require,
    and does not permit, naming the transformers. Instead, they will be given
    names automatically based on their types. It also does not allow weighting.

    Parameters
    ----------
    *transformers : list of estimators
        One or more estimators.

    n_jobs : int, default=None
        Number of jobs to run in parallel.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

        .. versionchanged:: v0.20
           `n_jobs` default changed from 1 to None.

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

    Returns
    -------
    f : FeatureUnion
        A :class:`FeatureUnion` object for concatenating the results of multiple
        transformer objects.

    See Also
    --------
    FeatureUnion : Class for concatenating the results of multiple transformer
        objects.

    Examples
    --------
    >>> from sklearn.decomposition import PCA, TruncatedSVD
    >>> from sklearn.pipeline import make_union
    >>> make_union(PCA(), TruncatedSVD())
     FeatureUnion(transformer_list=[('pca', PCA()),
                                   ('truncatedsvd', TruncatedSVD())])
    r   )r   r   )r   r)   rG   r"   r"   r#   r   	  s    ,)r   N)r   N).r   collectionsr   	itertoolsr   Znumpyr   Zscipyr   baser   r   Zpreprocessingr   Zutils._estimator_html_reprr	   Zutils.metaestimatorsr
   utilsr   r   Zutils._tagsr   Zutils.validationr   r   r   Zutils._set_outputr   r   Zutils.parallelr   r   
exceptionsr   r   __all__r%   r   r   r   r   rq   r   r   r   r"   r"   r"   r#   <module>   sH        t0	   

  }