如何使用R包Quadprog解决SVM?

gch*_*101 4 r quadprog quadratic-programming

我想知道实现Quadprog解决二次编程的正确方法是什么。

我有以下问题(从互联网上窃听),并且还在查看以下http://cbio.ensmp.fr/~thocking/mines-course/2011-04-01-svm/svm-qp.pdf

在此处输入图片说明

解决此问题的正确方法是什么?如果我遇到上述问题,本教程对解决问题会有用吗? http://www.r-bloggers.com/solving-quadratic-progams-with-rs-quadprog-package/

小智 5

这是基于原始优化问题的线性C-SVM实现:

min_{beta_0, beta, zeta} 1/2 w^T w + C sum_{i = 1}^N zeta_i
subject to:
    y_i (w^T x_i + b) >= 1 - zeta_i, for all i = 1, 2, ..., N
    zeta_i >= 0, for all i = 1, 2, ..., N
Run Code Online (Sandbox Code Playgroud)

N数据点的数量在哪里。

请注意quadprog,在某种程度上,用来解决此问题的方法是更多的教学工作,因为它quadprog依赖于内部点算法,而在实践中将使用专门的算法,例如Platt的SMO,该算法利用了SVM的特定属性优化问题。

为了使用quadprog,给出上述方程式,所有这些都归结为设置指定优化问题的矩阵和向量。

但是,一个问题是,quadprog要求二次函数中出现的矩阵是正定的(例如,请参见http://www.r-bloggers.com/more-on-quadratic-progamming-in-r/) ,尽管此处使用的实现导致其为正半定号,因为截距beta_0zeta_i不会出现在二次函数中。为了解决这个问题,我将与矩阵中这些值相对应的对角元素设置为非常小的值。

要使用spam数据集设置示例代码,请执行二进制分类问题:

library(kernlab) # for the spam data
# Load the input data to be used
data(spam)

# Use only a subset of the data (20%)
spam <- spam[sample(nrow(spam), round(0.2 * nrow(spam)), replace = FALSE), ]

# Retrieve the features and data
X <- spam[, 1:(ncol(spam) - 1)]
Y_f <- spam[, ncol(spam)]
Y <- 2 * (as.numeric(Y_f) - 1.5) # {-1, 1}

# Sample size
N <- nrow(X)
# Number of dimensions
n_d <- ncol(X)

# Value of the regularization parameter
C <- 1
Run Code Online (Sandbox Code Playgroud)

为了设置优化问题,请记住package所采用的格式quadprog

#
# Formulation: min(?d^T * b + 0.5 * b^T * D * b) with the constraints A^T * b >= b_0
#
# solve.QP(Dmat, dvec, Amat, bvec, meq=0, factorized=FALSE)
#
# Arguments
#   Dmat: matrix appearing in the quadratic function to be minimized.
#   dvec: vector appearing in the quadratic function to be minimized.
#   Amat: matrix defining the constraints under which we want to minimize the quadratic function.
#   bvec: vector holding the values of b0 (defaults to zero).
#   meq: the first meq constraints are treated as equality constraints, all further as inequality 
#   constraints (defaults to 0).
#   factorized logical flag: if TRUE, then we are passing R?1 (where D = RT R) instead of the
#   matrix D in the argument Dmat.
#
Run Code Online (Sandbox Code Playgroud)

然后,将参数向量组织为:

# b = (beta_0, beta, zeta), 
# where: beta_0 in R, beta in Re^n_d, zeta in Re^N
Run Code Online (Sandbox Code Playgroud)

这样:

d <- c(0, rep(0, n_d), rep(-C, N)) # -C * sum(zeta)

# Need a work-around for the matrix D, which must be positive definite (being 
# positive semi-definite is not enough...)
# See http://www.r-bloggers.com/more-on-quadratic-progamming-in-r/
eps <- 1e-10 # this will ultimately be the lowest eigenvalue of matrix D (with multiplicity N + 1)
D <- diag(c(eps, rep(1, n_d), rep(eps, N))) # beta^T * beta

#
# Matrix specifying the constraints
# For zeta_i > 0: 
#          beta_0 |    beta       |     zeta
#   A_1 = [      0,   0, 0, ..., 0, 1, 0, 0, ..., 0]
#         [      0,   0, 0, ..., 0, 0, 1, 0, ..., 0]
#         [      0,   0, 0, ..., 0, 0, 0, 1, ..., 0]  
#          ...
#         [      0,   0, 0, ..., 0, 0, 0, 0, ..., 1]
# where matrix A_1 has N rows, and N + n_d + 1 columns
#
# For beta_0 * y_i + beta^T * x_i * y_i + zeta_i >= 1:
#          beta_0 |                         beta                        |        zeta
#   A_2 = [    y_1, y_1 * x_{1, 1}, y_1 * x_{2, 2}, ..., y_1 * x{i, n_d}, 1, 0, 0, ..., 0]
#         [    y_2, y_2 * x_{2, 1}, y_2 * x_{2, 2}, ..., y_2 * x{i, n_d}, 0, 1, 0, ..., 0]
#          ...        
#         [    y_N, y_N * x_{N, 1}, y_2 * x_{N, 2}, ..., y_N * x{N, n_d}, 0, 0, 0, ..., 1]
#
I_N <- diag(N) # N x N identity matrix

A_1 <- cbind(matrix(0, ncol = n_d + 1, nrow = N), I_N) # zeta_i > 0, for all i; N rows
A_2 <- as.matrix(cbind(as.matrix(Y), X * as.matrix(Y)[, rep(1, n_d)], I_N)) # zeta_i + beta_0 * y_i + beta^T * x_i * y_i >= 1, for all i; N rows
rownames(A_1) <- NULL; rownames(A_2) <- NULL
colnames(A_1) <- NULL; colnames(A_2) <- NULL

A <- t(rbind(A_1, A_2))
b_0 <- c(rep(0, N), rep(1, N))
Run Code Online (Sandbox Code Playgroud)

最后,解决优化问题并检索参数值:

library(quadprog)
results <- solve.QP(D, d, A, b_0)

# Retrieve the results
b_optim <- results$solution

beta_0 <- b_optim[1]
beta <- b_optim[1 + (1:n_d)]
zeta <- b_optim[(n_d + 1) + (1:N)]
Run Code Online (Sandbox Code Playgroud)

然后,给定一个矩阵X_test,该模型可以用于通过以下方式进行预测:

Y_pred <- sign(apply(X_test, 1, function(x) beta_0 + sum(beta * as.vector(x))))
Run Code Online (Sandbox Code Playgroud)