o
    tBh?                    @   sL  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
Z
ddl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m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) 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-m0Z0 ddl1m2Z2 ddl1m3Z3 dd l1m4Z4 dd!l1m5Z5 dd"l6m7Z7 dd#l6m8Z8 dd$l6m9Z9 dd%l6m:Z: dd&l6m;Z; dd'l6m<Z< dd(l6m=Z= dd)l6m>Z> dd*l6m?Z? dd+l@mAZAmBZBmCZC dd,l@mDZDmEZE dd-lFmGZG dd.lHmIZI dd/lJmKZKmLZL dd0lMmNZN dd1lOmPZP dd2lQmRZR dd3lSmTZT dd4lUmVZV dd5lWmXZX dd6lYmZZZ dd7lYm[Z[ dd8l\m]Z] dd9l^m_Z_ dd:l1m`Z` dd;l1maZa dd<lbmcZc dd=lmdZd zee W n efy   dZeY nw G d>d? d?eZZgG d@dA dAegZhG dBdC dCeZZiG dDdE dEeiZjG dFdG dGZkeldHZmeemZneog dIZpeog dJZqeerdKZsdLdM ZtdNdO ZudPdQ ZvdRdS ZwdTdU ZxdVdW ZydXdY ZzdZd[ Z{e
j|}d\d]d^ Z~d_d` Zdadb Zdcdd Zdedf Zdgdh Zdidj Zdkdl Zdmdn Zdodp Zdqdr Zdsdt Zdudv Zdwdx Zdydz Zd{d| Zd}d~ Zdd Ze
j|}d\dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd deGZddĄ ZddƄ ZddȄ Zddʄ Zdd̄ Zdd΄ Ze
j|}d\ddЄ Zdd҄ ZddԄ ZG ddք deZZe
j|dejdgddل Ze
j|dejdgddۄ Zdd݄ Ze
j|}dޡe
j|dejddgdd Ze
j|}dޡe
j|dejddge
j|dddge
j|dddgdd Zdd Ze
j|ddeddddfdeedddddfdeedddddfgdd Zdd Zdd Zdd ZdS )zTest the validation module    N)partial)sleep)
coo_matrix
csr_matrix)FitFailedWarning)FailingClassifier)assert_almost_equal)assert_array_almost_equal)assert_array_equal)assert_allclose)CheckingClassifierMockDataFrame)_num_samples)cross_val_scoreShuffleSplit)cross_val_predict)cross_validate)permutation_test_score)KFold)StratifiedKFold)LeaveOneOut)LeaveOneGroupOut)LeavePGroupsOut)
GroupKFold)GroupShuffleSplit)learning_curve)validation_curve)_check_is_permutation)_fit_and_score)_score)make_regression)load_diabetes)	load_iris)load_digits)explained_variance_score)make_scorer)accuracy_score)confusion_matrix)precision_recall_fscore_support)precision_score)r2_score)mean_squared_error)check_scoring)RidgeLogisticRegressionSGDClassifier)PassiveAggressiveClassifierRidgeClassifier)RandomForestClassifier)KNeighborsClassifier)SVC	LinearSVC)KMeans)MLPRegressor)SimpleImputer)LabelEncoder)Pipeline)StringIO)BaseEstimator)clone)OneVsRestClassifier)shuffle)make_classification)make_multilabel_classification)OneTimeSplitter)GridSearchCVc                   @   s<   e Zd ZdZdd ZdddZdd Zdd	d
Zdd ZdS )MockImprovingEstimatorz+Dummy classifier to test the learning curvec                 C   s   || _ d| _d | _d S Nr   )n_max_train_sizestrain_sizesX_subset)selfrF    rJ   /var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/model_selection/tests/test_validation.py__init__Z   s   
zMockImprovingEstimator.__init__Nc                 C      || _ |jd | _| S rE   rH   shaperG   rI   rH   y_subsetrJ   rJ   rK   fit_      zMockImprovingEstimator.fitc                 C      t NNotImplementedErrorrI   XrJ   rJ   rK   predictd      zMockImprovingEstimator.predictc                 C   s.   |  |rdt| j| j  S t| j| j S )Ng       @)_is_training_datafloatrG   rF   rI   rY   YrJ   rJ   rK   scoreg   s   
zMockImprovingEstimator.scorec                 C   
   || j u S rU   rH   rX   rJ   rJ   rK   r\   n      
z(MockImprovingEstimator._is_training_datarU   NN	__name__
__module____qualname____doc__rL   rR   rZ   r`   r\   rJ   rJ   rJ   rK   rD   W   s    

rD   c                       s4   e Zd ZdZd	 fdd	Zdd Zd	ddZ  ZS )
!MockIncrementalImprovingEstimatorz*Dummy classifier that provides partial_fitNc                    s   t  | d | _|| _d S rU   )superrL   xexpected_fit_params)rI   rF   rm   	__class__rJ   rK   rL   u   s   
z*MockIncrementalImprovingEstimator.__init__c                 C   s
   | j |v S rU   rl   rX   rJ   rJ   rK   r\   z   rc   z3MockIncrementalImprovingEstimator._is_training_datac              	   K   s   |  j |jd 7  _ |d | _| jrOt| jt| }|r'tdt| d| D ]%\}}|| jv rNt|t|krNtd| dt| dt| dq+d S d S )Nr   zExpected fit parameter(s) z
 not seen.zFit parameter z has length z; expected .)	rG   rO   rl   rm   setAssertionErrorlistitemsr   )rI   rY   yparamsmissingkeyvaluerJ   rJ   rK   partial_fit}   s,   
z-MockIncrementalImprovingEstimator.partial_fitrU   )rf   rg   rh   ri   rL   r\   r{   __classcell__rJ   rJ   rn   rK   rj   r   s
    rj   c                   @   s<   e Zd ZdZdddZdd Zdd Zdd
dZdd Zd	S )MockEstimatorWithParameterz-Dummy classifier to test the validation curve      ?c                 C   s   d | _ || _d S rU   )rH   param)rI   r   rJ   rJ   rK   rL         
z#MockEstimatorWithParameter.__init__c                 C   rM   rE   rN   rP   rJ   rJ   rK   rR      rS   zMockEstimatorWithParameter.fitc                 C   rT   rU   rV   rX   rJ   rJ   rK   rZ      r[   z"MockEstimatorWithParameter.predictNc                 C   s   |  |r| jS d| j S )N   )r\   r   )rI   rY   rv   rJ   rJ   rK   r`      s   z MockEstimatorWithParameter.scorec                 C   ra   rU   rb   rX   rJ   rJ   rK   r\      rc   z,MockEstimatorWithParameter._is_training_data)r~   rd   re   rJ   rJ   rJ   rK   r}      s    

r}   c                       s(   e Zd ZdZ fddZdd Z  ZS )%MockEstimatorWithSingleFitCallAllowedz<Dummy classifier that disallows repeated calls of fit methodc                    s&   t | dr	J dd| _t ||S )Nfit_called_zfit is called the second timeT)hasattrr   rk   rR   rP   rn   rJ   rK   rR      s   z)MockEstimatorWithSingleFitCallAllowed.fitc                 C   rT   rU   rV   rX   rJ   rJ   rK   rZ      r[   z-MockEstimatorWithSingleFitCallAllowed.predict)rf   rg   rh   ri   rR   rZ   r|   rJ   rJ   rn   rK   r      s    r   c                   @   sZ   e Zd ZdZdddZ									dddZd	d
 Zdd ZdddZdddZ	dS )MockClassifierz-Dummy classifier to test the cross-validationr   Fc                 C   s   || _ || _d S rU   aallow_nd)rI   r   r   rJ   rJ   rK   rL      r   zMockClassifier.__init__Nc                 C   sL  || _ || _|	| _|
