<s> import sys <EOL> import math <EOL> import numpy as np <EOL> class AdaBoost ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , weak_classifier ) : <EOL> self . WeakClassifier = weak_classifier <EOL> def ada_train ( self , T , X , Y , optional = False ) : <EOL> '''<STR_LIT>''' <EOL> self . weak_classifier_ens = [ ] <EOL> self . alpha = [ ] <EOL> self . X = X <EOL> self . Y = Y <EOL> self . T = T <EOL> self . e = [ ] <EOL> N = len ( self . Y ) <EOL> Z = ( <NUM_LIT:1.0> / N ) * np . ones ( N ) <EOL> for t in range ( T ) : <EOL> weak_learner = self . WeakClassifier ( ) <EOL> weak_learner . set_training_sample ( X , Y ) <EOL> weak_learner . weights = Z <EOL> if t < <NUM_LIT:10> and optional : <EOL> print ( "<STR_LIT>" , t + <NUM_LIT:1> ) <EOL> print ( '<STR_LIT>' , int ( Y [ t ] ) ) <EOL> opt = True <EOL> else : opt = False <EOL> weak_learner . stump_train ( opt ) <EOL> self . weak_classifier_ens . append ( weak_learner ) <EOL> Y_p = weak_learner . stump_predict ( X ) <EOL> epsilon = sum ( <NUM_LIT:0.5> * Z * abs ( ( Y - Y_p ) ) ) / sum ( Z ) <EOL> self . e = epsilon <EOL> inside = abs ( ( <NUM_LIT:1> - epsilon ) / ( epsilon * <NUM_LIT:1.0> ) + <NUM_LIT> ) <EOL> print inside <EOL> alpha = <NUM_LIT:0.5> * math . log ( inside ) <EOL> self . alpha . append ( alpha ) <EOL> Z *= np . exp ( - alpha * Y * Y_p ) <EOL> Z /= sum ( Z ) <EOL> def ada_predict ( self , X = [ ] ) : <EOL> '''<STR_LIT>''' <EOL> if X == None : return <EOL> X = np . array ( X ) <EOL> N , d = X . shape <EOL> Y = np . zeros ( N ) <EOL> score = [ ] <EOL> for t in range ( self . T ) : <EOL> weak_learner = self . weak_classifier_ens [ t ] <EOL> Y += self . alpha [ t ] * weak_learner . stump_predict ( X ) <EOL> score . append ( np . sign ( Y ) ) <EOL> return score <EOL> def run_adaboost ( self , X_train , Y_train , T , X_test = None , optional = False ) : <EOL> '''<STR_LIT>''' <EOL> self . ada_train ( T , X_train , Y_train , optional ) <EOL> return self . ada_predict ( X_train ) , self . ada_predict ( X_test ) </s>
