`L idZddlZddlmZmZddlmZddlZddl m Z ddl m Z mZmZmZmZmZmZddlmZddlmZmZmZdd lmZmZmZdd lmZdd l m!Z!dd l"m#Z#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)ddl*m+Z+ddl,m-Z-m.Z.ddl/m0Z0m1Z1m2Z2m3Z3m4Z4gdZ5d#dZ6 d$dZ7dZ8Gddee eZ9Gddee9Z:Gddee9Z;dZ<Gdde eZ=Gdd eee=Z>Gd!d"eee=Z?y)%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. N)ABCMetaabstractmethod)Integral) BaseEstimatorClassifierMixinMetaEstimatorMixinRegressorMixin _fit_contextclone is_classifier)cross_val_predict)Bunchcheck_random_stateget_tags) HasMethodsHidden StrOptions)_get_response_values)_print_elapsed_time)MetadataRouter MethodMapping_raise_for_params_routing_enabledprocess_routing available_if)check_classification_targets)Paralleldelayed)_check_method_params_check_response_methodcheck_is_fittedhas_fit_parameter validate_data)ClassifierChainMultiOutputClassifierMultiOutputRegressorRegressorChainc xt|}||j||fd|i||S|j||fi||S)N sample_weight)r fit) estimatorXyr+ fit_paramss Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/multioutput.py_fit_estimatorr2>sQi I  aF-F:F   a)j) c|in|}|r t|}||j||fd|i||S|j||fi||S)Nclasses)r partial_fit)r-r.r/r5partial_fit_params first_times r1_partial_fit_estimatorr9Gsj 29?Q)$  aJGJ7IJ   a9&89 r3c$fd}t|S)zpReturn a function to check if the sub-estimator(s) has(have) `attr`. Helper for Chain implementations. ct|drtfd|jDSt|jryy)N estimators_c36K|]}t|ywNhasattr.0estattrs r1 z>_available_if_estimator_has.._check..]sFcwsD)FTF)r@allr<r-selfrDs r1_checkz+_available_if_estimator_has.._check[s8 4 'FT5E5EFF F 4>>4 (r3rrDrJs` r1_available_if_estimator_hasrLUs   r3ceZdZUeddggedgdZeed<edddZ e de d dd Z e d dd Z d ZfdZdZxZS)_MultiOutputEstimatorr,predictNr-n_jobs_parameter_constraintsrQc ||_||_yr>rP)rIr-rQs r1__init__z_MultiOutputEstimator.__init__ms" r3r6Fprefer_skip_nested_validationc t|dtd tddjdk(r t dt r|||d<t dfi|nd|!tjds t d |!ttt|   nttt  tj fdtjdD_r7tjddrjdj_r7tjddrjdj _S)aIncrementally 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 `. .. versionadded:: 1.3 Returns ------- self : object Returns a fitted instance. r6r< no_validationTr.r/ multi_outputrQy must have at least two dimensions for multi-output regression but has only one.r+5Underlying estimator does not support sample weights.)r+)r6r-rSc 3K|]\}ttsj|n jdd|f|ndjj^yw)N)r7r8)r r9r<r-r6)rBir.r5r8 routed_paramsrIr/s r1rEz4_MultiOutputEstimator.partial_fit..sq 8  ,G* ++5  #4>>!Q$%1 t#0#:#:#F#F%    8 sA"A%rn_features_in_feature_names_in_)rr@r%ndim ValueErrorrrr$r-rrrQrangeshaper<rbrc)rIr.r/r5r+r7r8ras```` @@r1r6z!_MultiOutputEstimator.partial_fitrsX ,dMB }55 $/QT J 66Q;<   (6C"?3+%M (1B2!K( %#M0RS! !&%'0J K 784;;7 8 8 1771:& 8   '$"2"21"57GH"&"2"21"5"D"DD  '$"2"21"57JK%)%5%5a%8%J%JD " r3c 0tjds tdtddt r t j dk(r tdtr|||d<tdfi|na|!tjds td t| }tt|  ||jjd<tj fdtj dD_tj"ddrj"dj$_tj"ddrj"dj&_S)ahFit 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 methodrYTrZrr\r+r])paramsr,r^rSc3K|]A}ttjdd|ffijjCywr>)r r2r-r,)rBr`r.rarIr/s r1rEz,_MultiOutputEstimator.fit..sN8  $GN #1QT7 .;.E.E.I.I 8 sAA rrbrc)r@r-rer%r rrdrrr$r!rr,rrQrfrgr<rbrc)rIr.r/r+r0fit_params_validatedras``` @r1r,z_MultiOutputEstimator.fits>t~~u-OP P $/QT J   ( + 66Q;<   (.; ?++M (1B2!K$8*#M !E6J,KLM(?L ''++O<784;;78 1771:& 8   4##A&(8 9"&"2"21"5"D"DD  4##A&(; <%)%5%5a%8%J%JD " r3ct|t|jdds tdt |j fd|jD}t j|jS)aPredict 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. rrOz4The base estimator should implement a predict methodrSc3TK|]}t|j!ywr>)r rO)rBer.s r1rEz0_MultiOutputEstimator.predict..6s') &' GAII q !) s%() r#r@r<rerrQnpasarrayT)rIr.r/s ` r1rOz_MultiOutputEstimator.predict$sj t''*I6ST T (HDKK () +/+;+;)  zz!}r3ct|}t|jjj |j_d|j _d|j _|SNFT) super__sklearn_tags__rr- input_tagssparse target_tags single_outputr[rItags __class__s r1rvz&_MultiOutputEstimator.__sklearn_tags__<sRw')!)$..!9!D!D!K!K).&(,% r3ct|jjj|jt jddjdd}|S)jGet metadata routing of this object. Please check :ref:`User Guide ` on how the routing mechanism works. .. versionadded:: 1.3 Returns ------- routing : MetadataRouter A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating routing information. ownerr6callercalleer,r-method_mapping)rr}__name__addr-rrIrouters r1get_metadata_routingz*_MultiOutputEstimator.get_metadata_routingCsX dnn&=&=>BBnn(? S mS < SeS , C   r3)NNr>)r __module__ __qualname__rrrRdict__annotations__rrUrLr r6r,rOrvr __classcell__r}s@r1rNrNgs %!345T"$D ,0!/&+[ 0 [z&+J JX0r3rN) metaclasscJeZdZdZddfd Zeddfd ZxZS)r(aMulti 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 ` 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.1, 57.1]]) NrSc(t|||yNrSrurUrIr-rQr}s r1rUzMultiOutputRegressor.__init__ 62r3r6c ,t|||fd|i|y)aIncrementally 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 `. .. versionadded:: 1.3 Returns ------- self : object Returns a fitted instance. r+N)rur6)rIr.r/r+r7r}s r1r6z MultiOutputRegressor.partial_fits> AqT TASTr3r>)rrr__doc__rUrLr6rrs@r1r(r(Zs.?B-13!/U0Ur3r(cfeZdZdZddfd Zd fd ZdZeedZdZ fd Z xZ 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 ` 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]]) NrSc(t|||yrrrs r1rUzMultiOutputClassifier.__init__rr3c t|||fd|i||jDcgc]}|jc}|_|Scc}w)aFit 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+)rur,r<classes_)rIr.Yr+r0r-r}s r1r,zMultiOutputClassifier.fit sF4  AqD DD=A=M=MN ++N  OsAct|dr%|jDcgc]}t|dc}yt|jdycc}w)Nr< predict_probaT)r@r<getattrr-)rIrCs r1_check_predict_probaz*MultiOutputClassifier._check_predict_proba'sE 4 '7;6F6F GsWS/ * G 0 HsA cvt||jDcgc]}|j|}}|Scc}w)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. )r#r<r)rIr.r-resultss r1rz#MultiOutputClassifier.predict_proba2s;0 ?C?O?OP)9**1-PPQs6cft|t|j}|jdk(r t d|j d|k7r(t dj ||j d|j|}tjtj||k(dS)aReturn 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. rzTy 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<rdrergformatrOrpmeanrG)rIr.r/ n_outputs_y_preds r1scorezMultiOutputClassifier.scoreNs ))* 66Q;?  771: #,,2F:qwwqz,J awwrvva6k233r3c2t|}d|_|S)NT)rurv _skip_testr{s r1rvz&MultiOutputClassifier.__sklearn_tags__msw') r3r>) rrrrrUr,rrrrrvrrs@r1r'r'sGBH-13< &'(64>r3r'c$fd}t|S)zwReturn a function to check if `base_estimator` or `estimators_` has `attr`. Helper for Chain implementations. cxt|jxstfd|jDS)Nc36K|]}t|ywr>r?rAs r1rEzC_available_if_base_estimator_has.._check..{s; #&GC ; rF)r@_get_estimatorrGr<rHs r1rJz0_available_if_base_estimator_has.._checkzs:t**,d3 s; *.*:*:; 8  r3rrKs` r1 _available_if_base_estimator_hasrts   r3ceZdZUeddgedhgeddgedgdedhdgdedhgd gd gd Zeed < ddddd dddZ dZ dZ dZ e dZdZfdZxZS) _BaseChainr,rO deprecatedNz array-likerandom cv_objectprefit random_stateboolean)base_estimatorr-ordercvrverboserRFrrrrrcX||_||_||_||_||_||_yr>)r-rrrrr)rIr-rrrrrs r1rUz_BaseChain.__init__s0#, ( r3c|j|jdk7r td|jdk7r(d}tj|t |jS|jS)zGet and validate estimator.rzBoth `estimator` and `base_estimator` are provided. You should only pass `estimator`. `base_estimator` as a parameter is deprecated in version 1.7, and will be removed in version 1.9.zj`base_estimator` as an argument was deprecated in 1.7 and will be removed in 1.9. Use `estimator` instead.)r-rrewarningswarn FutureWarning)rI warning_msgs r1rz_BaseChain._get_estimatorsn >> %4+>+>,+ND    , .<  MM+} 5&& &>> !r3c2|jsyd|d|d|S)N(z of z) )r)rI estimator_idx n_estimatorsprocessing_msgs r1 _log_messagez_BaseChain._log_messages&||=/l^2n5EFFr3ct|t||dd}tj|jdt |j f}tj|jdt |j f}t|dd}tj|rtjntj}t|j D]\}}|ddd|f} tj|r9tj|s$|jdk(rtj|}||| f} t|| | \} } | |dd|f<t|| | \} } | |dd|f<tj |j"}tj$t |j"||j"<|dd|f}|S) z,Get predictions for each model in the chain.TF) accept_sparseresetr chain_method_rONdok)response_method)r#r%rpzerosrgrr<rspissparsehstack enumerate isspmatrixr coo_arrayr empty_likeorder_arange)rIr. output_methodY_output_chainY_feature_chain chain_methodr chain_idxr-previous_predictionsX_augfeature_predictions_output_predictions inv_orderY_outputs r1_get_predictionsz_BaseChain._get_predictionss $U C1771:s43C3C/D"EF((AGGAJD4D4D0E#FGt_i@  kk!n"))$-d.>.>$? > Iy#21jyj=#A {{1~bmmA&6188u;LLLOA345E%9 ,& "  -@OAyL )$8 -% !  ,>N1i< (+ >.MM$++. !#3t{{+;!< $++!!Y,/r3c : t|||dd\}}t|j}|j|_t |jt r$tj|j|_|j1tjt|jd|_nt |jtr3|jdk(rf|j|jd|_nBt|jtt|jdk7r tdt|jdDcgc]}t!|j#c}|_|j&l|dd|jf}t)j*|r+t)j,||fd}|j/}n,tj,||f}nt)j*|rt)j0|sV|j2d k(rt)j4|}t)j4|jd |jdf}n1t)j6|jd |jdf}t)j,||fd}nHtj8|jd |jdf}tj,||f}~t;rt=|d fi|}nt?t?|  }tA|dr6tC|j#|jDjF} | |_$nd} tK|j$D]m\} } |jM| dztO|j$d|j| } |dd|j| f} tQd| 5| jR|ddd|jd| zf| fi|jTjRddd|j&| tO|j$dz ks|jd| z}tW|j#|ddd|f| |j&| }|jXdkDr |dddf}t)j*|rtjZ|d|dd|f<e||dd|f<p|Scc}w#1swYxYw)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)r[rNrrz invalid orderlil)rrrr,rjr^rrOzProcessing order )rrrChain)r/rmethod).r%rrrr isinstancetuplerparrayrfrgstr permutationsortedlistrer rr<rrrrtocsrrrr coo_matrixrrrrr@r"rrrrrrrr,r-rrd expand_dims)rIr.rr0rr Y_pred_chainrrarrr-messager/col_idx cv_results r1r,z_BaseChain.fits@,T1ad$O1)$*;*;< jj dkk5 )((4;;/DK ;; ((5#45DK  S ){{h&*66qwwqzB DKK Dqwwqz):$; ;_- -BGPQ BSTQE$"5"5"78T 77?Q ^,L{{1~ 1l"3EB  1l"34 [[^==#88u$ QA "||QWWQZ,DE !}}aggaj!''!*-EF IIq,/>E88QWWQZ$<=LIIq,/0E   +D%F:FM!Ej,ABM 4 (1##%!!h ".D %L$-d.>.>$? 2 Iy'''!m !1!12!24;;y3I2JK(G !T[[++,A$Wg6  !7 Y 6778$--11 ww"y3t7G7G3H13L'L''!*y0-'')!XgX+&ww'  >>A% )!Q$I;;u%(*y!(DE!W*%(1E!W*%; 2> ]Ul  s T )rurvrrrwrxr{s r1rvz_BaseChain.__sklearn_tags__ks;w')!)$*=*=*?!@!K!K!R!R r3r>)rrrrrrrRrrrUrrrrr,rOrvrrs@r1rrs y) *  ~ & y) * 4L  H: 6=Jz23'(; $D $ #&"(G &Prrh Ar3rceZdZUdZiej deeehdgiZe e d< dddddddd fd Z e d fd Z ed dZdZeddZdZfdZxZS)r&aA 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 ` example. Read more in the :ref:`User Guide `. .. versionadded:: 0.19 Parameters ---------- 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 `. verbose : bool, default=False If True, chain progress is output as each model is completed. .. versionadded:: 1.2 base_estimator : estimator, default="deprecated" Use `estimator` instead. .. deprecated:: 1.7 `base_estimator` is deprecated and will be removed in 1.9. Use `estimator` instead. 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>rOrdecision_functionpredict_log_probarRNrOFr)rrrrrrc>t|||||||||_y)Nr)rurUr) rIr-rrrrrrr}s r1rUzClassifierChain.__init__s3  %)  )r3rVc t||dt|||fi||jDcgc]}|jc}|_|Scc}w)ayFit 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 `. .. versionadded:: 1.3 Returns ------- self : object Class instance. r,)rrur,r<r)rIr.rr0r-r}s r1r,zClassifierChain.fit#sM8 *dE2  Aq'J'=A=M=MN ++N  OsA rc(|j|dS)a9Predict 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. rrrrs r1rzClassifierChain.predict_probaEs$$Qo$FFr3cJtj|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. )rplogrrs r1rz!ClassifierChain.predict_log_probaUsvvd((+,,r3rc(|j|dS)aEvaluate 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. rrrrs r1rz!ClassifierChain.decision_functionds$$Q6I$JJr3ct|jjj|j t jdd}|Srrr,rrrr}rrrrrs r1rz$ClassifierChain.get_metadata_routinguQ dnn&=&=>BB))+(?..eE.JC  r3cvt|}d|_d|j_d|j_|S)NTF)rurvrryrzr[r{s r1rvz ClassifierChain.__sklearn_tags__s8w')).&(,% r3r>)rrrrrrRrrrrrrUr r,rrrrrrvrrs@r1r&r&qsN` $  + + $   V   $D ) #)*&+ <&o6 G7 G -&&9:K;K *r3r&cJeZdZdZedfdZdZfdZxZS)r)aA 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 `. .. versionadded:: 0.20 Parameters ---------- 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 `. verbose : bool, default=False If True, chain progress is output as each model is completed. .. versionadded:: 1.2 base_estimator : estimator, default="deprecated" Use `estimator` instead. .. deprecated:: 1.7 `base_estimator` is deprecated and will be removed in 1.9. Use `estimator` instead. 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(logreg, order=[0, 1]).fit(X, Y) >>> chain.predict(X) array([[0., 2.], [1., 1.], [2., 0.]]) FrVc *t|||fi||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. )rur,)rIr.rr0r}s r1r,zRegressorChain.fits4  Aq'J' r3ct|jjj|j t jdd}|Sr r rs r1rz#RegressorChain.get_metadata_routingrr3cht|}d|j_d|j_|Srt)rurvryrzr[r{s r1rvzRegressorChain.__sklearn_tags__,s1w')).&(,% r3) rrrrr r,rrvrrs@r1r)r)s5dL&+ 2*r3r)r>)NNT)@rrabcrrnumbersrnumpyrp scipy.sparserxrbaserrr r r r r model_selectionrutilsrrrutils._param_validationrrrutils._responserutils._user_interfacerutils.metadata_routingrrrrrutils.metaestimatorsrutils.multiclassrutils.parallelrr utils.validationr!r"r#r$r%__all__r2r9rLrNr(r'rrr&r)r3r1r%s'/66 26/:- HL  $p. pfdU>+@dUNpO-Bpf  l'l^_(/:_D ]']r3