當前位置: 首頁>>代碼示例>>Python>>正文


Python solvers.qp方法代碼示例

本文整理匯總了Python中cvxopt.solvers.qp方法的典型用法代碼示例。如果您正苦於以下問題:Python solvers.qp方法的具體用法?Python solvers.qp怎麽用?Python solvers.qp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在cvxopt.solvers的用法示例。


在下文中一共展示了solvers.qp方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: fit

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def fit(self, Xs, Xt):
        '''
        Fit source and target using KMM (compute the coefficients)
        :param Xs: ns * dim
        :param Xt: nt * dim
        :return: Coefficients (Pt / Ps) value vector (Beta in the paper)
        '''
        ns = Xs.shape[0]
        nt = Xt.shape[0]
        if self.eps == None:
            self.eps = self.B / np.sqrt(ns)
        K = kernel(self.kernel_type, Xs, None, self.gamma)
        kappa = np.sum(kernel(self.kernel_type, Xs, Xt, self.gamma) * float(ns) / float(nt), axis=1)

        K = matrix(K)
        kappa = matrix(kappa)
        G = matrix(np.r_[np.ones((1, ns)), -np.ones((1, ns)), np.eye(ns), -np.eye(ns)])
        h = matrix(np.r_[ns * (1 + self.eps), ns * (self.eps - 1), self.B * np.ones((ns,)), np.zeros((ns,))])

        sol = solvers.qp(K, -kappa, G, h)
        beta = np.array(sol['x'])
        return beta 
開發者ID:jindongwang,項目名稱:transferlearning,代碼行數:24,代碼來源:KMM.py

示例2: radius

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def radius(K):
    """evaluate the radius of the MEB (Minimum Enclosing Ball) of examples in
    feature space.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.

    Returns
    -------
    r : np.float64,
        the radius of the minimum enclosing ball of examples in feature space.
    """
    K = validation.check_K(K).numpy()
    n = K.shape[0]
    P = 2 * matrix(K)
    p = -matrix(K.diagonal())
    G = -spdiag([1.0] * n)
    h = matrix([0.0] * n)
    A = matrix([1.0] * n).T
    b = matrix([1.0])
    solvers.options['show_progress']=False
    sol = solvers.qp(P,p,G,h,A,b)
    return abs(sol['primal objective'])**.5 
開發者ID:IvanoLauriola,項目名稱:MKLpy,代碼行數:27,代碼來源:evaluate.py

示例3: projection_in_norm

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def projection_in_norm(self, x, M):
        """
        Projection of x to simplex induced by matrix M. Uses quadratic programming.
        """
        m = M.shape[0]

        # Constrains matrices
        P = opt.matrix(2 * M)
        q = opt.matrix(-2 * M * x)
        G = opt.matrix(-np.eye(m))
        h = opt.matrix(np.zeros((m, 1)))
        A = opt.matrix(np.ones((1, m)))
        b = opt.matrix(1.)

        # Solve using quadratic programming
        sol = opt.solvers.qp(P, q, G, h, A, b)
        return np.squeeze(sol['x']) 
開發者ID:naripok,項目名稱:cryptotrader,代碼行數:19,代碼來源:apriori.py

示例4: _QP

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def _QP(self, x, y):
        # In QP formulation (dual): m variables, 2m+1 constraints (1 equation, 2m inequations)
        m = len(y)
        print x.shape, y.shape
        P = self._kernel(x) * np.outer(y, y)
        P, q = matrix(P, tc='d'), matrix(-np.ones((m, 1)), tc='d')
        G = matrix(np.r_[-np.eye(m), np.eye(m)], tc='d')
        h = matrix(np.r_[np.zeros((m,1)), np.zeros((m,1)) + self.C], tc='d')
        A, b = matrix(y.reshape((1,-1)), tc='d'), matrix([0.0])
        # print "P, q:"
        # print P, q
        # print "G, h"
        # print G, h
        # print "A, b"
        # print A, b
        solution = solvers.qp(P, q, G, h, A, b)
        if solution['status'] == 'unknown':
            print 'Not PSD!'
            exit(2)
        else:
            self.alphas = np.array(solution['x']).squeeze() 
