Beispiel #1
0
def test_multi_class():
    estimator = lightgbm.LGBMClassifier(n_estimators=1,
                                        random_state=1,
                                        max_depth=1)
    estimator.fit(np.array([[1], [2], [3]]), np.array([1, 2, 3]))

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

    exponent = ast.ExpExpr(ast.BinNumExpr(ast.NumVal(0.0),
                                          ast.NumVal(-1.0986122886681098),
                                          ast.BinNumOpType.ADD),
                           to_reuse=True)

    exponent_sum = ast.BinNumExpr(ast.BinNumExpr(exponent, exponent,
                                                 ast.BinNumOpType.ADD),
                                  exponent,
                                  ast.BinNumOpType.ADD,
                                  to_reuse=True)

    softmax = ast.BinNumExpr(exponent, exponent_sum, ast.BinNumOpType.DIV)

    expected = ast.VectorVal([softmax] * 3)

    assert utils.cmp_exprs(actual, expected)
def test_regression_random_forest():
    estimator = lightgbm.LGBMRegressor(boosting_type="rf",
                                       n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       subsample=0.7,
                                       subsample_freq=1)
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.BinNumExpr(
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(12),
                             ast.NumVal(5.200000000000001), ast.CompOpType.GT),
                ast.NumVal(20.206688945020474), ast.NumVal(38.30000037757679)),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.837500000000001),
                             ast.CompOpType.GT), ast.NumVal(36.40634951405711),
                ast.NumVal(19.57067132709245)), ast.BinNumOpType.ADD),
        ast.NumVal(0.5), ast.BinNumOpType.MUL)

    assert utils.cmp_exprs(actual, expected)
Beispiel #3
0
def test_regression():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1)
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.SubroutineExpr(
        ast.BinNumExpr(
            ast.BinNumExpr(
                ast.NumVal(0),
                ast.SubroutineExpr(
                    ast.IfExpr(
                        ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.918),
                                     ast.CompOpType.GT),
                        ast.NumVal(24.011454621684155),
                        ast.NumVal(22.289277544391084))),
                ast.BinNumOpType.ADD),
            ast.SubroutineExpr(
                ast.IfExpr(
                    ast.CompExpr(ast.FeatureRef(12), ast.NumVal(9.63),
                                 ast.CompOpType.GT),
                    ast.NumVal(-0.49461212269771115),
                    ast.NumVal(0.7174324413014594))), ast.BinNumOpType.ADD))

    assert utils.cmp_exprs(actual, expected)
Beispiel #4
0
def test_regression_categorical():
    estimator = lightgbm.LGBMRegressor(n_estimators=2, random_state=1,
                                       max_depth=1, 
                                       min_data_per_group=1000)
    X = np.column_stack(( np.random.choice([0,1,2,3,4,5], size=[10000, 1]), np.random.choice([0,1,2,3], size=[10000,1])))
    y = np.random.normal(size=[10000, 1])
    estimator.fit(X, y, categorical_feature=[1])
    
    assembler = assemblers.LightGBMModelAssembler(estimator)
    actual = assembler.assemble()

    expected = ast.BinNumExpr(
        ast.IfExpr(
            ast.CompExpr(
                ast.FeatureRef(5),
                ast.NumVal(6.918),
                ast.CompOpType.GT),
            ast.NumVal(24.011454621684155),
            ast.NumVal(22.289277544391084)),
        ast.IfExpr(
            ast.CompExpr(
                ast.FeatureRef(12),
                ast.NumVal(9.63),
                ast.CompOpType.GT),
            ast.NumVal(-0.49461212269771115),
            ast.NumVal(0.7174324413014594)),
        ast.BinNumOpType.ADD)

    assert utils.cmp_exprs(actual, expected)
Beispiel #5
0
def test_regression_random_forest():
    estimator = lightgbm.LGBMRegressor(boosting_type="rf",
                                       n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       subsample=0.7,
                                       subsample_freq=1)
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.BinNumExpr(
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(12),
                             ast.NumVal(9.605), ast.CompOpType.GT),
                ast.NumVal(17.398543657369768),
                ast.NumVal(29.851408659650296)),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.888),
                             ast.CompOpType.GT), ast.NumVal(37.2235298136268),
                ast.NumVal(19.948122884684025)), ast.BinNumOpType.ADD),
        ast.NumVal(0.5), ast.BinNumOpType.MUL)

    assert utils.cmp_exprs(actual, expected)
