o
    \i\8                  	   @   sD  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 d dlmZ e Zeejejd d\ZZ Z!Z"dZ#e!$ Z%de%e#d< dddddZ&e'e&j(e%)e*Z+de+e%dk< dd Z,ej-.de eddd dgej-.dddgdd  Z/d!d" Z0d#d$ Z1d%d& Z2ej-.de eddd dgej-.d'e%e+gd(d) Z3d*d+ Z4ej-.d,e5d-d.d/d0 Z6d1d2 Z7d3d4 Z8d5d6 Z9ej-.d7dd8gd9d: Z:d;d< Z;d=d> Z<d?d@ Z=dAdB Z>dCdD Z?ej-@dEej-.dFg dGdHdI ZAdS )J    )ceilN)assert_array_equal)	load_iris
make_blobs)StackingClassifier)NotFittedError)accuracy_score)train_test_split)KNeighborsClassifier)SelfTrainingClassifier)SVC)SimpleEstimator)DecisionTreeClassifier)random_state2   ABCz-1)r         r   c                  C   s\   t t ddd} tjtdd | tt W d    n1 s w   Y  | jdks,J d S )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_st r%   /var/www/www-root/data/www/176.119.141.140/sports-predictor/venv/lib/python3.10/site-packages/sklearn/semi_supervised/tests/test_self_training.pytest_warns_k_best&   s
   r'   	estimatorscaleTgammaprobabilityr   selection_crit	thresholdr   c                 C   s<  d}d}t | |||d}|tt |t}|t}t | |||d}|tt |t}|t}	tt	
tj|| t||	 |j|jksLJ tdk}
t|jdk|
 tt|
 |j|
  t	|j|j  krr|ksuJ  J t	|j|j  kr|ksJ  J |jj|jjksJ |jj|jjksJ d S )Ng      ?
   )max_iterr.   r   )r0   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.   r0   r$   predproba	st_stringpred_stringproba_stringlabeledr%   r%   r&   test_classification.   s0   	




&&rD   c                  C   s   t tddddd d} tt}d|dd < tjd }t|d d }| t| | j	|ks0J t
| jdkdks<J td|D ]}t
| j|kdksOJ qAt
| j|k|d d ks`J | jdksgJ d S )	Nr   n_neighborsr   r/   )r   r   r0   r   r   r   )r   r
   r5   copyy_trainr=   r   r   r    r<   sumr9   ranger"   )r$   y_train_only_one_label	n_samplesn_expected_iterir%   r%   r&   test_k_best\   s"   

 rO   c                  C   s   t ddd} | ttd  ttd   t| }|tt | t|t}}t	
||r1J t| tt}t|tt}||ksGJ d S )Nr)   Tr+   r,   )r   r   r    n_labeled_samplesrH   r   r!   r1   r2   r5   array_equalr   y_test)r(   r$   pred1pred2score_supervisedscore_self_trainingr%   r%   r&   test_sanity_classifications   s   rX   c                  C   s<   t t dd d} | tt | jdk sJ | jdksJ d S )Ng?)r.   r0   r/   r   )r   r
   r   r    r!   r<   r"   r#   r%   r%   r&   test_none_iter   s   rY   yc                 C   s\   t | dd}|t| | td t |d t }t|t|t |jdks,J d S )Nr   r0   r0   )r   r   r    rQ   r   r1   r2   r"   )r(   rZ   clf1clf2r%   r%   r&   test_zero_iterations   s
   r^   c                  C   sX   t  } | tt t| }tjtdd |t W d    d S 1 s%w   Y  d S )Nz6This SelfTrainingClassifier instance is not fitted yetr   )	r
   r   r    rH   r   r   raisesr   r1   knnr$   r%   r%   r&   test_prefitted_throws_error   s   "rb   r0   r      c                 C   sd   t t | d}|tt t|j|jdk }|tksJ t	|j|j
  kr-| ks0J  J d S )Nr[   r   )r   r
   r   r    r!   lenr9   rQ   r5   r;   r<   )r0   r$   amount_iter_0r%   r%   r&   test_labeled_iter   s
   *rf   c                  C   s   t  } | tt t| }tjtdd |tt W d    n1 s%w   Y  t| 	t
|	t
 t|jdks?J |jdksFJ d S )Nzy contains no unlabeled samplesr   r   r   )r
   r   r    rH   r   r   r   r   r   r1   r2   r5   allr9   r"   r`   r%   r%   r&   test_no_unlabeled   s   rh   c                  C   s\   t ddd} t| }dgdgdgdgg}g d}||| |jdks%J |jdks,J d S )	Nr)   TrP   r   r   g      ?)r   r   r   r   	no_change)r   r   r   r<   r"   )svcr$   X_train_easyy_train_easyr%   r%   r&   test_early_stopping   s   rm   c                  C   sp   t t } tdddd\}}g d}t||}tjtdd | || W d    d S 1 s1w   Y  d S )N   r   g?)rL   r   cluster_std)onetwothreedtyper   )	r   r
   r   r5   taker   r_   
