Пример #1
0
 def test_copy(self):
     # train model #1
     metric = Metric({"name": "logloss"})
     cat = CatBoostAlgorithm(self.params)
     cat.fit(self.X, self.y)
     y_predicted = cat.predict(self.X)
     loss = metric(self.y, y_predicted)
     # create model #2
     cat2 = CatBoostAlgorithm(self.params)
     # model #2 is initialized in constructor
     self.assertTrue(cat2.model is not None)
     # do a copy and use it for predictions
     cat2 = cat.copy()
     self.assertEqual(type(cat), type(cat2))
     y_predicted = cat2.predict(self.X)
     loss2 = metric(self.y, y_predicted)
     self.assertEqual(loss, loss2)
     # fit model #1, there should be improvement in loss
     cat.fit(self.X, self.y)
     y_predicted = cat.predict(self.X)
     loss3 = metric(self.y, y_predicted)
     self.assertTrue(loss3 < loss)
     # the loss of model #2 should not change
     y_predicted = cat2.predict(self.X)
     loss4 = metric(self.y, y_predicted)
     assert_almost_equal(loss2, loss4)
Пример #2
0
    def test_save_and_load(self):
        metric = Metric({"name": "logloss"})
        cat = CatBoostAlgorithm(self.params)
        cat.fit(self.X, self.y)
        y_predicted = cat.predict(self.X)
        loss = metric(self.y, y_predicted)

        with tempfile.NamedTemporaryFile() as tmp:
            cat.save(tmp.name)
            cat2 = CatBoostAlgorithm(self.params)
            self.assertTrue(cat.uid != cat2.uid)
            self.assertTrue(cat2.model is not None)
            cat2.load(tmp.name)

            y_predicted = cat2.predict(self.X)
            loss2 = metric(self.y, y_predicted)
            assert_almost_equal(loss, loss2)
Пример #3
0
 def test_copy(self):
     # train model #1
     metric = Metric({"name": "logloss"})
     cat = CatBoostAlgorithm(self.params)
     cat.fit(self.X, self.y)
     y_predicted = cat.predict(self.X)
     loss = metric(self.y, y_predicted)
     # create model #2
     cat2 = CatBoostAlgorithm(self.params)
     # model #2 is initialized in constructor
     self.assertTrue(cat2.model is not None)
     # do a copy and use it for predictions
     cat2 = cat.copy()
     self.assertEqual(type(cat), type(cat2))
     y_predicted = cat2.predict(self.X)
     loss2 = metric(self.y, y_predicted)
     self.assertEqual(loss, loss2)
    def test_save_and_load(self):
        metric = Metric({"name": "logloss"})
        cat = CatBoostAlgorithm(self.params)
        cat.fit(self.X, self.y)
        y_predicted = cat.predict(self.X)
        loss = metric(self.y, y_predicted)

        json_desc = cat.save()
        cat2 = CatBoostAlgorithm({})
        self.assertTrue(cat.uid != cat2.uid)
        self.assertTrue(cat2.model is not None)
        cat2.load(json_desc)
        self.assertTrue(cat.uid == cat2.uid)

        y_predicted = cat2.predict(self.X)
        loss2 = metric(self.y, y_predicted)
        assert_almost_equal(loss, loss2)
Пример #5
0
 def test_fit_predict(self):
     metric = Metric({"name": "logloss"})
     cat = CatBoostAlgorithm(self.params)
     loss_prev = None
     for _ in range(5):
         cat.fit(self.X, self.y)
         y_predicted = cat.predict(self.X)
         loss = metric(self.y, y_predicted)
         if loss_prev is not None:
             self.assertTrue(loss + 0.001 < loss_prev)
         loss_prev = loss
Пример #6
0
 def test_reproduce_fit(self):
     metric = Metric({"name": "logloss"})
     prev_loss = None
     for _ in range(3):
         model = CatBoostAlgorithm(self.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
Пример #7
0
 def test_fit_predict(self):
     metric = Metric({"name": "logloss"})
     loss_prev = None
     for _ in range(2):
         cat = CatBoostAlgorithm(self.params)
         cat.fit(self.X, self.y)
         y_predicted = cat.predict(self.X)
         loss = metric(self.y, y_predicted)
         if loss_prev is not None:
             assert_almost_equal(loss, loss_prev)
         loss_prev = loss
Пример #8
0
    def test_save_and_load(self):
        metric = Metric({"name": "logloss"})
        cat = CatBoostAlgorithm(self.params)
        cat.fit(self.X, self.y)
        y_predicted = cat.predict(self.X)
        loss = metric(self.y, y_predicted)

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

        cat.save(filename)
        cat2 = CatBoostAlgorithm(self.params)
        self.assertTrue(cat.uid != cat2.uid)
        self.assertTrue(cat2.model is not None)
        cat2.load(filename)
        # Finished with the file, delete it
        os.remove(filename)

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