Example #1
0
    def test_xgb_trainer_equal_classifier(self):

        model1 = XGBClassifier(n_estimators=100,
                               learning_rate=0.1,
                               max_depth=3,
                               features=self.features,
                               random_state=42)

        model2 = XGBTrainer(features=self.features,
                            objective='reg:logistic',
                            booster='gbtree',
                            tree_method='exact',
                            n_estimators=100,
                            learning_rate=0.1,
                            max_depth=3,
                            random_state=42)

        y = np.where(self.y > 0, 1, 0)
        model1.fit(self.x, y)
        model2.fit(self.x, y)

        predict1 = model1.predict(self.sample_x)
        predict2 = model2.predict(self.sample_x)
        predict2 = np.where(predict2 > 0.5, 1., 0.)
        np.testing.assert_array_almost_equal(predict1, predict2)
Example #2
0
def load_model(model_desc: dict) -> ModelBase:
    model_name = model_desc['model_name']
    model_name_parts = set(model_name.split('.'))

    if 'ConstLinearModel' in model_name_parts:
        return ConstLinearModel.load(model_desc)
    elif 'LinearRegression' in model_name_parts:
        return LinearRegression.load(model_desc)
    elif 'LassoRegression' in model_name_parts:
        return LassoRegression.load(model_desc)
    elif 'LogisticRegression' in model_name_parts:
        return LogisticRegression.load(model_desc)
    elif 'RandomForestRegressor' in model_name_parts:
        return RandomForestRegressor.load(model_desc)
    elif 'RandomForestClassifier' in model_name_parts:
        return RandomForestClassifier.load(model_desc)
    elif 'XGBRegressor' in model_name_parts:
        return XGBRegressor.load(model_desc)
    elif 'XGBClassifier' in model_name_parts:
        return XGBClassifier.load(model_desc)
    elif 'XGBTrainer' in model_name_parts:
        return XGBTrainer.load(model_desc)
    elif 'NvSVR' in model_name_parts:
        return NvSVRModel.load(model_desc)
    else:
        raise ValueError('{0} is not currently supported in model loader.'.format(model_name))
    def test_xgb_trainer_persistence(self):
        model = XGBTrainer(features=self.features,
                           objective='binary:logistic',
                           booster='gbtree',
                           tree_method='hist',
                           n_estimators=200)
        y = np.where(self.y > 0, 1, 0)
        model.fit(self.x, y)

        desc = model.save()
        new_model = load_model(desc)
        self.assertEqual(model.features, new_model.features)

        np.testing.assert_array_almost_equal(model.predict(self.sample_x),
                                             new_model.predict(self.sample_x))
        np.testing.assert_array_almost_equal(model.importances,
                                             new_model.importances)