<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> class PUAdapter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , estimator , hold_out_ratio = <NUM_LIT:0.1> , precomputed_kernel = False ) : <EOL> """<STR_LIT>""" <EOL> self . estimator = estimator <EOL> self . c = <NUM_LIT:1.0> <EOL> self . hold_out_ratio = hold_out_ratio <EOL> if precomputed_kernel : <EOL> self . fit = self . __fit_precomputed_kernel <EOL> else : <EOL> self . fit = self . __fit_no_precomputed_kernel <EOL> self . estimator_fitted = False <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' + str ( self . estimator ) + '<STR_LIT:\n>' + '<STR_LIT>' + str ( self . c ) + '<STR_LIT:\n>' + '<STR_LIT>' + str ( self . estimator_fitted ) <EOL> def __fit_precomputed_kernel ( self , X , y ) : <EOL> """<STR_LIT>""" <EOL> positives = np . where ( y == <NUM_LIT:1.> ) [ <NUM_LIT:0> ] <EOL> hold_out_size = np . ceil ( len ( positives ) * self . hold_out_ratio ) <EOL> if len ( positives ) <= hold_out_size : <EOL> raise ( '<STR_LIT>' + str ( hold_out_size + <NUM_LIT:1> ) + '<STR_LIT:.>' ) <EOL> np . random . shuffle ( positives ) <EOL> hold_out = positives [ : hold_out_size ] <EOL> X_test_hold_out = X [ hold_out ] <EOL> keep = list ( set ( np . arange ( len ( y ) ) ) - set ( hold_out ) ) <EOL> X_test_hold_out = X_test_hold_out [ : , keep ] <EOL> X = X [ : , keep ] <EOL> X = X [ keep ] <EOL> y = np . delete ( y , hold_out ) <EOL> self . estimator . fit ( X , y ) <EOL> hold_out_predictions = self . estimator . predict_proba ( X_test_hold_out ) <EOL> try : <EOL> hold_out_predictions = hold_out_predictions [ : , <NUM_LIT:1> ] <EOL> except : <EOL> pass <EOL> c = np . mean ( hold_out_predictions ) <EOL> self . c = c <EOL> self . estimator_fitted = True <EOL> def __fit_no_precomputed_kernel ( self , X , y ) : <EOL> """<STR_LIT>""" <EOL> positives = np . where ( y == <NUM_LIT:1.> ) [ <NUM_LIT:0> ] <EOL> hold_out_size = np . ceil ( len ( positives ) * self . hold_out_ratio ) <EOL> if len ( positives ) <= hold_out_size : <EOL> raise ( '<STR_LIT>' + str ( hold_out_size + <NUM_LIT:1> ) + '<STR_LIT:.>' ) <EOL> np . random . shuffle ( positives ) <EOL> hold_out = positives [ : hold_out_size ] <EOL> X_hold_out = X [ hold_out ] <EOL> X = np . delete ( X , hold_out , <NUM_LIT:0> ) <EOL> y = np . delete ( y , hold_out ) <EOL> self . estimator . fit ( X , y ) <EOL> hold_out_predictions = self . estimator . predict_proba ( X_hold_out ) <EOL> try : <EOL> hold_out_predictions = hold_out_predictions [ : , <NUM_LIT:1> ] <EOL> except : <EOL> pass <EOL> c = np . mean ( hold_out_predictions ) <EOL> self . c = c <EOL> self . estimator_fitted = True <EOL> def predict_proba ( self , X ) : <EOL> """<STR_LIT>""" <EOL> if not self . estimator_fitted : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> probabilistic_predictions = self . estimator . predict_proba ( X ) <EOL> try : <EOL> probabilistic_predictions = probabilistic_predictions [ : , <NUM_LIT:1> ] <EOL> except : <EOL> pass <EOL> return probabilistic_predictions / self . c <EOL> def predict ( self , X , treshold = <NUM_LIT:0.5> ) : <EOL> """<STR_LIT>""" <EOL> if not self . estimator_fitted : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> return np . array ( [ <NUM_LIT:1.> if p > treshold else - <NUM_LIT:1.> for p in self . predict_proba ( X ) ] ) </s>