ValueErrorr   )clfXrZ   labels_multiclass	y_stringsr%   r%   r&   test_strings_dtype   s   
"rz   verboseFc                 C   sJ   t t |d}|tt |  }|rd|jv sJ d S d|jvs#J d S )N)r{   	iteration)r   r
   r   r    r!   
readouterrout)capsysr{   rv   capturedr%   r%   r&   test_verbose   s   r   c                 C   s   t tdddddd d}tt}d|dd < tjd }t|d d }|t| | 	 }d	}t
d|D ]}||d|jv sBJ q5|||d d |jv sRJ d S )
Nr   rE   r   r/   T)r   r   r{   r0   r   r   z)End of iteration {}, added {} new labels.)r   r
   r5   rG   rH   r=   r   r   r    r}   rJ   formatr~   )r   r$   rK   rL   rM   r   msgrN   r%   r%   r&   test_verbose_k_best   s"   

"r   c            	      C   s   t dddd} t| dddd}td	k}|tt | |jd	k@ }| t| t|  | t|  }tj|dd
}t|  t	|dd   }tt
|  }| D ]}||v s]J qUd S )Nr)   Tr   r*   r   r   r/   )r   r0   r   r   )axisi)r   r   r!   r   r    r:   r3   r5   r;   argsortwheretolist)	rj   r$   	has_label	got_labelr>   	max_probamost_confident_svcadded_by_strowr%   r%   r&   test_k_best_selects_best  s   r   c                  C   s   t dtddfdtddfgtdddd} t| dsJ t| d}|tt |t t dtd	dfdtd	dfgtd	ddd} t| drJJ t| d}t	
t |tt W d    d S 1 sfw   Y  d S )
Nsvc_1T)r,   svc_2r   )
estimatorsfinal_estimatorcvr3   r(   F)r   r   hasattrr   r   r    r!   r3   r2   r   r_   AttributeError)r(   rv   r%   r%   r&   test_estimator_meta_estimator  s.   	

	
"r   c                  C   s   t ddd} t| }tjtdd |tt W d   n1 s"w   Y  tt d}d}d	}tjt|d}|tt	t W d   n1 sLw   Y  t
|jjtsZJ |t|jjv sdJ dS )
a>  Check that we raise the proper AttributeErrors when the `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   r_   r   r   r    r!   r   decision_function
isinstancevalue	__cause__str)r(   self_training	outer_msg	inner_msg	exec_infor%   r%   r&   ,test_self_training_estimator_attribute_error@  s   r   c                  C   s   d} t jt| d tt dtt W d    n1 sw   Y  d}t jt	|d t tt W d    n1 s>w   Y  d}t jt	|d tt t dtt W d    d S 1 sdw   Y  d S )Nz?`base_estimator` has been deprecated in 1.6 and will be removedr   )base_estimatorz4You must pass an estimator to SelfTrainingClassifierz;You must pass only one estimator to SelfTrainingClassifier.)r   r(   )
r   r   FutureWarningr   r   r   r    r!   r_   ru   )warn_msg	error_msgr%   r%   r&   'test_deprecation_warning_base_estimator^  s"   "r   z2ignore:y contains no unlabeled samples:UserWarningmethod)r   predict_log_probar3   r1   c                 C   s   t t d}tjtdd |jdgdggddgdgdd W d   n1 s(w   Y  t t d}tjtdd d|_t|| dggdgdd W d   dS 1 sVw   Y  dS )	zTest that the right error message is raised when metadata is passed while
    not supported when `enable_metadata_routing=False`.r   z1is only supported if enable_metadata_routing=Truer   r   a)sample_weightpropNT)r   r   r   r_   ru   r   fitted_params_getattr)r   estr%   r%   r&   *test_routing_passed_metadata_not_supportedt  s   ""r   )Bmathr   numpyr5   r   numpy.testingr   sklearn.datasetsr   r   sklearn.ensembler   sklearn.exceptionsr   sklearn.metricsr   sklearn.model_selectionr	   sklearn.neighborsr
   sklearn.semi_supervisedr   sklearn.svmr   sklearn.tests.test_pipeliner   sklearn.treer   irisdatatargetr    r2   rH   rS   rQ   rG   r!   r7   r6   r8   astypeobjectr4   r'   markparametrizerD   rO   rX   rY   r^   rb   rJ   rf   rh   rm   rz   r   r   r   r   r   r   filterwarningsr   r%   r%   r%   r&   <module>   sx    
)


$
