U
    3d                  -   @   sH  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Z	d dl
mZ d dl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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#lm1Z1 d d$lm2Z2 d d%lm3Z3 d d&lm4Z4 d d'lm5Z5 d d(lm6Z6 d d)lm7Z7 d d*lm8Z8 d d+lm9Z9 d d,lm:Z: d d-lm;Z; d d.lm<Z< d d/lm=Z= d d0lm>Z> d d1lm?Z? d d2lm@Z@ d d3lmAZA d d4lmBZB d d5lmCZC d d6lmDZD d d7lmEZE d d8lmFZF d d9lmGZG d d:lmHZH d d;lImJZJ e4e6e8e>e<e7e+eeAd<d=ee9d d>e:e;ee9d?d>ee'd?d>e(e)d@ZKe e"ee"dAdBee dCdDe%dEdF e=ee=dAdGe.eEeeEdCdDe0e@eBe,ee-dHdIee-dJdIe5ee-dKdJdLee,dKdMee-dKdHdLee@dKdMeeBdKdMee0dKdMee-dNdJdLee,dNdMee-dNdHdLee@dNdMeeBdNdMee0dNdMee-dOdJdLee,dOdMee-dOdHdLee@dOdMeeBdOdMee0dOdMee-dPdJdLee,dPdMee-dPdHdLee@dPdMeeBdPdMee0dPdMe$dQ+ZLdRdS ZMeDeMe*dTZNe&e2e3ee3dCdDe/e#eCeeCdKdMeeCdPdMeeCdNdMeeCdOdUdVeeCdKdUdVeeCdOdWdVeeCdKdWdVeeCdJdXe!ee!dKdMee!dPdMee!dNdMe1eFeGeHdYZOeP ZQeQReO eQReL eQReK eQReN dZd[d\d]d^d_d`dadbdcdddehZSdfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxhZTeSUeTZVdqdrdsdtdudphZWdjdldihZXdvdwdxdfdqdrdsdtdudpdldmdndohZYdydzdvdwdxdqdrdsdtdudpd{d|d}d~ddddddddddddddddadh ZZdddhZ[dddjdkdhdgdidldmdodnd`dbdedddchZ\dddddd{d|d}d~ddddddddddddddddZd[d\d]d^d_hZ]dddddddddh	Z^ddddddddpd_dsddddddddddddddddhZ_dddddydzdvdwdxdqdrdtdud{d|d}d~ddddddddddddddhZ`ddddhZaddddhZbdd Zcdd Zdejefdege_dd Zhejefdege`dd ZiejefdegejeQeV dd Zkedd ZlejefdegejeQeV dd ZmejefdegejeLeV dd ZnejefdeOdd Zod dge	jpe	jpgfd dge	jqe	jqgfd dge	jqe	jpgfd dge	jpdgfd dge	jqdgfgZrejefdeeOs eKs ejefderddÄ ZtejefdeLs ejefdere	jqddHgddHdgfe	jpddHgddHdgfg ddƄ ZuejefdeLs ddȄ Zveddʄ Zwedd̄ ZxejefdegejeQeV ejeO dd΄ Zyejefdege^e]B ddЄ Zzejefdege^dd҄ Z{ejefdege^ddԄ Z|eddք Z}ejefdege]dd؄ Z~ejefdege[ddڄ Zejefdege[dd܄ Zejefdege[e]ddބ Zedd Zdd ZejefdegeWdd ZejefdegeWeXB dd ZejefdegeWdd Zdd ZejefdegeWdd Zedd ZejefdegejeQejeKea dd ZejefdegejeQejeK ea eS dd ZejefdegejeQejeK ea eV dd Zejefdege]e\B e^B ea dd Zedd Zejefdege]dh dd Zejefdege\e^B dd ZejefdegejeOeV dd ZejefdeLd d Zejefde!dAfe#dAfe,dCfee-ddIdCfe0dCfe?dAfe@dCfeBdCfeDdAfg	ejefdeegdd ZdS (      )partial)	signature)product)chain)permutationsN)make_multilabel_classification)LabelBinarizer)type_of_target)_num_samples)check_random_state)shuffle)assert_allclose)assert_almost_equal)assert_array_equal)assert_array_less)ignore_warnings)accuracy_score)average_precision_score)balanced_accuracy_score)brier_score_loss)cohen_kappa_score)confusion_matrix)coverage_error)d2_tweedie_score)d2_pinball_score)d2_absolute_error_score)	det_curve)explained_variance_score)f1_score)fbeta_score)hamming_loss)
hinge_loss)jaccard_score)%label_ranking_average_precision_score)label_ranking_loss)log_loss)	max_error)matthews_corrcoef)mean_absolute_error)mean_absolute_percentage_error)mean_squared_error)mean_tweedie_deviance)mean_poisson_deviance)mean_gamma_deviance)median_absolute_error)multilabel_confusion_matrix)mean_pinball_loss)precision_recall_curve)precision_score)r2_score)recall_score)roc_auc_score)	roc_curve)zero_one_loss)
ndcg_score)	dcg_score)top_k_accuracy_score)_average_binary_scoreZvariance_weighted)Zmultioutput)powergffffff?)r&   r(   r*   r0   r.   r)   r   r3   mean_normal_deviancer,   r-   mean_compound_poisson_deviancer   r   r   T)adjustedF	normalizec                  O   s0   t | |dt | |jddd d tjf  S )Nfloat   axis)r   astypesumnpZnewaxis)argskwargs rK   E/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/metrics/tests/test_common.py<lambda>   s    rM   )Z
samplewise   )betag      ?weighted)averagerO   rQ   micromacrosamples)+r   r    adjusted_balanced_accuracy_scoreunnormalized_accuracy_scoreunnormalized_confusion_matrixnormalized_confusion_matrix(unnormalized_multilabel_confusion_matrix/unnormalized_multilabel_confusion_matrix_sampler    r7   unnormalized_zero_one_lossr"   r2   r4   r   f2_score
f0.5_scorematthews_corrcoef_scoreweighted_f0.5_scoreweighted_f1_scoreweighted_f2_scoreweighted_precision_scoreweighted_recall_scoreweighted_jaccard_scoremicro_f0.5_scoremicro_f1_scoremicro_f2_scoremicro_precision_scoremicro_recall_scoremicro_jaccard_scoremacro_f0.5_scoremacro_f1_scoremacro_f2_scoremacro_precision_scoremacro_recall_scoremacro_jaccard_scoresamples_f0.5_scoresamples_f1_scoresamples_f2_scoresamples_precision_scoresamples_recall_scoresamples_jaccard_scorer   c               
   O   sN   t | |\}}}t|t| }t||tj|tjd|fdtjgdgS )a'  
    The dimensions of precision-recall pairs and the threshold array as
    returned by the precision_recall_curve do not match. See
    func:`sklearn.metrics.precision_recall_curve`

    This prevents implicit conversion of return value triple to an higher
    dimensional np.array of dtype('float64') (it will be of dtype('object)
    instead). This again is needed for assert_array_equal to work correctly.

    As a workaround we pad the threshold array with NaN values to match
    the dimension of precision and recall arrays respectively.
    r   Zconstant)Z	pad_widthmodeZconstant_values)r1   lenrH   arraypadrF   Zfloat64nan)rI   rJ   Z	precisionZrecallZ