dur|
|  | jr|t|d}|jdkr(| js(td|durD|jd |jd ksDJ d	|jd |jd |durd|jd tt
tksdJ d	|jd tt
t|durd}|jd |jd ksJ |	|jd |jd |durd	}|jtjksJ |	|jd |jd
 tjd tjd
 | S )zThe dummy arguments are to test that this fit function can
        accept non-array arguments through cross-validation, such as:
            - int
            - str (this is actually array-like)
            - object
            - function
        N   zX cannot be dr   zKMockClassifier extra fit_param sample_weight.shape[0] is {0}, should be {1}zIMockClassifier extra fit_param class_prior.shape[0] is {0}, should be {1}zRMockClassifier extra fit_param sparse_sample_weight.shape[0] is {0}, should be {1}zUMockClassifier extra fit_param sparse_param.shape is ({0}, {1}), should be ({2}, {3})r   )	dummy_int	dummy_str	dummy_objr   reshapelenndim
ValueErrorrO   formatnpuniquerv   P_sparse)rI   rY   r_   sample_weightclass_priorsparse_sample_weightsparse_paramr   r   r   callbackfmtrJ   rJ   rK   rR      sJ   zMockClassifier.fitc                 C   s&   | j r|t|d}|d d df S )Nr   r   )r   r   r   rI   TrJ   rJ   rK   rZ      s   zMockClassifier.predictc                 C   s   |S rU   rJ   r   rJ   rJ   rK   predict_proba   r[   zMockClassifier.predict_probac                 C   s   ddt | j  S )N      ?r   )r   absr   r^   rJ   rJ   rK   r`      s   zMockClassifier.scorec                 C   s   | j | jdS )Nr   r   )rI   deeprJ   rJ   rK   
get_params  s   zMockClassifier.get_params)r   F)	NNNNNNNNNrd   )F)
rf   rg   rh   ri   rL   rR   rZ   r   r`   r   rJ   rJ   rJ   rK   r      s"    

?
r   )
      )
r   r   r   r   r   r   r   r      r   )
r   r   r   r   r   r   r   r   r   r      c                  C   s  t  } tddD ]M}|| _t| tt}t|| tt t	ttd d d g}t| t
|}t|| t
| t| t
t}t|| t
t t| t
|}t|| t
| qdd }t|d} t| t t dd}t|d	} t| tt dd}tt t| ttd
d W d    n1 sw   Y  td d d d tjf }t dd} t| |t}t dd} tt t| |tdd W d    d S 1 sw   Y  d S )Nir   r   c                 S   
   t | tS rU   
isinstancert   rp   rJ   rJ   rK   <lambda>(     
 z&test_cross_val_score.<locals>.<lambda>check_Xr   cvcheck_ysklearnscoringT)r   Fraiseerror_score)r   ranger   r   rY   y2r
   r`   r   column_stackX_sparser   tolistpytestraisesr   newaxis)clfr   scoresmultioutput_y
list_checkX_3drJ   rJ   rK   test_cross_val_score  s6   



"r   c                  C   s@   t dd\} }tdd}t|dddgid}t|| |d	d
 d S )NT
return_X_yauto)gammaCr   r   )
param_gridr   )n_jobs)r"   r4   rC   r   )rY   rv   r   gridrJ   rJ   rK   test_cross_validate_many_jobs<  s   
r   c                  C   s  t dd\} }t }d}tjt|d t|| |ttttfd W d    n1 s,w   Y  tjt|d t|| |ttfd W d    n1 sNw   Y  tjt|d d t|| |dd W d    n1 sow   Y  tjt|d d t|| |d	d W d    n1 sw   Y  tjt|d t|| |ttggd W d    n1 sw   Y  d
}tjtdd t|| |t	 d W d    n1 sw   Y  tjt|d t|| |dd W d    n1 sw   Y  tt
}dtj d}tjt|d t|| ||d W d    n	1 sw   Y  tjt|d t|| |d|id W d    n	1 sAw   Y  tjtdd tt | |dd W d    d S 1 scw   Y  d S )Nr   random_statez.*must be unique strings.*matchr   zEmpty list.*rJ   zDuplicate.*)f1_micror   zB.*scoring is invalid.*Refer to the scoring glossary for details:.*zAn empty dictr   [Scoring failed. The score on this train-test partition for these parameters will be set to z. Details: 
fooz#'mse' is not a valid scoring value.mse)r@   r   r   r   r   r   r%   r)   r&   dictr(   r   nanwarnsUserWarningr4   )rY   rv   	estimatorerror_message_regexpmulticlass_scorerwarning_messagerJ   rJ   rK   )test_cross_validate_invalid_scoring_paramF  sZ   $r   c                  C   sf   t dd\} }tdt fdt fg}t|| |dd}|d }t|ts&J tdd |D s1J d S )	NTr   imputer
classifier)return_estimatorr   c                 s   s    | ]}t |tV  qd S rU   )r   r:   ).0r   rJ   rJ   rK   	<genexpr>  s    z7test_cross_validate_nested_estimator.<locals>.<genexpr>)r"   r:   r8   r   r   r   rt   all)rY   rv   pipelineresults
estimatorsrJ   rJ   rK   $test_cross_validate_nested_estimator  s   r   c               	   C   s  t  } tddd\}}tdd}tddd\}}tddd}|||f|||ffD ]\}}}	t|	dd}
t|	d	d}g }g }g }g }g }| ||D ]I\}}t||| || }	|	|
|	|| ||  |	||	|| ||  |	|
|	|| ||  |	||	|| ||  |	|	 qIt
|}t
|}t
|}t
|}t
|}|||||f}t|	||| t|	||| q(d S )
N   r   	n_samplesr   r   linearkernelr   neg_mean_squared_errorr   r2)r   r    r-   r@   r4   r,   splitr=   rR   appendr   array"check_cross_validate_single_metric!check_cross_validate_multi_metric)r   X_regy_regregX_clfy_clfr   rY   rv   est
mse_scorer	r2_scorertrain_mse_scorestest_mse_scorestrain_r2_scorestest_r2_scoresfitted_estimatorstraintestr   rJ   rJ   rK   test_cross_validate  sD   





r  c                 C   s:  |\}}}}}dD ]k\}	}
|	r t | ||ddd}t|d | n	t | ||ddd}t|ts0J t||
ks8J t|d | |	rTt | ||dgdd}t|d	 |d n
t | ||dgdd}t|tseJ t||
ksmJ t|d
 | q	t | ||ddd}t|d D ]\}}t|j|| j t|j|| j qd S )N))Tr   )Fr   r   Tr   return_train_scoretrain_scoreF
test_scorer   train_r2test_r2)r   r   r   )	r   r	   r   r   r   	enumerater   coef_
intercept_)r   rY   rv   r   r   r   r  r  r  r  dict_lenmse_scores_dictr2_scores_dictkr   rJ   rJ   rK   r     sJ   


r   c              	   C   s  |\}}}}}dd }	dt tdd|	f}
h d}|ddh}dD ]}|
D ]}|rBt| |||d	d
}t|d | t|d | n	t| |||dd
}t|tsRJ t| |r[|n|ks`J t|d | t|d | t	|d t
jksyJ t	|d t
jksJ t	|d t
jksJ t	|d t
jksJ t
|d dksJ t
|d dk sJ t
|d dksJ t
|d dk sJ q&q"d S )Nc                 S   s"   |  |}t||t|| dS )Nr   r   )rZ   r*   r+   )r   rY   rv   y_predrJ   rJ   rK   custom_scorer  s   

