Exemplo n.º 1
0
def test_regression_saved_without_feature_names():
    base_score = 0.6
    estimator = xgboost.XGBRegressor(n_estimators=2,
                                     random_state=1,
                                     max_depth=1,
                                     base_score=base_score)
    utils.train_model_regression(estimator)

    with utils.tmp_dir() as tmp_dirpath:
        filename = os.path.join(tmp_dirpath, "tmp.file")
        estimator.save_model(filename)
        estimator = xgboost.XGBRegressor(base_score=base_score)
        estimator.load_model(filename)

    assembler = assemblers.XGBoostModelAssembler(estimator)
    actual = assembler.assemble()

    expected = ast.SubroutineExpr(
        ast.BinNumExpr(
            ast.BinNumExpr(
                ast.NumVal(base_score),
                ast.SubroutineExpr(
                    ast.IfExpr(
                        ast.CompExpr(ast.FeatureRef(12),
                                     ast.NumVal(9.72500038),
                                     ast.CompOpType.GTE),
                        ast.NumVal(1.6614188), ast.NumVal(2.91697121))),
                ast.BinNumOpType.ADD),
            ast.SubroutineExpr(
                ast.IfExpr(
                    ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.94099998),
                                 ast.CompOpType.GTE), ast.NumVal(3.33810854),
                    ast.NumVal(1.71813202))), ast.BinNumOpType.ADD))

    assert utils.cmp_exprs(actual, expected)
Exemplo n.º 2
0
def test_regression_saved_without_feature_names():
    base_score = 0.6
    estimator = xgb.XGBRegressor(n_estimators=2,
                                 random_state=1,
                                 max_depth=1,
                                 base_score=base_score)
    utils.get_regression_model_trainer()(estimator)

    with utils.tmp_dir() as tmp_dirpath:
        filename = tmp_dirpath / "tmp.file"
        estimator.save_model(filename)
        estimator = xgb.XGBRegressor(base_score=base_score)
        estimator.load_model(filename)

    assembler = XGBoostModelAssemblerSelector(estimator)
    actual = assembler.assemble()

    expected = ast.BinNumExpr(
        ast.NumVal(base_score),
        ast.BinNumExpr(
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(12),
                             ast.NumVal(9.72500038), ast.CompOpType.GTE),
                ast.NumVal(4.995625019073486), ast.NumVal(8.715502738952637)),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(5),
                             ast.NumVal(6.94099998), ast.CompOpType.GTE),
                ast.NumVal(8.309040069580078), ast.NumVal(3.930694580078125)),
            ast.BinNumOpType.ADD), ast.BinNumOpType.ADD)

    assert utils.cmp_exprs(actual, expected)
Exemplo n.º 3
0
def test_regression_saved_without_feature_names():
    base_score = 0.6
    estimator = xgboost.XGBRegressor(n_estimators=2,
                                     random_state=1,
                                     max_depth=1,
                                     base_score=base_score)
    utils.get_regression_model_trainer()(estimator)

    with utils.tmp_dir() as tmp_dirpath:
        filename = os.path.join(tmp_dirpath, "tmp.file")
        estimator.save_model(filename)
        estimator = xgboost.XGBRegressor(base_score=base_score)
        estimator.load_model(filename)

    assembler = assemblers.XGBoostModelAssemblerSelector(estimator)
    actual = assembler.assemble()

    expected = ast.BinNumExpr(
        ast.BinNumExpr(
            ast.NumVal(base_score),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(12), ast.NumVal(9.72500038),
                             ast.CompOpType.GTE), ast.NumVal(4.98425627),
                ast.NumVal(8.75091362)), ast.BinNumOpType.ADD),
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.94099998),
                         ast.CompOpType.GTE), ast.NumVal(8.34557438),
            ast.NumVal(3.9141891)), ast.BinNumOpType.ADD)

    assert utils.cmp_exprs(actual, expected)
Exemplo n.º 4
0
    def prepare_then_cleanup(self):
        with tmp_dir() as tmp_dirpath:
            self._resource_tmp_dir = tmp_dirpath
            self.prepare()

            try:
                yield
            finally:
                self._resource_tmp_dir = None
Exemplo n.º 5
0
def global_tmp_dir():
    with tmp_dir() as directory:
        yield directory