thresholdsZpad_threshholdsrK   rK   rL   (precision_recall_curve_padded_thresholds   s    
r}   )r6   r1   r   Zovr)rQ   Zmulti_classZovo)Zmax_fpr)r   r$   r%   unnormalized_log_lossr!   r   r5   weighted_roc_aucsamples_roc_aucmicro_roc_aucZovr_roc_aucZweighted_ovr_roc_aucovo_roc_aucweighted_ovo_roc_aucpartial_roc_aucr    weighted_average_precision_scoresamples_average_precision_scoremicro_average_precision_scorer#   r8   r9   r:   rr   rs   rt   ru   rv   rw   r   r[   r$   r#   r9   r8   r   r   r   r   r5   r   r   r   r   r   r"   r2   r4   r   r]   r^   r6   r1   r   rX   rY   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rZ   r   r   r:   r7   r%   r~   rW   r    r\   r(   r.   r*   r3   r   r)   r0   r   r   r_   r&   r=   r   rV   r!   r-   r,   r>   r   r   r   c                 C   s2   t t|  | d }| |7 } ||7 }| |fS )zMake targets strictly positiverC   )absmin)y1y2offsetrK   rK   rL   _require_positive_targets  s    r   c                   C   s6   t tB ttB tB ttks tt t@ t ks2td S N)SYMMETRIC_METRICSNOT_SYMMETRIC_METRICSsetTHRESHOLDED_METRICS"METRIC_UNDEFINED_BINARY_MULTICLASSALL_METRICSAssertionErrorrK   rK   rK   rL   test_symmetry_consistency'  s    r   namec                 C   s   t d}|jdddd}|jdddd}| tkr>t||\}}|jdddd}|jdddd}t|  }| tkr| tkrt||||||d|  d qdstd	nt||||||d|  d d S )
Nr   rN      sizer      z%s is not symmetricerr_msgFz This case is currently unhandled)	r   randintMETRICS_REQUIRE_POSITIVE_Yr   r   METRIC_UNDEFINED_BINARYMULTILABELS_METRICSr   r   )r   random_statey_truey_predZ
y_true_binZ
y_pred_binmetricrK   rK   rL   test_symmetric_metric4  s*    r   c              	   C   s   t d}|jdddd}|jdddd}| tkr>t||\}}t|  }tt( t|||||| t	d|  W 5 Q R X d S )Nr   rN   r   r   z%s seems to be symmetric)
r   r   r   r   r   pytestraisesr   r   
ValueError)r   r   r   r   r   rK   rK   rL   test_not_symmetric_metricS  s    r   c              	   C   s   t d}|jdddd}|jdddd}| tkr>t||\}}t||dd\}}t , t|  }t||||||d|  d W 5 Q R X d S )Nr   rN   r   r   r    %s is not sample order invariantr   )r   r   r   r   r   r   r   r   )r   r   r   r   y_true_shuffley_pred_shuffler   rK   rK   rL   test_sample_order_invariancee  s    r   c            	      C   s   t d} | jdddd}| jdddd}| j|jd}t|||dd\}}}tD ]*}t| }t||||||d| d qPtD ]*}t| }t||||||d| d qt	D ]H}t| }t||||||d| d t||||||d| d qd S )Nr   rN   r   r   r   r   r   )
r   r   normalshaper   r   r   r   THRESHOLDED_MULTILABEL_METRICSMULTIOUTPUT_METRICS)	r   r   r   y_scorer   r   Zy_score_shuffler   r   rK   rK   rL   7test_sample_order_invariance_multilabel_and_multioutput{  sH       r   c              
   C   s  t d}|jdddd}|jdddd}| tkr>t||\}}t|}t|}t|t| }}t|jd t|jd t	|d}t	|d}	t	|d}