z8check_cross_validate_multi_metric.<locals>.custom_scorerr  r   >   r  fit_time
score_timetest_neg_mean_squared_errorr  train_neg_mean_squared_error)TFTr  Fr  r  r  r  r   r   )r%   r*   unionr   r	   r   r   rr   keystyper   ndarrayr   )r   rY   rv   r   r   r   r  r  r  r  all_scoringkeys_sans_trainkeys_with_trainr  r   
cv_resultsrJ   rJ   rK   r     sb   	



r   c               	   C   s   t dddd\} }tdd}t tdt t g}d}|D ]@}tjt|d t	|| ||d	 W d    n1 s9w   Y  tjt|d t
|| ||d	 W d    n1 sXw   Y  qd S )
N   r   r   )r   	n_classesr   r   r   z*The 'groups' parameter should not be None.r   )r   rY   rv   r   )r@   r4   r   r   r   r   r   r   r   r   r   )rY   rv   r   	group_cvserror_messager   rJ   rJ   rK   #test_cross_val_score_predict_groups@  s"   
r(  z(ignore: Using or importing the ABCs fromc                        t t fg} zddlm}m} | ||f W n	 ty   Y nw | D ]'\  tt}} fdd}fdd}t||d}t	|||dd q"d S )	Nr   Series	DataFramec                    
   t |  S rU   r   rp   InputFeatureTyperJ   rK   r   d  r   z-test_cross_val_score_pandas.<locals>.<lambda>c                    r-  rU   r.  rp   
TargetTyperJ   rK   r   e  r   r   r   r   r   )
r   pandasr+  r,  r   ImportErrorrY   r   r   r   typesr+  r,  X_dfy_sercheck_dfcheck_seriesr   rJ   r0  r2  rK   test_cross_val_score_pandasV  s   
r=  c                  C   s   t dd} t }|j|j}}td}t| |||d}td}g }|||D ]%\}}tjt	|t
d}	tjt	|t
d}
d|	|< d|
|< |||f q't| |||d}t|| d S )Nr   r%  r   r   dtyper   )r4   r"   datatargetr   r   r   r   zerosr   boolr   r
   )svmirisrY   rv   kfoldscores_indicescv_masksr  r  
mask_train	mask_testscores_masksrJ   rJ   rK   test_cross_val_score_maskj  s   
rL  c                  C   s   t dd} t }|j|j}}t||j}t| ||}t dd} t| ||}t|| t dd d} t| ||}t|| t dd} t	
t t| || W d    n1 sYw   Y  t	
t t| | | W d    d S 1 sww   Y  d S )Nprecomputedr%  r   c                 S   s   t | |jS rU   )r   dotr   )rl   rv   rJ   rJ   rK   r     s    z2test_cross_val_score_precomputed.<locals>.<lambda>)r4   r"   r@  rA  r   rN  r   r   r	   r   r   r   r   )rD  rE  rY   rv   linear_kernelscore_precomputedscore_linearscore_callablerJ   rJ   rK    test_cross_val_score_precomputed}  s$   




"rS  c               	      s   t  } tjd }ttt}ttdgtdgtdgffdd}tt	d}d dt
  fdd	}t|t|d
