
    0Ph                        d Z ddlmZmZ ddlmZ ddlZddlm	Z
 ddlmZmZmZmZmZmZmZ ddlmZ ddlmZmZmZ dd	lmZmZ dd
lmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$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/m0Z0m1Z1m2Z2 g dZ3d$dZ4	 d%dZ5d Z6 G d deee          Z7 G d dee7          Z8 G d dee7          Z9d Z: G d dee          Z; G d  d!eee;          Z< G d" d#eee;          Z=dS )&zMultioutput regression and classification.

The estimators provided in this module are meta-estimators: they require
a base estimator to be provided in their constructor. The meta-estimator
extends single output estimators to multioutput estimators.
    )ABCMetaabstractmethod)IntegralN   )BaseEstimatorClassifierMixinMetaEstimatorMixinRegressorMixin_fit_contextcloneis_classifier)cross_val_predict)Bunchcheck_random_stateget_tags)
HasMethods
StrOptions)_get_response_values)_print_elapsed_time)MetadataRouterMethodMapping_raise_for_params_routing_enabledprocess_routingavailable_if)check_classification_targets)Paralleldelayed)_check_method_params_check_response_methodcheck_is_fittedhas_fit_parametervalidate_data)MultiOutputRegressorMultiOutputClassifierClassifierChainRegressorChainc                 j    t          |           } | | j        ||fd|i| n | j        ||fi | | S )Nsample_weight)r   fit)	estimatorXyr*   
fit_paramss        S/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/multioutput.py_fit_estimatorr1   :   s[    i  I 	aFF-F:FFFF	a))j)))    Tc                 z    |i n|}|rt          |           } | | j        ||fd|i| n | j        ||fi | | S )Nclasses)r   partial_fit)r,   r-   r.   r4   partial_fit_params
first_times         r0   _partial_fit_estimatorr8   C   sy      29?Q %)$$		aJJGJ7IJJJJ	a99&8999r2   c                 ,      fd}t          |          S )zpReturn a function to check if the sub-estimator(s) has(have) `attr`.

    Helper for Chain implementations.
    c                     t          | d          r t          fd| j        D                       S t          | j                  rdS dS )Nestimators_c              3   8   K   | ]}t          |          V  d S Nhasattr.0estattrs     r0   	<genexpr>z>_available_if_estimator_has.<locals>._check.<locals>.<genexpr>Y   s-      FFcwsD))FFFFFFr2   TF)r?   allr;   r,   selfrC   s    r0   _checkz+_available_if_estimator_has.<locals>._checkW   sZ    4'' 	GFFFFT5EFFFFFF4>4(( 	4ur2   r   rC   rH   s   ` r0   _available_if_estimator_hasrJ   Q   s,         r2   c                        e Zd ZU  eddg          gedgdZeed<   eddd            Z	 e
d           ed	
          dd                        Z ed	
          dd            Zd Z fdZd Z xZS )_MultiOutputEstimatorr+   predictNr,   n_jobs_parameter_constraintsrO   c                "    || _         || _        d S r=   rN   )rG   r,   rO   s      r0   __init__z_MultiOutputEstimator.__init__i   s    "r2   r5   Fprefer_skip_nested_validationc                 ^    t          | d           t           d           t           dd          j        dk    rt	          d          t                      r|||d	<   t           dfi |n|$t           j        d	          st	          d
          |-t          t          t          |                              n*t          t          t                                           t           j                   fdt          j        d                   D                        _        r2t           j        d         d          r j        d         j         _        r2t           j        d         d          r j        d         j         _         S )a  Incrementally fit a separate model for each class output.

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

        y : {array-like, sparse matrix} of shape (n_samples, n_outputs)
            Multi-output targets.

        classes : list of ndarray of shape (n_outputs,), default=None
            Each array is unique classes for one output in str/int.
            Can be obtained via
            ``[np.unique(y[:, i]) for i in range(y.shape[1])]``, where `y`
            is the target matrix of the entire dataset.
            This argument is required for the first call to partial_fit
            and can be omitted in the subsequent calls.
            Note that `y` doesn't need to contain all labels in `classes`.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights. If `None`, then samples are equally weighted.
            Only supported if the underlying regressor supports sample
            weights.

        **partial_fit_params : dict of str -> object
            Parameters passed to the ``estimator.partial_fit`` method of each
            sub-estimator.

            Only available if `enable_metadata_routing=True`. See the
            :ref:`User Guide <metadata_routing>`.

            .. versionadded:: 1.3

        Returns
        -------
        self : object
            Returns a fitted instance.
        r5   r;   no_validationTr-   r.   multi_outputr   Qy must have at least two dimensions for multi-output regression but has only one.Nr*   5Underlying estimator does not support sample weights.)r*   )r5   r,   rQ   c           	   3      K   | ]Y} t          t                    sj        |         nj        d d |f         |         nd j        j                  V  Zd S )N)r6   r7   )r   r8   r;   r,   r5   )rA   ir-   r4   r7   routed_paramsrG   r.   s     r0   rD   z4_MultiOutputEstimator.partial_fit.<locals>.<genexpr>   s       
8
 
8
  ,G*+++5I ##4>!!!Q$%1

t#0#:#F%  
8
 
8
 
8
 
8
 
8
 
8
r2   r   n_features_in_feature_names_in_)r   r?   r$   ndim
ValueErrorr   r   r#   r,   r   r   rO   rangeshaper;   r`   ra   )rG   r-   r.   r4   r*   r6   r7   r_   s   ````  @@r0   r5   z!_MultiOutputEstimator.partial_fitn   s#   X 	,dMBBB }555
$/QTJJJ6Q;;<  
  	L(6C"?3+  % MM (1B2 2( !K   ( %#M0R0R0RSSS! ! ! !&%''0J0J0J K K K784;777 
8
 
8
 
8
 
8
 
8
 
8
 
8
 
8
 
8
 171:&&
8
 
8
 
8
 

 

  	E'$"21"57GHH 	E"&"21"5"DD 	K'$"21"57JKK 	K%)%5a%8%JD"r2   c                 \    t           j        d          st          d          t           dd          t	                     rt                     j        dk    rt          d          t                      r|||d	<   t           dfi |nf|$t           j        d	          st          d
          t          |          }t          t          |                    ||j        j        d	<    t           j                   fdt          j        d                   D                        _        t           j        d         d          r j        d         j         _        t           j        d         d          r j        d         j         _         S )ah  Fit the model to data, separately for each output variable.

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

        y : {array-like, sparse matrix} of shape (n_samples, n_outputs)
            Multi-output targets. An indicator matrix turns on multilabel
            estimation.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights. If `None`, then samples are equally weighted.
            Only supported if the underlying regressor supports sample
            weights.

        **fit_params : dict of string -> object
            Parameters passed to the ``estimator.fit`` method of each step.

            .. versionadded:: 0.23

        Returns
        -------
        self : object
            Returns a fitted instance.
        r+   z0The base estimator should implement a fit methodrW   TrX   r   rZ   Nr*   r[   )paramsr+   r\   rQ   c              3      K   | ]:} t          t                    j        d d |f         fi j        j        V  ;d S r=   )r   r1   r,   r+   )rA   r^   r-   r_   rG   r.   s     r0   rD   z,_MultiOutputEstimator.fit.<locals>.<genexpr>  su       8
 8
  $GN##1QQQT7 .;.E.I 8
 8
 8
 8
 8
 8
