python3.7使用cvxpy包实现SVM二分类

目前网络上解决该问题都使用python2.7 而且程序还有各种错误,于是自己改动了下程序:

import numpy as np
import cvxpy as cp
import matplotlib.pyplot as plt

# Problem data.
y = np.genfromtxt('q1y.dat')*2-1
y = y.reshape(99,1)
x = np.genfromtxt('q1x.dat')
m = x.shape[0]
n = x.shape[1]
C = 1



# Construct the problem.
w = cp.Variable((n,1))
b = cp.Variable()
xi = cp.Variable((m,1))


objective = cp.Minimize(0.5*cp.norm(w)**2+C*cp.sum(xi))
constraints = [cp.multiply(y, x*w + b) >= 1 - xi, xi >= 0]
prob = cp.Problem(objective, constraints)

# The optimal objective value is returned by `prob.solve()`.
# The optimal value for x is stored in `x.value`.
result = prob.solve()
print(w.value)
# The optimal Lagrange multiplier for a constraint is stored in
# `constraint.dual_value`.
print(prob.value)
#print(constraints[0].dual_value)
#print(0.5*np.linalg.norm(w.value, ord =2)**2+C*np.sum(xi.value))

xp = np.linspace(min(x[:,0]), max(x[:,0]), 100)
yp = - (w.value[0]*xp + b.value)/w.value[1]
yp1 = np.array(- (w.value[0]*xp + b.value - C)/w.value[1] )# margin boundary for support vectors for y=1
yp0 = np.array(- (w.value[0]*xp + b.value + C)/w.value[1] )# margin boundary for support vectors for y=0

idx0 = np.where(y==-1) 
idx1 = np.where(y==1)

plt.plot(x[idx0, 0], x[idx0, 1], 'rx')
plt.plot(x[idx1, 0], x[idx1, 1], 'go')
plt.plot(xp, yp, '-b', xp, yp1, '--g', xp, yp0, '--r')
plt.title('decision boundary for a linear SVM classifier with C={}'.format(C) )
plt.show()
q1x:
[[ 1.3432504 -1.3311479 ] [ 1.8205529 -0.6346681 ] [ 0.98632067 -1.8885762 ] [ 1.9443734 -1.635452 ] [ 0.97673352 -1.3533151 ] [ 1.9458584 -2.0443278 ] [ 2.1075153 -2.1256684 ] [ 2.070373 -2.4634101 ] [ 0.86864964 -2.4119348 ] [ 1.8006594 -2.7739689 ] [ 3.1283787 -3.4452432 ] [ 3.0947429 -3.6446145 ] [ 2.9086652 -4.0065037 ] [ 2.6770338 -3.0198592 ] [ 2.7458671 -2.7100561 ] [ 4.1714647 -3.4622482 ] [ 3.931322 -2.1099044 ] [ 4.378687 -2.3804743 ] [ 4.8016565 -3.3803344 ] [ 4.166105 -2.8138844 ] [ 2.4670141 -1.6108444 ] [ 3.4826743 -1.5533872 ] [ 3.3652482 -1.8164936 ] [ 2.8772788 -1.8511689 ] [ 3.1090444 -1.6384946 ] [ 2.2183701 0.07427956] [ 1.9949873 0.16268659] [ 2.9500308 0.01687302] [ 2.0216009 0.17227387] [ 2.0486921 -0.63581041] [ 0.87548563 -0.54586168] [ 0.57079941 -0.03327866] [ 1.4266468 -0.75288337] [ 0.72265633 -0.8669193 ] [ 0.95346198 -1.4896956 ] [ 4.8333333 0.07017544] [ 4.3070175 1.4152047 ] [ 6.0321637 0.4502924 ] [ 5.4181287 -2.7076023 ] [ 3.4590643 -2.8245614 ] [ 2.7280702 -0.92397661] [ 1.002924 0.77192982] [ 3.6637427 -0.77777778] [ 4.3070175 -1.0409357 ] [ 3.6929825 -0.10526316] [ 5.7397661 -1.625731 ] [ 4.9795322 -1.5087719 ] [ 6.5 -2.9122807 ] [ 5.2426901 0.91812865] [ 1.6754386 0.56725146] [ 5.1708997 1.2103667 ] [ 4.8795188 1.6081848 ] [ 4.664987 1.0695532 ] [ 4.4934321 1.2351592 ] [ 4.1512967 0.8672126 ] [ 3.717708 1.15172 ] [ 3.6224477 1.3106769 ] [ 3.0606943 1.4857163 ] [ 7.0718465 -0.34961651] [ 6.0391832 -0.24756832] [ 6.674748 -0.12484766] [ 6.8461291 0.25977167] [ 6.4270724 -0.14713863] [ 6.8456065 1.4754967 ] [ 7.7054006 1.6045555 ] [ 6.2870658 2.4156427 ] [ 6.9810956 1.2599865 ] [ 7.0990172 2.2155151 ] [ 5.5275479 0.29968421] [ 5.8303489 -0.21974408] [ 6.3594527 0.23944217] [ 6.1004524 -0.04095741] [ 5.6237412 0.37135914] [ 5.8836969 2.7768186 ] [ 5.5781611 3.0682889 ] [ 7.0050662 -0.25781727] [ 4.4538114 0.83941831] [ 5.6495924 1.3053929 ] [ 4.6337489 1.9467546 ] [ 3.6986847 2.2594084 ] [ 4.1193005 2.547451 ] [ 4.7665558 2.7531209 ] [ 3.0812098 2.7985255 ] [ 4.0730994 -3.0292398 ] [ 3.4883041 -1.8888889 ] [ 0.76900585 1.2105263 ] [ 1.5 3.8128655 ] [ 5.7982456 -2.0935673 ] [ 6.8114529 -0.8345673 ] [ 7.1106096 -1.0201158 ] [ 7.494152 -1.7426901 ] [ 3.1374269 0.42105263] [ 1.6754386 0.50877193] [ 2.494152 -0.86549708] [ 4.7748538 0.09941521] [ 5.8274854 -0.69005848] [ 2.2894737 1.9707602 ] [ 2.494152 1.4152047 ] [ 2.0847953 1.3567251 ]]
q1y:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

你可能感兴趣的:(python3.7使用cvxpy包实现SVM二分类)