Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

AdaBoost in Python

import numpy as np

# Decision stump used as weak classifier
class DecisionStump():
    def __init__(self):
        self.polarity = 1
        self.feature_idx = None
        self.threshold = None
        self.alpha = None

    def predict(self, X):
        n_samples = X.shape[0]
        X_column = X[:, self.feature_idx]
        predictions = np.ones(n_samples)
        if self.polarity == 1:
            predictions[X_column < self.threshold] = -1
        else:
            predictions[X_column > self.threshold] = -1

        return predictions


class Adaboost():

    def __init__(self, n_clf=5):
        self.n_clf = n_clf

    def fit(self, X, y):
        n_samples, n_features = X.shape

        # Initialize weights to 1/N
        w = np.full(n_samples, (1 / n_samples))

        self.clfs = []
        # Iterate through classifiers
        for _ in range(self.n_clf):
            clf = DecisionStump()

            min_error = float('inf')
            # greedy search to find best threshold and feature
            for feature_i in range(n_features):
                X_column = X[:, feature_i]
                thresholds = np.unique(X_column)

                for threshold in thresholds:
                    # predict with polarity 1
                    p = 1
                    predictions = np.ones(n_samples)
                    predictions[X_column < threshold] = -1

                    # Error = sum of weights of misclassified samples
                    misclassified = w[y != predictions]
                    error = sum(misclassified)

                    if error > 0.5:
                        error = 1 - error
                        p = -1

                    # store the best configuration
                    if error < min_error:
                        clf.polarity = p
                        clf.threshold = threshold
                        clf.feature_idx = feature_i
                        min_error = error

            # calculate alpha
            EPS = 1e-10
            clf.alpha = 0.5 * np.log((1.0 - min_error + EPS) / (min_error + EPS))

            # calculate predictions and update weights
            predictions = clf.predict(X)

            w *= np.exp(-clf.alpha * y * predictions)
            # Normalize to one
            w /= np.sum(w)

            # Save classifier
            self.clfs.append(clf)

    def predict(self, X):
        clf_preds = [clf.alpha * clf.predict(X) for clf in self.clfs]
        y_pred = np.sum(clf_preds, axis=0)
        y_pred = np.sign(y_pred)

        return y_pred
Comment

PREVIOUS NEXT
Code Example
Python :: Delete the node at a given position 2 in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. 
Python :: isinstance float or int 
Python :: read pdf py 
Python :: all alphanumeric characters for python python 
Python :: django get or 404 
Python :: how to open sound file in python 
Python :: list to excel python 
Python :: move mouse round in python 
Python :: extract month as integer python 
Python :: how to seperate words and number in a list 
Python :: string to datetime python 
Python :: how to check if item is file in python or not 
Python :: python extract value from a list of dictionaries 
Python :: python3 hello world 
Python :: drop duplicate rows pandas except nan 
Python :: http.server python 
Python :: beautifulsoup find_all by id 
Python :: generate gif py 
Python :: python inf 
Python :: how to add subplots for histogram 
Python :: python mysqldb 
Python :: maping value to data in pandas dataframe 
Python :: sort df by column 
Python :: euclidean division in python 
Python :: how to create obtain any random 3 items of list in python 
Python :: python remove form list 
Python :: how to disable resizing in tkinter 
Python :: dataframe change column value 
Python :: pi in python math 
Python :: django template tag multiple arguments 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =