
    -iU                         S SK r S SKJrJr  S SK Jr  S SKrSSKJrJ	r	J
r
JrJr  SSKJrJrJr  SSKJrJrJrJr  SSKJrJrJrJrJr  SS	KJr  SS
KJrJ r J!r!  S/r" " S S\	\
\5      r#g)    N)IntegralReal)warn   )BaseEstimatorClassifierMixinMetaEstimatorMixin_fit_contextclone)Bunchget_tags	safe_mask)
HasMethodsHiddenInterval
StrOptions)MetadataRouterMethodMapping_raise_for_params_routing_enabledprocess_routing)available_if)_estimator_hascheck_is_fittedvalidate_dataSelfTrainingClassifierc                     ^  \ rS rSr% SrS\" S/5      /\" S/5      \" \" S15      5      /\" \	SSSS	9/\" S
S15      /\" \
SSSS	9/\" \
SSSS	9S/S/S.r\\S'          S#S jrS r\" SS9S 5       r\" \" S5      5      S 5       r\" \" S5      5      S 5       r\" \" S5      5      S 5       r\" \" S5      5      S 5       r\" \" S5      5      S 5       rS  rU 4S! jrS"rU =r$ )$r       a  Self-training classifier.

This :term:`metaestimator` allows a given supervised classifier to function as a
semi-supervised classifier, allowing it to learn from unlabeled data. It
does this by iteratively predicting pseudo-labels for the unlabeled data
and adding them to the training set.

The classifier will continue iterating until either max_iter is reached, or
no pseudo-labels were added to the training set in the previous iteration.

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

Parameters
----------
estimator : estimator object
    An estimator object implementing `fit` and `predict_proba`.
    Invoking the `fit` method will fit a clone of the passed estimator,
    which will be stored in the `estimator_` attribute.

    .. versionadded:: 1.6
        `estimator` was added to replace `base_estimator`.

base_estimator : estimator object
    An estimator object implementing `fit` and `predict_proba`.
    Invoking the `fit` method will fit a clone of the passed estimator,
    which will be stored in the `estimator_` attribute.

    .. deprecated:: 1.6
        `base_estimator` was deprecated in 1.6 and will be removed in 1.8.
        Use `estimator` instead.

threshold : float, default=0.75
    The decision threshold for use with `criterion='threshold'`.
    Should be in [0, 1). When using the `'threshold'` criterion, a
    :ref:`well calibrated classifier <calibration>` should be used.

criterion : {'threshold', 'k_best'}, default='threshold'
    The selection criterion used to select which labels to add to the
    training set. If `'threshold'`, pseudo-labels with prediction
    probabilities above `threshold` are added to the dataset. If `'k_best'`,
    the `k_best` pseudo-labels with highest prediction probabilities are
    added to the dataset. When using the 'threshold' criterion, a
    :ref:`well calibrated classifier <calibration>` should be used.

k_best : int, default=10
    The amount of samples to add in each iteration. Only used when
    `criterion='k_best'`.

max_iter : int or None, default=10
    Maximum number of iterations allowed. Should be greater than or equal
    to 0. If it is `None`, the classifier will continue to predict labels
    until no new pseudo-labels are added, or all unlabeled samples have
    been labeled.

verbose : bool, default=False
    Enable verbose output.

Attributes
----------
estimator_ : estimator object
    The fitted estimator.

classes_ : ndarray or list of ndarray of shape (n_classes,)
    Class labels for each output. (Taken from the trained
    `estimator_`).

transduction_ : ndarray of shape (n_samples,)
    The labels used for the final fit of the classifier, including
    pseudo-labels added during fit.

labeled_iter_ : ndarray of shape (n_samples,)
    The iteration in which each sample was labeled. When a sample has
    iteration 0, the sample was already labeled in the original dataset.
    When a sample has iteration -1, the sample was not labeled in any
    iteration.