| || |d}t| tt|d d S )Nr   r   )r   r   rO   r   *   42c                    s.   | j  ksJ | jksJ | jksJ d S rU   )r   r   r   )r   	DUMMY_INT	DUMMY_OBJ	DUMMY_STRrJ   rK   assert_fit_params  s   z:test_cross_val_score_fit_params.<locals>.assert_fit_paramsr   )r   r   r   r   r   r   r   r   
fit_params)r   rY   rO   r   r   r   rv   r   r   eyeobjectonesfullr   )r   r   r$  W_sparser   r[  r]  rJ   rW  rK   test_cross_val_score_fit_params  s*   
$	
rc  c                     s~   t  } g   fdd}tjdd t|}t| tt|dd}W d    n1 s)w   Y  t|g d t dks=J d S )Nc                    s     | |f dS )Nr   )r   )y_test	y_predict_score_func_argsrJ   rK   
score_func  s   z3test_cross_val_score_score_func.<locals>.score_funcTrecordr   )r   r   )r   r   r   )	r   warningscatch_warningsr%   r   rY   rv   r
   r   )r   rh  r   r`   rJ   rf  rK   test_cross_val_score_score_func  s   rm  c                  C   sJ   G dd d} t t t|  t W d    d S 1 sw   Y  d S )Nc                   @   s   e Zd ZdS )z4test_cross_val_score_errors.<locals>.BrokenEstimatorN)rf   rg   rh   rJ   rJ   rJ   rK   BrokenEstimator  s    rn  )r   r   	TypeErrorr   rY   )rn  rJ   rJ   rK   test_cross_val_score_errors  s   "rp  c                  C   s|   t  } tdd}t|| j| j}t|g dd t|| j| jdd}t|g dd t|| j| jdd}t|g dd d S )Nr   r%  )
ףp=
?r   rq  rq  r   r   accuracyr   f1_weighted)r"   r4   r   r@  rA  r	   )rE  r   r   	zo_scores	f1_scoresrJ   rJ   rK   3test_cross_val_score_with_score_func_classification  s   
rv  c            	      C   s   t ddddd\} }t }t|| |}t|g dd t|| |dd	}t|g dd t|| |d
d	}tg d}t||d tt}t|| ||d	}t|g dd d S )Nr   r#  r   r   )r   
n_featuresn_informativer   )gGz?rq  rq  gGz?gq=
ףp?r   r   r   r   )g(\؇gzGIgGz&qg\(qg)\G)r    r-   r   r	   r   r   r%   r$   )	rY   rv   r   r   	r2_scoresneg_mse_scoresexpected_neg_mser   	ev_scoresrJ   rJ   rK   /test_cross_val_score_with_score_func_regression  s   r}  c               
   C   s|  t  } | j}t|}| j}tdd}td}t|||d|dd\}}}|dks)J t|dd	 t|||d|dt	|j
d
d\}	}
}|	|ksHJ ||ksNJ tdd}td}t|||d|dt	|j
d
d\}	}
}|	|kspJ ||ksvJ dd }t|}t|||d||d
d\}}
}t|dd t|dd ttt|d}t|||d|dd\}}}|dk sJ |dksJ d S )Nr   r%  r   r   rr  )n_permutationsr   r   g?        r   r   )r~  r   r   groupsr   c                 S   s"   | |k  | |k   | jd  S rE   )sumrO   )y_truer  rJ   rJ   rK   custom_score+  s   "z,test_permutation_score.<locals>.custom_scored   )r~  r   r   r   g(\?g{Gz?r   r~   皙?)r"   r@  r   rA  r4   r   r   r   r   r`  sizer%   modaranger   )rE  rY   r   rv   rD  r   r`   r   pvaluescore_group_pvalue_group
svm_sparse	cv_sparser  scorerrJ   rJ   rK   test_permutation_score  sd   




r  c                  C   t   t jdt jddd} t j| dd d f< t ddg| jd d }tdtd	t jd
fdt	 fg}t
|| | d S N   r>  r   r   r   r   r   r   mean)strategymissing_valuesr   )r   r  float64r   r   repeatrO   r:   r8   r   r   rY   rv   prJ   rJ   rK   &test_permutation_test_score_allow_nans@     r  c               	   C   s   t ddd} t dgd dgd  }tdd}d}tjt|d	 t|| | W d    n1 s5w   Y  d
}tjt	|d	 t|| |dt 
did W d    n1 s[w   Y  t|| |dt 
did d S )Nr  r   r   r   r   Texpected_sample_weight#Expected sample_weight to be passedr   /sample_weight.shape == \(1,\), expected \(8,\)!r   r\  )r   r  r   r   r   r   r   rs   r   r   r`  rY   rv   r   err_msgrJ   rJ   rK   &test_permutation_test_score_fit_paramsN  s   
r  c                  C   r  r  )r   r  r  r   r   r  rO   r:   r8   r   r   r  rJ   rJ   rK   test_cross_val_score_allow_nans]  r  r  c            	      C   s  t ddgddgddgddgddgddgddgddgddgddgg
} t ddgddgddgddgddgddgddgddgddgddgg
}tdd	}ttd
d}ttdd}ttdd}t|| ||d}t|| ||d}t|| ||d}t|g d t|g d t|g d d S )Nr   r   r   r   r   r   )n_neighborsmicro)averagemacrosamplesr   )r   r~         ?r~   gUUUUUU?)r   r~   r  r~         ?)r   r   r3   r%   r)   r   r   )	rY   rv   r   scoring_microscoring_macroscoring_samplesscore_microscore_macroscore_samplesrJ   rJ   rK   test_cross_val_score_multilabelk  s4   >
r  c                  C   s  t dd\} }t }t }t|}|| |D ]\}}|| | ||  || | ||< qt|| ||d}t	|| t|| |}t
|t
|ksMJ t }t|| ||d}t
|t
|ksbJ |  }||t|k9 }t|}t|||}t	t
|t
| tt | }t
|t
|ksJ G dd d}	tt t|| ||	 d W d    n1 sw   Y  tdd\} }d}
tjt|
d ttdd	| |d
tdd W d    d S 1 sw   Y  d S )NTr   r   c                   @   s   e Zd ZdddZdS )z%test_cross_val_predict.<locals>.BadCVNc                 s   s2    t dD ]}tg dtg dfV  qd S )Nr   )r   r   r   r   )r   r            )r   r   r   )rI   rY   rv   r  irJ   rJ   rK   r     s    z+test_cross_val_predict.<locals>.BadCV.splitrd   )rf   rg   rh   r   rJ   rJ   rJ   rK   BadCV  s    r  zNumber of classes in training fold \(2\) does not match total number of classes \(3\). Results may not be appropriate for your use case.r   	liblinearsolverr   r   methodr   )r!   r   r-   r   
zeros_liker   rR   rZ   r   r	   r   r   copymedianr   r6   r   r   r   r"   r   RuntimeWarningr.   )rY   rv   r   r   preds2r  r  predsXspr  r   rJ   rJ   rK   test_cross_val_predict  sJ   

"r  c                  C   sn  t dddd\} }ttdd| |dd}|jd	ksJ td
d\} }ttdd| |dd}|jdks4J | d d } |d d }d}tjt|d tt | |dt	dd W d    n1 s`w   Y  t
d
d\} }tddd}t|| |dd}|jdksJ t|}| | || } }d}tjt|d t|| |t	dddd W d    d S 1 sw   Y  d S )Nr   2   r   r$  r   r   r  r  decision_functionr  )r  Tr      r   r  zOnly 1 class/es in training fold, but 2 in overall dataset. This is not supported for decision_function with imbalanced folds. To fix this, use a cross-validation technique resulting in properly stratified foldsr   r  r   ovo)r   decision_function_shape)i  -   zOutput shape \(599L?, 21L?\) of decision_function does not match number of classes \(7\) in fold. Irregular decision_function .*r   n_splitsr   r  )r@   r   r.   rO   r"   r   r   r   r1   r   r#   r4   r   argsort)rY   rv   r  r'  r   indr   rJ   rJ   rK   .test_cross_val_predict_decision_function_shape  s<   
"r  c                  C   l   t dddd\} }ttdd| |dd}|jd	ksJ td
d\} }ttdd| |dd}|jdks4J d S )Nr   r  r   r  r  r  r   r  r  r   Tr   r  r@   r   r.   rO   r"   rY   rv   r  rJ   rJ   rK   *test_cross_val_predict_predict_proba_shape     r  c                  C   r  )Nr   r  r   r  r  r  predict_log_probar  r  Tr   r  r  r  rJ   rJ   rK   .test_cross_val_predict_predict_log_proba_shape  r  r  c            
      C   sb  t  } | j| j}}t|}t||d d d g}tddd}t|||}|jdks-J t|||}|jdks:J t|||}t	|jd t|||}t	|jd dd }t
|d	}t|| | }t
|d
}t||| }ttdd| | dd}ttdd|| dd}|d d d d tjf }dd }	t
|	d	}t|||}t	|jd d S )Nr   Fr   )fit_interceptr   )r  )r  r   c                 S   r   rU   r   rp   rJ   rJ   rK   r   -  r   z4test_cross_val_predict_input_types.<locals>.<lambda>r   r   r  r  r  r  c                 S   s
   | j dkS )Nr   )r   rp   rJ   rJ   rK   r   D  r   )r"   r@  rA  r   r   r   r-   r   rO   r
   r   r   r.   r   )
rE  rY   rv   r   r   r   predictionsr   r   check_3drJ   rJ   rK   "test_cross_val_predict_input_types  sF   


r  c                     r)  )	Nr   r*  c                    r-  rU   r.  rp   r/  rJ   rK   r   X  r   z/test_cross_val_predict_pandas.<locals>.<lambda>c                    r-  rU   r.  rp   r1  rJ   rK   r   Y  r   r3  r   r   )
r   r4  r+  r,  r   r5  rY   r   r   r   r6  rJ   r<  rK   test_cross_val_predict_pandasJ  s   
r  c                  C   s   t ddddddd\} }d|d< tddd}tdd}t|| |\}}t|| ||d	d
}||d  d dks:J t||d  d d df dksMJ t||d  d d ddf dksbJ t||d  dksoJ t|j	ddt
|jdd d S )Nr  r   r   r   )r   rw  n_redundantrx  n_clusters_per_classr   r  r   r  r  r   r  axis   )decimal)r@   r.   r   rt   r   r   r   r   r	   r  r`  rO   )rY   rv   r   r   r  r  
yhat_probarJ   rJ   rK   !test_cross_val_predict_unbalanced^  s$   
	