r2   r   r`   ra   )r?   r,   rc   r$   r   r   rb   r   r   r#   r    r   r+   r   rO   rd   re   r;   r`   ra   )rG   r-   r.   r*   r/   fit_params_validatedr_   s   ```   @r0   r+   z_MultiOutputEstimator.fit   s   > t~u-- 	QOPPP$/QTJJJ 	,(+++6Q;;<  
  	M(.;
?++   MM (1B2 2( !K   $8*#M#M#M !E6J,K,K,KLLLM(?L'+O<784;777 8
 8
 8
 8
 8
 8
 8
 171:&&	8
 8
 8
 
 
 4#A&(899 	E"&"21"5"DD4#A&(;<< 	K%)%5a%8%JD"r2   c                    t          |            t          | j        d         d          st          d           t	          | j                  fd| j        D                       }t          j        |          j        S )a  Predict multi-output variable using model for each target variable.

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

        Returns
        -------
        y : {array-like, sparse matrix} of shape (n_samples, n_outputs)
            Multi-output targets predicted across multiple predictors.
            Note: Separate models are generated for each predictor.
        r   rM   z4The base estimator should implement a predict methodrQ   c              3   R   K   | ]!} t          |j                            V  "d S r=   )r   rM   )rA   er-   s     r0   rD   z0_MultiOutputEstimator.predict.<locals>.<genexpr>2  sI       )
 )
&'GAIq!!)
 )
 )
 )
 )
 )
r2   )	r"   r?   r;   rc   r   rO   npasarrayT)rG   r-   r.   s    ` r0   rM   z_MultiOutputEstimator.predict   s     	t'*I66 	USTTT(HDK((( )
 )
 )
 )
+/+;)
 )
 )
 
 
 z!}}r2   c                     t                                                      }t          | j                  j        j        |j        _        d|j        _        d|j        _        |S NFT)	super__sklearn_tags__r   r,   
