<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , absolute_import <EOL> import os <EOL> import sys <EOL> from contextlib import contextmanager <EOL> import numpy as np <EOL> from numpy . testing import assert_array_equal , assert_array_less <EOL> from numpy . testing import assert_array_almost_equal , assert_warns <EOL> from scipy . linalg import norm <EOL> from scipy . optimize import fmin_bfgs <EOL> from nose . tools import raises , assert_almost_equal <EOL> from sklearn . exceptions import ConvergenceWarning <EOL> from sklearn . linear_model import LinearRegression , TheilSenRegressor <EOL> from sklearn . linear_model . theil_sen import _spatial_median , _breakdown_point <EOL> from sklearn . linear_model . theil_sen import _modified_weiszfeld_step <EOL> from sklearn . utils . testing import assert_greater , assert_less <EOL> @ contextmanager <EOL> def no_stdout_stderr ( ) : <EOL> old_stdout = sys . stdout <EOL> old_stderr = sys . stderr <EOL> with open ( os . devnull , '<STR_LIT:w>' ) as devnull : <EOL> sys . stdout = devnull <EOL> sys . stderr = devnull <EOL> yield <EOL> devnull . flush ( ) <EOL> sys . stdout = old_stdout <EOL> sys . stderr = old_stderr <EOL> def gen_toy_problem_1d ( intercept = True ) : <EOL> random_state = np . random . RandomState ( <NUM_LIT:0> ) <EOL> w = <NUM_LIT> <EOL> if intercept : <EOL> c = <NUM_LIT> <EOL> n_samples = <NUM_LIT:50> <EOL> else : <EOL> c = <NUM_LIT:0.1> <EOL> n_samples = <NUM_LIT:100> <EOL> x = random_state . normal ( size = n_samples ) <EOL> noise = <NUM_LIT:0.1> * random_state . normal ( size = n_samples ) <EOL> y = w * x + c + noise <EOL> if intercept : <EOL> x [ <NUM_LIT> ] , y [ <NUM_LIT> ] = ( - <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> x [ <NUM_LIT> ] , y [ <NUM_LIT> ] = ( - <NUM_LIT> , <NUM_LIT:8> ) <EOL> x [ <NUM_LIT> ] , y [ <NUM_LIT> ] = ( <NUM_LIT> , <NUM_LIT:1> ) <EOL> x [ <NUM_LIT> ] , y [ <NUM_LIT> ] = ( <NUM_LIT> , <NUM_LIT:2> ) <EOL> else : <EOL> x [ <NUM_LIT> ] , y [ <NUM_LIT> ] = ( - <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> x [ <NUM_LIT> ] , y [ <NUM_LIT> ] = ( - <NUM_LIT> , <NUM_LIT:8> ) <EOL> x [ <NUM_LIT> ] , y [ <NUM_LIT> ] = ( <NUM_LIT> , <NUM_LIT:1> ) <EOL> x [ <NUM_LIT> ] , y [ <NUM_LIT> ] = ( <NUM_LIT> , <NUM_LIT:2> ) <EOL> x [ <NUM_LIT> ] , y [ <NUM_LIT> ] = ( <NUM_LIT> , - <NUM_LIT:7> ) <EOL> return x [ : , np . newaxis ] , y , w , c <EOL> def gen_toy_problem_2d ( ) : <EOL> random_state = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_samples = <NUM_LIT:100> <EOL> X = random_state . normal ( size = ( n_samples , <NUM_LIT:2> ) ) <EOL> w = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> c = <NUM_LIT:1.> <EOL> noise = <NUM_LIT:0.1> * random_state . normal ( size = n_samples ) <EOL> y = np . dot ( X , w ) + c + noise <EOL> n_outliers = n_samples // <NUM_LIT:10> <EOL> ix = random_state . randint ( <NUM_LIT:0> , n_samples , size = n_outliers ) <EOL> y [ ix ] = <NUM_LIT:50> * random_state . normal ( size = n_outliers ) <EOL> return X , y , w , c <EOL> def gen_toy_problem_4d ( ) : <EOL> random_state = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_samples = <NUM_LIT> <EOL> X = random_state . normal ( size = ( n_samples , <NUM_LIT:4> ) ) <EOL> w = np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> c = <NUM_LIT:1.> <EOL> noise = <NUM_LIT:0.1> * random_state . normal ( size = n_samples ) <EOL> y = np . dot ( X , w ) + c + noise <EOL> n_outliers = n_samples // <NUM_LIT:10> <EOL> ix = random_state . randint ( <NUM_LIT:0> , n_samples , size = n_outliers ) <EOL> y [ ix ] = <NUM_LIT:50> * random_state . normal ( size = n_outliers ) <EOL> return X , y , w , c <EOL> def test_modweiszfeld_step_1d ( ) : <EOL> X = np . array ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> ] ) . reshape ( <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> median = <NUM_LIT> <EOL> new_y = _modified_weiszfeld_step ( X , median ) <EOL> assert_array_almost_equal ( new_y , median ) <EOL> y = <NUM_LIT> <EOL> new_y = _modified_weiszfeld_step ( X , y ) <EOL> assert_array_less ( median , new_y ) <EOL> assert_array_less ( new_y , y ) <EOL> y = <NUM_LIT> <EOL> new_y = _modified_weiszfeld_step ( X , y ) <EOL> assert_array_less ( median , new_y ) <EOL> assert_array_less ( new_y , y ) <EOL> X = np . array ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> ] ) . reshape ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> y = X [ <NUM_LIT:0> , ] <EOL> new_y = _modified_weiszfeld_step ( X , y ) <EOL> assert_array_equal ( y , new_y ) <EOL> def test_modweiszfeld_step_2d ( ) : <EOL> X = np . array ( [ <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:1.> ] ) . reshape ( <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> y = np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ) <EOL> new_y = _modified_weiszfeld_step ( X , y ) <EOL> assert_array_almost_equal ( new_y , np . array ( [ <NUM_LIT:1> / <NUM_LIT:3> , <NUM_LIT:2> / <NUM_LIT:3> ] ) ) <EOL> new_y = _modified_weiszfeld_step ( X , new_y ) <EOL> assert_array_almost_equal ( new_y , np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> y = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> new_y = _modified_weiszfeld_step ( X , y ) <EOL> assert_array_almost_equal ( new_y , y ) <EOL> def test_spatial_median_1d ( ) : <EOL> X = np . array ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> ] ) . reshape ( <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> true_median = <NUM_LIT> <EOL> _ , median = _spatial_median ( X ) <EOL> assert_array_almost_equal ( median , true_median ) <EOL> random_state = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = random_state . randint ( <NUM_LIT:100> , size = ( <NUM_LIT:1000> , <NUM_LIT:1> ) ) <EOL> true_median = np . median ( X . ravel ( ) ) <EOL> _ , median = _spatial_median ( X ) <EOL> assert_array_equal ( median , true_median ) <EOL> def test_spatial_median_2d ( ) : <EOL> X = np . array ( [ <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:1.> ] ) . reshape ( <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> _ , median = _spatial_median ( X , max_iter = <NUM_LIT:100> , tol = <NUM_LIT> ) <EOL> def cost_func ( y ) : <EOL> dists = np . array ( [ norm ( x - y ) for x in X ] ) <EOL> return np . sum ( dists ) <EOL> fermat_weber = fmin_bfgs ( cost_func , median , disp = False ) <EOL> assert_array_almost_equal ( median , fermat_weber ) <EOL> assert_warns ( ConvergenceWarning , _spatial_median , X , max_iter = <NUM_LIT:30> , tol = <NUM_LIT:0.> ) <EOL> def test_theil_sen_1d ( ) : <EOL> X , y , w , c = gen_toy_problem_1d ( ) <EOL> lstq = LinearRegression ( ) . fit ( X , y ) <EOL> assert_greater ( np . abs ( lstq . coef_ - w ) , <NUM_LIT> ) <EOL> theil_sen = TheilSenRegressor ( random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> assert_array_almost_equal ( theil_sen . coef_ , w , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( theil_sen . intercept_ , c , <NUM_LIT:1> ) <EOL> def test_theil_sen_1d_no_intercept ( ) : <EOL> X , y , w , c = gen_toy_problem_1d ( intercept = False ) <EOL> lstq = LinearRegression ( fit_intercept = False ) . fit ( X , y ) <EOL> assert_greater ( np . abs ( lstq . coef_ - w - c ) , <NUM_LIT:0.5> ) <EOL> theil_sen = TheilSenRegressor ( fit_intercept = False , <EOL> random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> assert_array_almost_equal ( theil_sen . coef_ , w + c , <NUM_LIT:1> ) <EOL> assert_almost_equal ( theil_sen . intercept_ , <NUM_LIT:0.> ) <EOL> def test_theil_sen_2d ( ) : <EOL> X , y , w , c = gen_toy_problem_2d ( ) <EOL> lstq = LinearRegression ( ) . fit ( X , y ) <EOL> assert_greater ( norm ( lstq . coef_ - w ) , <NUM_LIT:1.0> ) <EOL> theil_sen = TheilSenRegressor ( max_subpopulation = <NUM_LIT> , <EOL> random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> assert_array_almost_equal ( theil_sen . coef_ , w , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( theil_sen . intercept_ , c , <NUM_LIT:1> ) <EOL> def test_calc_breakdown_point ( ) : <EOL> bp = _breakdown_point ( <NUM_LIT> , <NUM_LIT:2> ) <EOL> assert_less ( np . abs ( bp - <NUM_LIT:1> + <NUM_LIT:1> / ( np . sqrt ( <NUM_LIT:2> ) ) ) , <NUM_LIT> ) <EOL> @ raises ( ValueError ) <EOL> def test_checksubparams_negative_subpopulation ( ) : <EOL> X , y , w , c = gen_toy_problem_1d ( ) <EOL> TheilSenRegressor ( max_subpopulation = - <NUM_LIT:1> , random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> @ raises ( ValueError ) <EOL> def test_checksubparams_too_few_subsamples ( ) : <EOL> X , y , w , c = gen_toy_problem_1d ( ) <EOL> TheilSenRegressor ( n_subsamples = <NUM_LIT:1> , random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> @ raises ( ValueError ) <EOL> def test_checksubparams_too_many_subsamples ( ) : <EOL> X , y , w , c = gen_toy_problem_1d ( ) <EOL> TheilSenRegressor ( n_subsamples = <NUM_LIT> , random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> @ raises ( ValueError ) <EOL> def test_checksubparams_n_subsamples_if_less_samples_than_features ( ) : <EOL> random_state = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_samples , n_features = <NUM_LIT:10> , <NUM_LIT:20> <EOL> X = random_state . normal ( size = ( n_samples , n_features ) ) <EOL> y = random_state . normal ( size = n_samples ) <EOL> TheilSenRegressor ( n_subsamples = <NUM_LIT:9> , random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> def test_subpopulation ( ) : <EOL> X , y , w , c = gen_toy_problem_4d ( ) <EOL> theil_sen = TheilSenRegressor ( max_subpopulation = <NUM_LIT> , <EOL> random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> assert_array_almost_equal ( theil_sen . coef_ , w , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( theil_sen . intercept_ , c , <NUM_LIT:1> ) <EOL> def test_subsamples ( ) : <EOL> X , y , w , c = gen_toy_problem_4d ( ) <EOL> theil_sen = TheilSenRegressor ( n_subsamples = X . shape [ <NUM_LIT:0> ] , <EOL> random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> lstq = LinearRegression ( ) . fit ( X , y ) <EOL> assert_array_almost_equal ( theil_sen . coef_ , lstq . coef_ , <NUM_LIT:9> ) <EOL> def test_verbosity ( ) : <EOL> X , y , w , c = gen_toy_problem_1d ( ) <EOL> with no_stdout_stderr ( ) : <EOL> TheilSenRegressor ( verbose = True , random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> TheilSenRegressor ( verbose = True , <EOL> max_subpopulation = <NUM_LIT:10> , <EOL> random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> def test_theil_sen_parallel ( ) : <EOL> X , y , w , c = gen_toy_problem_2d ( ) <EOL> lstq = LinearRegression ( ) . fit ( X , y ) <EOL> assert_greater ( norm ( lstq . coef_ - w ) , <NUM_LIT:1.0> ) <EOL> theil_sen = TheilSenRegressor ( n_jobs = - <NUM_LIT:1> , <EOL> random_state = <NUM_LIT:0> , <EOL> max_subpopulation = <NUM_LIT> ) . fit ( X , y ) <EOL> assert_array_almost_equal ( theil_sen . coef_ , w , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( theil_sen . intercept_ , c , <NUM_LIT:1> ) <EOL> def test_less_samples_than_features ( ) : <EOL> random_state = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_samples , n_features = <NUM_LIT:10> , <NUM_LIT:20> <EOL> X = random_state . normal ( size = ( n_samples , n_features ) ) <EOL> y = random_state . normal ( size = n_samples ) <EOL> theil_sen = TheilSenRegressor ( fit_intercept = False , <EOL> random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> lstq = LinearRegression ( fit_intercept = False ) . fit ( X , y ) <EOL> assert_array_almost_equal ( theil_sen . coef_ , lstq . coef_ , <NUM_LIT:12> ) <EOL> theil_sen = TheilSenRegressor ( fit_intercept = True , random_state = <NUM_LIT:0> ) . fit ( X , y ) <EOL> y_pred = theil_sen . predict ( X ) <EOL> assert_array_almost_equal ( y_pred , y , <NUM_LIT:12> ) </s>
