求解argmax时用到二次规划,matlab中 quadprog使用很简单,但是在python中这一类资料却很少,网上给出的大部分答案都是,但是对各个变量的说明几乎没有,我带入自己的变量时总是报错
def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None): qp_G = .5 * (P + P.T) # make sure P is symmetric qp_a = -q if A is not None: qp_C = -numpy.vstack([A, G]).T qp_b = -numpy.hstack([b, h]) meq = A.shape[0] else: # no equality constraint qp_C = -G.T qp_b = -h meq = 0 return quadprog.solve_qp(qp_G, qp_a, qp_C, qp_b, meq)[0]
最后使用了另一个module qpsolvers解决了二次规划问题
1. 待解决的二次规划如下:
from qpsolvers import solve_qp
from cvxopt import matrix
def reg_LASSO(phi, output, lam, dim_the=6):
h = phi.dot(phi.T) # φ(x) function
P = matrix(np.vstack((np.hstack((h, -h)), np.hstack((-h, h))))) # coefficient matrix of x^2
q = matrix(np.squeeze(lam*np.ones(shape=(dim_the*2,1))-np.concatenate((phi.dot(output),-phi.dot(output)), axis=0)) )# coefficient matrix of x^1
G = matrix(-np.eye((dim_the*2))) # coefficient matrix(left) of constrained condition
h = matrix(np.zeros(shape=(dim_the*2,1))) # coefficient matrix(right) of constrained condition
the = solvers.qp(P, q, G, h)['x']# output of this function is a dict
the = the[0:dim_the,:]-the[dim_the:,:]
return the
numpy 点乘连乘的简单写法
a.dot(b).dot(c) # a,b and c are numpy array
2. 一次型
import scipy
def reg_RR(phi, output, lam=1, dim_the=6):
[d,n] = phi.shape
f = matrix(np.vstack((np.zeros((d,1)),np.zeros((n,1)))))
A = matrix(np.vstack((np.hstack((-phi.T, -np.eye(n))),np.hstack((phi.T,-np.eye(n))))))
b = np.vstack((-output,output))
the = scipy.optimize.linprog(f,A,b)['x']
the = the[0:d,np.newaxis]
return the
3. numpy array 的逆矩阵不能用A.I,要用np.linalg.inv(A)