t	|d}t
  t|  }|||}t||||d|  d	 t||||d
|  d	 t|||	|d|  d	 t||||d|  d	 t||||d|  d	 t|||	|d|  d	 t||||d|  d	 t|||	|d|  d	 t||||d|  d	 tt ||| W 5 Q R X tt ||
| W 5 Q R X tt ||| W 5 Q R X tt ||
| W 5 Q R X tt ||| W 5 Q R X tt ||
|	 W 5 Q R X | ttB tB krtt ||
| W 5 Q R X W 5 Q R X d S )Nr   rN   r   r   rC   )rC   )rC   r   z,%s is not representation invariant with listr   z3%s is not representation invariant with np-array-1dz7%s is not representation invariant with np-array-columnz@%s is not representation invariant with mix np-array-1d and listzK%s is not representation invariant with mix np-array-1d and np-array-columnzD%s is not representation invariant with mix list and np-array-column)r   r   r   r   listrH   rz   r   ndimreshaper   r   r   r   r   r   r   r   r   )r   r   r   r   Zy1_listZy2_listZy1_1dZy2_1dZ	y1_columnZ	y2_columnZy1_rowZy2_rowr   measurerK   rK   rL   &test_format_invariance_with_1d_vectors  s    



	

r   c              	   C   s>  t d}|jdddd}|jdddd}tddg| }tddg| }d}ddg}t  t|  }|||}	|}
| tkrt|
|d}
|
||}t|	|d	| d	 |
|
d
|
d
}t|	|d	| d	 | tkr0t|
|d}
|
||}t|	|d	| d	 |
|
d
|
d
}t|	|d	| d	 W 5 Q R X d S )Nr   rN   r   r   eggsspam	pos_label+{0} failed string vs number invariance testr   O2{0} failed string object vs number invariance test)labelsz,{0} failed string vs number  invariance test)r   r   rH   rz   r   CLASSIFICATION_METRICSMETRICS_WITH_POS_LABELr   r   formatrF   METRICS_WITH_LABELS)r   r   r   r   y1_strZy2_strpos_label_strZ
labels_strr   measure_with_number
metric_strmeasure_with_strmeasure_with_strobjrK   rK   rL   7test_classification_invariance_string_vs_numbers_labels  sN    



