Пример #1
0
    def test_save_and_load(self):
        metric = Metric({"name": "logloss"})
        params = {"objective": "binary:logistic", "eval_metric": "logloss"}
        xgb = XgbAlgorithm(params)
        xgb.fit(self.X, self.y)
        y_predicted = xgb.predict(self.X)
        loss = metric(self.y, y_predicted)

        with tempfile.NamedTemporaryFile() as tmp:
            xgb.save(tmp.name)

            xgb2 = XgbAlgorithm(params)
            self.assertTrue(xgb2.model is None)
            xgb2.load(tmp.name)

            y_predicted = xgb2.predict(self.X)
            loss2 = metric(self.y, y_predicted)
            assert_almost_equal(loss, loss2)
Пример #2
0
    def test_save_and_load(self):
        metric = Metric({"name": "logloss"})
        params = {"objective": "binary:logistic", "eval_metric": "logloss"}
        xgb = XgbAlgorithm(params)
        xgb.fit(self.X, self.y)
        y_predicted = xgb.predict(self.X)
        loss = metric(self.y, y_predicted)

        filename = os.path.join(tempfile.gettempdir(), os.urandom(12).hex())

        xgb.save(filename)

        xgb2 = XgbAlgorithm(params)
        self.assertTrue(xgb2.model is None)
        xgb2.load(filename)
        # Finished with the file, delete it
        os.remove(filename)

        y_predicted = xgb2.predict(self.X)
        loss2 = metric(self.y, y_predicted)
        assert_almost_equal(loss, loss2)