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)
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_composer_persistence(self): freq = '5b' universe = Universe('zz800') batch = 4 neutralized_risk = ['SIZE'] risk_model = 'long' pre_process = ['standardize', 'winsorize_normal'] post_process = ['standardize', 'winsorize_normal'] warm_start = 2 data_source = 'postgresql://*****:*****@server/dummy' data_meta = DataMeta(freq=freq, universe=universe, batch=batch, neutralized_risk=neutralized_risk, risk_model=risk_model, pre_process=pre_process, post_process=post_process, warm_start=warm_start, data_source=data_source) features = {'f1': 'closePrice', 'f2': 'openPrice'} alpha_model = XGBClassifier(features=features) composer = Composer(alpha_model=alpha_model, data_meta=data_meta) comp_desc = composer.save() loaded_comp = Composer.load(comp_desc) self._assert_composer_equal(composer, loaded_comp)
def test_xgb_classify_persistence(self): model = XGBClassifier(features=self.features) 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)