77
88from sklearn .utils ._testing import assert_array_almost_equal
99
10+ from ..metrics import make_uplift_scorer
1011from ..metrics import uplift_curve , uplift_auc_score , perfect_uplift_curve
1112from ..metrics import qini_curve , qini_auc_score , perfect_qini_curve
1213from ..metrics import (uplift_at_k , response_rate_by_percentile ,
13- weighted_average_uplift , uplift_by_percentile , treatment_balance_curve )
14+ weighted_average_uplift , uplift_by_percentile , treatment_balance_curve , average_squared_deviation )
1415
1516
1617def make_predictions (binary ):
@@ -221,6 +222,12 @@ def test_perfect_qini_curve_hard():
221222
222223 assert_array_almost_equal (x_actual , np .array ([0. , 0. , 3. ]))
223224 assert_array_almost_equal (y_actual , np .array ([0.0 , 0.0 , 0.0 ]))
225+
226+ def test_perfect_qini_curve_error ():
227+ y_true , uplift , treatment = make_predictions (binary = True )
228+ with pytest .raises (TypeError ):
229+ perfect_qini_curve (y_true , treatment , negative_effect = 5 )
230+
224231
225232
226233def test_qini_auc_score ():
@@ -255,11 +262,33 @@ def test_qini_auc_score():
255262 treatment = [1 , 0 , 1 ]
256263 assert_array_almost_equal (qini_auc_score (y_true , uplift , treatment ), 0.75 )
257264
265+ def test_qini_auc_score_error ():
266+ y_true = [1 , 0 ]
267+ uplift = [0.1 , 0.3 ]
268+ treatment = [0 , 1 ]
269+ with pytest .raises (TypeError ):
270+ qini_auc_score (y_true , uplift , treatment , negative_effect = 5 )
271+
258272
259273def test_uplift_at_k ():
260274 y_true , uplift , treatment = make_predictions (binary = True )
261275
262276 assert_array_almost_equal (uplift_at_k (y_true , uplift , treatment , strategy = 'by_group' , k = 1 ), np .array ([0. ]))
277+ #assert_array_almost_equal(uplift_at_k(y_true, uplift, treatment, strategy='overall', k=2), np.array([0.]))
278+
279+ @pytest .mark .parametrize (
280+ "strategy, k" ,
281+ [
282+ ('new_strategy' , 1 ),
283+ ('by_group' , - 0.5 ),
284+ ('by_group' , '1' ),
285+ ('by_group' , 2 )
286+ ]
287+ )
288+ def test_uplift_at_k_errors (strategy , k ):
289+ y_true , uplift , treatment = make_predictions (binary = True )
290+ with pytest .raises (ValueError ):
291+ uplift_at_k (y_true , uplift , treatment , strategy , k )
263292
264293
265294@pytest .mark .parametrize (
@@ -277,6 +306,19 @@ def test_response_rate_by_percentile(strategy, group, response_rate):
277306 assert_array_almost_equal (response_rate_by_percentile (y_true , uplift , treatment , group , strategy , bins = 1 ),
278307 response_rate )
279308
309+ @pytest .mark .parametrize (
310+ "strategy, group, bins" ,
311+ [
312+ ('new_strategy' , 'control' , 1 ),
313+ ('by_group' , 'ctrl' , 1 ),
314+ ('by_group' , 'control' , 0.5 ),
315+ ('by_group' , 'control' , 9999 )
316+ ]
317+ )
318+ def test_response_rate_by_percentile_errors (strategy , group , bins ):
319+ y_true , uplift , treatment = make_predictions (binary = True )
320+ with pytest .raises (ValueError ):
321+ response_rate_by_percentile (y_true , uplift , treatment , group = group , strategy = strategy , bins = bins )
280322
281323@pytest .mark .parametrize (
282324 "strategy, weighted_average" ,
@@ -289,7 +331,21 @@ def test_weighted_average_uplift(strategy, weighted_average):
289331 y_true , uplift , treatment = make_predictions (binary = True )
290332
291333 assert_array_almost_equal (weighted_average_uplift (y_true , uplift , treatment , strategy , bins = 1 ), weighted_average )
334+
292335
336+ @pytest .mark .parametrize (
337+ "strategy, bins" ,
338+ [
339+ ('new_strategy' , 1 ),
340+ ('by_group' , 0.5 ),
341+ ('by_group' , 9999 )
342+ ]
343+ )
344+ def test_weighted_average_uplift_errors (strategy , bins ):
345+ y_true , uplift , treatment = make_predictions (binary = True )
346+ with pytest .raises (ValueError ):
347+ weighted_average_uplift (y_true , uplift , treatment , strategy = strategy , bins = bins )
348+
293349
294350@pytest .mark .parametrize (
295351 "strategy, bins, std, total, string_percentiles, data" ,
@@ -307,11 +363,68 @@ def test_uplift_by_percentile(strategy, bins, std, total, string_percentiles, da
307363
308364 assert_array_almost_equal (
309365 uplift_by_percentile (y_true , uplift , treatment , strategy , bins , std , total , string_percentiles ), data )
366+
367+ @pytest .mark .parametrize (
368+ "strategy, bins, std, total, string_percentiles" ,
369+ [
370+ ('new_strategy' , 1 , True , True , True ),
371+ ('by_group' , 0.5 , True , True , True ),
372+ ('by_group' , 9999 , True , True , True ),
373+ ('by_group' , 1 , 2 , True , True ),
374+ ('by_group' , 1 , True , True , 2 ),
375+ ('by_group' , 1 , True , 2 , True )
376+ ]
377+ )
378+ def test_uplift_by_percentile_errors (strategy , bins , std , total , string_percentiles ):
379+ y_true , uplift , treatment = make_predictions (binary = True )
380+ with pytest .raises (ValueError ):
381+ uplift_by_percentile (y_true , uplift , treatment , strategy , bins , std , total , string_percentiles )
310382
311383
312384def test_treatment_balance_curve ():
313385 y_true , uplift , treatment = make_predictions (binary = True )
314386
315387 idx , balance = treatment_balance_curve (uplift , treatment , winsize = 2 )
316388 assert_array_almost_equal (idx , np .array ([1. , 100. ]))
317- assert_array_almost_equal (balance , np .array ([1. , 0.5 ]))
389+ assert_array_almost_equal (balance , np .array ([1. , 0.5 ]))
390+
391+ @pytest .mark .parametrize (
392+ "strategy" ,
393+ [
394+ ('overall' ),
395+ ('by_group' )
396+ ]
397+ )
398+ def test_average_squared_deviation (strategy ):
399+ y_true , uplift , treatment = make_predictions (binary = True )
400+ assert (average_squared_deviation (y_true , uplift , treatment , y_true , uplift , treatment , strategy , bins = 1 ) == 0 )
401+
402+ @pytest .mark .parametrize (
403+ "strategy, bins" ,
404+ [
405+ ('new_strategy' , 1 ),
406+ ('by_group' , 0.5 ),
407+ ('by_group' , 9999 )
408+ ]
409+ )
410+ def test_average_squared_deviation_errors (strategy , bins ):
411+ y_true , uplift , treatment = make_predictions (binary = True )
412+ with pytest .raises (ValueError ):
413+ average_squared_deviation (y_true , uplift , treatment , y_true , uplift , treatment , strategy = strategy , bins = bins )
414+
415+ def test_metric_name_error ():
416+ with pytest .raises (ValueError ):
417+ make_uplift_scorer ('new_scorer' , [0 , 1 ])
418+
419+ def test_make_scorer_error ():
420+ with pytest .raises (TypeError ):
421+ make_uplift_scorer ('qini_auc_score' , [])
422+
423+
424+
425+
426+
427+
428+
429+
430+
0 commit comments