&*"r  c                  C   sf   t  } tjd}|dd}t| |d ddd}t|d d df | t| |d ddd}t|| d S )	NrU  r  r   r   rZ   )rv   r   r  r   r   )r   r   randomRandomStaterandr   r   )mock_classifierrngrY   y_haty_hat_probarJ   rJ   rK   test_cross_val_predict_y_nonet  s   
r  c                  C   sX   t  } | j| j}}t }dtt|jd i}t||||dd}t	|t
d d S )Nr   r   r   )r]  r   )r"   r@  rA  r   r   r   r^  rO   r   r
   r`  )rE  rY   rv   r   r]  r   rJ   rJ   rK   &test_cross_val_score_sparse_fit_params  s   r  c                  C   s  d} d}t | ddddddd\}}t| |d |  }dD ]}tjdd	 }t|||t|d
tddd|dd\}}}	}
}W d    n1 sHw   Y  t|dkr\t	d|d j
 |jdkscJ |	jdksjJ |
jdksqJ |jdksxJ t|tddd t|jddtddd t|	jddtddd |
jdksJ |jdksJ tjdd	}t|||t|| dtddd|d\}}}W d    n1 sw   Y  t|dkrt	d|d j
 t|| t||	 qd S )Nr   r   r   r   r   r   rw  rx  r  r$  r  r   FTTri  r  皙?r   r   )r   rG   r?   return_timesUnexpected warning: %r)r   r   r#  r  ffffff?r  r  r   )r   rG   r?   )r@   rD   rk  rl  r   r   r   linspacer   RuntimeErrormessagerO   r
   r	   r  r?  rB   )r   r  rY   rv   r   shuffle_trainwrG   train_scorestest_scores	fit_timesscore_timestrain_sizes2train_scores2test_scores2rJ   rJ   rK   test_learning_curve  sp   
	
	
r  c               
   C   s   t dddddddd\} }td}t|| d dtdd	d
d\}}}t|tddd
 t|jddtdd	d
 t|jddtdd	d
 d S )Nr   r   r   r   r  r#  r   r  r   r   )rv   r   rG   r  r  )r@   rD   r   r   r  r
   r	   r  rY   r  r   rG   r  r  rJ   rJ   rK    test_learning_curve_unsupervised  s    
	 r
  c               	   C   s   t dddddddd\} }td}tj}t t_zt|| |ddd\}}}W tj }tj  |t_ntj }tj  |t_w d	|v sHJ d S )
Nr   r   r   r   r  r#  r   )r   verbosez[learning_curve])r@   rD   sysstdoutr;   r   getvalueclose)rY   rv   r   
old_stdoutrG   r  r  outrJ   rJ   rK   test_learning_curve_verbose  s.   
	




r  c               	   C   sb   t dddddddd\} }td}tt t|| |dd W d    d S 1 s*w   Y  d S )Nr   r   r   r  T)exploit_incremental_learningr@   rD   r   r   r   r   rY   rv   r   rJ   rJ   rK   5test_learning_curve_incremental_learning_not_possible  s   

"r  c                  C   s   t dddddddd\} }td}dD ];}t|| |dd	td
dd|d\}}}t|tddd t|jddtddd t|jddtd
dd qd S )Nr   r   r   r   r  r#  r  r   Tr  r   r   )r   r  rG   r?   r  r  r@   rj   r   r   r  r
   r	   r  )rY   rv   r   r  rG   r  r  rJ   rJ   rK   (test_learning_curve_incremental_learning  s0   
		r  c                  C   s   t dddddddd\} }td}t|| d ddtd	d
dd\}}}t|tddd t|jddtdd
d t|jddtd	d
d d S )Nr   r   r   r   r  r#  r   Tr  r   r   )rv   r   r  rG   r  r  r  r	  rJ   rJ   rK   5test_learning_curve_incremental_learning_unsupervised  s*   
	 r  c            
   	   C   s   t dddddddd\} }tddd}tdd d	d
}t|| ||ddd\}}}t|| |d|d	d\}}}	t|| t|jdd|jdd t|jdd|	jdd d S )Nr   r   r   r   r  r  r   r   Fmax_itertolr?   r   T)rG   r   r  )r   rG   r  r  )r@   r   r  r0   r   r
   r	   r  )
rY   rv   rG   r   train_sizes_inctrain_scores_inctest_scores_inctrain_sizes_batchtrain_scores_batchtest_scores_batchrJ   rJ   rK   <test_learning_curve_batch_and_incremental_learning_are_equal6  sD   
	
	r#  c               	   C   sh  t dddddddd\} }td}tt t|| |dddgd W d    n1 s,w   Y  tt t|| |dd	d
gd W d    n1 sLw   Y  tt t|| |dddgd W d    n1 slw   Y  tt t|| |dddgd W d    n1 sw   Y  tt t|| |dddgd W d    d S 1 sw   Y  d S )Nr   r   r   r   r  r#  r   r   rG   r  r   r  g?   r  r  rJ   rJ   rK   0test_learning_curve_n_sample_range_out_of_bounds]  s2   
	"r&  c                  C   s   t dddddddd\} }td}d}tjt|d t|| |dtdd	dd
\}}}W d    n1 s6w   Y  t|ddg d S )Nr   r   r   r   r  zzRemoved duplicate entries from 'train_sizes'. Number of ticks will be less than the size of 'train_sizes': 2 instead of 3.r   gQ?r   r$  )	r@   rD   r   r   r  r   r   r  r
   )rY   rv   r   r   rG   r  rJ   rJ   rK   1test_learning_curve_remove_duplicate_sample_sizest  s$   
	r'  c               
   C   s   t dddddddd\} }td}tdd}t|| ||td	d
dd\}}}t|tddd t|jddtdd
d t|jddtd	d
d d S )Nr   r   r   r   r  r#  r   r  r  r   r   r$  r  r  )	r@   rD   r   r   r   r  r
   r	   r  )rY   rv   r   r   rG   r  r  rJ   rJ   rK   (test_learning_curve_with_boolean_indices  s"   
	
 r(  c                  C   s  t ddgddgddgddgd	d
gddgddgddgddgddgddgd	d
gddgddgddgg} t g d}t g d}tdd dd}tdd}t|| ||dt ddd|ddd	\}}}t|jddt g d t|jddt g d  t	t
 t|| ||dt ddd|d!d" W d    n1 sw   Y  t|| ||dt ddd|dddd#
\}}	}
t|	jdd|jdd t|
jdd|jdd d S )$Nr   r   r   r   r   r  r  r     r                       r#  	   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Fr  r  333333?r   T)r   r   rG   r  r?   r   r  )r  r2  Iq?)r3  r  r  r   )r   r   rG   r  r   )r   r   rG   r  r?   r   r  )r   r   r0   r   r   r  r	   r  r   r   r   )rY   rv   r  r   r   r   r!  r"  r  r  r  rJ   rJ   rK    test_learning_curve_with_shuffle  s   
r4  c               
   C   s   t ddd} t dgd dgd  }tdd}d}tjt|d	 t|| |d
d W d    n1 s7w   Y  d}tjt	|d	 t|| |d
dt 
did W d    n1 s^w   Y  t|| |d
dt 
did d S )Nr  r   r   r   r   Tr  r  r   r   r   z/sample_weight.shape == \(1,\), expected \(2,\)!r   )r   r]  )r   r  r   r   r   r   r   rs   r   r   r`  r  rJ   rJ   rK   test_learning_curve_fit_params  s    