Beispiel #6
0
def test_simple_sigmoid_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       objective="cross_entropy")
    utils.get_bounded_regression_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.BinNumExpr(
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(12), ast.NumVal(19.23),
                                         ast.CompOpType.GT),
                            ast.NumVal(4.0026305187),
                            ast.NumVal(4.0880438137)),
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(12),
                                         ast.NumVal(14.895),
                                         ast.CompOpType.GT),
                            ast.NumVal(-0.0412703078),
                            ast.NumVal(0.0208393767)), ast.BinNumOpType.ADD),
                    ast.BinNumOpType.SUB)), ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV)

    assert utils.cmp_exprs(actual, expected)
Beispiel #7
0
def test_maybe_sqr_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       reg_sqrt=True,
                                       objective="regression_l1")
    utils.get_regression_model_trainer()(estimator)

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

    raw_output = ast.IdExpr(ast.BinNumExpr(
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(12), ast.NumVal(9.905),
                         ast.CompOpType.GT), ast.NumVal(4.5658116817),
            ast.NumVal(4.6620790482)),
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(12), ast.NumVal(9.77),
                         ast.CompOpType.GT), ast.NumVal(-0.0340889740),
            ast.NumVal(0.0543687153)), ast.BinNumOpType.ADD),
                            to_reuse=True)

    expected = ast.BinNumExpr(ast.AbsExpr(raw_output), raw_output,
                              ast.BinNumOpType.MUL)

    assert utils.cmp_exprs(actual, expected)
Beispiel #8
0
def test_multi_class_sigmoid_output_transform():
    estimator = lightgbm.LGBMClassifier(n_estimators=1,
                                        random_state=1,
                                        max_depth=1,
                                        sigmoid=0.5,
                                        objective="ovr")
    estimator.fit(np.array([[1], [2], [3]]), np.array([1, 2, 3]))

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

    sigmoid = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.BinNumExpr(ast.NumVal(0.5), ast.NumVal(-1.3862943611),
                                   ast.BinNumOpType.MUL),
                    ast.BinNumOpType.SUB)), ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV)

    expected = ast.VectorVal([sigmoid] * 3)

    assert utils.cmp_exprs(actual, expected)
Beispiel #9
0
def test_bin_class_sigmoid_output_transform():
    estimator = lightgbm.LGBMClassifier(n_estimators=1,
                                        random_state=1,
                                        max_depth=1,
                                        sigmoid=0.5)
    utils.get_binary_classification_model_trainer()(estimator)

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

    sigmoid = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.BinNumExpr(
                        ast.NumVal(0.5),
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(20),
                                         ast.NumVal(16.795),
                                         ast.CompOpType.GT),
                            ast.NumVal(0.5500419366076967),
                            ast.NumVal(1.2782342253678096)),
                        ast.BinNumOpType.MUL), ast.BinNumOpType.SUB)),
            ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV,
        to_reuse=True)

    expected = ast.VectorVal([
        ast.BinNumExpr(ast.NumVal(1), sigmoid, ast.BinNumOpType.SUB), sigmoid
    ])

    assert utils.cmp_exprs(actual, expected)
Beispiel #10
0
def test_log1p_exp_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       objective="cross_entropy_lambda")
    utils.get_bounded_regression_model_trainer()(estimator)

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

    expected = ast.Log1pExpr(
        ast.ExpExpr(
            ast.BinNumExpr(
                ast.IfExpr(
                    ast.CompExpr(ast.FeatureRef(12), ast.NumVal(19.23),
                                 ast.CompOpType.GT),
                    ast.NumVal(0.6622623010380544),
                    ast.NumVal(0.6684065452877841)),
                ast.IfExpr(
                    ast.CompExpr(ast.FeatureRef(12), ast.NumVal(15.145),
                                 ast.CompOpType.GT),
                    ast.NumVal(0.1404975120475147),
                    ast.NumVal(0.14535916856709272)), ast.BinNumOpType.ADD)))

    assert utils.cmp_exprs(actual, expected)