開發者ID:soloice,項目名稱:SVM-python,代碼行數:23,代碼來源:svm.py

示例5: update_h

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def update_h(self):
        """ alternating least squares step, update H under the convexity
        constraint """
        def update_single_h(i):
            """ compute single H[:,i] """
            # optimize alpha using qp solver from cvxopt
            FA = base.matrix(np.float64(np.dot(-self.W.T, self.data[:,i])))
            al = solvers.qp(HA, FA, INQa, INQb, EQa, EQb)
            self.H[:,i] = np.array(al['x']).reshape((1, self._num_bases))

        EQb = base.matrix(1.0, (1,1))
        # float64 required for cvxopt
        HA = base.matrix(np.float64(np.dot(self.W.T, self.W)))
        INQa = base.matrix(-np.eye(self._num_bases))
        INQb = base.matrix(0.0, (self._num_bases,1))
        EQa = base.matrix(1.0, (1, self._num_bases))

        for i in range(self._num_samples):
            update_single_h(i) 
開發者ID:urinieto,項目名稱:msaf,代碼行數:21,代碼來源:aa.py

示例6: update_w

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def update_w(self):
        """ alternating least squares step, update W under the convexity
        constraint """
        def update_single_w(i):
            """ compute single W[:,i] """
            # optimize beta     using qp solver from cvxopt
            FB = base.matrix(np.float64(np.dot(-self.data.T, W_hat[:,i])))
            be = solvers.qp(HB, FB, INQa, INQb, EQa, EQb)
            self.beta[i,:] = np.array(be['x']).reshape((1, self._num_samples))

        # float64 required for cvxopt
        HB = base.matrix(np.float64(np.dot(self.data[:,:].T, self.data[:,:])))
        EQb = base.matrix(1.0, (1, 1))
        W_hat = np.dot(self.data, pinv(self.H))
        INQa = base.matrix(-np.eye(self._num_samples))
        INQb = base.matrix(0.0, (self._num_samples, 1))
        EQa = base.matrix(1.0, (1, self._num_samples))

        for i in range(self._num_bases):
            update_single_w(i)

        self.W = np.dot(self.beta, self.data.T).T 
開發者ID:urinieto,項目名稱:msaf,代碼行數:24,代碼來源:aa.py

示例7: optimize_portfolio

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def optimize_portfolio(n, avg_ret, covs, r_min):
	P = covs
	# x = variable(n)
	q = matrix(numpy.zeros((n, 1)), tc='d')
	# inequality constraints Gx <= h
	# captures the constraints (avg_ret'x >= r_min) and (x >= 0)
	G = matrix(numpy.concatenate((
		-numpy.transpose(numpy.array(avg_ret)),
		-numpy.identity(n)), 0))
	h = matrix(numpy.concatenate((
		-numpy.ones((1,1))*r_min,
		numpy.zeros((n,1))), 0))
	# equality constraint Ax = b; captures the constraint sum(x) == 1
	A = matrix(1.0, (1,n))
	b = matrix(1.0)
	sol = solvers.qp(P, q, G, h, A, b)
	return sol

### setup the parameters 
開發者ID:HPatel-Github,項目名稱:Python_QuantFinance_Research,代碼行數:21,代碼來源:portfolio_allocation.py

示例8: solve_qp

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def solve_qp(P, q, G, h,
             A=None, b=None, sym_proj=False,
             solver='cvxopt'):
    if sym_proj:
        P = .5 * (P + P.T)
    cvxmat(P)
    cvxmat(q)
    cvxmat(G)
    cvxmat(h)
    args = [cvxmat(P), cvxmat(q), cvxmat(G), cvxmat(h)]
    if A is not None:
        args.extend([cvxmat(A), cvxmat(b)])
    sol = qp(*args, solver=solver)
    if not ('optimal' in sol['status']):
        raise ValueError('QP optimum not found: %s' % sol['status'])
    return np.array(sol['x']).reshape((P.shape[1],)) 