n_features_in_ : int
    Number of features seen during :term:`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

n_iter_ : int
    The number of rounds of self-training, that is the number of times the
    base estimator is fitted on relabeled variants of the training set.

termination_condition_ : {'max_iter', 'no_change', 'all_labeled'}
    The reason that fitting was stopped.

    - `'max_iter'`: `n_iter_` reached `max_iter`.
    - `'no_change'`: no new labels were predicted.
    - `'all_labeled'`: all unlabeled samples were labeled before `max_iter`
      was reached.

See Also
--------
LabelPropagation : Label propagation classifier.
LabelSpreading : Label spreading model for semi-supervised learning.

References
----------
:doi:`David Yarowsky. 1995. Unsupervised word sense disambiguation rivaling
supervised methods. In Proceedings of the 33rd annual meeting on
Association for Computational Linguistics (ACL '95). Association for
Computational Linguistics, Stroudsburg, PA, USA, 189-196.
<10.3115/981658.981684>`

Examples
--------
>>> import numpy as np
>>> from sklearn import datasets
>>> from sklearn.semi_supervised import SelfTrainingClassifier
>>> from sklearn.svm import SVC
>>> rng = np.random.RandomState(42)
>>> iris = datasets.load_iris()
>>> random_unlabeled_points = rng.rand(iris.target.shape[0]) < 0.3
>>> iris.target[random_unlabeled_points] = -1
>>> svc = SVC(probability=True, gamma="auto")
>>> self_training_model = SelfTrainingClassifier(svc)
>>> self_training_model.fit(iris.data, iris.target)
SelfTrainingClassifier(...)
Nfit
deprecatedg        g      ?left)closed	thresholdk_best   r   verbose)	estimatorbase_estimatorr#   	criterionr$   max_iterr&   _parameter_constraintsFc                 X    Xl         X0l        X@l        XPl        X`l        Xpl        X l        g N)r'   r#   r)   r$   r*   r&   r(   )selfr'   r(   r#   r)   r$   r*   r&   s           Y/var/www/html/venv/lib/python3.13/site-packages/sklearn/semi_supervised/_self_training.py__init__SelfTrainingClassifier.__init__   s*     #""  -    c                 X   U R                   c7  U R                  S:w  a'  [        U R                  5      n[        S[        5        U$ U R                   c  U R                  S:X  a  [        S5      eU R                   b  U R                  S:w  a  [        S5      e[        U R                   5      nU$ )zcGet the estimator.

Returns
-------
estimator_ : estimator object
    The cloned estimator object.
r    zg`base_estimator` has been deprecated in 1.6 and will be removed in 1.8. Please use `estimator` instead.zFYou must pass an estimator to SelfTrainingClassifier. Use `estimator`.zLYou must pass only one estimator to SelfTrainingClassifier. Use `estimator`.)r'   r(   r   r   FutureWarning
ValueError)r.   
estimator_s     r/   _get_estimator%SelfTrainingClassifier._get_estimator   s     >>!d&9&9\&It223J? &  ^^#(;(;|(KX  ^^'D,?,?<,O$ 
 t~~.Jr2   )prefer_skip_nested_validationc                 *	   [        X0S5        U R                  5       U l        [        U UU/ SQSS9u  pUR                  R
                  S;   a  [        S5      eUS:g  n[        R                  " U5      (       a  [        R                  " S[        5        U R                  S	:X  aO  U R                  UR                  S
   [        R                  " U5      -
  :  a  [        R                  " S[        5        [!        5       (       a  [#        U S40 UD6nO[%        [%        0 S9S9n[        R&                  " U5      U l        [        R*                  " US5      U l        S
U R,                  U'   S
U l        [        R                  " U5      (       Gdb  U R0                  b  U R.                  U R0                  :  Ga:  U =R.                  S-  sl        U R                  R2                  " U[5        X5         U R(                  U   40 UR6                  R2                  D6  U R                  R9                  U[5        X) 5         5      nU R                  R:                  [        R<                  " USS9   n[        R>                  " USS9nU R                  S:X  a  XR@                  :  n	Oj[C        U R                  UR                  S
   5      n
XR                  S
   :X  a  [        RD                  " U[F        S9n	O[        RH                  " U* U
5      SU
 n	[        RJ                  " U) 5      S
   U	   nXy   U R(                  U'   SXK'   U R.                  U R,                  U'   UR                  S
   S
:X  a  SU l&        OU RN                  (       a)  [Q        SU R.                   SUR                  S
    S35        [        R                  " U5      (       d-  U R0                  c  GM  U R.                  U R0                  :  a  GM:  U R.                  U R0                  :X  a  SU l&        [        R                  " U5      (       a  SU l&        U R                  R2                  " U[5        X5         U R(                  U   40 UR6                  R2                  D6  U R                  R:                  U l        U $ )a  
Fit self-training classifier using `X`, `y` as training data.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Array representing the data.

y : {array-like, sparse matrix} of shape (n_samples,)
    Array representing the labels. Unlabeled samples should have the
    label -1.

**params : dict
    Parameters to pass to the underlying estimators.

    .. versionadded:: 1.6
        Only available if `enable_metadata_routing=True`,
        which can be set by using
        ``sklearn.set_config(enable_metadata_routing=True)``.
        See :ref:`Metadata Routing User Guide <metadata_routing>` for
        more details.

Returns
-------
self : object
    Fitted estimator.
r   )csrcsclildokF)accept_sparseensure_all_finite)USz~y has dtype string. If you wish to predict on string targets, use dtype object, and use -1 as the label for unlabeled samples.zy contains no unlabeled samplesr$   r   zsk_best is larger than the amount of unlabeled samples. All unlabeled samples will be labeled in the first iteration)r   r'   Nr%   )axisr#   )dtypeT	no_changezEnd of iteration z, added z new labels.r*   all_labeled))r   r7   r6   r   rF   kindr5   npallwarningsr   UserWarningr)   r$   shapesumr   r   r   copytransduction_	full_likelabeled_iter_n_iter_r*   r   r   r'   predict_probaclasses_argmaxmaxr#   min	ones_likeboolargpartitionnonzerotermination_condition_r&   print)r.   Xyparams	has_labelrouted_paramsprobpred	max_probaselectedn_to_selectselected_fulls               r/   r   SelfTrainingClassifier.fit   s   @ 	&.--/ 6#
 77<<:%7  G	66)MM;[I>>X%KK!''!*rvvi'888MM*  +D%B6BM!EbM:MWWQZ\\!R0()9%&&##MM!T\\DMM%ALLALOO)A)*""9-  ))-- ??009Q
3K1LMD??++BIId,CDDt!,I ~~,$~~5!$++yq/AB//!"44!||ITBH  "	z;GUH JJ	z215h?M 15D}-'+I$04D}-""1%*.9+||'~ 6+11!45\CQ &&##MM!T\\DMM%AX <<4==(*4D'66)*7D'i%&y)	
 %%))	

 00r2   predictc                    [        U 5        [        X S5        [        5       (       a  [        U S40 UD6nO[	        [	        0 S9S9n[        U USSSS9nU R                  R                  " U40 UR                  R                  D6$ )ae  Predict the classes of `X`.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Array representing the data.

**params : dict of str -> object
    Parameters to pass to the underlying estimator's ``predict`` method.

    .. versionadded:: 1.6
        Only available if `enable_metadata_routing=True`,
        which can be set by using
        ``sklearn.set_config(enable_metadata_routing=True)``.
        See :ref:`Metadata Routing User Guide <metadata_routing>` for
        more details.

Returns
-------
y : ndarray of shape (n_samples,)
    Array with predicted labels.
rl   )rl   rD   TFr?   r@   reset)	r   r   r   r   r   r   r6   rl   r'   r.   r`   rb   rd   s       r/   rl   SelfTrainingClassifier.predictr  s    0 	&	2+D)FvFM!E",=>M#
 &&qLM,C,C,K,KLLr2   rU   c                    [        U 5        [        X S5        [        5       (       a  [        U S40 UD6nO[	        [	        0 S9S9n[        U USSSS9nU R                  R                  " U40 UR                  R                  D6$ )a  Predict probability for each possible outcome.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Array representing the data.

**params : dict of str -> object
    Parameters to pass to the underlying estimator's
    ``predict_proba`` method.

    .. versionadded:: 1.6
        Only available if `enable_metadata_routing=True`,
        which can be set by using
        ``sklearn.set_config(enable_metadata_routing=True)``.
        See :ref:`Metadata Routing User Guide <metadata_routing>` for
        more details.

Returns
-------
y : ndarray of shape (n_samples, n_features)
    Array with prediction probabilities.
rU   )rU   rD   TFrn   )	r   r   r   r   r   r   r6   rU   r'   rp   s       r/   rU   $SelfTrainingClassifier.predict_proba  s    2 	&8+D/LVLM!E,CDM#
 ,,QX-2I2I2W2WXXr2   decision_functionc                    [        U 5        [        X S5        [        5       (       a  [        U S40 UD6nO[	        [	        0 S9S9n[        U USSSS9nU R                  R                  " U40 UR                  R                  D6$ )a  Call decision function of the `estimator`.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Array representing the data.

**params : dict of str -> object
    Parameters to pass to the underlying estimator's
    ``decision_function`` method.

    .. versionadded:: 1.6
        Only available if `enable_metadata_routing=True`,
        which can be set by using
        ``sklearn.set_config(enable_metadata_routing=True)``.
        See :ref:`Metadata Routing User Guide <metadata_routing>` for
        more details.

Returns
-------
y : ndarray of shape (n_samples, n_features)
    Result of the decision function of the `estimator`.
rt   )rt   rD   TFrn   )	r   r   r   r   r   r   r6   rt   r'   rp   s       r/   rt   (SelfTrainingClassifier.decision_function      2 	&(;<+D2EPPM!EB,GHM#
 00
((::
 	
r2   predict_log_probac                    [        U 5        [        X S5        [        5       (       a  [        U S40 UD6nO[	        [	        0 S9S9n[        U USSSS9nU R                  R                  " U40 UR                  R                  D6$ )a  Predict log probability for each possible outcome.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Array representing the data.

**params : dict of str -> object
    Parameters to pass to the underlying estimator's
    ``predict_log_proba`` method.

    .. versionadded:: 1.6
        Only available if `enable_metadata_routing=True`,
        which can be set by using
        ``sklearn.set_config(enable_metadata_routing=True)``.
        See :ref:`Metadata Routing User Guide <metadata_routing>` for
        more details.

Returns
-------
y : ndarray of shape (n_samples, n_features)
    Array with log prediction probabilities.
rx   )rx   rD   TFrn   )	r   r   r   r   r   r   r6   rx   r'   rp   s       r/   rx   (SelfTrainingClassifier.predict_log_proba  rw   r2   scorec                    [        U 5        [        X0S5        [        5       (       a  [        U S40 UD6nO[	        [	        0 S9S9n[        U USSSS9nU R                  R                  " X40 UR                  R                  D6$ )a  Call score on the `estimator`.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Array representing the data.

y : array-like of shape (n_samples,)
    Array representing the labels.

**params : dict of str -> object
    Parameters to pass to the underlying estimator's ``score`` method.

    .. versionadded:: 1.6
        Only available if `enable_metadata_routing=True`,
        which can be set by using
        ``sklearn.set_config(enable_metadata_routing=True)``.
        See :ref:`Metadata Routing User Guide <metadata_routing>` for
        more details.

Returns
-------
score : float
    Result of calling score on the `estimator`.
r{   )r{   rD   TFrn   )	r   r   r   r   r   r   r6   r{   r'   )r.   r`   ra   rb   rd   s        r/   r{   SelfTrainingClassifier.score!  s    6 	&0+D'DVDM!EO<M#
 $$QK]-D-D-J-JKKr2   c                 H   [        U R                  R                  S9nUR                  U R                  [        5       R                  SSS9R                  SSS9R                  SSS9R                  SSS9R                  SSS9R                  SSS9R                  SSS9S	9  U$ )
a"  Get metadata routing of this object.

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

.. versionadded:: 1.6

Returns
-------
routing : MetadataRouter
    A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
    routing information.
)ownerr   )calleecallerr{   rl   rU   rt   rx   )r'   method_mapping)r   	__class____name__addr'   r   )r.   routers     r/   get_metadata_routing+SelfTrainingClassifier.get_metadata_routingN  s      dnn&=&=>

nnE%0GE2Ii8OOD/8KL/8KLGG4 	 	
 r2   c                    > [         TU ]  5       nU R                  b8  [        U R                  5      R                  R
                  UR                  l        U$ r-   )super__sklearn_tags__r'   r   
input_tagssparse)r.   tagsr   s     r/   r   'SelfTrainingClassifier.__sklearn_tags__l  s@    w')>>%%-dnn%=%H%H%O%ODOO"r2   )r(   rV   r)   r'   r6   r$   rS   r*   rT   r^   r#   rQ   r&   )Nr    g      ?r#   
   r   F)r   
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r+   dict__annotations__r0   r7   r
   r   r   r   rl   rU   rt   rx   r{   r   r   __static_attributes____classcell__)r   s   @r/   r   r       s   ~H Jw/0 w:|n-.
 tS#f=> +x!89:Haf=>h4?F;$D & #-(B &+E	EN .+,'M -'MR .12(Y 3(YT .!456*
 7*
X .!456*
 7*
X .)**L +*LX< r2   )$rL   numbersr   r   r   numpyrJ   baser   r   r	   r
   r   utilsr   r   r   utils._param_validationr   r   r   r   utils.metadata_routingr   r   r   r   r   utils.metaestimatorsr   utils.validationr   r   r   __all__r    r2   r/   <module>r      sY     "    / . N N  0 M M#
$Q	_.@- Q	r2   