Beispiel #11
0
def test_maybe_sqr_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       reg_sqrt=True,
                                       objective="regression_l1")
    utils.get_regression_model_trainer()(estimator)

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

    raw_output = ast.IdExpr(ast.BinNumExpr(
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(12), ast.NumVal(9.725),
                         ast.CompOpType.GT), ast.NumVal(4.569350528717041),
            ast.NumVal(4.663526439666748)),
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(12), ast.NumVal(11.655),
                         ast.CompOpType.GT), ast.NumVal(-0.04462450027465819),
            ast.NumVal(0.033305134773254384)), ast.BinNumOpType.ADD),
                            to_reuse=True)

    expected = ast.BinNumExpr(ast.AbsExpr(raw_output), raw_output,
                              ast.BinNumOpType.MUL)

    assert utils.cmp_exprs(actual, expected)
Beispiel #12
0
def test_regression_with_negative_values():
    estimator = lightgbm.LGBMRegressor(n_estimators=3,
                                       random_state=1,
                                       max_depth=1)
    utils.get_regression_w_missing_values_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.BinNumExpr(
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(8),
                             ast.NumVal(0.0), ast.CompOpType.GT),
                ast.NumVal(156.64462853604854),
                ast.NumVal(148.40956590509697)),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(2),
                             ast.NumVal(0.00780560282464346),
                             ast.CompOpType.GT), ast.NumVal(4.996373375352607),
                ast.NumVal(-3.1063596100284814)), ast.BinNumOpType.ADD),
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(8), ast.NumVal(-0.0010539205031971832),
                         ast.CompOpType.LTE), ast.NumVal(-3.5131100858883424),
            ast.NumVal(3.6285643795846214)), ast.BinNumOpType.ADD)

    assert utils.cmp_exprs(actual, expected)
Beispiel #13
0
def test_simple_sigmoid_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       objective="cross_entropy")
    utils.get_bounded_regression_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.BinNumExpr(
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(12), ast.NumVal(19.23),
                                         ast.CompOpType.GT),
                            ast.NumVal(4.002437528537838),
                            ast.NumVal(4.090096709787509)),
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(12),
                                         ast.NumVal(14.895),
                                         ast.CompOpType.GT),
                            ast.NumVal(-0.0417499606641773),
                            ast.NumVal(0.02069953712454655)),
                        ast.BinNumOpType.ADD), ast.BinNumOpType.SUB)),
            ast.BinNumOpType.ADD), ast.BinNumOpType.DIV)

    assert utils.cmp_exprs(actual, expected)
Beispiel #14
0
def test_regression():
    estimator = lightgbm.LGBMRegressor(n_estimators=2, random_state=1,
                                       max_depth=1)
    utils.train_model_regression(estimator)

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

    expected = ast.SubroutineExpr(
        ast.BinNumExpr(
            ast.BinNumExpr(
                ast.NumVal(0),
                ast.IfExpr(
                    ast.CompExpr(
                        ast.FeatureRef(5),
                        ast.NumVal(6.8455),
                        ast.CompOpType.GT),
                    ast.NumVal(24.007392728914056),
                    ast.NumVal(22.35695742616179)),
                ast.BinNumOpType.ADD),
            ast.IfExpr(
                ast.CompExpr(
                    ast.FeatureRef(12),
                    ast.NumVal(9.63),
                    ast.CompOpType.GT),
                ast.NumVal(-0.4903836928981587),
                ast.NumVal(0.7222498915097475)),
            ast.BinNumOpType.ADD))

    assert utils.cmp_exprs(actual, expected)
Beispiel #15
0
def test_exp_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2, random_state=1,
                                       max_depth=1, objective="poisson")
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.ExpExpr(
        ast.BinNumExpr(
            ast.IfExpr(
                ast.CompExpr(
                    ast.FeatureRef(5),
                    ast.NumVal(6.918),
                    ast.CompOpType.GT),
                ast.NumVal(3.1480683932),
                ast.NumVal(3.1101554907)),
            ast.IfExpr(
                ast.CompExpr(
                    ast.FeatureRef(12),
                    ast.NumVal(9.63),
                    ast.CompOpType.GT),
                ast.NumVal(-0.0111969636),
                ast.NumVal(0.0160298303)),
            ast.BinNumOpType.ADD))

    assert utils.cmp_exprs(actual, expected)
