<commit_before>
from decision_stump import DecisionStump

import sys, math, random
import numpy
import operator

class AdaBoost(object):
    """ Class for an adaboost classifier, adapted from pyclassic. """

    def fit(self, X, Y, w=None, T=100):
        self.X = X
        self.Y = Y
        N = len(self.Y)
        
        if w is None:
            w = (1.0/N)*numpy.ones(N)
        self.weights = w
        self.weak_classifier_ensemble = []
        self.alpha = []
        
        for t in range(T):
            weak_learner = DecisionStump()
            weak_learner.fit(X,Y,w)
            Y_pred = weak_learner.predict(X)
            e = sum(0.5*w*abs((Y-Y_pred)))/sum(w)
            if e > 0.5:
                sys.stdout.write('WARNING: ending training due to no good weak classifiers.')
                break
            ee = (1-e)/(e*1.0)
            alpha = 0.5*math.log(ee+0.00001)
            w *= numpy.exp(-alpha*Y*Y_pred) #*ww) # increase weights for wrongly classified
            w /= sum(w)
            self.weak_classifier_ensemble.append(weak_learner)
            self.alpha.append(alpha)
            self.T = t+1
            sys.stdout.write('.')
            sys.stdout.flush()
        print "\n"

    def predict(self,X):
        X = numpy.array(X)
        N, d = X.shape
        Y = numpy.zeros(N)
        for t in range(self.T):
            #sys.stdout.write('.')
            weak_learner = self.weak_classifier_ensemble[t]
            #print Y.shape, self.alpha[t], weak_learner.predict(X).shape
            Y += self.alpha[t]*weak_learner.predict(X)
        return Y
        
    def predict_proba(self, X):
        return 1.0/(1.0 + numpy.exp(-self.predict(X)))
        

    def measure_accuracy(self, Y, o, threshold=0):
        oo = o.copy()
        oo[numpy.where(o>threshold)[0]] = 1
        oo[numpy.where(o<threshold)[0]] = -1
        d = (oo - Y)
        return len(d[numpy.where(d==0)[0]])*1.0/len(Y)




<commit_msg>added adaboost classifier<commit_after>
from decision_stump import DecisionStump

import sys, math, random
import numpy
import operator

class AdaBoost(object):
    """ Class for an adaboost classifier, adapted from pyclassic. """

    def fit(self, X, Y, w=None, T=100):
        self.X = X
        self.Y = Y
        N = len(self.Y)
        
        if w is None:
            w = (1.0/N)*numpy.ones(N)
        self.weights = w
        self.weak_classifier_ensemble = []
        self.alpha = []
        
        for t in range(T):
            weak_learner = DecisionStump()
            weak_learner.fit(X,Y,w)
            Y_pred = weak_learner.predict(X)
            e = sum(0.5*w*abs((Y-Y_pred)))/sum(w)
            if e > 0.5:
                sys.stdout.write('WARNING: ending training due to no good weak classifiers.')
                break
            ee = (1-e)/(e*1.0)
            alpha = 0.5*math.log(ee+0.00001)
            w *= numpy.exp(-alpha*Y*Y_pred) #*ww) # increase weights for wrongly classified
            w /= sum(w)
            self.weak_classifier_ensemble.append(weak_learner)
            self.alpha.append(alpha)
            self.T = t+1
            sys.stdout.write('.')
            sys.stdout.flush()
        print "\n"
        return self

    def predict(self,X):
        X = numpy.array(X)
        N, d = X.shape
        Y = numpy.zeros(N)
        for t in range(self.T):
            #sys.stdout.write('.')
            weak_learner = self.weak_classifier_ensemble[t]
            #print Y.shape, self.alpha[t], weak_learner.predict(X).shape
            Y += self.alpha[t]*weak_learner.predict(X)
        return Y
        
    def predict_proba(self, X):
        prob = 1.0/(1.0 + numpy.exp(-self.predict(X)))
        return numpy.array([[1.0-p, p] for p in prob])
        

    def measure_accuracy(self, Y, o, threshold=0):
        oo = o.copy()
        oo[numpy.where(o>threshold)[0]] = 1
        oo[numpy.where(o<threshold)[0]] = -1
        d = (oo - Y)
        return len(d[numpy.where(d==0)[0]])*1.0/len(Y)





{'fraction_non_alphanumeric': 0.137215696075981, 'fraction_numerical': 0.01749562609347663, 'mean_word_length': 1.7150610583446404, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4570084', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1351, 'n_words': 285}