def test_fit_predict(self): metric = Metric({"name": "logloss"}) params = {"trees_in_step": 50, "ml_task": "binary_classification"} rf = ExtraTreesAlgorithm(params) rf.fit(self.X, self.y) y_predicted = rf.predict(self.X) self.assertTrue(metric(self.y, y_predicted) < 0.6)
def test_reproduce_fit(self): metric = Metric({"name": "logloss"}) params = {"trees_in_step": 1, "seed": 1, "ml_task": "binary_classification"} prev_loss = None for _ in range(3): model = ExtraTreesAlgorithm(params) model.fit(self.X, self.y) y_predicted = model.predict(self.X) loss = metric(self.y, y_predicted) if prev_loss is not None: assert_almost_equal(prev_loss, loss) prev_loss = loss
def test_copy(self): metric = Metric({"name": "logloss"}) rf = ExtraTreesAlgorithm({"ml_task": "binary_classification"}) rf.fit(self.X, self.y) y_predicted = rf.predict(self.X) loss = metric(self.y, y_predicted) rf2 = ExtraTreesAlgorithm({"ml_task": "binary_classification"}) rf2 = rf.copy() self.assertEqual(type(rf), type(rf2)) y_predicted = rf2.predict(self.X) loss2 = metric(self.y, y_predicted) assert_almost_equal(loss, loss2)
def test_save_and_load(self): metric = Metric({"name": "logloss"}) rf = ExtraTreesAlgorithm({"ml_task": "binary_classification"}) rf.fit(self.X, self.y) y_predicted = rf.predict(self.X) loss = metric(self.y, y_predicted) filename = os.path.join(tempfile.gettempdir(), os.urandom(12).hex()) rf.save(filename) rf2 = ExtraTreesAlgorithm({"ml_task": "binary_classification"}) rf2.load(filename) # Finished with the file, delete it os.remove(filename) y_predicted = rf2.predict(self.X) loss2 = metric(self.y, y_predicted) assert_almost_equal(loss, loss2)
def test_save_and_load(self): metric = Metric({"name": "logloss"}) rf = ExtraTreesAlgorithm({"ml_task": "binary_classification"}) rf.fit(self.X, self.y) y_predicted = rf.predict(self.X) loss = metric(self.y, y_predicted) with tempfile.NamedTemporaryFile() as tmp: rf.save(tmp.name) rf2 = ExtraTreesAlgorithm({"ml_task": "binary_classification"}) rf2.load(tmp.name) y_predicted = rf2.predict(self.X) loss2 = metric(self.y, y_predicted) assert_almost_equal(loss, loss2)
def test_is_fitted(self): params = {"trees_in_step": 50, "ml_task": "binary_classification"} model = ExtraTreesAlgorithm(params) self.assertFalse(model.is_fitted()) model.fit(self.X, self.y) self.assertTrue(model.is_fitted())