Beispiel #16
0
def test_log1p_exp_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2, random_state=1,
                                       max_depth=1,
                                       objective="cross_entropy_lambda")
    utils.get_bounded_regression_model_trainer()(estimator)

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

    expected = ast.Log1pExpr(
        ast.ExpExpr(
            ast.BinNumExpr(
                ast.IfExpr(
                    ast.CompExpr(
                        ast.FeatureRef(12),
                        ast.NumVal(19.23),
                        ast.CompOpType.GT),
                    ast.NumVal(0.6623502468),
                    ast.NumVal(0.6683497987)),
                ast.IfExpr(
                    ast.CompExpr(
                        ast.FeatureRef(12),
                        ast.NumVal(15.145),
                        ast.CompOpType.GT),
                    ast.NumVal(0.1405181490),
                    ast.NumVal(0.1453602134)),
                ast.BinNumOpType.ADD)))

    assert utils.cmp_exprs(actual, expected)
Beispiel #17
0
def test_regression_with_negative_values():
    estimator = lightgbm.LGBMRegressor(n_estimators=3,
                                       random_state=1,
                                       max_depth=1)
    utils.get_regression_w_missing_values_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.BinNumExpr(
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(8),
                             ast.NumVal(0.0), ast.CompOpType.GT),
                ast.NumVal(155.96889994777868),
                ast.NumVal(147.72971715548434)),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(2),
                             ast.NumVal(0.00780560282464346),
                             ast.CompOpType.GT), ast.NumVal(4.982244683562974),
                ast.NumVal(-2.978315963345233)), ast.BinNumOpType.ADD),
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(8), ast.NumVal(-0.0010539205031971832),
                         ast.CompOpType.LTE), ast.NumVal(-3.488666332734598),
            ast.NumVal(3.670539900363904)), ast.BinNumOpType.ADD)

    assert utils.cmp_exprs(actual, expected)
Beispiel #18
0
def test_regression_random_forest():
    estimator = lightgbm.LGBMRegressor(boosting_type="rf",
                                       n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       subsample=0.7,
                                       subsample_freq=1)
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.BinNumExpr(
            ast.BinNumExpr(
                ast.NumVal(0),
                ast.IfExpr(
                    ast.CompExpr(ast.FeatureRef(5),
                                 ast.NumVal(6.954000000000001),
                                 ast.CompOpType.GT),
                    ast.NumVal(37.24347877367631),
                    ast.NumVal(19.936999995530854)), ast.BinNumOpType.ADD),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.971500000000001),
                             ast.CompOpType.GT), ast.NumVal(38.48600037864964),
                ast.NumVal(20.183783757300255)), ast.BinNumOpType.ADD),
        ast.NumVal(0.5), ast.BinNumOpType.MUL)

    assert utils.cmp_exprs(actual, expected)
Beispiel #19
0
def test_simple_sigmoid_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       objective="cross_entropy")
    utils.get_bounded_regression_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.BinNumExpr(
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(12), ast.NumVal(19.23),
                                         ast.CompOpType.GT),
                            ast.NumVal(4.0050691250),
                            ast.NumVal(4.0914737728)),
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(12),
                                         ast.NumVal(15.065),
                                         ast.CompOpType.GT),
                            ast.NumVal(-0.0420531079),
                            ast.NumVal(0.0202891577)), ast.BinNumOpType.ADD),
                    ast.BinNumOpType.SUB)), ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV)

    assert utils.cmp_exprs(actual, expected)
Beispiel #20
0
def test_regression_random_forest():
    estimator = lightgbm.LGBMRegressor(boosting_type="rf",
                                       n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       subsample=0.7,
                                       subsample_freq=1)
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.BinNumExpr(
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(12),
                             ast.NumVal(5.200000000000001), ast.CompOpType.GT),
                ast.NumVal(20.195681040256623), ast.NumVal(38.30000037757679)),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.853000000000001),
                             ast.CompOpType.GT), ast.NumVal(36.14745794219976),
                ast.NumVal(19.778245570310993)), ast.BinNumOpType.ADD),
        ast.NumVal(0.5), ast.BinNumOpType.MUL)

    assert utils.cmp_exprs(actual, expected)