r5  c                  C   s  t dddddddd\} }tddg}d}tjt|d	 t|| |d
dtddddd W d    n1 s7w   Y  d}tjt|d	 t|| |d
dtdddddtd
id W d    n1 sfw   Y  t|| |d
dtdddddtdid d S )Nr   r   r   r   r  r#  r   z9Expected fit parameter\(s\) \['sample_weight'\] not seen.r   r   Tr  r   r   r   )r   r  rG   r   z2Fit parameter sample_weight has length 3; expected)r   r  rG   r   r]  )	r@   rj   r   r   rs   r   r   r  r`  )rY   rv   r   r  rJ   rJ   rK   3test_learning_curve_incremental_learning_fit_params  sZ   
	
r6  c               	   C   s   t dddddddd\} }tddd}tjdd}tt | |d|dd	\}}W d    n1 s2w   Y  t|dkrFtd
|d j	 t
|jdd| t
|jddd|  d S )Nr   r   r   r  r   Tri  r   
param_nameparam_ranger   r  r  )r@   r   r  rk  rl  r   r}   r   r  r  r	   r  )rY   rv   r9  r   r  r  rJ   rJ   rK   test_validation_curve/  s0   
		r:  c               	   C   sF   t dddddddd\} }tddd}tt | |d|dd\}}d S )Nr   r   r   r  r   r   r7  )r@   r   r  r   r   )rY   rv   r9  r  rJ   rJ   rK   %test_validation_curve_clone_estimatorJ  s$   

r;  c               
   C   s   d} d}t ddd\}}ttddd||dg dt|| d	d
}ttt|dd d f d  ttddd||dg dt|ddd
}ttt|dd d f d  ttddd||dg dt|dd
}tt	|t	| d S )Nr  r   r   r   r   r   r   )r  r  r  r  r  r7  )r   r   r   r   r   Tr  r?   r  )
r@   r   r4   rB   r	   r   vsplithstackr   r   )r   r  rY   rv   scores1scores2scores3rJ   rJ   rK   +test_validation_curve_cv_splits_consistency`  s<   

"

"

rB  c                  C   s   t ddd} t dgd dgd  }tdd}d}tjt|d	 t|| |d
g ddd W d    n1 s;w   Y  d}tjt	|d	 t|| |d
g dddt 
did W d    n1 sfw   Y  t|| |d
g dddt 
did d S )Nr  r   r   r   r   Tr  r  r   	foo_param)r   r   r   r   )r8  r9  r   r  r   )r8  r9  r   r]  )r   r  r   r   r   r   r   rs   r   r   r`  r  rJ   rJ   rK    test_validation_curve_fit_params  sF   



rD  c                  C   sv   t jd} t d}| | t|dsJ tt |ddr"J d|d< t|dr-J tt |dfdr9J d S )Nr   r     )r   r  r  r  r?   r   deleter>  )r  r  rJ   rJ   rK   test_check_is_permutation  s   

rG  c                  C   sj   t dddddd\} }t| }t|}ttdd}t|| |dd	}t|||dd	}| }t|| d S )
Nr   r   FT)r$  n_labelsallow_unlabeledreturn_indicatorr   r   r%  r   r   )rA   r   r>   r4   r   toarrayr	   )rY   rv   r   y_sparseclassifr  preds_sparserJ   rJ   rK   (test_cross_val_predict_sparse_prediction  s   
rO  c           
   	   C   s   t ddd}|jdkr|dkrt|fnt|df}n|j}t|}|||D ]\}}t| || || } t	| ||| ||< q)||d |d |
dfD ]}	tt| ||	||d| qRd	S )
z@Helper for tests of cross_val_predict with binary classificationr   Fr<  r   r  r   strr  N)r   r   r   rO   r   rB  r   r=   rR   getattrastyper   r   )
r   rY   rv   r  r   	exp_shapeexpected_predictionsr  r  tgrJ   rJ   rK   check_cross_val_predict_binary  s   
 
rV  c              	   C   s   t ddd}ttjj}||dd}tjt|tt|f|| tjd}tj|dd\}}	|	||	D ](\}
}t
| ||
 |	|
 } t| ||| }t|	|
 }||t||< q5||d	 |d
 |dfD ]}tt| ||||d| qkdS )zDHelper for tests of cross_val_predict with multiclass classificationr   Fr<  r   r  r  r   r>  Treturn_inverser   r   rP  r  N)r   r   finfor  minra  r   rr   r   r   r=   rR   rQ  ix_rR  r   r   )r   rY   rv   r  r   	float_mindefault_valuesrT  r  y_encr  r  
fold_preds
i_cols_fitrU  rJ   rJ   rK   "check_cross_val_predict_multiclass  s(   rb  c                    s  t ddd}ttjj}||dd} jd }g }t|D ]0}	tt dd|	f }
|
dkr:|d	kr:t|f}nt||
f}|	tj
||| tjd
 q fddt jd D }tj|dd}|||D ]K\}}t| || || } t| ||| }t|D ]-}	t|| dd|	f }||	 jdkr||	 ||	 |< qt||}||	 ||	 |< qqj  d  d  dfD ]'}t| ||||d}t|t|ksJ tt|D ]}t|| ||  qqdS )zCheck the output of cross_val_predict for 2D targets using
    Estimators which provide a predictions as a list with one
    element per class.
    r   Fr<  r   rW  r   Nr   r  r>  c                    s8   g | ]}t j d d |f ddd d d t jf qS )NTrX  r   )r   r   r   )r   r  rv   rJ   rK   
<listcomp>   s    *z6check_cross_val_predict_multilabel.<locals>.<listcomp>r  rP  r  )r   r   rZ  r  r[  rO   r   r   rr   r   ra  concatenater   r=   rR   rQ  r   r   r\  rR  r   r   )r   rY   rv   r  r   r]  r^  	n_targetsexpected_predsi_coln_classes_in_labelrS  
y_enc_colsr_  r  r  r`  	fold_colsidxrU  cv_predict_outputr  rJ   rc  rK   "check_cross_val_predict_multilabel  sJ   


rn  c                 C   s,   t ddd\}}dD ]	}t| ||| q
d S )Nr   r   )r$  r   r  r   r  )r@   rV  )r   rY   rv   r  rJ   rJ   rK   *check_cross_val_predict_with_method_binary9  s   rp  c                 C   sB   t  }|j|j}}t||dd\}}dD ]	}t| ||| qd S )Nr   r   ro  )r"   r@  rA  r?   rb  )r   rE  rY   rv   r  rJ   rJ   rK   .check_cross_val_predict_with_method_multiclassA  s   rq  c                   C   s    t tdd ttdd d S )Nr  r  )rp  r.   rq  rJ   rJ   rJ   rK   "test_cross_val_predict_with_methodI  s   rr  c                  C   sN   t  } | j| j}}t||dd\}}dD ]}tddd}t|||| qd S )Nr   r   ro  log_lossr   )lossr   )r"   r@  rA  r?   r/   rb  )rE  rY   rv   r  r   rJ   rJ   rK   &test_cross_val_predict_method_checkingP  s   ru  c                  C   s`   t  } | j| j}}t||dd\}}ttddddddgid	d
}dD ]	}t|||| q$d S )Nr   r   rU  r  r  r   r  r   r   r   ro  )r"   r@  rA  r?   rC   r.   rb  )rE  rY   rv   r   r  rJ   rJ   rK   /test_gridsearchcv_cross_val_predict_with_method[  s   rv  c                  C   sL   d} d}t | d|ddd\}}ttddd	}d
D ]
}t||||d qd S )Nr  r   r   r   rU  r   rH  r$  rw  r   r  r   )r  r   )r   r  r  )rA   r>   r.   rV  )n_sampr$  rY   rv   r   r  rJ   rJ   rK   1test_cross_val_predict_with_method_multilabel_ovrf  s   