input_tagssparsetarget_tagssingle_outputrY   rG   tags	__class__s     r0   rt   z&_MultiOutputEstimator.__sklearn_tags__8  sM    ww''))!)$.!9!9!D!K).&(,%r2   c                     t          | j        j                                      | j        t                                          dd                              dd                    }|S )j  Get metadata routing of this object.

        Please check :ref:`User Guide <metadata_routing>` on how the routing
        mechanism works.

        .. versionadded:: 1.3

        Returns
        -------
        routing : MetadataRouter
            A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
            routing information.
        ownerr5   callercalleer+   r,   method_mapping)r   r{   __name__addr,   r   rG   routers     r0   get_metadata_routingz*_MultiOutputEstimator.get_metadata_routing?  sd      dn&=>>>BBn(??SmS<<SeS,,	 C 
 
 r2   )NNr=   )r   
__module____qualname__r   r   rP   dict__annotations__r   rS   rJ   r   r5   r+   rM   rt   r   __classcell__r{   s   @r0   rL   rL   c   sI         j%!3445T"$ $D   
 ,0     ^ ! //\&+  [ [ [	  0/
[z \&+  J J J	 JX  0          r2   rL   )	metaclassc                   V     e Zd ZdZdd fd
Z ed          d fd	            Z xZS )r%   a  Multi target regression.

    This strategy consists of fitting one regressor per target. This is a
    simple strategy for extending regressors that do not natively support
    multi-target regression.

    .. versionadded:: 0.18

    Parameters
    ----------
    estimator : estimator object
        An estimator object implementing :term:`fit` and :term:`predict`.

    n_jobs : int or None, optional (default=None)
        The number of jobs to run in parallel.
        :meth:`fit`, :meth:`predict` and :meth:`partial_fit` (if supported
        by the passed estimator) will be parallelized for each target.

        When individual estimators are fast to train or predict,
        using ``n_jobs > 1`` can result in slower performance due
        to the parallelism overhead.

        ``None`` means `1` unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all available processes / threads.
        See :term:`Glossary <n_jobs>` for more details.

        .. versionchanged:: 0.20
            `n_jobs` default changed from `1` to `None`.

    Attributes
    ----------
    estimators_ : list of ``n_output`` estimators
        Estimators used for predictions.

    n_features_in_ : int
        Number of features seen during :term:`fit`. Only defined if the
        underlying `estimator` 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 estimators expose such an attribute when fit.

        .. versionadded:: 1.0

    See Also
    --------
    RegressorChain : A multi-label model that arranges regressions into a
        chain.
    MultiOutputClassifier : Classifies each output independently rather than
        chaining.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.datasets import load_linnerud
    >>> from sklearn.multioutput import MultiOutputRegressor
    >>> from sklearn.linear_model import Ridge
    >>> X, y = load_linnerud(return_X_y=True)
    >>> regr = MultiOutputRegressor(Ridge(random_state=123)).fit(X, y)
    >>> regr.predict(X[[0]])
    array([[176..., 35..., 57...]])
    NrQ   c                N    t                                          ||           d S NrQ   rs   rS   rG   r,   rO   r{   s      r0   rS   zMultiOutputRegressor.__init__  &    622222r2   r5   c                 B     t                      j        ||fd|i| dS )a  Incrementally fit the model to data, for each output variable.

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

        y : {array-like, sparse matrix} of shape (n_samples, n_outputs)
            Multi-output targets.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights. If `None`, then samples are equally weighted.
            Only supported if the underlying regressor supports sample
            weights.

        **partial_fit_params : dict of str -> object
            Parameters passed to the ``estimator.partial_fit`` method of each
            sub-estimator.

            Only available if `enable_metadata_routing=True`. See the
            :ref:`User Guide <metadata_routing>`.

            .. versionadded:: 1.3

        Returns
        -------
        self : object
            Returns a fitted instance.
        r*   N)rs   r5   )rG   r-   r.   r*   r6   r{   s        r0   r5   z MultiOutputRegressor.partial_fit  s2    > 	AqTTTASTTTTTr2   r=   )r   r   r   __doc__rS   rJ   r5   r   r   s   @r0   r%   r%   V  s        ? ?B -1 3 3 3 3 3 3 3 ! //U U U U U 0/U U U U Ur2   r%   c                   r     e Zd ZdZdd fd