Beispiel #21
0
def test_multi_class():
    estimator = lightgbm.LGBMClassifier(n_estimators=1,
                                        random_state=1,
                                        max_depth=1)
    estimator.fit(np.array([[1], [2], [3]]), np.array([1, 2, 3]))

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

    num_expr = ast.NumVal(-1.0986122886681098)
    expected = ast.SoftmaxExpr([num_expr] * 3)

    assert utils.cmp_exprs(actual, expected)
Beispiel #22
0
def test_leaves_cutoff_threshold():
    estimator = lightgbm.LGBMClassifier(n_estimators=2, random_state=1,
                                        max_depth=1)
    utils.train_model_classification_binary(estimator)

    assembler = assemblers.LightGBMModelAssembler(estimator,
                                                  leaves_cutoff_threshold=1)
    actual = assembler.assemble()

    sigmoid = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.SubroutineExpr(
                        ast.BinNumExpr(
                            ast.BinNumExpr(
                                ast.NumVal(0),
                                ast.SubroutineExpr(
                                    ast.SubroutineExpr(
                                        ast.IfExpr(
                                            ast.CompExpr(
                                                ast.FeatureRef(23),
                                                ast.NumVal(868.2000000000002),
                                                ast.CompOpType.GT),
                                            ast.NumVal(0.25986931215073095),
                                            ast.NumVal(0.6237178414050242)))),
                                ast.BinNumOpType.ADD),
                            ast.SubroutineExpr(
                                ast.SubroutineExpr(
                                    ast.IfExpr(
                                        ast.CompExpr(
                                            ast.FeatureRef(7),
                                            ast.NumVal(0.05142),
                                            ast.CompOpType.GT),
                                        ast.NumVal(-0.1909605544006228),
                                        ast.NumVal(0.1293965108676673)))),
                            ast.BinNumOpType.ADD)),
                    ast.BinNumOpType.SUB)),
            ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV,
        to_reuse=True)

    expected = ast.VectorVal([
        ast.BinNumExpr(ast.NumVal(1), sigmoid, ast.BinNumOpType.SUB),
        sigmoid])

    assert utils.cmp_exprs(actual, expected)
Beispiel #23
0
def test_leaves_cutoff_threshold():
    estimator = lightgbm.LGBMClassifier(n_estimators=2,
                                        random_state=1,
                                        max_depth=1)
    utils.train_model_classification_binary(estimator)

    assembler = assemblers.LightGBMModelAssembler(estimator,
                                                  leaves_cutoff_threshold=1)
    actual = assembler.assemble()

    sigmoid = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.SubroutineExpr(
                        ast.BinNumExpr(
                            ast.BinNumExpr(
                                ast.NumVal(0),
                                ast.SubroutineExpr(
                                    ast.IfExpr(
                                        ast.CompExpr(
                                            ast.FeatureRef(23),
                                            ast.NumVal(868.2000000000002),
                                            ast.CompOpType.GT),
                                        ast.NumVal(0.2762557140263451),
                                        ast.NumVal(0.6399134166614473))),
                                ast.BinNumOpType.ADD),
                            ast.SubroutineExpr(
                                ast.IfExpr(
                                    ast.CompExpr(
                                        ast.FeatureRef(27),
                                        ast.NumVal(0.14205000000000004),
                                        ast.CompOpType.GT),
                                    ast.NumVal(-0.2139321843285849),
                                    ast.NumVal(0.1151466338793227))),
                            ast.BinNumOpType.ADD)), ast.BinNumOpType.SUB)),
            ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV,
        to_reuse=True)

    expected = ast.VectorVal([
        ast.BinNumExpr(ast.NumVal(1), sigmoid, ast.BinNumOpType.SUB), sigmoid
    ])

    assert utils.cmp_exprs(actual, expected)
Beispiel #24
0
def test_regression():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1)
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.837500000000001),
                         ast.CompOpType.GT), ast.NumVal(23.961356387224317),
            ast.NumVal(22.32858336612959)),
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(12), ast.NumVal(9.725000000000003),
                         ast.CompOpType.GT), ast.NumVal(-0.5031712645462916),
            ast.NumVal(0.6885501354513913)), ast.BinNumOpType.ADD)

    assert utils.cmp_exprs(actual, expected)