r   c              
   C   s  t d}|jdddd}|jdddd}tddg| }d}t  t|  }| tkr|}| tkrnt||d}|||}|||}	t	||	d
| d	 ||d
|}
t	||
d
| d	 nFtt ||| W 5 Q R X tt ||d
| W 5 Q R X W 5 Q R X d S )Nr   rN   r   r   r   r   r   r   r   r   r   )r   r   rH   rz   r   r   r   r   r   r   r   rF   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   rK   rK   rL   4test_thresholded_invariance_string_vs_numbers_labelsW  s<    

	r   rC   r   zy_true, y_scorec              	   C   s<   | t kr|g}|g}tjtdd | || W 5 Q R X d S )Nzcontains (NaN|infinity)match)r   r   r   r   )r   r   r   rK   rK   rL   )test_regression_thresholded_inf_nan_input  s
    r      c              	   C   s   t | s,d}t | r&d}qHd}nd}t | rDd}nd}d| d| }tjt|d | || W 5 Q R X dS )	z{check that classification metrics raise a message mentioning the
    occurrence of non-finite values in the target vectors.r   NaNzinfinity or a value too larger   zInput z
 contains r   N)rH   isfiniteallisnananyr   r   r   )r   r   r   Z
input_nameZunexpected_valuer   rK   rK   rL   !test_classification_inf_nan_input  s    r   c              	   C   sB   dddgdddg }}d}t jt|d | || W 5 Q R X dS )	zocheck that classification metrics raise a message of mixed type data
    with continuous/binary target vectors.abg?g?333333?zJClassification metrics can't handle a mix of binary and continuous targetsr   N)r   r   r   )r   r   r   r   rK   rK   rL   +test_classification_binary_continuous_input  s
    r   c                 C   sJ   t |  }| tkrddg}nddg}t|ddD ]\}}||g|g q.d S )NrC   rN   r   repeat)r   r   r   )r   r   valuesijrK   rK   rL   check_single_sample  s    
r   c                 C   sL   t |  }tddgddD ].\}}}}|t||ggt||gg qd S )Nr   rC      r   )r   r   rH   rz   )r   r   r   r   klrK   rK   rL   check_single_sample_multioutput  s    r   c                 C   s   t |  d S r   )r   r   rK   rK   rL   test_single_sample  s    r   c                 C   s   t |  d S r   )r   r   rK   rK   rL   test_single_sample_multioutput  s    r   c              	   C   sp   t ddddgddddgddddgg}t ddgddgddgg}t|  }tt ||| W 5 Q R X d S )NrC   r   rH   rz   r   r   r   r   )r   r   r   r   rK   rK   rL   (test_multioutput_number_of_output_differ  s
    (r   c                 C   s   t d}|jdddd}|jdddd}t|  }|||}tdD ]D}||jd }t||d d |f |d d |f |d|  d qBd S )	Nr   rN   )r      r   r   rC   z'%s is not dimension shuffling invariantr   )r   uniformr   rangeZpermutationr   r   )r   r   r   r   r   error_permrK   rK   rL   =test_multioutput_regression_invariance_to_dimension_shuffling  s    
 r   c                  C   s  d} d}t d| d|dd\}}t d| d|dd\}}t|dg|  gg}t|dg|  gg}t|}t|}t|}t|}dd |D }	d	d |D }