開發者ID:iory,項目名稱:scikit-robot,代碼行數:18,代碼來源:cvxopt_solver.py

示例9: margin

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def margin(K,Y):
    """evaluate the margin in a classification problem of examples in feature space.
    If the classes are not linearly separable in feature space, then the
    margin obtained is 0.

    Note that it works only for binary tasks.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.
    Y : (n) array_like,
        the labels vector.
    """
    K, Y = validation.check_K_Y(K, Y, binary=True)
    n = Y.size()[0]
    Y = [1 if y==Y[0] else -1 for y in Y]
    YY = spdiag(Y)
    P = 2*(YY*matrix(K.numpy())*YY)
    p = matrix([0.0]*n)
    G = -spdiag([1.0]*n)
    h = matrix([0.0]*n)
    A = matrix([[1.0 if Y[i]==+1 else 0 for i in range(n)],
                [1.0 if Y[j]==-1 else 0 for j in range(n)]]).T
    b = matrix([[1.0],[1.0]],(2,1))
    solvers.options['show_progress']=False
    sol = solvers.qp(P,p,G,h,A,b)
    return sol['primal objective']**.5 
開發者ID:IvanoLauriola,項目名稱:MKLpy,代碼行數:30,代碼來源:evaluate.py

示例10: opt_radius

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def opt_radius(K, init_sol=None): 
    n = K.shape[0]
    K = matrix(K.numpy())
    P = 2 * K
    p = -matrix([K[i,i] for i in range(n)])
    G = -spdiag([1.0] * n)
    h = matrix([0.0] * n)
    A = matrix([1.0] * n).T
    b = matrix([1.0])
    solvers.options['show_progress']=False
    sol = solvers.qp(P,p,G,h,A,b,initvals=init_sol)
    radius2 = (-p.T * sol['x'])[0] - (sol['x'].T * K * sol['x'])[0]
    return sol, radius2 
開發者ID:IvanoLauriola,項目名稱:MKLpy,代碼行數:15,代碼來源:GRAM.py

示例11: opt_margin

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def opt_margin(K, YY, init_sol=None):
    '''optimized margin evaluation'''
    n = K.shape[0]
    P = 2 * (YY * matrix(K.numpy()) * YY)
    p = matrix([0.0]*n)
    G = -spdiag([1.0]*n)
    h = matrix([0.0]*n)
    A = matrix([[1.0 if YY[i,i]==+1 else 0 for i in range(n)],
                [1.0 if YY[j,j]==-1 else 0 for j in range(n)]]).T
    b = matrix([[1.0],[1.0]],(2,1))
    solvers.options['show_progress']=False
    sol = solvers.qp(P,p,G,h,A,b,initvals=init_sol) 
    margin2 = sol['primal objective']
    return sol, margin2 
開發者ID:IvanoLauriola,項目名稱:MKLpy,代碼行數:16,代碼來源:GRAM.py

示例12: opt_margin

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def opt_margin(K,YY,init_sol=None):
	'''optimized margin evaluation'''
	n = K.shape[0]
	P = 2 * (YY * matrix(K) * YY)
	p = matrix([0.0]*n)
	G = -spdiag([1.0]*n)
	h = matrix([0.0]*n)
	A = matrix([[1.0 if YY[i,i]==+1 else 0 for i in range(n)],
				[1.0 if YY[j,j]==-1 else 0 for j in range(n)]]).T
	b = matrix([[1.0],[1.0]],(2,1))
	solvers.options['show_progress']=False
	sol = solvers.qp(P,p,G,h,A,b,initvals=init_sol)	
	margin2 = sol['primal objective']
	return margin2, sol['x'], sol 
