Example #1
0
def test_unknown_kernel():
    estimator = svm.SVC(kernel=lambda x, y: np.transpose(x) * y)

    estimator.fit([[1], [2]], [1, 2])

    assembler = assemblers.SVMModelAssembler(estimator)
    assembler.assemble()
Example #2
0
def test_rbf_kernel():
    estimator = svm.SVC(kernel="rbf", random_state=1, gamma=2.0)

    estimator.fit([[1], [2]], [1, 2])

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

    negative_gamma_ast = ast.BinNumExpr(ast.NumVal(0),
                                        ast.NumVal(estimator.gamma),
                                        ast.BinNumOpType.SUB)

    def kernel_ast(sup_vec_value):
        return ast.SubroutineExpr(
            ast.ExpExpr(
                ast.BinNumExpr(
                    negative_gamma_ast,
                    ast.PowExpr(
                        ast.BinNumExpr(ast.NumVal(sup_vec_value),
                                       ast.FeatureRef(0),
                                       ast.BinNumOpType.SUB), ast.NumVal(2)),
                    ast.BinNumOpType.MUL)))

    expected = _create_expected_ast(
        estimator, [kernel_ast(1.0), kernel_ast(2.0)])

    assert utils.cmp_exprs(actual, expected)
Example #3
0
def test_rbf_kernel():
    estimator = svm.SVC(kernel="rbf", random_state=1, gamma=2.0)

    estimator.fit([[1], [2]], [1, 2])

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

    kernels = [_rbf_kernel_ast(estimator, 1.), _rbf_kernel_ast(estimator, 2.)]
    expected = _create_expected_single_output_ast(estimator, kernels)

    assert utils.cmp_exprs(actual, expected)
Example #4
0
def test_linear_kernel():
    estimator = svm.SVC(kernel="linear", random_state=1)

    estimator.fit([[1], [2]], [1, 2])

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

    def kernel_ast(sup_vec_value):
        return ast.SubroutineExpr(
            ast.BinNumExpr(ast.NumVal(sup_vec_value), ast.FeatureRef(0),
                           ast.BinNumOpType.MUL))

    expected = _create_expected_single_output_ast(
        estimator, [kernel_ast(1.0), kernel_ast(2.0)])

    assert utils.cmp_exprs(actual, expected)
Example #5
0
def test_multi_class_rbf_kernel():
    estimator = svm.SVC(kernel="rbf", random_state=1, gamma=2.0)

    estimator.fit([[1], [2], [3]], [1, 2, 3])

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

    kernels = [
        _rbf_kernel_ast(estimator, float(i), to_reuse=True)
        for i in range(1, 4)
    ]

    expected = ast.VectorVal([
        ast.BinNumExpr(
            ast.BinNumExpr(
                ast.NumVal(0.0),
                ast.BinNumExpr(kernels[1], ast.NumVal(-1.0),
                               ast.BinNumOpType.MUL), ast.BinNumOpType.ADD),
            ast.BinNumExpr(kernels[0], ast.NumVal(1.0), ast.BinNumOpType.MUL),
            ast.BinNumOpType.ADD),
        ast.BinNumExpr(
            ast.BinNumExpr(
                ast.NumVal(0.0),
                ast.BinNumExpr(kernels[2], ast.NumVal(-1.0),
                               ast.BinNumOpType.MUL), ast.BinNumOpType.ADD),
            ast.BinNumExpr(kernels[0], ast.NumVal(1.0), ast.BinNumOpType.MUL),
            ast.BinNumOpType.ADD),
        ast.BinNumExpr(
            ast.BinNumExpr(
                ast.NumVal(0.0),
                ast.BinNumExpr(kernels[2], ast.NumVal(-1.0),
                               ast.BinNumOpType.MUL), ast.BinNumOpType.ADD),
            ast.BinNumExpr(kernels[1], ast.NumVal(1.0), ast.BinNumOpType.MUL),
            ast.BinNumOpType.ADD)
    ])

    assert utils.cmp_exprs(actual, expected)
Example #6
0
def test_poly_kernel():
    estimator = svm.SVC(kernel="poly", random_state=1, gamma=2.0, degree=2)

    estimator.fit([[1], [2]], [1, 2])

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

    def kernel_ast(sup_vec_value):
        return ast.SubroutineExpr(
            ast.PowExpr(
                ast.BinNumExpr(
                    ast.BinNumExpr(
                        ast.NumVal(estimator.gamma),
                        ast.BinNumExpr(ast.NumVal(sup_vec_value),
                                       ast.FeatureRef(0),
                                       ast.BinNumOpType.MUL),
                        ast.BinNumOpType.MUL), ast.NumVal(0.0),
                    ast.BinNumOpType.ADD), ast.NumVal(estimator.degree)))

    expected = _create_expected_single_output_ast(
        estimator, [kernel_ast(1.0), kernel_ast(2.0)])

    assert utils.cmp_exprs(actual, expected)