Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _linear_to_ast(coef, intercept):
feature_weight_mul_ops = []
for index, value in enumerate(coef):
feature_weight_mul_ops.append(
utils.mul(ast.FeatureRef(index), ast.NumVal(value)))
return utils.apply_op_to_expressions(
ast.BinNumOpType.ADD,
ast.NumVal(intercept),
*feature_weight_mul_ops)
range_end = range_start + n_support[i]
support_ranges.append((range_start, range_end))
# One-vs-one decisions.
decisions = []
for i in range(n_support_len):
for j in range(i + 1, n_support_len):
kernel_weight_mul_ops = [
utils.mul(kernel_exprs[k], ast.NumVal(coef[i][k]))
for k in range(*support_ranges[j])
]
kernel_weight_mul_ops.extend([
utils.mul(kernel_exprs[k], ast.NumVal(coef[j - 1][k]))
for k in range(*support_ranges[i])
])
decision = utils.apply_op_to_expressions(
ast.BinNumOpType.ADD,
ast.NumVal(intercept[len(decisions)]),
*kernel_weight_mul_ops
)
decisions.append(decision)
# TODO convert One-vs-one decisions to One-vs-rest
return ast.VectorVal(decisions)
def assemble(self):
coef = 1.0 / self.model.n_estimators
trees = self.model.estimators_
def assemble_tree_expr(t):
assembler = TreeModelAssembler(t)
return utils.apply_bin_op(
ast.SubroutineExpr(assembler.assemble()),
ast.NumVal(coef),
ast.BinNumOpType.MUL)
assembled_trees = [assemble_tree_expr(t) for t in trees]
return utils.apply_op_to_expressions(
ast.BinNumOpType.ADD, *assembled_trees)