CVXOPT中svm分类器的轮廓图

问题描述

我已经在CVXOPT中构建了一个svm,现在需要它来绘制决策边界。我只是不知所措,因此任何帮助将不胜感激。 svm正在工作,因为它得到零错误分类,我不确定如何绘制决策边界。有人告诉我使用matplotlib.contour,但是我不确定要给它提供什么参数。这是我的代码

import cvxopt as cvx
import numpy as np
import matplotlib.pyplot as plt
import scipy.spatial

bv = cvx.matrix(0.0)


def makeB(K,lambdas,X,t,zero_tol=1e-10,precision=6):
    """Make bias under kernel K.  Lambdas and ts cvxopt column vectors.
       X is a cvxopt matrix with 1 training vector per row."""
    supports = 0
    b = 0
    for s in range(len(lambdas)):
        if lambdas[s] > zero_tol:  # lambdas[s] is a support vector if > 0.
            supports += 1
            b += t[s] - sum(cvx.mul(cvx.mul(lambdas,t),K(X,X[s,:])))
    return round(b / supports,precision)


def kernClassify(xvec,K,Xs,ts,b,zero_tol=1e-10):
    """Requires X to be a matrix of training inputs arranged as cvxopt row vectors.
       'xvec',the input to be classified can be a cvxopt row vector,a Python list
       representing a row vector,or a NumPy 1-d array."""
    # Do conversions on xvec if needed to coerce to a cvxopt matrix with 1 row and n cols
    # (i.e.,a cvxopt row vector).
    if isinstance(xvec,list):  # Convert Python list to cvxopt row vector.
        xvec = cvx.matrix(xvec).T
    elif isinstance(xvec,np.ndarray):  #  Convert NumPy array to cvxopt row vector.
        xv = xvec
        xvec = cvx.matrix(xv) if xv.shape[0] == 1 else cvx.matrix(xv).T
    # -----------------------------------------------------------
    # Actual calculation.  y is activation level.
    y = b + sum(cvx.mul(cvx.mul(lambdas,ts),K(Xs,xvec)))
    return +1 if y > 0 else -1


dataset = np.loadtxt("svm-train-20.txt",dtype='float')
print(f"Dataset is of shape {dataset.shape[0]} x {dataset.shape[1]}.")

points,labels = dataset[:,:2],dataset[:,2]

#print("This is points",points)
#print("This is labels",labels)



N = len(labels)

# CVXOPT inputs to qp.
qn = cvx.matrix(-np.ones(N))
Gn = cvx.matrix(-np.eye(N))
hn = cvx.matrix(np.zeros(N))
Xn = cvx.matrix(points)
tn = cvx.matrix(labels)


def Krbf(x,y,s2):
    """RBF kernel on two CVXOPT row vectors,or matrices. s2 is the RBF variance parameter."""
    return cvx.matrix(np.exp(-scipy.spatial.distance.cdist(x,metric='sqeuclidean') / (2 * s2)))


S2 = 0.25  # Variance for RBF kernel.

Pn_rbf = cvx.mul(tn * tn.T,Krbf(Xn,Xn,S2))
r_n_rbf = cvx.solvers.qp(Pn_rbf,qn,Gn,hn,tn.T,bv)

lambdas_rbf = cvx.matrix([round(li,6) for li in r_n_rbf['x']])

Krbf_02 = lambda x,y: Krbf(x,S2)  # RBF kernel with s2 = 0.2

b_rbf = makeB(Krbf_02,lambdas_rbf,tn)

np.set_printoptions(precision=2,suppress=True)
print(np.array(lambdas_rbf).ravel())

print('\n',b_rbf)

misclass = 0
for xvec,lab in zip(points,labels):
    op = kernClassify(xvec,Krbf_02,tn,b_rbf)
    if op != lab:
        misclass += 1
        print(f"[{xvec[0]:7.4f},{xvec[1]:7.4f}] --> {op:+2d} ({lab:+2.0f})")

print(f"\n{misclass}/{len(labels)} misclassifications")

print(np.sum(np.array(lambdas_rbf) > 1e-10))

plt.figure(figsize=(5,5))
plt.scatter(points.T[0],points.T[1],c=labels.T,cmap='bwr')
plt.axis('equal')
plt.grid()
plt.show()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)