<s> import io <EOL> import time <EOL> from datetime import datetime <EOL> from sklearn . preprocessing import RobustScaler <EOL> from . import util <EOL> from . scorer_model import MLScorerModel <EOL> from . test_statistics import ( accuracy , precision , precision_recall , recall , <EOL> roc , table ) <EOL> class ScikitLearnClassifier ( MLScorerModel ) : <EOL> def __init__ ( self , features , classifier_model , version = None , <EOL> balanced_sample = False , balanced_sample_weight = False , <EOL> scale = False , center = False , <EOL> test_statistics = None ) : <EOL> super ( ) . __init__ ( features , version = version ) <EOL> self . classifier_model = classifier_model <EOL> self . balanced_sample = balanced_sample <EOL> self . balanced_sample_weight = balanced_sample_weight <EOL> if scale or center : <EOL> self . scaler = RobustScaler ( with_centering = center , <EOL> with_scaling = scale ) <EOL> else : <EOL> self . scaler = None <EOL> self . test_statistics = test_statistics <EOL> self . test_stats = None <EOL> self . params = { <EOL> '<STR_LIT>' : balanced_sample , <EOL> '<STR_LIT>' : balanced_sample_weight , <EOL> '<STR_LIT>' : scale , <EOL> '<STR_LIT>' : center <EOL> } <EOL> def __getattr__ ( self , attr ) : <EOL> if attr is "<STR_LIT>" : <EOL> return None <EOL> elif attr is "<STR_LIT>" : <EOL> return None <EOL> else : <EOL> raise AttributeError ( attr ) <EOL> def train ( self , values_labels , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> start = time . time ( ) <EOL> if self . scaler is not None : <EOL> values , labels = zip ( * values_labels ) <EOL> values = self . scaler . fit_transform ( values ) <EOL> values_labels = zip ( values , labels ) <EOL> if self . balanced_sample : <EOL> values_labels = util . balance_sample ( values_labels ) <EOL> values , labels = zip ( * values_labels ) <EOL> if self . balanced_sample_weight : <EOL> sample_weight = util . balance_sample_weights ( labels ) <EOL> else : <EOL> sample_weight = None <EOL> self . classifier_model . fit ( values , labels , sample_weight = sample_weight , <EOL> ** kwargs ) <EOL> self . trained = time . time ( ) <EOL> return { <EOL> '<STR_LIT>' : time . time ( ) - start <EOL> } <EOL> def score ( self , feature_values ) : <EOL> """<STR_LIT>""" <EOL> if self . scaler is not None : <EOL> feature_values = self . scaler . transform ( [ feature_values ] ) [ <NUM_LIT:0> ] <EOL> prediction = self . classifier_model . predict ( [ feature_values ] ) [ <NUM_LIT:0> ] <EOL> labels = self . classifier_model . classes_ <EOL> probas = self . classifier_model . predict_proba ( [ feature_values ] ) [ <NUM_LIT:0> ] <EOL> probability = { label : proba for label , proba in zip ( labels , probas ) } <EOL> doc = { <EOL> '<STR_LIT>' : prediction , <EOL> '<STR_LIT>' : probability <EOL> } <EOL> return util . normalize_json ( doc ) <EOL> def test ( self , values_labels , test_statistics = None , store_stats = True ) : <EOL> """<STR_LIT>""" <EOL> values , labels = zip ( * values_labels ) <EOL> test_statistics = test_statistics or self . test_statistics or [ table ( ) , accuracy ( ) , precision ( ) , recall ( ) , <EOL> roc ( ) , precision_recall ( ) ] <EOL> scores = [ self . score ( feature_values ) for feature_values in values ] <EOL> if self . scaler is not None : <EOL> values = self . scaler . transform ( values ) <EOL> test_stats = { } <EOL> for statistic in test_statistics : <EOL> test_stats [ statistic ] = statistic . score ( scores , labels ) <EOL> if store_stats : <EOL> self . test_statistics = test_statistics <EOL> self . test_stats = test_stats <EOL> return test_stats <EOL> def info ( self ) : <EOL> params = { } <EOL> params . update ( self . params or { } ) <EOL> params . update ( self . classifier_model . get_params ( ) ) <EOL> return util . normalize_json ( { <EOL> '<STR_LIT:type>' : self . __class__ . __name__ , <EOL> '<STR_LIT>' : params , <EOL> '<STR_LIT:version>' : self . version , <EOL> '<STR_LIT>' : self . trained , <EOL> '<STR_LIT>' : self . test_stats <EOL> } ) <EOL> def format_info ( self , format = "<STR_LIT:str>" ) : <EOL> if format == "<STR_LIT:str>" : <EOL> return self . format_info_str ( ) <EOL> elif format == "<STR_LIT>" : <EOL> return self . format_info_json ( ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> . format ( format , self . __class__ . __name__ ) ) <EOL> def format_info_str ( self , format = "<STR_LIT:str>" ) : <EOL> info = self . info ( ) <EOL> formatted = io . StringIO ( ) <EOL> formatted . write ( "<STR_LIT>" ) <EOL> formatted . write ( "<STR_LIT>" . format ( info . get ( '<STR_LIT:type>' ) ) ) <EOL> formatted . write ( "<STR_LIT>" <EOL> . format ( util . format_params ( info . get ( '<STR_LIT>' ) ) ) ) <EOL> formatted . write ( "<STR_LIT>" . format ( info . get ( '<STR_LIT:version>' ) ) ) <EOL> if isinstance ( info [ '<STR_LIT>' ] , float ) : <EOL> date_string = datetime . fromtimestamp ( info [ '<STR_LIT>' ] ) . isoformat ( ) <EOL> formatted . write ( "<STR_LIT>" . format ( date_string ) ) <EOL> else : <EOL> formatted . write ( "<STR_LIT>" . format ( info . get ( '<STR_LIT>' ) ) ) <EOL> formatted . write ( "<STR_LIT:\n>" ) <EOL> formatted . write ( self . format_stats_str ( ) ) <EOL> return formatted . getvalue ( ) <EOL> def format_stats_str ( self ) : <EOL> if self . test_stats is None : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT:\n>" . join ( stat . format ( self . test_stats [ stat ] ) for stat in <EOL> self . test_statistics ) <EOL> def format_info_json ( self ) : <EOL> params = { } <EOL> params . update ( self . params or { } ) <EOL> params . update ( self . classifier_model . get_params ( ) ) <EOL> test_stats = { } <EOL> if self . test_stats is not None : <EOL> for test_stat in self . test_statistics : <EOL> stats = self . test_stats [ test_stat ] <EOL> test_stats [ str ( test_stat ) ] = test_stat . format ( stats , format = "<STR_LIT>" ) <EOL> return util . normalize_json ( { <EOL> '<STR_LIT:type>' : self . __class__ . __name__ , <EOL> '<STR_LIT>' : params , <EOL> '<STR_LIT:version>' : self . version , <EOL> '<STR_LIT>' : self . trained , <EOL> '<STR_LIT>' : test_stats <EOL> } ) </s>