ry  c                   @   s   e Zd Zdd ZdS )RFWithDecisionFunctionc                 C   s2   |  |}d}t|tsJ |dd |D }|S )Nz?This helper should only be used on multioutput-multiclass tasksc                 S   s.   g | ]}|j d  dkr|dddf n|qS )r   r   Nr   rT  )r   r  rJ   rJ   rK   rd  |  s   . z<RFWithDecisionFunction.decision_function.<locals>.<listcomp>)r   r   rt   )rI   rY   probsmsgrJ   rJ   rK   r  x  s
   
z(RFWithDecisionFunction.decision_functionN)rf   rg   rh   r  rJ   rJ   rJ   rK   rz  t  s    rz  c               	   C   s   d} t dd| ddd\}}|d d df  |d d df 7  < d	D ])}tddd
}t  td t||||d W d    n1 sEw   Y  q!d S )Nr   r  r   r   rU  rw  r   r   r   r  r  n_estimatorsr   ignorer  )rA   rz  rk  rl  simplefilterrn  )r$  rY   rv   r  r   rJ   rJ   rK   0test_cross_val_predict_with_method_multilabel_rf  s   

$

r  c               	   C   s   t jd} | jdddd}t g d}tdd}dD ]"}t  td	 t	|||| W d    n1 s9w   Y  qd S )
Nr   r   )r+  r   r  )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r}  r  )
r   r  r  normalr   r.   rk  rl  r  rb  )r  rY   rv   r   r  rJ   rJ   rK   -test_cross_val_predict_with_method_rare_class  s   


r  c               	   C   s   t jd} | jdddd}t ddgddgddgddgddgg}dD ])}tddd}t  td	 t	||||d
 W d    n1 sHw   Y  q$d S )Nr   r   )r   r   r  r   )r   r  r   r~  r  r  )
r   r  r  r  r   rz  rk  rl  r  rn  )r  rY   rv   r  r   rJ   rJ   rK   ;test_cross_val_predict_with_method_multilabel_rf_rare_class  s   (

r  c                 C   s   t t||g}t||}|| |D ]>\}}	|| | ||  || |	 }
|dkr6t t|	|f}nt t|	|ft |jj	}|
|d d |j
f< |||	< q|S )Nr   )r   rB  r   rQ  r   rR   ra  rZ  r?  r[  classes_)rY   rv   r   classesr   r  rT  funcr  r  expected_predictions_exp_pred_testrJ   rJ   rK   get_expected_predictions  s   

r  c                  C   s  t ddd} t dd tdD }d}tdd}td	d}t }g d
}|D ]]}tdd}t|| |||d}	t	| |||||}
t
|
|	 t|| |||d}	t	| |||||}
t
|
|	 tt tdddd}t|| |||d}	||}t	| |||||}
t
|
|	 q*d S )Nr  r  r   c                 S   s   g | ]}|d  qS )r   rJ   )r   rl   rJ   rJ   rK   rd    s    z7test_cross_val_predict_class_subset.<locals>.<listcomp>r   r   r  r   ro  r  r  r  r   r   )r   r  r   r   r   r   r9   r.   r   r  r	   r?   r  fit_transform)rY   rv   r  kfold3kfold4lemethodsr  r   r  rT  rJ   rJ   rK   #test_cross_val_predict_class_subset  s6   





r  c                     sL  t  } | j| j}}t }tjddd}|d |  tj	|j
tjdtj	|j
ddtjd zRt||| fd	d
d tt t|||fdd
d W d    n1 sZw   Y  W d\ tdD ]}z
t|j
 W  d S  ty   td Y qhw d S d\ tdD ]}z	t|j
 W  w  ty   td Y qw w )NwbF)moderF  s   Hello world!!!!!r>  rJ   r)rO   r  r?  c                        S rU   rJ   r   rY   rv   )r`   rJ   rK   r         z#test_score_memmap.<locals>.<lambda>r   c                    r  rU   rJ   r  )r   rJ   rK   r     r  rd   r   r   )r"   r@  rA  r   tempfileNamedTemporaryFilewriter  r   memmapnamer  r   r   r   r   r   osunlinkWindowsErrorr   )rE  rY   rv   r   tfr  rJ   )r`   r   rK   test_score_memmap  s@   
r  c                     s   t t fg} zddlm}m} | ||f W n	 ty   Y nw | D ]/\ t }|j|j}} ||}} fdd}fdd}	t	||	d}
t
|
|| q"d S )Nr   r*  c                    r-  rU   r.  rp   r/  rJ   rK   r     r   z4test_permutation_test_score_pandas.<locals>.<lambda>c                    r-  rU   r.  rp   r1  rJ   rK   r     r   r3  )r   r4  r+  r,  r   r5  r"   r@  rA  r   r   )r7  r+  r,  rE  rY   rv   r8  r9  r:  r;  r   rJ   r<  rK   "test_permutation_test_score_pandas  s    
r  c               
   C   s  t t j} tdd}td}| |d t d d dd d g	}ddi}tjtdd t	|i | W d    n1 s9w   Y  t
d	}tjt|d t| |d
dd W d    n1 s]w   Y  tjt|d t| |d
dd W d    n1 s|w   Y  tjt|d t| ||d
dd W d    n1 sw   Y  tjt|d t| ||dt jgd
dd W d    n1 sw   Y  |  dksJ d S )Nr   r   r1  r   r   r   z%Failing classifier failed as requiredr   zerror_score must be the string 'raise' or a numeric value. (Hint: if using 'raise', please make sure that it has been spelled correctly.)r   zunvalid-stringr   r   	parameter)r8  r9  r   r   r  )r   FAILING_PARAMETERr   r  r`  r   r   r   r   r   reescaper   r   r   r   r`   )failing_clfrY   rv   fit_and_score_argsfit_and_score_kwargsr'  rJ   rJ   rK   test_fit_and_score_failing  s@   

r  c                  C   s|   t ddd\} }tddd}tt | \}}|| |t ||dg}dddd d	d
}t|i |}|d |d ks<J d S )Nr   r   r   r   r   r  r  )r  r  T)
parametersr]  return_parametersr  )r@   r4   nextr   r   r   r   )rY   rv   r   r  r  r  r  resultrJ   rJ   rK   test_fit_and_score_workingF  s   r  c                   @   s*   e Zd ZdddZdddZd	ddZdS )
DataDependentFailingClassifierNc                 C   s
   || _ d S rU   max_x_value)rI   r  rJ   rJ   rK   rL   V  rc   z'DataDependentFailingClassifier.__init__c                 C   s&   || j k }|rtd| dd S )NzClassifier fit failed with z values too high)r  r  r   )rI   rY   rv   num_values_too_highrJ   rJ   rK   rR   Y  s   
