
    h$,f0              	          d dl mZ d dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ  e       Z eej6                  ej8                  d       \  ZZZZ dZ!ejE                         Z#de#e!d dddddZ$  ejJ                  e$jL                        e#      jO                  e(      Z)de)e#dk(  <   d Z*ejV                  jY                  d e        eddd       g      ejV                  jY                  dddg      d               Z-d Z.d Z/d  Z0ejV                  jY                  d e        eddd       g      ejV                  jY                  d!e#e)g      d"               Z1d# Z2ejV                  jY                  d$ e3d%d&            d'        Z4d( Z5d) Z6d* Z7ejV                  jY                  d+dd,g      d-        Z8d. Z9d/ Z:d0 Z;d1 Z<y)2    )ceilN)assert_array_equal)	load_iris
make_blobs)StackingClassifier)NotFittedError)accuracy_score)train_test_split)KNeighborsClassifier)SelfTrainingClassifier)SVC)DecisionTreeClassifier)random_state2   ABCz-1)r         r   c                      t        t               dd      } t        j                  t        d      5  | j                  t        t               d d d        | j                  dk(  sJ y # 1 sw Y   xY w)Nk_besti  )	criterionr   zk_best is larger thanmatchall_labeled)	r   r   pytestwarnsUserWarningfitX_trainy_train_missing_labelstermination_condition_sts    Plib/python3.12/site-packages/sklearn/semi_supervised/tests/test_self_training.pytest_warns_k_bestr'   %   s^    	 4 6(SW	XB	k)@	A 0
w./0 $$5550 0s   A''A0base_estimatorscaleTgammaprobabilityr   selection_crit	thresholdr   c                 (   d}d}t        | |||      }|j                  t        t               |j	                  t
              }|j                  t
              }t        | |||      }|j                  t        t               |j	                  t
              }|j                  t
              }	t         t        j                  t        j                        |      |       t        ||	       |j                  |j                  k(  sJ t        dk7  }
t        |j                  dk(  |
       t        t        |
   |j                  |
          t        j                   |j                        |j"                  cxk  r|k  sJ  J t        j                   |j                        |j"                  cxk  r|k  sJ  J |j                  j$                  |j                  j$                  k(  sJ |j                  j$                  |j                  j$                  k(  sJ y )Ng      ?
   )max_iterr.   r   )r1   r   r.   r   r   )r   r    r!   r"   predictX_testpredict_probay_train_missing_stringsr   np	vectorizemappinggetr#   labeled_iter_transduction_maxn_iter_shape)r(   r-   r.   r1   r%   predproba	st_stringpred_stringproba_stringlabeleds              r&   test_classificationrE   -   s    IH	Y.
