Created
October 29, 2017 06:16
-
-
Save zhenghaoz/8b50a01209f16ddcbe5fe9d184623bbd to your computer and use it in GitHub Desktop.
Bagging Algorithm Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
import numpy as np | |
class Bagging: | |
num_base = 0 | |
classifiers = [] | |
minvs = [] | |
maxvs = [] | |
def __init__(self, classifier, num_base=0): | |
self.num_base = num_base | |
self.classifiers.clear() | |
for i in range(0, self.num_base): | |
self.classifiers.append(copy.copy(classifier)) | |
def fit(self, X, y): | |
for i in range(0, self.num_base): | |
sample_X, sample_y = self.boostrap(X, y) | |
self.minvs.append(np.max(sample_X, axis=0)) | |
self.maxvs.append(np.min(sample_X, axis=0)) | |
self.classifiers[i].fit(sample_X, sample_y) | |
def predict(self, x): | |
arr = np.array([w.predict(x) for w in self.classifiers]) | |
(values, counts) = np.unique(arr, return_counts=True) | |
ind = np.argmax(counts) | |
return values[ind] | |
@staticmethod | |
def boostrap(X, y): | |
assert len(X) == len(y) | |
m = len(X) | |
sample_X = np.empty([0,2]) | |
sample_y = np.array([]) | |
for i in range(0, m): | |
sample_X = np.vstack([sample_X, X[np.random.randint(0, m)]]) | |
sample_y = np.append(sample_y, y[np.random.randint(0, m)]) | |
return sample_X, sample_y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment