<s> import numpy as np <EOL> from scipy import interpolate <EOL> from scipy . stats import spearmanr <EOL> from . base import BaseEstimator , TransformerMixin , RegressorMixin <EOL> from . utils import as_float_array , check_array , check_consistent_length <EOL> from . utils import deprecated <EOL> from . utils . fixes import astype <EOL> from . _isotonic import _isotonic_regression , _make_unique <EOL> import warnings <EOL> import math <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def check_increasing ( x , y ) : <EOL> """<STR_LIT>""" <EOL> rho , _ = spearmanr ( x , y ) <EOL> increasing_bool = rho >= <NUM_LIT:0> <EOL> if rho not in [ - <NUM_LIT:1.0> , <NUM_LIT:1.0> ] : <EOL> F = <NUM_LIT:0.5> * math . log ( ( <NUM_LIT:1.> + rho ) / ( <NUM_LIT:1.> - rho ) ) <EOL> F_se = <NUM_LIT:1> / math . sqrt ( len ( x ) - <NUM_LIT:3> ) <EOL> rho_0 = math . tanh ( F - <NUM_LIT> * F_se ) <EOL> rho_1 = math . tanh ( F + <NUM_LIT> * F_se ) <EOL> if np . sign ( rho_0 ) != np . sign ( rho_1 ) : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return increasing_bool <EOL> def isotonic_regression ( y , sample_weight = None , y_min = None , y_max = None , <EOL> increasing = True ) : <EOL> """<STR_LIT>""" <EOL> y = np . asarray ( y , dtype = np . float64 ) <EOL> if sample_weight is None : <EOL> sample_weight = np . ones ( len ( y ) , dtype = y . dtype ) <EOL> else : <EOL> sample_weight = np . asarray ( sample_weight , dtype = np . float64 ) <EOL> if not increasing : <EOL> y = y [ : : - <NUM_LIT:1> ] <EOL> sample_weight = sample_weight [ : : - <NUM_LIT:1> ] <EOL> if y_min is not None or y_max is not None : <EOL> y = np . copy ( y ) <EOL> sample_weight = np . copy ( sample_weight ) <EOL> C = np . dot ( sample_weight , y * y ) * <NUM_LIT:10> <EOL> if y_min is not None : <EOL> y [ <NUM_LIT:0> ] = y_min <EOL> sample_weight [ <NUM_LIT:0> ] = C <EOL> if y_max is not None : <EOL> y [ - <NUM_LIT:1> ] = y_max <EOL> sample_weight [ - <NUM_LIT:1> ] = C <EOL> solution = np . empty ( len ( y ) ) <EOL> y_ = _isotonic_regression ( y , sample_weight , solution ) <EOL> if increasing : <EOL> return y_ <EOL> else : <EOL> return y_ [ : : - <NUM_LIT:1> ] <EOL> class IsotonicRegression ( BaseEstimator , TransformerMixin , RegressorMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , y_min = None , y_max = None , increasing = True , <EOL> out_of_bounds = '<STR_LIT>' ) : <EOL> self . y_min = y_min <EOL> self . y_max = y_max <EOL> self . increasing = increasing <EOL> self . out_of_bounds = out_of_bounds <EOL> @ property <EOL> @ deprecated ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def X_ ( self ) : <EOL> return self . _X_ <EOL> @ X_ . setter <EOL> def X_ ( self , value ) : <EOL> self . _X_ = value <EOL> @ X_ . deleter <EOL> def X_ ( self ) : <EOL> del self . _X_ <EOL> @ property <EOL> @ deprecated ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def y_ ( self ) : <EOL> return self . _y_ <EOL> @ y_ . setter <EOL> def y_ ( self , value ) : <EOL> self . _y_ = value <EOL> @ y_ . deleter <EOL> def y_ ( self ) : <EOL> del self . _y_ <EOL> def _check_fit_data ( self , X , y , sample_weight = None ) : <EOL> if len ( X . shape ) != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def _build_f ( self , X , y ) : <EOL> """<STR_LIT>""" <EOL> if self . out_of_bounds not in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> . format ( self . out_of_bounds ) ) <EOL> bounds_error = self . out_of_bounds == "<STR_LIT>" <EOL> if len ( y ) == <NUM_LIT:1> : <EOL> self . f_ = lambda x : y . repeat ( x . shape ) <EOL> else : <EOL> self . f_ = interpolate . interp1d ( X , y , kind = '<STR_LIT>' , <EOL> bounds_error = bounds_error ) <EOL> def _build_y ( self , X , y , sample_weight , trim_duplicates = True ) : <EOL> """<STR_LIT>""" <EOL> check_consistent_length ( X , y , sample_weight ) <EOL> X , y = [ check_array ( x , ensure_2d = False ) for x in [ X , y ] ] <EOL> y = as_float_array ( y ) <EOL> self . _check_fit_data ( X , y , sample_weight ) <EOL> if self . increasing == '<STR_LIT>' : <EOL> self . increasing_ = check_increasing ( X , y ) <EOL> else : <EOL> self . increasing_ = self . increasing <EOL> if sample_weight is not None : <EOL> sample_weight = check_array ( sample_weight , ensure_2d = False ) <EOL> mask = sample_weight > <NUM_LIT:0> <EOL> X , y , sample_weight = X [ mask ] , y [ mask ] , sample_weight [ mask ] <EOL> else : <EOL> sample_weight = np . ones ( len ( y ) ) <EOL> order = np . lexsort ( ( y , X ) ) <EOL> X , y , sample_weight = [ astype ( array [ order ] , np . float64 , copy = False ) <EOL> for array in [ X , y , sample_weight ] ] <EOL> unique_X , unique_y , unique_sample_weight = _make_unique ( <EOL> X , y , sample_weight ) <EOL> self . _X_ = X = unique_X <EOL> self . _y_ = y = isotonic_regression ( unique_y , unique_sample_weight , <EOL> self . y_min , self . y_max , <EOL> increasing = self . increasing_ ) <EOL> self . X_min_ , self . X_max_ = np . min ( X ) , np . max ( X ) <EOL> if trim_duplicates : <EOL> keep_data = np . ones ( ( len ( y ) , ) , dtype = bool ) <EOL> keep_data [ <NUM_LIT:1> : - <NUM_LIT:1> ] = np . logical_or ( <EOL> np . not_equal ( y [ <NUM_LIT:1> : - <NUM_LIT:1> ] , y [ : - <NUM_LIT:2> ] ) , <EOL> np . not_equal ( y [ <NUM_LIT:1> : - <NUM_LIT:1> ] , y [ <NUM_LIT:2> : ] ) <EOL> ) <EOL> return X [ keep_data ] , y [ keep_data ] <EOL> else : <EOL> return X , y <EOL> def fit ( self , X , y , sample_weight = None ) : <EOL> """<STR_LIT>""" <EOL> X , y = self . _build_y ( X , y , sample_weight ) <EOL> self . _necessary_X_ , self . _necessary_y_ = X , y <EOL> self . _build_f ( X , y ) <EOL> return self <EOL> def transform ( self , T ) : <EOL> """<STR_LIT>""" <EOL> T = as_float_array ( T ) <EOL> if len ( T . shape ) != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . out_of_bounds not in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> . format ( self . out_of_bounds ) ) <EOL> if self . out_of_bounds == "<STR_LIT>" : <EOL> T = np . clip ( T , self . X_min_ , self . X_max_ ) <EOL> return self . f_ ( T ) <EOL> def predict ( self , T ) : <EOL> """<STR_LIT>""" <EOL> return self . transform ( T ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = dict ( self . __dict__ ) <EOL> state . pop ( '<STR_LIT>' , None ) <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ . update ( state ) <EOL> if hasattr ( self , '<STR_LIT>' ) and hasattr ( self , '<STR_LIT>' ) : <EOL> self . _build_f ( self . _necessary_X_ , self . _necessary_y_ ) </s>
