<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
from sklearn.datasets import load_iris

from classifer.base import BaseClassifier
from classifer.decision_tree import DecisionTreeClassifier
import numpy as np


class AbsAdaBoostClassifier(BaseClassifier):



    def __init__(self, num_rounds):
        super(AbsAdaBoostClassifier, self).__init__()
        self.num_rounds = num_rounds
        self.clf = None

    def create_classifer(self, index=0):
        """
        create a new classifer.
        :return: BaseClassifier
        """
        pass

    def train(self, x, y):
        """

        :param x:
        :param y:
        :return:
        """
        num_rows = len(x)
        classifiers = []
        alphas = []
        weights = np.ones(num_rows) * 1.0 / num_rows
        for n in range(self.num_rounds):
            error = 0.
            random_indices = resample(weights)
            resampled_entries_x = []
            resampled_entries_y =[]
            for i in range(num_rows):
                resampled_entries_x.append(x[random_indices[i]])
                resampled_entries_y.append(y[random_indices[i]])

            print 'round ' + str(n + 1) + ' training...'
            weak_classifier = self.create_classifer(n)
            print len(resampled_entries_x)
            weak_classifier.train(resampled_entries_x, resampled_entries_y)

            # training and calculate the rate of error
            classifications = weak_classifier.predict(x)
            error = 0
            for i in range(len(classifications)):
                predicted = classifications[i]
                error += (predicted != np.argmax(y[i])) * weights[i]

            print 'Error', error

            if error == 0.:
                alpha = 4.0
            elif error > 0.7:
                print 'Discarding learner'
                continue  # discard classifier with error > 0.5
            else:
                alpha = 0.5 * np.log((1 - error) / error)

            alphas.append(alpha)
            classifiers.append(weak_classifier)<commit_msg>fix adaboost object bugs<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
from sklearn.datasets import load_iris

from classifer.base import BaseClassifier
from classifer.decision_tree import DecisionTreeClassifier
import numpy as np


class AbsAdaBoostClassifier(BaseClassifier):



    def __init__(self, num_rounds):
        super(AbsAdaBoostClassifier, self).__init__()
        self.num_rounds = num_rounds
        self.clf = None

    def create_classifer(self, index=0):
        """
        create a new classifer.
        :return: BaseClassifier
        """
        pass

    def train(self, x, y):
        """

        :param x:
        :param y:
        :return:
        """
        num_rows = len(x)
        classifiers = []
        alphas = []
        weights = np.ones(num_rows) * 1.0 / num_rows
        for n in range(self.num_rounds):
            error = 0.
            random_indices = AbsAdaBoostClassifier.resample(weights)
            resampled_entries_x = []
            resampled_entries_y =[]
            for i in range(num_rows):
                resampled_entries_x.append(x[random_indices[i]])
                resampled_entries_y.append(y[random_indices[i]])

            print 'round ' + str(n + 1) + ' training...'
            weak_classifier = self.create_classifer(n)
            print len(resampled_entries_x)
            weak_classifier.train(resampled_entries_x, resampled_entries_y)

            # training and calculate the rate of error
            classifications = weak_classifier.predict(x)
            error = 0
            for i in range(len(classifications)):
                predicted = classifications[i]
                error += (predicted != np.argmax(y[i])) * weights[i]

            print 'Error', error

            if error == 0.:
                alpha = 4.0
            elif error > 0.7:
                print 'Discarding learner'
                continue  # discard classifier with error > 0.5
            else:
                alpha = 0.5 * np.log((1 - error) / error)

            alphas.append(alpha)
            classifiers.append(weak_classifier)
{'fraction_non_alphanumeric': 0.09595597032782963, 'fraction_numerical': 0.008135917683656377, 'mean_word_length': 1.8072531900604432, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5949515', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1183, 'n_words': 268}