<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from numpy . testing import assert_array_equal <EOL> from numpy . testing import assert_almost_equal <EOL> from numpy . testing import assert_equal <EOL> from nose . tools import assert_raises <EOL> from sklearn . utils import check_random_state <EOL> from sklearn . ensemble . gradient_boosting import BinomialDeviance <EOL> from sklearn . ensemble . gradient_boosting import LogOddsEstimator <EOL> from sklearn . ensemble . gradient_boosting import LeastSquaresError <EOL> from sklearn . ensemble . gradient_boosting import RegressionLossFunction <EOL> from sklearn . ensemble . gradient_boosting import LOSS_FUNCTIONS <EOL> from sklearn . ensemble . gradient_boosting import _weighted_percentile <EOL> def test_binomial_deviance ( ) : <EOL> bd = BinomialDeviance ( <NUM_LIT:2> ) <EOL> assert_equal ( bd ( np . array ( [ <NUM_LIT:0.0> ] ) , np . array ( [ <NUM_LIT:0.0> ] ) ) , <EOL> bd ( np . array ( [ <NUM_LIT:1.0> ] ) , np . array ( [ <NUM_LIT:0.0> ] ) ) ) <EOL> assert_almost_equal ( bd ( np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) , <EOL> <NUM_LIT:0.0> ) <EOL> assert_almost_equal ( bd ( np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) , <EOL> np . array ( [ <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> ] ) ) , <NUM_LIT:0> ) <EOL> alt_dev = lambda y , pred : np . mean ( np . logaddexp ( <NUM_LIT:0.0> , - <NUM_LIT> * <EOL> ( <NUM_LIT> * y - <NUM_LIT:1> ) * pred ) ) <EOL> test_data = [ ( np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) , np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) , <EOL> ( np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) , np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) , <EOL> ( np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) , <EOL> np . array ( [ - <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> ] ) ) , <EOL> ( np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) , <EOL> np . array ( [ - <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> ] ) ) ] <EOL> for datum in test_data : <EOL> assert_almost_equal ( bd ( * datum ) , alt_dev ( * datum ) ) <EOL> alt_ng = lambda y , pred : ( <NUM_LIT:2> * y - <NUM_LIT:1> ) / ( <NUM_LIT:1> + np . exp ( <NUM_LIT:2> * ( <NUM_LIT:2> * y - <NUM_LIT:1> ) * pred ) ) <EOL> for datum in test_data : <EOL> assert_almost_equal ( bd . negative_gradient ( * datum ) , alt_ng ( * datum ) ) <EOL> def test_log_odds_estimator ( ) : <EOL> est = LogOddsEstimator ( ) <EOL> assert_raises ( ValueError , est . fit , None , np . array ( [ <NUM_LIT:1> ] ) ) <EOL> est . fit ( None , np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:0.0> ] ) ) <EOL> assert_equal ( est . prior , <NUM_LIT:0.0> ) <EOL> assert_array_equal ( est . predict ( np . array ( [ [ <NUM_LIT:1.0> ] , [ <NUM_LIT:1.0> ] ] ) ) , <EOL> np . array ( [ [ <NUM_LIT:0.0> ] , [ <NUM_LIT:0.0> ] ] ) ) <EOL> def test_sample_weight_smoke ( ) : <EOL> rng = check_random_state ( <NUM_LIT> ) <EOL> y = rng . rand ( <NUM_LIT:100> ) <EOL> pred = rng . rand ( <NUM_LIT:100> ) <EOL> loss = LeastSquaresError ( <NUM_LIT:1> ) <EOL> loss_wo_sw = loss ( y , pred ) <EOL> loss_w_sw = loss ( y , pred , np . ones ( pred . shape [ <NUM_LIT:0> ] , dtype = np . float32 ) ) <EOL> assert_almost_equal ( loss_wo_sw , loss_w_sw ) <EOL> def test_sample_weight_init_estimators ( ) : <EOL> rng = check_random_state ( <NUM_LIT> ) <EOL> X = rng . rand ( <NUM_LIT:100> , <NUM_LIT:2> ) <EOL> sample_weight = np . ones ( <NUM_LIT:100> ) <EOL> reg_y = rng . rand ( <NUM_LIT:100> ) <EOL> clf_y = rng . randint ( <NUM_LIT:0> , <NUM_LIT:2> , size = <NUM_LIT:100> ) <EOL> for Loss in LOSS_FUNCTIONS . values ( ) : <EOL> if Loss is None : <EOL> continue <EOL> if issubclass ( Loss , RegressionLossFunction ) : <EOL> k = <NUM_LIT:1> <EOL> y = reg_y <EOL> else : <EOL> k = <NUM_LIT:2> <EOL> y = clf_y <EOL> if Loss . is_multi_class : <EOL> continue <EOL> loss = Loss ( k ) <EOL> init_est = loss . init_estimator ( ) <EOL> init_est . fit ( X , y ) <EOL> out = init_est . predict ( X ) <EOL> assert_equal ( out . shape , ( y . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> sw_init_est = loss . init_estimator ( ) <EOL> sw_init_est . fit ( X , y , sample_weight = sample_weight ) <EOL> sw_out = init_est . predict ( X ) <EOL> assert_equal ( sw_out . shape , ( y . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> assert_array_equal ( out , sw_out ) <EOL> def test_weighted_percentile ( ) : <EOL> y = np . empty ( <NUM_LIT> , dtype = np . float64 ) <EOL> y [ : <NUM_LIT:50> ] = <NUM_LIT:0> <EOL> y [ - <NUM_LIT> : ] = <NUM_LIT:2> <EOL> y [ - <NUM_LIT:1> ] = <NUM_LIT> <EOL> y [ <NUM_LIT:50> ] = <NUM_LIT:1> <EOL> sw = np . ones ( <NUM_LIT> , dtype = np . float64 ) <EOL> sw [ - <NUM_LIT:1> ] = <NUM_LIT:0.0> <EOL> score = _weighted_percentile ( y , sw , <NUM_LIT:50> ) <EOL> assert score == <NUM_LIT:1> <EOL> def test_weighted_percentile_equal ( ) : <EOL> y = np . empty ( <NUM_LIT> , dtype = np . float64 ) <EOL> y . fill ( <NUM_LIT:0.0> ) <EOL> sw = np . ones ( <NUM_LIT> , dtype = np . float64 ) <EOL> sw [ - <NUM_LIT:1> ] = <NUM_LIT:0.0> <EOL> score = _weighted_percentile ( y , sw , <NUM_LIT:50> ) <EOL> assert score == <NUM_LIT:0> <EOL> def test_weighted_percentile_zero_weight ( ) : <EOL> y = np . empty ( <NUM_LIT> , dtype = np . float64 ) <EOL> y . fill ( <NUM_LIT:1.0> ) <EOL> sw = np . ones ( <NUM_LIT> , dtype = np . float64 ) <EOL> sw . fill ( <NUM_LIT:0.0> ) <EOL> score = _weighted_percentile ( y , sw , <NUM_LIT:50> ) <EOL> assert score == <NUM_LIT:1.0> <EOL> def test_sample_weight_deviance ( ) : <EOL> rng = check_random_state ( <NUM_LIT> ) <EOL> X = rng . rand ( <NUM_LIT:100> , <NUM_LIT:2> ) <EOL> sample_weight = np . ones ( <NUM_LIT:100> ) <EOL> reg_y = rng . rand ( <NUM_LIT:100> ) <EOL> clf_y = rng . randint ( <NUM_LIT:0> , <NUM_LIT:2> , size = <NUM_LIT:100> ) <EOL> mclf_y = rng . randint ( <NUM_LIT:0> , <NUM_LIT:3> , size = <NUM_LIT:100> ) <EOL> for Loss in LOSS_FUNCTIONS . values ( ) : <EOL> if Loss is None : <EOL> continue <EOL> if issubclass ( Loss , RegressionLossFunction ) : <EOL> k = <NUM_LIT:1> <EOL> y = reg_y <EOL> p = reg_y <EOL> else : <EOL> k = <NUM_LIT:2> <EOL> y = clf_y <EOL> p = clf_y <EOL> if Loss . is_multi_class : <EOL> k = <NUM_LIT:3> <EOL> y = mclf_y <EOL> p = np . zeros ( ( y . shape [ <NUM_LIT:0> ] , k ) , dtype = np . float64 ) <EOL> for i in range ( k ) : <EOL> p [ : , i ] = y == i <EOL> loss = Loss ( k ) <EOL> deviance_w_w = loss ( y , p , sample_weight ) <EOL> deviance_wo_w = loss ( y , p ) <EOL> assert deviance_wo_w == deviance_w_w </s>
