<s> from sklearn . base import BaseEstimator <EOL> import sklearn . metrics <EOL> import sys <EOL> import numpy <EOL> from sklearn . linear_model import LogisticRegression as LR <EOL> class SelfLearningModel ( BaseEstimator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , basemodel , max_iter = <NUM_LIT:200> , prob_threshold = <NUM_LIT> ) : <EOL> self . model = basemodel <EOL> self . max_iter = max_iter <EOL> self . prob_threshold = prob_threshold <EOL> def fit ( self , X , y ) : <EOL> """<STR_LIT>""" <EOL> unlabeledX = X [ y == - <NUM_LIT:1> , : ] <EOL> labeledX = X [ y != - <NUM_LIT:1> , : ] <EOL> labeledy = y [ y != - <NUM_LIT:1> ] <EOL> self . model . fit ( labeledX , labeledy ) <EOL> unlabeledy = self . predict ( unlabeledX ) <EOL> unlabeledprob = self . predict_proba ( unlabeledX ) <EOL> unlabeledy_old = [ ] <EOL> i = <NUM_LIT:0> <EOL> while ( len ( unlabeledy_old ) == <NUM_LIT:0> or numpy . any ( unlabeledy != unlabeledy_old ) ) and i < self . max_iter : <EOL> unlabeledy_old = numpy . copy ( unlabeledy ) <EOL> uidx = numpy . where ( ( unlabeledprob [ : , <NUM_LIT:0> ] > self . prob_threshold ) | ( unlabeledprob [ : , <NUM_LIT:1> ] > self . prob_threshold ) ) [ <NUM_LIT:0> ] <EOL> self . model . fit ( numpy . vstack ( ( labeledX , unlabeledX [ uidx , : ] ) ) , numpy . hstack ( ( labeledy , unlabeledy_old [ uidx ] ) ) ) <EOL> unlabeledy = self . predict ( unlabeledX ) <EOL> unlabeledprob = self . predict_proba ( unlabeledX ) <EOL> i += <NUM_LIT:1> <EOL> if not getattr ( self . model , "<STR_LIT>" , None ) : <EOL> self . plattlr = LR ( ) <EOL> preds = self . model . predict ( labeledX ) <EOL> self . plattlr . fit ( preds . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) , labeledy ) <EOL> return self <EOL> def predict_proba ( self , X ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( self . model , "<STR_LIT>" , None ) : <EOL> return self . model . predict_proba ( X ) <EOL> else : <EOL> preds = self . model . predict ( X ) <EOL> return self . plattlr . predict_proba ( preds . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> def predict ( self , X ) : <EOL> """<STR_LIT>""" <EOL> return self . model . predict ( X ) <EOL> def score ( self , X , y , sample_weight = None ) : <EOL> return sklearn . metrics . accuracy_score ( y , self . predict ( X ) , sample_weight = sample_weight ) </s>
