<s> from __future__ import print_function , division , absolute_import <EOL> from math import sqrt <EOL> import numpy <EOL> import pandas <EOL> from sklearn . base import BaseEstimator , ClassifierMixin , clone <EOL> from sklearn . utils . validation import column_or_1d <EOL> from . commonutils import sigmoid_function , check_sample_weight <EOL> __author__ = "<STR_LIT>" <EOL> class HidingClassifier ( BaseEstimator , ClassifierMixin ) : <EOL> def __init__ ( self , train_variables = None , base_estimator = None ) : <EOL> """<STR_LIT>""" <EOL> self . train_variables = train_variables <EOL> self . base_estimator = base_estimator <EOL> def fit ( self , X , y , ** kwargs ) : <EOL> assert self . base_estimator is not None , "<STR_LIT>" <EOL> self . _trained_estimator = clone ( self . base_estimator ) <EOL> self . _trained_estimator . fit ( X [ self . train_variables ] , y , ** kwargs ) <EOL> return self <EOL> def predict ( self , X ) : <EOL> return self . _trained_estimator . predict ( X [ self . train_variables ] ) <EOL> def predict_proba ( self , X ) : <EOL> return self . _trained_estimator . predict_proba ( X [ self . train_variables ] ) <EOL> def staged_predict_proba ( self , X ) : <EOL> return self . _trained_estimator . staged_predict_proba ( X [ self . train_variables ] ) <EOL> class AbstractBoostingClassifier ( BaseEstimator , ClassifierMixin ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def check_input ( X , y , sample_weight , check_two_classes = True ) : <EOL> sample_weight = check_sample_weight ( y , sample_weight = sample_weight ) <EOL> assert len ( X ) == len ( y ) , '<STR_LIT>' <EOL> X = pandas . DataFrame ( X ) <EOL> y = column_or_1d ( y ) <EOL> if check_two_classes : <EOL> assert numpy . all ( numpy . in1d ( y , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ) , '<STR_LIT>' <EOL> return X , y , sample_weight <EOL> def staged_predict_score ( self , X ) : <EOL> X = self . get_train_vars ( X ) <EOL> score = numpy . zeros ( len ( X ) ) <EOL> for clf in self . estimators : <EOL> score += self . learning_rate * self . compute_score ( clf , X ) <EOL> yield score <EOL> def predict_score ( self , X ) : <EOL> X = self . get_train_vars ( X ) <EOL> return self . learning_rate * sum ( self . compute_score ( clf , X ) for clf in self . estimators ) <EOL> def score_to_proba ( self , score ) : <EOL> """<STR_LIT>""" <EOL> proba = numpy . zeros ( ( score . shape [ <NUM_LIT:0> ] , <NUM_LIT:2> ) , dtype = numpy . float ) <EOL> proba [ : , <NUM_LIT:1> ] = sigmoid_function ( score , sqrt ( self . n_estimators ) ) <EOL> proba [ : , <NUM_LIT:0> ] = <NUM_LIT:1.0> - proba [ : , <NUM_LIT:1> ] <EOL> return proba <EOL> @ staticmethod <EOL> def compute_score ( clf , X ) : <EOL> """<STR_LIT>""" <EOL> p = clf . predict_proba ( X ) <EOL> p += <NUM_LIT> <EOL> return numpy . log ( p [ : , <NUM_LIT:1> ] / p [ : , <NUM_LIT:0> ] ) <EOL> def get_train_vars ( self , X ) : <EOL> """<STR_LIT>""" <EOL> if self . train_variables is None : <EOL> return X <EOL> else : <EOL> return X [ self . train_variables ] <EOL> def predict_proba ( self , X ) : <EOL> return self . score_to_proba ( self . predict_score ( X ) ) <EOL> def staged_predict_proba ( self , X ) : <EOL> for score in self . staged_predict_score ( X ) : <EOL> yield self . score_to_proba ( score ) </s>