tD ]t}t| }t|t	rd
|_
||_|||}t||||d| d t||	|
|d| d t||||d| d qd S )Nr   2   rC   r   T
n_features	n_classesr   	n_samplesallow_unlabeledc                 S   s   g | ]}t |qS rK   r   .0r   rK   rK   rL   
<listcomp>-  s     z=test_multilabel_representation_invariance.<locals>.<listcomp>c                 S   s   g | ]}t |qS rK   r   r   rK   rK   rL   r   .  s     tmpzO%s failed representation invariance between dense and sparse indicator formats.r   z\%s failed representation invariance  between dense array and list of list indicator formats.zW%s failed representation invariance  between dense and list of array indicator formats.)r   rH   vstackspZ
coo_matrixr   r   r   
isinstancer   
__module____name__r   r   )r   r   r   r   r   Zy1_sparse_indicatorZy2_sparse_indicatorZy1_list_array_indicatorZy2_list_array_indicatorZy1_list_list_indicatorZy2_list_list_indicatorr   r   r   rK   rK   rL   )test_multilabel_representation_invariance  sf    

	



	
r  c              
   C   sn   dgdgddggdddgg gdgt jg ddggddg}t|  }|D ]$}tt ||| W 5 Q R X qDd S )NrC   rN   r   rK   )r   rC   objectdtyper   )r   Zmultilabel_sequencesr   seqrK   rK   rL   +test_raise_value_error_multilabel_sequencesZ  s    r  c                 C   s   d}d}t d}|jd||fd}|jd||fd}|j|jd}t|  }| tkrV|n|}|||dd}	|||dd}
td|	 dd	d
 t|	|
| d|  d
 d S )NrN   r   r   r   Tr@   F      0We failed to test correctly the normalize optionr   Failed with )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   metricspredmeasure_normalizedmeasure_not_normalizedrK   rK   rL   +test_normalize_option_binary_classificationk  s(    r  c                 C   s   d}d}t d}|jd||fd}|jd||fd}|j||fd}t|  }| tkrX|n|}|||dd}	|||dd}
td|	 dd	d
 t|	|
| d|  d
 d S )Nr   r   r   r   Tr@   Fr  r  r   r  )r   r   r   r   r   r   r   r  rK   rK   rL   /test_normalize_option_multiclass_classification  s(    r  c                 C   s   d}d}t d}td|dd|d\}}td|dd|d\}}|j|jd}|dg| 7 }|dg| 7 }t|  }| tkrz|n|}	|||	dd}
|||	d	d}td
|
 ddd t|
|| d|  d d S )Nr   d   r   rC   T)r   r   r   r   r   r   r@   Fr  r  r   r  )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r  r  r  r  rK   rK   rL   /test_normalize_option_multilabel_classification  sD    

r  c              	      s\  j \}} ||d d}t| fddt|D   ||dd}	t|	     ||dd}
t|
t| tjdtd}t|dkr ||dd}t|tj||d	 n ||dd}t|d |r ||d
d}t|t fddt|D  t	
t  ||dd W 5 Q R X t	
t  ||dd W 5 Q R X d S )NrR   c                    s.   g | ]&} d d |f d d |f qS r   rK   r   r   r   y_pred_binarizey_true_binarizerK   rL   r     s   z$_check_averaging.<locals>.<listcomp>rS   rT   r   )rE   r	  rP   )weightsrU   c                    s   g | ]} | | qS rK   rK   r  r  rK   rL   r     s   unknowngarbage)r   r   r   ZravelrH   ZmeanrG   intrQ   r   r   r   )r   r   r   r  r  is_multilabelr   r   Zlabel_measureZmicro_measureZmacro_measurer  Zweighted_measureZsample_measurerK   r  rL   _check_averaging  sF    
	 

r!  c                 C   sZ   t |d}t|  }| tkr2t|||||| n$| tkrNt|||||| ntdd S )NZ
multilabelz2Metric is not recorded as having an average option)r	   
startswithr   METRICS_WITH_AVERAGINGr!  "THRESHOLDED_METRICS_WITH_AVERAGINGr   )r   r   r  r   r  r   r   r   rK   rK   rL   check_averaging  s*              r%  c           
      C   sz   d\}}t d}|jd||fd}|jd||fd}|j||fd}t |}||}||}	t| ||||	| d S )N)r   r   r   r   )r   r   r   r   ZfitZ	transformr%  )
r   r   r   r   r   r   r   Zlbr  r  rK   rK   rL   test_averaging_multiclass  s    

r&  c           
      C   sh   d\}}t d|d|dd\}}|d d }|dd  }tdjd|fd}|}|}	t| ||||	| d S )	N)(   r   rC   r   Fr   r   r   r   )r   r   r   r%  )
r   r   r   r   yr   r   r   r  r  rK   rK   rL   test_averaging_multilabel.  s    
r)  c                 C   s<   t d}t d}t d}|}|}t| ||||| d S Nr   r   )rH   zerosr%  r   r   r   r   r  r  rK   rK   rL   $test_averaging_multilabel_all_zeroesC  s    


