def test_copy(self): metric = Metric({"name": "logloss"}) rf = RandomForestAlgorithm({"ml_task": "binary_classification"}) rf.fit(self.X, self.y) y_predicted = rf.predict(self.X) loss = metric(self.y, y_predicted) rf2 = RandomForestAlgorithm({"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 = RandomForestAlgorithm({"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 = RandomForestAlgorithm({"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_fit_predict(self): metric = Metric({"name": "logloss"}) params = {"ml_task": "binary_classification"} rf = RandomForestAlgorithm(params) rf.fit(self.X, self.y) y_predicted = rf.predict(self.X) self.assertTrue(metric(self.y, y_predicted) < 1.0)
def test_fit_predict(self): metric = Metric({"name": "logloss"}) params = {"trees_in_step": 50} rf = RandomForestAlgorithm(params) rf.fit(self.X, self.y) y_predicted = rf.predict(self.X) self.assertTrue(metric(self.y, y_predicted) < 0.6)
def test_save_and_load(self): metric = Metric({"name": "logloss"}) rf = RandomForestAlgorithm({"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 = RandomForestAlgorithm({"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_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 = RandomForestAlgorithm(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_is_fitted(self): model = RandomForestAlgorithm({"ml_task": "binary_classification"}) self.assertFalse(model.is_fitted()) model.fit(self.X, self.y) self.assertTrue(model.is_fitted())