Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

bagging now implemented

  • Loading branch information...
commit 8a1a996d1166819a4263285a7fb7f6d1a2defe2b 1 parent 9f4a53f
@ndawe authored
View
43 scikits/learn/ensemble/bagging.py
@@ -0,0 +1,43 @@
+from ..base import BaseEnsemble
+import numpy as np
+
+class Bagged(BaseEnsemble):
+
+ def fit(self, X, Y, sample_weight = [], sample_fraction = .5, baggs = 10, **params):
+ """
+ X: list of instance vectors
+ Y: target values/classes
+ sample_fraction: fraction of X and Y randomly sampled
+ baggs: number of sampling/training iterations
+ """
+ if not 0. < sample_fraction < 1:
+ raise ValueError("You must specify sample_fraction between 0 and 1 (exclusive)")
+ if baggs < 2:
+ raise ValueError("baggs must be greater than 1")
+
+ if len(sample_weight) == 0:
+ # initialize weights to 1/N
+ sample_weight = np.ones(X.shape[0], dtype = np.float64)
+ else:
+ sample_weight = np.copy(sample_weight)
+ # remove any previous ensemble
+ self[:] = []
+ for bagg in xrange(baggs):
+ # weight a random subsample with 0
+ random_sample_weight = sample_weight * \
+ (np.random.random_sample(sample_weight.shape[0])>sample_fraction)
+ estimator = self.estimator(**self.params)
+ estimator.fit(X,Y,random_sample_weight,**params)
+ self.append(estimator)
+ return self
+
+ def predict(self, X):
+
+ if len(self) == 0:
+ return None
+ prediction = np.zeros(X.shape[0], dtype = np.float64)
+ for estimator in self:
+ prediction += estimator.predict(X)
+ prediction /= len(self)
+ return prediction
+
View
0  scikits/learn/ensemble/bagging/__init__.py
No changes.
View
1  scikits/learn/ensemble/bagging/bagging.py
@@ -1 +0,0 @@
-from ..base import BaseEnsemble
View
8 scikits/learn/ensemble/boosting/adaboost.py → scikits/learn/ensemble/boosting.py
@@ -61,3 +61,11 @@ def predict(self, X):
if norm > 0:
prediction /= norm
return prediction
+
+"""
+YET TO BE IMPLEMENTED
+
+class GradientBoost(BaseEnsemble): pass
+
+class StochasticGradientBoost(BaseEnsemble): pass
+"""
View
2  scikits/learn/ensemble/boosting/__init__.py
@@ -1,2 +0,0 @@
-from .adaboost import AdaBoost
-from .gradboost import GradientBoost, StochasticGradientBoost
View
5 scikits/learn/ensemble/boosting/gradboost.py
@@ -1,5 +0,0 @@
-from ..base import BaseEnsemble
-
-class GradientBoost(BaseEnsemble): pass
-
-class StochasticGradientBoost(BaseEnsemble): pass
Please sign in to comment.
Something went wrong with that request. Please try again.