小弟用 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