Beispiel #25
0
def test_regression():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1)
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.BinNumExpr(
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(12), ast.NumVal(9.725),
                         ast.CompOpType.GT), ast.NumVal(22.030283219508686),
            ast.NumVal(23.27840740210207)),
        ast.IfExpr(
            ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.8375),
                         ast.CompOpType.GT), ast.NumVal(1.2777791671888081),
            ast.NumVal(-0.2686772850549309)), ast.BinNumOpType.ADD)

    assert utils.cmp_exprs(actual, expected)
Beispiel #26
0
def test_binary_classification():
    estimator = lightgbm.LGBMClassifier(n_estimators=2,
                                        random_state=1,
                                        max_depth=1)
    utils.get_binary_classification_model_trainer()(estimator)

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

    sigmoid = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.BinNumExpr(
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(20),
                                         ast.NumVal(16.795),
                                         ast.CompOpType.GT),
                            ast.NumVal(0.27502096830384837),
                            ast.NumVal(0.6391171126839048)),
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(27),
                                         ast.NumVal(0.14205),
                                         ast.CompOpType.GT),
                            ast.NumVal(-0.21340153096570616),
                            ast.NumVal(0.11583109256834748)),
                        ast.BinNumOpType.ADD), ast.BinNumOpType.SUB)),
            ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV,
        to_reuse=True)

    expected = ast.VectorVal([
        ast.BinNumExpr(ast.NumVal(1), sigmoid, ast.BinNumOpType.SUB), sigmoid
    ])

    assert utils.cmp_exprs(actual, expected)
Beispiel #27
0
def test_exp_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       objective="poisson")
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.ExpExpr(
        ast.BinNumExpr(
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.8375),
                             ast.CompOpType.GT), ast.NumVal(3.1481886430),
                ast.NumVal(3.1123367238)),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(12), ast.NumVal(9.725),
                             ast.CompOpType.GT), ast.NumVal(-0.0113689739),
                ast.NumVal(0.0153551274)), ast.BinNumOpType.ADD))

    assert utils.cmp_exprs(actual, expected)
Beispiel #28
0
def test_binary_classification():
    estimator = lightgbm.LGBMClassifier(n_estimators=2,
                                        random_state=1,
                                        max_depth=1)
    utils.get_binary_classification_model_trainer()(estimator)

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

    sigmoid = ast.BinNumExpr(
        ast.NumVal(1),
        ast.BinNumExpr(
            ast.NumVal(1),
            ast.ExpExpr(
                ast.BinNumExpr(
                    ast.NumVal(0),
                    ast.BinNumExpr(
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(23),
                                         ast.NumVal(868.2000000000002),
                                         ast.CompOpType.GT),
                            ast.NumVal(0.26400127816506497),
                            ast.NumVal(0.633133056485969)),
                        ast.IfExpr(
                            ast.CompExpr(ast.FeatureRef(22),
                                         ast.NumVal(105.95000000000002),
                                         ast.CompOpType.GT),
                            ast.NumVal(-0.18744882409486507),
                            ast.NumVal(0.13458899352064668)),
                        ast.BinNumOpType.ADD), ast.BinNumOpType.SUB)),
            ast.BinNumOpType.ADD),
        ast.BinNumOpType.DIV,
        to_reuse=True)

    expected = ast.VectorVal([
        ast.BinNumExpr(ast.NumVal(1), sigmoid, ast.BinNumOpType.SUB), sigmoid
    ])

    assert utils.cmp_exprs(actual, expected)
Beispiel #29
0
def test_exp_output_transform():
    estimator = lightgbm.LGBMRegressor(n_estimators=2,
                                       random_state=1,
                                       max_depth=1,
                                       objective="poisson")
    utils.get_regression_model_trainer()(estimator)

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

    expected = ast.ExpExpr(
        ast.BinNumExpr(
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(12),
                             ast.NumVal(9.725), ast.CompOpType.GT),
                ast.NumVal(3.1043985065105892),
                ast.NumVal(3.1318783133960197)),
            ast.IfExpr(
                ast.CompExpr(ast.FeatureRef(5), ast.NumVal(6.8375),
                             ast.CompOpType.GT),
                ast.NumVal(0.028409619436010138),
                ast.NumVal(-0.0060740730485278754)), ast.BinNumOpType.ADD))

    assert utils.cmp_exprs(actual, expected)