<s> """<STR_LIT>""" <EOL> from abc import ABCMeta , abstractmethod <EOL> import numpy as np <EOL> from . import ( r2_score , median_absolute_error , mean_absolute_error , <EOL> mean_squared_error , accuracy_score , f1_score , <EOL> roc_auc_score , average_precision_score , <EOL> precision_score , recall_score , log_loss ) <EOL> from . cluster import adjusted_rand_score <EOL> from . . utils . multiclass import type_of_target <EOL> from . . externals import six <EOL> from . . base import is_regressor <EOL> class _BaseScorer ( six . with_metaclass ( ABCMeta , object ) ) : <EOL> def __init__ ( self , score_func , sign , kwargs ) : <EOL> self . _kwargs = kwargs <EOL> self . _score_func = score_func <EOL> self . _sign = sign <EOL> @ abstractmethod <EOL> def __call__ ( self , estimator , X , y , sample_weight = None ) : <EOL> pass <EOL> def __repr__ ( self ) : <EOL> kwargs_string = "<STR_LIT>" . join ( [ "<STR_LIT>" % ( str ( k ) , str ( v ) ) <EOL> for k , v in self . _kwargs . items ( ) ] ) <EOL> return ( "<STR_LIT>" <EOL> % ( self . _score_func . __name__ , <EOL> "<STR_LIT>" if self . _sign > <NUM_LIT:0> else "<STR_LIT>" , <EOL> self . _factory_args ( ) , kwargs_string ) ) <EOL> def _factory_args ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> class _PredictScorer ( _BaseScorer ) : <EOL> def __call__ ( self , estimator , X , y_true , sample_weight = None ) : <EOL> """<STR_LIT>""" <EOL> y_pred = estimator . predict ( X ) <EOL> if sample_weight is not None : <EOL> return self . _sign * self . _score_func ( y_true , y_pred , <EOL> sample_weight = sample_weight , <EOL> ** self . _kwargs ) <EOL> else : <EOL> return self . _sign * self . _score_func ( y_true , y_pred , <EOL> ** self . _kwargs ) <EOL> class _ProbaScorer ( _BaseScorer ) : <EOL> def __call__ ( self , clf , X , y , sample_weight = None ) : <EOL> """<STR_LIT>""" <EOL> y_pred = clf . predict_proba ( X ) <EOL> if sample_weight is not None : <EOL> return self . _sign * self . _score_func ( y , y_pred , <EOL> sample_weight = sample_weight , <EOL> ** self . _kwargs ) <EOL> else : <EOL> return self . _sign * self . _score_func ( y , y_pred , ** self . _kwargs ) <EOL> def _factory_args ( self ) : <EOL> return "<STR_LIT>" <EOL> class _ThresholdScorer ( _BaseScorer ) : <EOL> def __call__ ( self , clf , X , y , sample_weight = None ) : <EOL> """<STR_LIT>""" <EOL> y_type = type_of_target ( y ) <EOL> if y_type not in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( y_type ) ) <EOL> if is_regressor ( clf ) : <EOL> y_pred = clf . predict ( X ) <EOL> else : <EOL> try : <EOL> y_pred = clf . decision_function ( X ) <EOL> if isinstance ( y_pred , list ) : <EOL> y_pred = np . vstack ( p for p in y_pred ) . T <EOL> except ( NotImplementedError , AttributeError ) : <EOL> y_pred = clf . predict_proba ( X ) <EOL> if y_type == "<STR_LIT>" : <EOL> y_pred = y_pred [ : , <NUM_LIT:1> ] <EOL> elif isinstance ( y_pred , list ) : <EOL> y_pred = np . vstack ( [ p [ : , - <NUM_LIT:1> ] for p in y_pred ] ) . T <EOL> if sample_weight is not None : <EOL> return self . _sign * self . _score_func ( y , y_pred , <EOL> sample_weight = sample_weight , <EOL> ** self . _kwargs ) <EOL> else : <EOL> return self . _sign * self . _score_func ( y , y_pred , ** self . _kwargs ) <EOL> def _factory_args ( self ) : <EOL> return "<STR_LIT>" <EOL> def get_scorer ( scoring ) : <EOL> if isinstance ( scoring , six . string_types ) : <EOL> try : <EOL> scorer = SCORERS [ scoring ] <EOL> except KeyError : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( scoring , sorted ( SCORERS . keys ( ) ) ) ) <EOL> else : <EOL> scorer = scoring <EOL> return scorer <EOL> def _passthrough_scorer ( estimator , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return estimator . score ( * args , ** kwargs ) <EOL> def check_scoring ( estimator , scoring = None , allow_none = False ) : <EOL> """<STR_LIT>""" <EOL> has_scoring = scoring is not None <EOL> if not hasattr ( estimator , '<STR_LIT>' ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % estimator ) <EOL> elif has_scoring : <EOL> return get_scorer ( scoring ) <EOL> elif hasattr ( estimator , '<STR_LIT>' ) : <EOL> return _passthrough_scorer <EOL> elif allow_none : <EOL> return None <EOL> else : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % estimator ) <EOL> def make_scorer ( score_func , greater_is_better = True , needs_proba = False , <EOL> needs_threshold = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> sign = <NUM_LIT:1> if greater_is_better else - <NUM_LIT:1> <EOL> if needs_proba and needs_threshold : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if needs_proba : <EOL> cls = _ProbaScorer <EOL> elif needs_threshold : <EOL> cls = _ThresholdScorer <EOL> else : <EOL> cls = _PredictScorer <EOL> return cls ( score_func , sign , kwargs ) <EOL> r2_scorer = make_scorer ( r2_score ) <EOL> mean_squared_error_scorer = make_scorer ( mean_squared_error , <EOL> greater_is_better = False ) <EOL> mean_absolute_error_scorer = make_scorer ( mean_absolute_error , <EOL> greater_is_better = False ) <EOL> median_absolute_error_scorer = make_scorer ( median_absolute_error , <EOL> greater_is_better = False ) <EOL> accuracy_scorer = make_scorer ( accuracy_score ) <EOL> f1_scorer = make_scorer ( f1_score ) <EOL> roc_auc_scorer = make_scorer ( roc_auc_score , greater_is_better = True , <EOL> needs_threshold = True ) <EOL> average_precision_scorer = make_scorer ( average_precision_score , <EOL> needs_threshold = True ) <EOL> precision_scorer = make_scorer ( precision_score ) <EOL> recall_scorer = make_scorer ( recall_score ) <EOL> log_loss_scorer = make_scorer ( log_loss , greater_is_better = False , <EOL> needs_proba = True ) <EOL> adjusted_rand_scorer = make_scorer ( adjusted_rand_score ) <EOL> SCORERS = dict ( r2 = r2_scorer , <EOL> median_absolute_error = median_absolute_error_scorer , <EOL> mean_absolute_error = mean_absolute_error_scorer , <EOL> mean_squared_error = mean_squared_error_scorer , <EOL> accuracy = accuracy_scorer , roc_auc = roc_auc_scorer , <EOL> average_precision = average_precision_scorer , <EOL> log_loss = log_loss_scorer , <EOL> adjusted_rand_score = adjusted_rand_scorer ) <EOL> for name , metric in [ ( '<STR_LIT>' , precision_score ) , <EOL> ( '<STR_LIT>' , recall_score ) , ( '<STR_LIT>' , f1_score ) ] : <EOL> SCORERS [ name ] = make_scorer ( metric ) <EOL> for average in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> qualified_name = '<STR_LIT>' . format ( name , average ) <EOL> SCORERS [ qualified_name ] = make_scorer ( metric , pos_label = None , <EOL> average = average ) </s>
