V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zhuyongqi9
V2EX  ›  机器学习

关于 CVXOPT 求解二次规划问题报错

  •  
  •   zhuyongqi9 · 2020-11-26 19:06:29 +08:00 · 1386 次点击
    这是一个创建于 1456 天前的主题,其中的信息可能已经有所发展或是发生改变。

    小弟用 SVM 高斯核,样本是西瓜书的西瓜数据集,一直报错,请问是什么问题

    import scipy.io as sico
    import numpy as np
    import cvxopt
    from numpy import linalg
    from sklearn import preprocessing
    
    dataFile='/Users/zhuyongqi/Desktop/机器学习 /数据集 /watermelon_3a.mat'
    data=sico.loadmat(dataFile)
    np.set_printoptions(suppress=True) 
    a = data['watermelon_3a']
    
    b=a[:,[0,1]]
    features=a[:,2]
    # 进行归一化处理
    X_scaled = preprocessing.scale(b)
    #高斯核
    def gaussian_kernel(x, y, sigma=1.0):
        return np.exp(-np.dot((x-y),(x-y).T) / (2 * (sigma ** 2)))
        
    n_samples, n_features = X_scaled.shape
    
    K = np.zeros((n_samples, n_samples))
    for i in range(n_samples):
        for j in range(n_samples):
            K[i,j] = gaussian_kernel(X_scaled[i], X_scaled[j])
    
    E = np.ones([n_samples,1])
    Y = np.zeros([n_samples,n_samples])
    y = np.ones([n_samples,1])
    
    for i in range (n_samples):
        y[i] = features[i]
    y = y.T
    
    for i in range (n_samples):
        Y[i][i] = features[i]
    
    q = E
    P = np.dot(Y,K,Y)
    H = np.zeros([n_samples,1])
    
    G = -1 * np.ones([n_samples,n_samples])
    
    P = cvxopt.matrix(P,(17,17))
    q = cvxopt.matrix(q,(17,1))
    G = cvxopt.matrix(G,(17,17))
    A = cvxopt.matrix(y,(1,17))
    h = cvxopt.matrix(H,(17,1))
    b = cvxopt.matrix(0.0)
    
    sol = cvxopt.solvers.qp(P,q,G,h,A,b) 
    
    
    

    报错情况:

    Traceback (most recent call last):
      File "/Users/zhuyongqi/python/Experment_05/test.py", line 52, in <module>
        sol = cvxopt.solvers.qp(P,q,G,h,A,b) 
      File "/usr/local/anaconda3/lib/python3.8/site-packages/cvxopt/coneprog.py", line 4487, in qp
        return coneqp(P, q, G, h, None, A,  b, initvals, kktsolver = kktsolver, options = options)
      File "/usr/local/anaconda3/lib/python3.8/site-packages/cvxopt/coneprog.py", line 2067, in coneqp
        raise ValueError("Rank(A) < p or Rank([P; A; G]) < n")
    ValueError: Rank(A) < p or Rank([P; A; G]) < n
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2748 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 15:14 · PVG 23:14 · LAX 07:14 · JFK 10:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.