How can I find correct parameters in cvxopt.solvers.qp() for SVM?

102 views Asked by At

I have some data and I want to classification like svm with cvxopt function.

In documentation of cvxopt.solvers.qp, there were some matrixes with vectorized and transposed. How can I find correct params (P, q, G, h, A, b) when I know n_samples and n_features?

solution = cvxopt.solvers.qp(P, q, G, h, A, b)
1

There are 1 answers

0
Lazyer On

Solved it. I got a hint how to set parameters of cvx.opt from here. Code is as bellows.

I made a matrix (n_samples, n_samples) and get other parameters with cvxopt.matrix.


class SVM(object):

    def __init__(self, kernel=linear_kernel, C=None):
        self.kernel = kernel
        self.C = C
        if self.C is not None: self.C = float(self.C)

    def fit(self, X, y):
        n_samples, n_features = X.shape

        # Gram matrix
        K = np.zeros((n_samples, n_samples))
        for i in range(n_samples):
            for j in range(n_samples):
                K[i,j] = self.kernel(X[i], X[j])

        P = cvxopt.matrix(np.outer(y,y) * K)
        q = cvxopt.matrix(np.ones(n_samples) * -1)
        A = cvxopt.matrix(y, (1,n_samples))
        b = cvxopt.matrix(0.0)

        if self.C is None:
            G = cvxopt.matrix(np.diag(np.ones(n_samples) * -1))
            h = cvxopt.matrix(np.zeros(n_samples))
        else:
            tmp1 = np.diag(np.ones(n_samples) * -1)
            tmp2 = np.identity(n_samples)
            G = cvxopt.matrix(np.vstack((tmp1, tmp2)))
            tmp1 = np.zeros(n_samples)
            tmp2 = np.ones(n_samples) * self.C
            h = cvxopt.matrix(np.hstack((tmp1, tmp2)))