def testEdge(self): y = np.array([-1]*10) stump = Stump.trained(self.x, y, self.w) assert_approx_equal(stump.err, 0.0) self.assertTrue(all(stump.predict(np.array([3.5, 5.0, 8.0])) == np.array([-1, -1, -1]))) y = np.array([1]*10) stump = Stump.trained(self.x, y, self.w) assert_approx_equal(stump.err, 0.0) self.assertTrue(all(stump.predict(np.array([3.5, 5.0, 8.0])) == np.array([1, 1, 1])))
def testWeighted(self): our_w = np.ones(10) / 10 our_w[0] = 0.05 our_w[9] = 0.15 y = np.array([-1] + [1]*4 + [-1]*5) stump = Stump.trained(self.x, y, our_w) self.assertTrue(all(stump.predict(np.array([3.5, 5.0, 8.0])) == np.array([1, -1, -1]))) assert_approx_equal(stump.err, 0.05)
def testTrivial(self): y = np.array([1]*5 + [-1]*5) stump = Stump.trained(self.x, y, self.w) assert_approx_equal(stump.err, 0.0) self.assertTrue(all(stump.predict(np.array([3.5, 5.0])) == np.array([1, -1]))) y = np.array([-1]*5 + [1]*5) stump = Stump.trained(self.x, y, self.w) assert_approx_equal(stump.err, 0.0) self.assertTrue(all(stump.predict(np.array([3.5, 5.0])) == np.array([-1, +1]))) y = np.array([-1]*7 + [1]*3) stump = Stump.trained(self.x, y, self.w) assert_approx_equal(stump.err, 0.0) self.assertTrue(all(stump.predict(np.array([3.5, 5.0, 8.0])) == np.array([-1, -1, +1])))
def train(self, samples, weights=None): print('----------------start training-----------------') if weights is None: weights = self.initWeights(samples) stump0 = Stump() stump0.train(samples, weights) self.stumps.append(stump0) for _ in range(1, self.n_weakClassifier): weights = self.stumps[-1].updateWeights stump_i = Stump() stump_i.train(samples, weights) self.stumps.append(stump_i) print("----------adaboost training finished.-----------")