Python: sklearn svm providing a custom loss function

Now I use the sklearn svm module to use its default values. However, it is not well suited for my dataset. Is it possible to provide a custom loss function or a custom kernel? If so, what is the way to write such a function so that it matches what sklearn svm expects and how to pass such a function to the trainer?

Here is an example of how to do this:
SVM custom kernel

the code given here:

def my_kernel(x, y):
"""
We create a custom kernel:

             (2  0)
k(x, y) = x  (    ) y.T
             (0  1)
"""
M = np.array([[2, 0], [0, 1.0]])
return np.dot(np.dot(x, M), y.T)

I would like to understand the logic of this kernel. How to choose a kernel matrix? And what is it y.T?

+4
source share
1 answer

, , , . , .

, :

A - . , . Scikit-learn SVM . , (, , , , ), .

:

def my_kernel(x, y):
    """Compute My Kernel

    Parameters
    ----------
    x : array, shape=(N, D)
    y : array, shape=(M, D)
        input vectors for kernel similarity

    Returns
    -------
    K : array, shape=(N, M)
        matrix of similarities between x and y
    """
    # ... compute something here ...
    return similarity_matrix

, , :

def linear_kernel(x, y):
    return np.dot(x, y.T)

,

def linear_kernel_2(x, y):
    M = np.array([[1, 0],
                  [0, 1]])
    return np.dot(x, np.dot(M, y.T))

M , . ; M, .

, : ( ) SVM .

+1

Source: https://habr.com/ru/post/1613062/


All Articles