B FF7*+::fDV$E&^yI MM'23##F+K**62L0r||GKK06Dul+$$	(H(HHHH$*Gr''1,g6-g68H8H8QR 66"""#rzz=X=====66)))*i.?.?K8KKKKK !!R%5%5%;%;;;;""((I,C,C,I,IIII    c                  B   t        t        d      ddd       } t        j                  t              }d|dd  t        j
                  d   }t        |dz
  dz        }| j                  t        |       | j                  |k(  sJ t        j                  | j                  dk(        dk(  sJ t        d|      D ])  }t        j                  | j                  |k(        dk(  r)J  t        j                  | j                  |k(        |dz
  dz  k(  sJ | j                  dk(  sJ y )	Nr   n_neighborsr   r0   )r   r   r1   r   r   r   )r   r   r6   copyy_trainr>   r   r    r!   r=   sumr:   ranger#   )r%   y_train_only_one_label	n_samplesn_expected_iteris        r&   test_k_bestrR   [   s   	+	
B  WWW-!#12a IIMR/0OFF7*+::((( 66"""a'(A---1o& 3vvb&&!+,222366"""o569q=B:NNNN$$555rF   c                     t        dd      } | j                  t        t        d  t        t        d         t        |       }|j                  t        t               | j                  t              |j                  t              }}t        j                  ||      rJ t        | j                  t              t              }t        |j                  t              t              }||kD  sJ y )Nr)   Tr+   r,   )r   r    r!   n_labeled_samplesrK   r   r"   r2   r3   r6   array_equalr	   y_test)r(   r%   pred1pred2score_supervisedscore_self_trainings         r&   test_sanity_classificationr\   r   s    wD9Nw012G<M<N4OP		/BFF7*+!))&12::f3E5E~~eU+++%n&<&<V&DfM(F);VD!1111rF   c                      t        t               dd       } | j                  t        t               | j
                  dk  sJ | j                  dk(  sJ y )Ng?)r.   r1   r0   r   )r   r   r    r!   r"   r=   r#   r$   s    r&   test_none_iterr^      sJ     
  4 6$QU	VBFF7*+::??$$555rF   yc                    t        | d      }|j                  t        |       | j                  t        d t         |d t               }t	        |j                  t              |j                  t                     |j                  dk(  sJ y )Nr   r1   r1   )r   r    r!   rU   r   r2   r3   r#   )r(   r_   clf1clf2s       r&   test_zero_iterationsrd      sr     ".1=DHHWag&8'891=O>O;PQDt||F+T\\&-AB&&*444rF   c                      t               } | j                  t        t               t	        |       }t        j                  t        d      5  |j                  t               d d d        y # 1 sw Y   y xY w)Nz6This SelfTrainingClassifier instance is not fitted yetr   )	r   r    r!   rK   r   r   raisesr   r2   knnr%   s     r&   test_prefitted_throws_errorri      sY     
 CGGGW		$B	F
  	

7	  s   A**A3r1   r      c                 0   t        t               |       }|j                  t        t               t        |j                  |j                  dk(           }|t        k(  sJ t        j                  |j                        |j                  cxk  r| k  sJ  J y )Nra   r   )r   r   r    r!   r"   lenr:   rU   r6   r<   r=   )r1   r%   amount_iter_0s      r&   test_labeled_iterrn      s     
  4 6	JBFF7*+(()9)9Q)>?@M---- 66"""#rzz=X=====rF   c                     t               } | j                  t        t               t	        |       }t        j                  t        d      5  |j                  t        t               d d d        t        | j                  t              |j                  t                     t        j                  |j                  dk(        sJ |j                  dk(  sJ y # 1 sw Y   qxY w)Nzy contains no unlabeled samplesr   r   r   )r   r    r!   rK   r   r   r   r   r   r2   r3   r6   allr:   r#   rg   s     r&   test_no_unlabeledrq      s     
 CGGGW		$B	k)J	K !
w !s{{6*BJJv,>? 66"""a'((($$555! !s   CCc                      t        dd      } t        |       }dgdgdgdgg}g d}|j                  ||       |j                  dk(  sJ |j                  dk(  sJ y )	Nr)   TrT   r   r   g      ?)r   r   r   r   	no_change)r   r   r    r=   r#   )svcr%   X_train_easyy_train_easys       r&   test_early_stoppingrw      sg    
G
.C		$BC!qcC5)L!L FF<&::??$$333rF   c                     t        t                     } t        ddd      \  }}g d}t        j                  ||      }t        j                  t        d      5  | j                  ||       d d d        y # 1 sw Y   y xY w)N   r   g?)rO   r   cluster_std)onetwothreedtyper   )	r   r   r   r6   taker   rf   
ValueErrorr    )clfXr_   labels_multiclass	y_stringss        r&   test_strings_dtyper      sj    
 !5!7
8CDDAq/)1-I	z	1 9  s   A66A?verboseFc                     t        t               |      }|j                  t        t               | j                         }|rd|j                  v sJ y d|j                  vsJ y )N)r   	iteration)r   r   r    r!   r"   
readouterrout)capsysr   r   captureds       r&   test_verboser      sV    
 !5!7
ICGGG+,  "Hhll***(,,...rF   c                    t        t        d      dddd       }t        j                  t              }d|dd  t        j
                  d   }t        |dz
  dz        }|j                  t        |       | j                         }d	}t        d|      D ]"  }|j                  |d      |j                  v r"J  |j                  ||dz
  dz        |j                  v sJ y )