r.  c                  C   s>   t d} t d}| }|}ddd}t|| |||dd d S )Nr+  rT   c                 S   s   t t| ||S r   )r;   r2   )r   r   rQ   rK   rK   rL   rM   T  s
      z=test_averaging_binary_multilabel_all_zeroes.<locals>.<lambda>T)r   )rT   )rH   r,  r!  )r   r   r  r  Zbinary_metricrK   rK   rL   +test_averaging_binary_multilabel_all_zeroesN  s    


r/  c                 C   s<   t d}t d}t d}|}|}t| ||||| d S r*  )rH   onesr%  r-  rK   rK   rL   "test_averaging_multilabel_all_onesa  s    


r1  c              	   C   s  t jd}|jddt|d}| dkr4t|ddn|}|||d d}t||||t jt|ddd	|  d
 ||||d}t	t
" t|| td||| f W 5 Q R X |||| d}t||d||| f d
 |t j||ddt j||ddd d}	t||	d|  d
 |dd d }
t |}d|d d d< |dd d }|dd d }||||
d}||||d}t||d||| f d
 | dsdD ]&}t|||||| dd|  d
 qdt|t|t|d }tj	t|d |||t ||gd W 5 Q R X d S )Nr   rC   
   r   r:   )r   )sample_weight)r   zAFor %s sample_weight=None is not equivalent to sample_weight=onesr   zQUnweighted and weighted scores are unexpectedly almost equal (%s) and (%s) for %szVWeighted scores for array and list sample_weight input are not equal (%s != %s) for %srD   z.Weighting %s is not equal to repeating samplesrN   zeZeroing weights does not give the same result as removing the corresponding samples (%s != %s) for %sZunnormalized)rN   r   z/%s sample_weight is not invariant under scalingzJFound input variables with inconsistent numbers of samples: \[{}, {}, {}\]r   )rH   randomRandomStater   ry   r   r   r0  r   r   r   r   tolistr   copyr"  r   r
   Zhstack)r   r   r   r   rngr3  Zunweighted_scoreZweighted_scoreZweighted_score_listZrepeat_weighted_scoreZsample_weight_subsetZsample_weight_zeroedZ	y1_subsetZ	y2_subsetZweighted_score_subsetZweighted_score_zeroedZscalingerror_messagerK   rK   rL   check_sample_weight_invariancel  s    

  

	  
r:  c                 C   sB   d}t d}|j|fd}|j|fd}t|  }t| ||| d S )Nr   r   r   )r   random_sampler   r:  )r   r   r   r   r   r   rK   rK   rL   (test_regression_sample_weight_invariance  s    r<  c                 C   sp   d}t d}|jdd|fd}|jdd|fd}|j|fd}t|  }| tkr^t| ||| nt| ||| d S )Nr   r   rN   r   )r   r   r;  r   r   r:  )r   r   r   r   r   r   r   rK   rK   rL   $test_binary_sample_weight_invariance  s    r=  c           	      C   s   d}t d}|jdd|fd}|jdd|fd}|j|dfd}t|  }| tkrt| }||jdddd }t	| ||| nt	| ||| d S )Nr   r   r   r   r   rD   rC   )
r   r   r;  r   r   rH   exprG   r   r:  )	r   r   r   r   r   r   r   tempZy_score_normrK   rK   rL   (test_multiclass_sample_weight_invariance  s    r@  c           	      C   s   t d}tdddddd\}}tdddddd\}}t||g}t||g}|jdd|jd}t|  }| tkrt| ||| nt| ||| d S )	Nr   rC   r2  r   Fr   r   r   )	r   r   rH   r  r   r   r   r   r:  )	r   r   r   ZyaZybr   r   r   r   rK   rK   rL   (test_multilabel_sample_weight_invariance  s,    	    
    
rA  c                  C   s   t ddddgddddgg} t ddddgddddgg}t dddg}t dddg}t ddddg}t j|dd\}}tD ]f}||g| |gfD ]P\}}	|tkr|	jdkrqt| }
|
||	|d d}|
||	d d}t|||  qqd S )	NrC   r   rN   r   T)Zreturn_inverse)r   rQ   rR   )rH   rz   uniquer#  r   r   r   r   )Zy_true_multilabelZy_pred_multilabelZy_true_multiclassZy_pred_multiclassr   r   Zinverse_labelsr   r   r   r   Zscore_labelsscorerK   rK   rL   test_no_averaging_labels+  s     rD  c                 C   s   t d}d\}}|jdd||fd}|jdd||fd}t|  }|||}tt||D ]8}|d d |f }	|d d |f }
||
|	}t|| qXd S )Nr   r   r   rN   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   rC  r   y_score_permy_true_permcurrent_scorerK   rK   rL   -test_multilabel_label_permutations_invarianceE  s    

