#NEWLINE# @ ignore_warnings #NEWLINE# def check_sample_weight_invariance( name , metric , y1 , y2 ) : #NEWLINE# #INDENT# rng = np . random . RandomState ( 0 ) #NEWLINE# sample_weight = rng . randint ( 1 , 10 , size = len ( y1 ) ) #NEWLINE# unweighted_score = metric ( y1 , y2 , sample_weight = None ) #NEWLINE# assert_almost_equal ( unweighted_score , metric ( y1 , y2 , sample_weight = np . ones ( shape = len ( y1 ) ) ) , err_msg = ( 'For %s sample_weight=None is not equivalent to sample_weight=ones' % name ) ) #NEWLINE# weighted_score = metric ( y1 , y2 , sample_weight = sample_weight ) #NEWLINE# assert_not_equal ( unweighted_score , weighted_score , msg = ( 'Unweighted and weighted scores are unexpectedly equal (%f) for %s' % ( weighted_score , name ) ) ) #NEWLINE# weighted_score_list = metric ( y1 , y2 , sample_weight = sample_weight . tolist ( ) ) #NEWLINE# assert_almost_equal ( weighted_score , weighted_score_list , err_msg = ( 'Weighted scores for array and list sample_weight input are not equal (%f != %f) for %s' % ( weighted_score , weighted_score_list , name ) ) ) #NEWLINE# repeat_weighted_score = metric ( np . repeat ( y1 , sample_weight , axis = 0 ) , np . repeat ( y2 , sample_weight , axis = 0 ) , sample_weight = None ) #NEWLINE# assert_almost_equal ( weighted_score , repeat_weighted_score , err_msg = ( 'Weighting %s is not equal to repeating samples' % name ) ) #NEWLINE# sample_weight_subset = sample_weight [ 1 : : 2 ] #NEWLINE# sample_weight_zeroed = np . copy ( sample_weight ) #NEWLINE# sample_weight_zeroed [ : : 2 ] = 0 #NEWLINE# y1_subset = y1 [ 1 : : 2 ] #NEWLINE# y2_subset = y2 [ 1 : : 2 ] #NEWLINE# weighted_score_subset = metric ( y1_subset , y2_subset , sample_weight = sample_weight_subset ) #NEWLINE# weighted_score_zeroed = metric ( y1 , y2 , sample_weight = sample_weight_zeroed ) #NEWLINE# assert_almost_equal ( weighted_score_subset , weighted_score_zeroed , err_msg = ( 'Zeroing weights does not give the same result as removing the corresponding samples (%f != %f) for %s' % ( weighted_score_zeroed , weighted_score_subset , name ) ) ) #NEWLINE# if ( not name . startswith ( 'unnormalized' ) ) : #NEWLINE# #INDENT# for scaling in [ 2 , 0.3 ] : #NEWLINE# #INDENT# assert_almost_equal ( weighted_score , metric ( y1 , y2 , sample_weight = ( sample_weight * scaling ) ) , err_msg = ( '%s sample_weight is not invariant under scaling' % name ) ) #NEWLINE# #UNINDENT# #UNINDENT# assert_raises ( Exception , metric , y1 , y2 , sample_weight = np . hstack ( [ sample_weight , sample_weight ] ) )