Nr   rH   r   r0   T)r   r   r   r1   r   r   z)End of iteration {}, added {} new labels.)r   r   r6   rJ   rK   r>   r   r    r!   r   rM   formatr   )r   r%   rN   rO   rP   r   msgrQ   s           r&   test_verbose_k_bestr      s    	+
B  WWW-!#12a IIMR/0OFF7*+  "H
5C1o& 1zz!R HLL0001 ::o	A';<LLLrF   c                     t        ddd      } t        | ddd      }t        d	k7  }|j                  t        t               | |j
                  d	k7  z  }| j                  t        |   t        |          | j                  t        |          }t        j                  |d
      }t        |    t        j                  |      dd     }t        t        j                  |         j                         }|j                         D ]  }||v rJ  y )Nr)   Tr   r*   r   r   r0   )r   r1   r   r   )axisi)r   r   r"   r    r!   r;   r4   r6   r<   argsortwheretolist)	rt   r%   	has_label	got_labelr?   	max_probamost_confident_svcadded_by_strows	            r&   test_k_best_selects_bestr     s    
GA
>C	x!B	OB&",IFF7*+
b.."45IGGGI 6y ABWiZ01Dt!$I ),RZZ	-B34-HI"((9-.557K!((* "k!!!"rF   c                  "   t        dt        d      fdt        d      fgt        d      d      } t        | d      sJ t        |       }|j	                  t
        t               |j                  t               t        dt        d	      fdt        d	      fgt        d	      d      } t        | d      rJ t        |       }t        j                  t              5  |j	                  t
        t               d d d        y # 1 sw Y   y xY w)
Nsvc_1T)r,   svc_2r   )
estimatorsfinal_estimatorcvr4   r(   F)r   r   hasattrr   r    r!   r"   r4   r3   r   rf   AttributeError)r(   r   s     r&   "test_base_estimator_meta_estimatorr     s     (cd+,cd+,
 -N >?333
 
?CGGG+,f'ce,-ce,-
 .N ~777
 
?C	~	& 1/01 1 1s   !DDc                  D   t        dd      } t        |       }t        j                  t        d      5  |j                  t        t               ddd       t        t                     }d}d	}t        j                  t        |      5 }|j                  t        t              j                  t               ddd       t        j                  j                  t              sJ |t        |j                  j                        v sJ y# 1 sw Y   xY w# 1 sw Y   _xY w)
aC  Check that we raise the proper AttributeErrors when the `base_estimator`
    does not implement the `predict_proba` method, which is called from within
    `fit`, or `decision_function`, which is decorated with `available_if`.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/28108
    Fr)   )r,   r+   z has no attribute 'predict_proba'r   Nr   zBThis 'SelfTrainingClassifier' has no attribute 'decision_function'zD'DecisionTreeClassifier' object has no attribute 'decision_function')r   r   r   rf   r   r    r!   r"   r   decision_function
isinstancevalue	__cause__str)r(   self_training	outer_msg	inner_msg	exec_infos        r&   ,test_self_training_estimator_attribute_errorr   ?  s     U':N*>:M	~-O	P ;'#9:;
 +:P:RSMTIVI	~Y	7 V9'#9:LLWUVioo//@@@IOO556666; ;V Vs   D

.D
DD)=mathr   numpyr6   r   numpy.testingr   sklearn.datasetsr   r   sklearn.ensembler   sklearn.exceptionsr   sklearn.metricsr	   sklearn.model_selectionr
   sklearn.neighborsr   sklearn.semi_supervisedr   sklearn.svmr   sklearn.treer   irisdatatargetr!   r3   rK   rW   rU   rJ   r"   r8   r7   r9   astypeobjectr5   r'   markparametrizerE   rR   r\   r^   rd   ri   rM   rn   rq   rw   r   r   r   r   r   r    rF   r&   <module>r      s      , 2 / - * 4 2 :  / {#3IIt{{$  &    -/ () *cct
,3,",,w{{34JKRR
  9; ."4 56 SwDqQR )K+BC&J D	
&JR6.26 SwDqQR 57NOP5 Q	
5 
 U1a[1
> 2
>6	4 T5M2	/ 3	/M2"(!1H7rF   