rJ  c                 C   s   t d}d\}}|jdd||fd}|j|jd}d||ddkdf< d||ddkdf< t|  }|||}tt||D ]\}|d d |f }	|d d |f }
||
|	}|tkrt	
|st|dkstq~t|| q~d S )Nr   rE  rN   r   rC   r   g    .A)r   r   r   r   rG   r   r   r   r)   rH   r   r   r   rF  rK   rK   rL   ?test_thresholded_multilabel_multioutput_permutations_invarianceZ  s     

rK  c                 C   s   d\}}t d}|||}t| }||jdddd }|jd||d}t|  }|||}tt	||D ]T}	tj
|td}
t||
t|	< |d d |
f }t|	|}|||}t|| qpd S )N)r  r   r   r   rD   rC   r   r  )r   ZrandrH   r>  rG   r   r   r   r   r   r,  r  Zaranger   Ztaker   )r   r   r   r   r   r?  r   r   rC  r   Zinverse_permrG  rH  rI  rK   rK   rL   .test_thresholded_metric_permutation_invariance{  s    

rL  metric_namec              	   C   sl   t jd}t jdgd dgd  td}|jdd|jd}d	}tjt	|d
 t
|  || W 5 Q R X d S )N*   r   r   r   rN   r  r   r   z7Labels in y_true and y_pred should be of the same type.r   )rH   r4  r5  rz   r  r   r   r   r   	TypeErrorr   )rM  r8  r   r   r   rK   rK   rL   "test_metrics_consistent_type_error  s    rP  zmetric, y_pred_thresholddtype_y_strc           
   	   C   s   t jd}t jdgd dgd  |d}|jdd|jd}|sVt jddg|d| }d	}d
}t| jd j}|dkrz|n|}	t	j
t|	d | || W 5 Q R X d S )NrN  r   r   r   rN   r  r   r   zy_true takes value in {'eggs', 'spam'} and pos_label is not specified: either make y_true take value in {0, 1} or {-1, 1} or pass pos_label explicitzHpos_label=1 is not a valid label. It should be one of \['eggs', 'spam'\]r   rC   r   )rH   r4  r5  rz   r   r   r   
parametersdefaultr   r   r   )
r   Zy_pred_thresholdrQ  r8  r   r   Zerr_msg_pos_label_NoneZerr_msg_pos_label_1Zpos_label_defaultr   rK   rK   rL    test_metrics_pos_label_error_str  s    rT  )	functoolsr   inspectr   	itertoolsr   r   r   ZnumpyrH   Zscipy.sparsesparser  r   Zsklearn.datasetsr   Zsklearn.preprocessingr   Zsklearn.utils.multiclassr	   Zsklearn.utils.validationr
   r   Zsklearn.utilsr   Zsklearn.utils._testingr   r   r   r   r   Zsklearn.metricsr   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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   Zsklearn.metrics._baser;   ZREGRESSION_METRICSr   r}   ZCURVE_METRICSr   dictr   updater   ZMETRIC_UNDEFINED_MULTICLASSunionr   r#  r$  r   r   ZMETRICS_WITH_NORMALIZE_OPTIONr   r   r   r   r   ZMETRICS_WITHOUT_SAMPLE_WEIGHTr   r   r   markZparametrizesortedr   r   r   r   r   r   r   r   infr|   Zinvalids_nan_infr   r   r   r   r   r   r   r   r   r   r  r  r  r  intersectionr  r!  r%  r&  r)  r.  r/  r1  r:  r<  r=  r@  rA  rD  rJ  rK  rL  rP  strr  rT  rK   rK   rK   rL   <module>   s~  %





 


















9 



    
  
"



%#"%

 

* 
v 
5
+	 	






	

K


 
/
6
 






a

	
	



 
 

 