開發者ID:IvanoLauriola,項目名稱:MKLpy,代碼行數:16,代碼來源:MEMO.py

示例13: _fit

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def _fit(self,X,Y):    
        self.X = X
        values = np.unique(Y)
        Y = [1 if l==values[1] else -1 for l in Y]
        self.Y = Y
        npos = len([1.0 for l in Y if l == 1])
        nneg = len([1.0 for l in Y if l == -1])
        gamma_unif = matrix([1.0/npos if l == 1 else 1.0/nneg for l in Y])
        YY = matrix(np.diag(list(matrix(Y))))

        Kf = self.__kernel_definition__()
        ker_matrix = matrix(Kf(X,X).astype(np.double))
        #KLL = (1.0 / (gamma_unif.T * YY * ker_matrix * YY * gamma_unif)[0])*(1.0-self.lam)*YY*ker_matrix*YY
        KLL = (1.0-self.lam)*YY*ker_matrix*YY
        LID = matrix(np.diag([self.lam * (npos * nneg / (npos+nneg))]*len(Y)))
        Q = 2*(KLL+LID)
        p = matrix([0.0]*len(Y))
        G = -matrix(np.diag([1.0]*len(Y)))
        h = matrix([0.0]*len(Y),(len(Y),1))
        A = matrix([[1.0 if lab==+1 else 0 for lab in Y],[1.0 if lab2==-1 else 0 for lab2 in Y]]).T
        b = matrix([[1.0],[1.0]],(2,1))
        
        solvers.options['show_progress'] = False#True
        solvers.options['maxiters'] = self.max_iter
        sol = solvers.qp(Q,p,G,h,A,b)
        self.gamma = sol['x']
        if self.verbose:
            print ('[KOMD]')
            print ('optimization finished, #iter = %d' % sol['iterations'])
            print ('status of the solution: %s' % sol['status'])
            print ('objval: %.5f' % sol['primal objective'])
            
        bias = 0.5 * self.gamma.T * ker_matrix * YY * self.gamma
        self.bias = bias
        self.is_fitted = True
        self.ker_matrix = ker_matrix
        return self 
開發者ID:IvanoLauriola,項目名稱:MKLpy,代碼行數:39,代碼來源:komd.py

示例14: fit_nnl2reg

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def fit_nnl2reg(self):
        try:
            from cvxopt import matrix, solvers
        except ImportError:
            raise ImportError("To infer titer models, you need a working installation of cvxopt")
        n_params = self.design_matrix.shape[1]
        P = matrix(np.dot(self.design_matrix.T, self.design_matrix) + self.lam_drop*np.eye(n_params))
        q = matrix( -np.dot( self.titer_dist, self.design_matrix))
        h = matrix(np.zeros(n_params)) # Gw <=h
        G = matrix(-np.eye(n_params))
        W = solvers.qp(P,q,G,h)
        return np.array([x for x in W['x']]) 
開發者ID:nextstrain,項目名稱:augur,代碼行數:14,代碼來源:titer_model.py

示例15: projection_in_norm

# 需要導入模塊: from cvxopt import solvers [as 別名]
# 或者: from cvxopt.solvers import qp [as 別名]
def projection_in_norm(self, x, M):
        """ Projection of x to simplex indiced by matrix M. Uses quadratic programming.
        """
        m = M.shape[0]

        P = matrix(2*M)
        q = matrix(-2 * M * x)
        G = matrix(-np.eye(m))
        h = matrix(np.zeros((m,1)))
        A = matrix(np.ones((1,m)))
        b = matrix(1.)

        sol = solvers.qp(P, q, G, h, A, b)
        return np.squeeze(sol['x']) 
開發者ID:ZhengyaoJiang,項目名稱:PGPortfolio,代碼行數:16,代碼來源:ons.py


注:本文中的cvxopt.solvers.qp方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。