Beispiel #1
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.SklearnSVMModelAssembler(estimator)
    actual = assembler.assemble()

    def kernel_ast(sup_vec_value):
        return 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.dual_coef_, estimator.intercept_,
        [kernel_ast(1.0), kernel_ast(2.0)])

    assert utils.cmp_exprs(actual, expected)
Beispiel #2
0
def test_unknown_kernel():
    estimator = svm.SVC(kernel=lambda x, y: np.transpose(x) * y)

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

    assembler = assemblers.SklearnSVMModelAssembler(estimator)
    assembler.assemble()
Beispiel #3
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.SklearnSVMModelAssembler(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)
Beispiel #4
0
def test_rbf_kernel():
    estimator = svm.SVC(kernel="rbf", random_state=1, gamma=2.0)

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

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

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

    assert utils.cmp_exprs(actual, expected)