Zd
 fd	Zd Z ee          d             Zd Z	 fd	Z
 xZS )r&   av	  Multi target classification.

    This strategy consists of fitting one classifier per target. This is a
    simple strategy for extending classifiers that do not natively support
    multi-target classification.

    Parameters
    ----------
    estimator : estimator object
        An estimator object implementing :term:`fit` and :term:`predict`.
        A :term:`predict_proba` method will be exposed only if `estimator` implements
        it.

    n_jobs : int or None, optional (default=None)
        The number of jobs to run in parallel.
        :meth:`fit`, :meth:`predict` and :meth:`partial_fit` (if supported
        by the passed estimator) will be parallelized for each target.

        When individual estimators are fast to train or predict,
        using ``n_jobs > 1`` can result in slower performance due
        to the parallelism overhead.

        ``None`` means `1` unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all available processes / threads.
        See :term:`Glossary <n_jobs>` for more details.

        .. versionchanged:: 0.20
            `n_jobs` default changed from `1` to `None`.

    Attributes
    ----------
    classes_ : ndarray of shape (n_classes,)
        Class labels.

    estimators_ : list of ``n_output`` estimators
        Estimators used for predictions.

    n_features_in_ : int
        Number of features seen during :term:`fit`. Only defined if the
        underlying `estimator` 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 estimators expose such an attribute when fit.

        .. versionadded:: 1.0

    See Also
    --------
    ClassifierChain : A multi-label model that arranges binary classifiers
        into a chain.
    MultiOutputRegressor : Fits one regressor per target variable.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.datasets import make_multilabel_classification
    >>> from sklearn.multioutput import MultiOutputClassifier
    >>> from sklearn.linear_model import LogisticRegression
    >>> X, y = make_multilabel_classification(n_classes=3, random_state=0)
    >>> clf = MultiOutputClassifier(LogisticRegression()).fit(X, y)
    >>> clf.predict(X[-2:])
    array([[1, 1, 1],
           [1, 0, 1]])
    NrQ   c                N    t                                          ||           d S r   r   r   s      r0   rS   zMultiOutputClassifier.__init__  r   r2   c                 n     t                      j        ||fd|i| d | j        D             | _        | S )a  Fit the model to data matrix X and targets Y.

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

        Y : array-like of shape (n_samples, n_classes)
            The target values.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights. If `None`, then samples are equally weighted.
            Only supported if the underlying classifier supports sample
            weights.

        **fit_params : dict of string -> object
            Parameters passed to the ``estimator.fit`` method of each step.

            .. versionadded:: 0.23

        Returns
        -------
        self : object
            Returns a fitted instance.
        r*   c                     g | ]	}|j         
S  classes_rA   r,   s     r0   
<listcomp>z-MultiOutputClassifier.fit.<locals>.<listcomp>       NNN	+NNNr2   )rs   r+   r;   r   )rG   r-   Yr*   r/   r{   s        r0   r+   zMultiOutputClassifier.fit  sG    4 	AqDDDDDDNNT=MNNNr2   c                 v    t          | d          rd | j        D              dS t          | j        d           dS )Nr;   c                 .    g | ]}t          |d           S predict_proba)getattr)rA   rB   s     r0   r   z>MultiOutputClassifier._check_predict_proba.<locals>.<listcomp>'  s"    GGGsWS/**GGGr2   Tr   )r?   r;   r   r,   )rG   s    r0   _check_predict_probaz*MultiOutputClassifier._check_predict_proba#  sL    4'' 	 HGd6FGGGG4 	000tr2   c                 L    t          |            fd| j        D             }|S )a_  Return prediction probabilities for each class of each output.

        This method will raise a ``ValueError`` if any of the
        estimators do not have ``predict_proba``.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The input data.

        Returns
        -------
        p : array of shape (n_samples, n_classes), or a list of n_outputs                 such arrays if n_outputs > 1.
            The class probabilities of the input samples. The order of the
            classes corresponds to that in the attribute :term:`classes_`.

            .. versionchanged:: 0.19
                This function now returns a list of arrays where the length of
                the list is ``n_outputs``, and each array is (``n_samples``,
                ``n_classes``) for that particular output.
        c                 :    g | ]}|                               S r   r   )rA   r,   r-   s     r0   r   z7MultiOutputClassifier.predict_proba.<locals>.<listcomp>G  s'    PPP)9**1--PPPr2   )r"   r;   )rG   r-   resultss    ` r0   r   z#MultiOutputClassifier.predict_proba.  s4    0 	PPPPt?OPPPr2   c                 |   t          |            t          | j                  }|j        dk    rt	          d          |j        d         |k    r.t	          d                    ||j        d                             |                     |          }t          j	        t          j
        ||k    d                    S )a  Return the mean accuracy on the given test data and labels.

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

        y : array-like of shape (n_samples, n_outputs)
            True values for X.

        Returns
        -------
        scores : float
            Mean accuracy of predicted target versus true target.
        r   zTy must have at least two dimensions for multi target classification but has only onezCThe number of outputs of Y for fit {0} and score {1} should be same)axis)r"   lenr;   rb   rc   re   formatrM   rn   meanrE   )rG   r-   r.   
n_outputs_y_preds        r0   scorezMultiOutputClassifier.scoreJ  s      	)**
6Q;;?   71:##,,2F:qwqz,J,J   awrva6k222333r2   c                 V    t                                                      }d|_        |S )NT)rs   rt   
_skip_testry   s     r0   rt   z&MultiOutputClassifier.__sklearn_tags__i  s$    ww''))r2   r=   )r   r   r   r   rS   r+   r   r   r   r   rt   r   r   s   @r0   r&   r&     s        B BH -1 3 3 3 3 3 3 3     <	 	 	 \&''  ('64 4 4>        r2   r&   c                 ,      fd}t          |          S )zwReturn a function to check if `base_estimator` or `estimators_` has `attr`.

    Helper for Chain implementations.
    c                 n    t          | j                  pt          fd| j        D                       S )Nc              3   8   K   | ]}t          |          V  d S r=   r>   r@   s     r0   rD   zC_available_if_base_estimator_has.<locals>._check.<locals>.<genexpr>w  s>       9
 9
