Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.SubroutineExpr(
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)
def test_reused_expr():
reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)
interpreter = interpreters.PythonInterpreter()
expected_code = """
import math
def score(input):
var0 = math.exp(1.0)
return (var0) / (var0)
"""
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_reused_expr():
reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)
interpreter = interpreters.GoInterpreter()
expected_code = """
import "math"
func score(input []float64) float64 {
var var0 float64
var0 = math.Exp(1.0)
return (var0) / (var0)
}"""
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_leaves_cutoff_threshold():
estimator = xgboost.XGBClassifier(n_estimators=2, random_state=1,
max_depth=1)
utils.train_model_classification_binary(estimator)
assembler = assemblers.XGBoostModelAssembler(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.0),
ast.SubroutineExpr(
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(20),
ast.NumVal(16.7950001),
ast.CompOpType.GTE),
ast.NumVal(-0.17062147),
ast.NumVal(0.1638484))),
ast.BinNumOpType.ADD),
ast.SubroutineExpr(
def test_reused_expr():
reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)
interpreter = interpreters.CInterpreter()
expected_code = """
#include
double score(double * input) {
double var0;
var0 = exp(1.0);
return (var0) / (var0);
}"""
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_exp_expr():
expr = ast.ExpExpr(ast.NumVal(1.0))
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
score = Math.Exp(1.0)
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_exp_expr():
expr = ast.ExpExpr(ast.NumVal(1.0))
interpreter = interpreters.PythonInterpreter()
expected_code = """
import math
def score(input):
return math.exp(1.0)
"""
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def sigmoid_expr(expr, to_reuse=False):
neg_expr = ast.BinNumExpr(ast.NumVal(0), expr, ast.BinNumOpType.SUB)
exp_expr = ast.ExpExpr(neg_expr)
return ast.BinNumExpr(
ast.NumVal(1),
ast.BinNumExpr(ast.NumVal(1), exp_expr, ast.BinNumOpType.ADD),
ast.BinNumOpType.DIV,
to_reuse=to_reuse)