z"DataDependentFailingClassifier.fitc                 C      dS )Nr  rJ   r^   rJ   rJ   rK   r`   `  r[   z$DataDependentFailingClassifier.scorerU   rd   )rf   rg   rh   rL   rR   r`   rJ   rJ   rJ   rK   r  U  s    

r  r   c                 C   s   t dd}tdd}td}|||g}d| d}d}tjd	|d
  d| tjd}tjt	|d t
|i | W d    d S 1 sFw   Y  d S )Nr  r  r   r   r1  r   r  z8ValueError: Classifier fit failed with 1 values too highzh2 fits failed.+total of 3.+The score on these train-test partitions for these parameters will be set to r   z.+flagsr   )r  r   r  r`  r  compileDOTALLr   r   r   r   )r   r  rY   rv   cross_validate_argscross_validate_kwargsindividual_fit_error_messager   rJ   rJ   rK   -test_cross_validate_some_failing_fits_warningd  s$   



"r  c                 C   s   t t j}tdd}td}|||g}d| d}d}tjd| tjd}tj	t
|d	 t|i | W d    d S 1 sAw   Y  d S )
Nr   r   r1  r  r  z1ValueError: Failing classifier failed as requiredz4All the 7 fits failed.+your model is misconfigured.+r  r   )r   r  r   r  r`  r  r  r  r   r   r   r   )r   r  rY   rv   r  r  r  r'  rJ   rJ   rK   *test_cross_validate_all_failing_fits_error~  s   



"r  c                 C   s   t |rU   )r   )r   rY   rv   	error_msgrJ   rJ   rK   _failing_scorer  s   r  zignore:lbfgs failed to converger   c              	   C   s   t dd\}}tdd||}d}tt|d}| dkr@tjt|d t|||d	|| d
 W d    d S 1 s9w   Y  d S d|  }tj	t
|d t|||d	|| d
}t||  W d    d S 1 sgw   Y  d S )NTr   r   r  "This scorer is supposed to fail!!!r  r   r   r   )r   r   r   r   )r"   r.   rR   r   r  r   r   r   r   r   r   r   )r   rY   rv   r   r  failing_scorerwarning_msgr   rJ   rJ   rK   #test_cross_val_score_failing_scorer  s(   ""r  r  TFwith_multimetricc              
   C   s
  t dd\}}tdd||}d}tt|d}|r ||d}n|}| dkrKtjt|d	 t|||d
||| d W d    d S 1 sDw   Y  d S d|  }	tj	t
|	d	$ t|||d
||| d}
|
D ]}d|v rrt|
| |  qeW d    d S 1 s~w   Y  d S )NTr   r   r  r  r  )score_1score_2r   r   r   )r   r   r  r   r   r   )r"   r.   rR   r   r  r   r   r   r   r   r   r   )r   r  r  rY   rv   r   r  r  r   r  r   ry   rJ   rJ   rK   "test_cross_validate_failing_scorer  sN   	"	"r  c                 C   r  )Ng;pΈ^@rJ   )r  jr  rJ   rJ   rK   three_params_scorer  r[   r  z:train_score, scorer, verbose, split_prg, cdt_prg, expectedr   )r   r   r   r   zS\[CV\] END .................................................... total time=   0.\ds)sc1sc2r   ze\[CV 2/3\] END  sc1: \(train=3.421, test=3.421\) sc2: \(train=3.421, test=3.421\) total time=   0.\dsr   zW\[CV 2/3; 1/1\] END ....... sc1: \(test=3.421\) sc2: \(test=3.421\) total time=   0.\dsc              	   C   s   t ddd\}}tddd}	tt |\}
}|	||||
||d d g	}|||d}t|i | |  \}}|d}t|dkrMt	||d	 sKJ d S t	||d sWJ d S )
Nr   r   r   r   r   )r  split_progresscandidate_progress
r   r   )
r@   r4   r  r   r   r   
readouterrr   r  r   )capsysr	  r  r  	split_prgcdt_prgexpectedrY   rv   r   r  r  r  r  r  r  outlinesrJ   rJ   rK   test_fit_and_score_verbosity  s   #
r  c                  C   s\   d} dd }d d d |g}t jt| d t|dtji W d    d S 1 s'w   Y  d S )Nz&scoring must return a number, got Nonec                 S   s   d S rU   rJ   )r   X_testrJ   rJ   rK   two_params_scorer&	  r[   z%test_score.<locals>.two_params_scorerr   r   )r   r   r   r   r   r   )r'  r  r  rJ   rJ   rK   r
  #	  s   "r
  c                  C   sj   dd } t dddd\}}tdd}||| t|||d| d	}g d
}|D ]}d||v s2J q'd S )Nc                 S   s2   |  |}t||}|d |d |d |d dS )N)r   r   r  )r   r   )r   r   tnfpfntp)rZ   r'   )r   rY   rv   r  cmrJ   rJ   rK   r  /	  s   

zPtest_callable_multimetric_confusion_matrix_cross_validate.<locals>.custom_scorer(   r   rU  )r   rw  r   r   r   )r   r   r  ztest_{})r@   r5   rR   r   r   )r  rY   rv   r   r"  score_namesr  rJ   rJ   rK   9test_callable_multimetric_confusion_matrix_cross_validate.	  s   
r  c                  C   s&   t dd\} }tt | |ddd dS )z^Check that regressors with partial_fit is supported.

    Non-regression test for #22981.
    rU  r   Tr   )r  r   N)r    r   r7   )rY   rv   rJ   rJ   rK   *test_learning_curve_partial_fit_regressors>	  s   r  )ri   r  r  r  r  rk  	functoolsr   timer   r   numpyr   scipy.sparser   r   sklearn.exceptionsr   )sklearn.model_selection.tests.test_searchr   sklearn.utils._testingr   r	   r
   r   sklearn.utils._mockingr   r   sklearn.utils.validationr   sklearn.model_selectionr   r   r   r   r   r   r   r   r   r   r   r   r   r   #sklearn.model_selection._validationr   r   r   sklearn.datasetsr    r!   r"   r#   sklearn.metricsr$   r%   r&   r'   r(   r)   r*   r+   r,   sklearn.linear_modelr-   r.   r/   r0   r1   sklearn.ensembler2   sklearn.neighborsr3   sklearn.svmr4   r5   sklearn.clusterr6   sklearn.neural_networkr7   sklearn.imputer8   sklearn.preprocessingr9   sklearn.pipeliner:   ior;   sklearn.baser<   r=   sklearn.multiclassr>   sklearn.utilsr?   r@   rA   $sklearn.model_selection.tests.commonrB   rC   r  	NameErrorrD   rj   r}   r   r   r`  rY   r   r   rv   r   r^  r   r   r   r   r   r  r   r   r(  markfilterwarningsr=  rL  rS  rc  rm  rp  rv  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  r  r  r#  r&  r'  r(  r4  r5  r6  r:  r;  rB  rD  rG  rO  rV  rb  rn  rp  rq  rr  ru  rv  ry  rz  r  r  r  r  r  r  r  r  r  r  parametrizer   r  r  r  r  r  r  r  r
  r  r  rJ   rJ   rJ   rK   <module>   s   
W*
?/0I

#B84
5
	='N0.&4)

)



0


 