#&GC9
 9
 9
 9
 9
 9
r2   )r?   base_estimatorrE   r;   rF   s    r0   rH   z0_available_if_base_estimator_has.<locals>._checkv  sR    t*D11 
S 9
 9
 9
 9
*.*:9
 9
 9
 6
 6
 	
r2   r   rI   s   ` r0    _available_if_base_estimator_hasr   p  s,    
 
 
 
 

 r2   c                        e Zd ZU  eddg          gd edh          dgd edh          gdgd	gd
Zeed<   ddddddZd Z	d Z
ed             Zd Z fdZ xZS )
_BaseChainr+   rM   z
array-likerandomN	cv_objectprefitrandom_statebooleanr   ordercvr   verboserP   Fr   r   r   r   c                L    || _         || _        || _        || _        || _        d S r=   r   )rG   r   r   r   r   r   s         r0   rS   z_BaseChain.__init__  s-     -
(r2   c                *    | j         sd S d| d| d| S )N(z of z) )r   )rG   estimator_idxn_estimatorsprocessing_msgs       r0   _log_messagez_BaseChain._log_message  s/    | 	4F=FFlFFnFFFr2   c                   t          |            t          | |dd          }t          j        |j        d         t          | j                  f          }t          j        |j        d         t          | j                  f          }t          | dd          }t          j	        |          rt          j
        nt          j
        }t          | j                  D ]\  }}|ddd|f         }	t          j	        |          r3t          j        |          s|j        dk    rt          j        |          } |||	f          }
t          ||
|	          \  }}||dd|f<   t          ||
|	          \  }}||dd|f<   t          j        | j                  }t          j        t          | j                            || j        <   |dd|f         }|S )
z,Get predictions for each model in the chain.TF)accept_sparseresetr   chain_method_rM   Ndok)response_method)r"   r$   rn   zerosre   r   r;   r   spissparsehstack	enumerate
isspmatrixr   	coo_arrayr   
empty_likeorder_arange)rG   r-   output_methodY_output_chainY_feature_chainchain_methodr   	chain_idxr,   previous_predictionsX_augfeature_predictions_output_predictions	inv_orderY_outputs                   r0   _get_predictionsz_BaseChain._get_predictions  s   $UCCC171:s43C/D/D"EFF(AGAJD4D0E0E#FGG t_i@@ k!nn;")$-d.>$?$? 	> 	> Iy#2111jyj=#A  {1~~ $bmA&6&6 $18u;L;LLOOFA3455E%9 ,& & &"
 -@OAAAyL)$8 -% % %!
 ,>N111i<((M$+..	!#3t{+;+;!<!<	$+!!!!Y,/r2   c           	      	    t           ||dd          \  }}t           j                  } j         _        t           j        t                    rt          j         j                   _         j        2t          j        t          |j
        d                              _        nt           j        t                    r1 j        dk    r%|                    |j
        d                    _        nLt           j                  t          t          |j
        d                             k    rt          d           fdt          |j
        d                   D              _         j        j|dd j        f         }t%          j        |          r.t%          j        ||fd	          }|                                }nt          j        ||f          }nt%          j        |          rt%          j        |          sL|j        d
k    rt%          j        |          }t%          j        |j
        d         |j
        d         f          }n,t%          j        |j
        d         |j
        d         f          }t%          j        ||fd	          }nBt          j        |j
        d         |j
        d         f          }t          j        ||f          }~t7                      rt9           dfi |}nt;          t;          |                    }t=           d          r't?           j          j!                  j"        }| _#        nd}tI           j                  D ]a\  }	}
 %                    |	dz   tM           j                  d j        |	                    }|dd j        |	         f         }tO          d|          5   |
j(        |ddd|j
        d         |	z   f         |fi |j)        j(         ddd           n# 1 swxY w Y    j        |	tM           j                  dz
  k     r|j
        d         |	z   }tU           j         |ddd|f         | j        |          }|j+        dk    r|dddf         }t%          j        |          rt          j,        |d          |dd|f<   X||dd|f<   c S )a  Fit the model to data matrix X and targets Y.

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

        Y : array-like of shape (n_samples, n_classes)
            The target values.

        **fit_params : dict of string -> object
            Parameters passed to the `fit` method of each step.

            .. versionadded:: 0.23

        Returns
        -------
        self : object
            Returns a fitted instance.
        T)rY   r   Nr   r   zinvalid orderc                 8    g | ]}t          j                  S r   )r   r   )rA   r   rG   s     r0   r   z"_BaseChain.fit.<locals>.<listcomp>  s$    RRR1E$"566RRRr2   lil)r   r   r   r+   rh   r\   r   rM   zProcessing order )r   r   r   Chain)r.   r   method)-r$   r   r   r   r   
isinstancetuplern   arrayrd   re   strpermutationsortedlistrc   r;   r   r   r   r   tocsrr   r   r   
coo_matrixr   r   r   r   r?   r!   r   r   r   r   r   r   r   r   r+   r,   r   rb   expand_dims)rG   r-   r   r/   r   Y_pred_chainr   r_   r   r   r,   messager.   col_idx	cv_results   `              r0   r+   z_BaseChain.fit  s   , T1ad$OOO1)$*;<<jdk5)) 	0(4;//DK;(5#4#455DKKS)) 	.{h&&*66qwqzBBDK  Dqwqz):):$;$;;;_---RRRRagaj@Q@QRRR7?QQQ^,L{1~~ 5	1l"3EBBB	1l"344[^^ 	1 =## 
G 8u$$QA  "|QWQZ,DEE!}agaj!'!*-EFFIq,/>>>EE 8QWQZ$<==LIq,/00E 	C+D%FF:FFMM!Ej,A,A,ABBBM4(( 	%1#!    ".D %L$-d.>$?$? 	2 	2 Iy'''!m !122K4;y3IKK (  G
 !!!T[++,A$Wg66  	!!!7
Y 6778  $-1                 w"y3t7G3H3H13L'L'L'!*y0-'!!!XgX+&w'  	 >A%% )!!!Q$I;u%% 2(*y!(D(DE!!!W*%%(1E!!!W*%s   	4P		P	P	c                 0    |                      |d          S )aQ  Predict on the data matrix X using the ClassifierChain model.

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

        Returns
        -------
        Y_pred : array-like of shape (n_samples, n_classes)
            The predicted values.
        rM   r   r   rG   r-   s     r0   rM   z_BaseChain.predict2  s     $$Qi$@@@r2   c                     t                                                      }t          | j                  j        j        |j        _        |S r=   )rs   rt   r   r   ru   rv   ry   s     r0   rt   z_BaseChain.__sklearn_tags__A  s8    ww''))!)$*=!>!>!I!Pr2   )r   r   r   r   r   rP   r   r   rS   r   r   r   r+   rM   rt   r   r   s   @r0   r   r   ~  s        %:ui&899:

H: 6 6=JJz223'(;$ $D    (,4QV    G G G
& & &P r r ^rhA A A        r2   r   c                       e Zd ZU dZi ej        dee eh d          giZe	e
d<   dddddd fd	
Z ed
           fd            Z ed          d             Zd Z ed          d             Zd Z fdZ xZS )r'   a  A multi-label model that arranges binary classifiers into a chain.

    Each model makes a prediction in the order specified by the chain using
    all of the available features provided to the model plus the predictions
    of models that are earlier in the chain.

    For an example of how to use ``ClassifierChain`` and benefit from its
    ensemble, see
    :ref:`ClassifierChain on a yeast dataset
    <sphx_glr_auto_examples_multioutput_plot_classifier_chain_yeast.py>` example.

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

    .. versionadded:: 0.19

    Parameters
    ----------
    base_estimator : estimator
        The base estimator from which the classifier chain is built.

    order : array-like of shape (n_outputs,) or 'random', default=None
        If `None`, the order will be determined by the order of columns in
        the label matrix Y.::

            order = [0, 1, 2, ..., Y.shape[1] - 1]

        The order of the chain can be explicitly set by providing a list of
        integers. For example, for a chain of length 5.::

            order = [1, 3, 2, 4, 0]

        means that the first model in the chain will make predictions for
        column 1 in the Y matrix, the second model will make predictions
        for column 3, etc.

        If order is `random` a random ordering will be used.

    cv : int, cross-validation generator or an iterable, default=None
        Determines whether to use cross validated predictions or true
        labels for the results of previous estimators in the chain.
        Possible inputs for cv are:

        - None, to use true labels when fitting,
        - integer, to specify the number of folds in a (Stratified)KFold,
        - :term:`CV splitter`,
        - An iterable yielding (train, test) splits as arrays of indices.

    chain_method : {'predict', 'predict_proba', 'predict_log_proba',             'decision_function'} or list of such str's, default='predict'

        Prediction method to be used by estimators in the chain for
        the 'prediction' features of previous estimators in the chain.

        - if `str`, name of the method;
        - if a list of `str`, provides the method names in order of
          preference. The method used corresponds to the first method in
          the list that is implemented by `base_estimator`.

        .. versionadded:: 1.5

    random_state : int, RandomState instance or None, optional (default=None)
        If ``order='random'``, determines random number generation for the
        chain order.
        In addition, it controls the random seed given at each `base_estimator`
        at each chaining iteration. Thus, it is only used when `base_estimator`
        exposes a `random_state`.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    verbose : bool, default=False
        If True, chain progress is output as each model is completed.

        .. versionadded:: 1.2

    Attributes
    ----------
    classes_ : list
        A list of arrays of length ``len(estimators_)`` containing the
        class labels for each estimator in the chain.

    estimators_ : list
        A list of clones of base_estimator.

    order_ : list
        The order of labels in the classifier chain.

    chain_method_ : str
        Prediction method used by estimators in the chain for the prediction
        features.

    n_features_in_ : int
        Number of features seen during :term:`fit`. Only defined if the
        underlying `base_estimator` 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`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    See Also
    --------
    RegressorChain : Equivalent for regression.
    MultiOutputClassifier : Classifies each output independently rather than
        chaining.

    References
    ----------
    Jesse Read, Bernhard Pfahringer, Geoff Holmes, Eibe Frank, "Classifier
    Chains for Multi-label Classification", 2009.

    Examples
    --------
    >>> from sklearn.datasets import make_multilabel_classification
    >>> from sklearn.linear_model import LogisticRegression
    >>> from sklearn.model_selection import train_test_split
    >>> from sklearn.multioutput import ClassifierChain
    >>> X, Y = make_multilabel_classification(
    ...    n_samples=12, n_classes=3, random_state=0
    ... )
    >>> X_train, X_test, Y_train, Y_test = train_test_split(
    ...    X, Y, random_state=0
    ... )
    >>> base_lr = LogisticRegression(solver='lbfgs', random_state=0)
    >>> chain = ClassifierChain(base_lr, order='random', random_state=0)
    >>> chain.fit(X_train, Y_train).predict(X_test)
    array([[1., 1., 0.],
           [1., 0., 0.],
           [0., 1., 0.]])
    >>> chain.predict_proba(X_test)
    array([[0.8387..., 0.9431..., 0.4576...],
           [0.8878..., 0.3684..., 0.2640...],
           [0.0321..., 0.9935..., 0.0626...]])
    r   >   rM   r   decision_functionpredict_log_probarP   NrM   F)r   r   r   r   r   c                b    t                                          |||||           || _        d S )Nr   )rs   rS   r   )rG   r   r   r   r   r   r   r{   s          r0   rS   zClassifierChain.__init__  sE     	% 	 	
 	
 	
 )r2   rT   c                     t          || d            t                      j        ||fi | d | j        D             | _        | S )ay  Fit the model to data matrix X and targets Y.

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

        Y : array-like of shape (n_samples, n_classes)
            The target values.

        **fit_params : dict of string -> object
            Parameters passed to the `fit` method of each step.

            Only available if `enable_metadata_routing=True`. See the
            :ref:`User Guide <metadata_routing>`.

            .. versionadded:: 1.3

        Returns
        -------
        self : object
            Class instance.
        r+   c                     g | ]	}|j         
S r   r   r   s     r0   r   z'ClassifierChain.fit.<locals>.<listcomp>  r   r2   )r   rs   r+   r;   r   rG   r-   r   r/   r{   s       r0   r+   zClassifierChain.fit  sT    8 	*dE222Aq''J'''NNT=MNNNr2   r   c                 0    |                      |d          S )a9  Predict probability estimates.

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

        Returns
        -------
        Y_prob : array-like of shape (n_samples, n_classes)
            The predicted probabilities.
        r   r   r  r  s     r0   r   zClassifierChain.predict_proba  s     $$Qo$FFFr2   c                 P    t          j        |                     |                    S )a[  Predict logarithm of probability estimates.

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

        Returns
        -------
        Y_log_prob : array-like of shape (n_samples, n_classes)
            The predicted logarithm of the probabilities.
        )rn   logr   r  s     r0   r  z!ClassifierChain.predict_log_proba!  s"     vd((++,,,r2   r  c                 0    |                      |d          S )a  Evaluate the decision_function of the models in the chain.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The input data.

        Returns
        -------
        Y_decision : array-like of shape (n_samples, n_classes)
            Returns the decision function of the sample for each model
            in the chain.
        r  r   r  r  s     r0   r  z!ClassifierChain.decision_function0  s     $$Q6I$JJJr2   c                     t          | j        j                                      | j        t                                          dd                    }|S r}   r~   r+   r   r   r   r{   r   r   r   r   r   s     r0   r   z$ClassifierChain.get_metadata_routingA  V      dn&=>>>BB)(??..eE.JJ C 
 
 r2   c                     t                                                      }d|_        d|j        _        d|j        _        |S )NTF)rs   rt   r   rw   rx   rY   ry   s     r0   rt   z ClassifierChain.__sklearn_tags__U  s:    ww''))).&(,%r2   )r   r   r   r   r   rP   r   r   r   r   r   rS   r   r+   r   r   r  r  r   rt   r   r   s   @r0   r'   r'   G  s~        G GR	$

