Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Sci工具包学习中SVM自定义核函数的参数解析_Python_Numpy_Scipy_Scikit Learn_Svm - Fatal编程技术网

Python Sci工具包学习中SVM自定义核函数的参数解析

Python Sci工具包学习中SVM自定义核函数的参数解析,python,numpy,scipy,scikit-learn,svm,Python,Numpy,Scipy,Scikit Learn,Svm,我遵循教程,尝试在SVM中使用自定义内核。例如,我实现多项式核函数如下: def poly_kernel(x, y): degree = 3 return np.dot(x, y.T) ** 3 # X is some data # y is some target svr = SVR(kernel=poly_kernel, C=1e3, degree=4) y = svr.fit(X, y).predict(X) 然后,结果

我遵循教程,尝试在SVM中使用自定义内核。例如,我实现多项式核函数如下:

   def poly_kernel(x, y):
       degree = 3
       return np.dot(x, y.T) ** 3
    # X is some data
    # y is some target
    svr = SVR(kernel=poly_kernel, C=1e3, degree=4)
    y = svr.fit(X, y).predict(X)
然后,结果似乎与原始的“多边形”非常相似,度数为3。然而,它带来了一个问题,我不知道如何将解析为内核函数的参数

例如,我按如下方式构建:

   def poly_kernel(x, y):
       degree = 3
       return np.dot(x, y.T) ** 3
    # X is some data
    # y is some target
    svr = SVR(kernel=poly_kernel, C=1e3, degree=4)
    y = svr.fit(X, y).predict(X)
它似乎没有正确地将参数解析到内核。我还尝试在内核函数中使用命名参数

    def poly_kernel(x, y, **kwargs):
        degree = 3
        try:
            degree = kwargs.get('degree')
        except:
            pass
        return np.dot(x, y.T) ** 3
但它不起作用

那么,在这种情况下,有没有办法正确解析参数


提前谢谢

在这种情况下,可以动态构造内核函数。我们可以使用lambda获取匿名函数作为变量

例如:

    def linear_kernel(c = 0):
        return lambda x, y: np.dot(x, y.T) + c
当我们想使用它时,我们只需:

    lkf = linear_kernel(c=20)
    svr_linear = SVR(kernel=lkf)
    y_linear = svr_linear.fit(X, y).predict(X)
我调用SVR()时忽略了参数

然而,我不确定这是否是一种肮脏的方式,但至少它是有效的