+	$JVVV 
	$ 	$D 	 	 	 ) ) ) ) ) ) )& \&+     	 < &%o66G G 76G- - - &%&9::K K ;:K   (        r2   r'   c                   V     e Zd ZdZ ed           fd            Zd Z fdZ xZS )r(   aH  A multi-label model that arranges regressions into a chain.

    Each model makes a prediction in the order specified by the chain using
    all of the available features provided to the model plus the predictions
    of models that are earlier in the chain.

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

    .. versionadded:: 0.20

    Parameters
    ----------
    base_estimator : estimator
        The base estimator from which the regressor chain is built.

    order : array-like of shape (n_outputs,) or 'random', default=None
        If `None`, the order will be determined by the order of columns in
        the label matrix Y.::

            order = [0, 1, 2, ..., Y.shape[1] - 1]

        The order of the chain can be explicitly set by providing a list of
        integers. For example, for a chain of length 5.::

            order = [1, 3, 2, 4, 0]

        means that the first model in the chain will make predictions for
        column 1 in the Y matrix, the second model will make predictions
        for column 3, etc.

        If order is 'random' a random ordering will be used.

    cv : int, cross-validation generator or an iterable, default=None
        Determines whether to use cross validated predictions or true
        labels for the results of previous estimators in the chain.
        Possible inputs for cv are:

        - None, to use true labels when fitting,
        - integer, to specify the number of folds in a (Stratified)KFold,
        - :term:`CV splitter`,
        - An iterable yielding (train, test) splits as arrays of indices.

    random_state : int, RandomState instance or None, optional (default=None)
        If ``order='random'``, determines random number generation for the
        chain order.
        In addition, it controls the random seed given at each `base_estimator`
        at each chaining iteration. Thus, it is only used when `base_estimator`
        exposes a `random_state`.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    verbose : bool, default=False
        If True, chain progress is output as each model is completed.

        .. versionadded:: 1.2

    Attributes
    ----------
    estimators_ : list
        A list of clones of base_estimator.

    order_ : list
        The order of labels in the classifier chain.

    n_features_in_ : int
        Number of features seen during :term:`fit`. Only defined if the
        underlying `base_estimator` 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`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    See Also
    --------
    ClassifierChain : Equivalent for classification.
    MultiOutputRegressor : Learns each output independently rather than
        chaining.

    Examples
    --------
    >>> from sklearn.multioutput import RegressorChain
    >>> from sklearn.linear_model import LogisticRegression
    >>> logreg = LogisticRegression(solver='lbfgs')
    >>> X, Y = [[1, 0], [0, 1], [1, 1]], [[0, 2], [1, 1], [2, 0]]
    >>> chain = RegressorChain(base_estimator=logreg, order=[0, 1]).fit(X, Y)
    >>> chain.predict(X)
    array([[0., 2.],
           [1., 1.],
           [2., 0.]])
    FrT   c                 >     t                      j        ||fi | | S )a/  Fit the model to data matrix X and targets Y.

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

        Y : array-like of shape (n_samples, n_classes)
            The target values.

        **fit_params : dict of string -> object
            Parameters passed to the `fit` method at each step
            of the regressor chain.

            .. versionadded:: 0.23

        Returns
        -------
        self : object
            Returns a fitted instance.
        )rs   r+   r
  s       r0   r+   zRegressorChain.fit  s*    4 	Aq''J'''r2   c                     t          | j        j                                      | j        t                                          dd                    }|S r  r  r   s     r0   r   z#RegressorChain.get_metadata_routing  r  r2   c                 x    t                                                      }d|j        _        d|j        _        |S rr   )rs   rt   rw   rx   rY   ry   s     r0   rt   zRegressorChain.__sklearn_tags__  s3    ww''))).&(,%r2   )	r   r   r   r   r   r+   r   rt   r   r   s   @r0   r(   r(   ^  s        ] ]~ \&+     	 2  (        r2   r(   r=   )NNT)>r   abcr   r   numbersr   numpyrn   scipy.sparserv   r   baser   r   r	   r
   r   r   r   model_selectionr   utilsr   r   r   utils._param_validationr   r   utils._responser   utils._user_interfacer   utils.metadata_routingr   r   r   r   r   utils.metaestimatorsr   utils.multiclassr   utils.parallelr   r   utils.validationr    r!   r"   r#   r$   __all__r1   r8   rJ   rL   r%   r&   r   r   r'   r(   r   r2   r0   <module>r(     s    ( ' ' ' ' ' ' '                                  / . . . . . 6 6 6 6 6 6 6 6 6 6 ; ; ; ; ; ; ; ; 1 1 1 1 1 1 6 6 6 6 6 6              / . . . . . : : : : : : - - - - - - - -                   HL        $p p p p p. p p p pfdU dU dU dU dU>+@ dU dU dUNp p p p pO-B p p pf     F F F F F' F F F FRT T T T T(/: T T TnU U U U U' U U U U Ur2   