python cvxpy搭建 SVM 二分类器

#python版本2.7
#依赖关系:numpy cvxpy
#cvxpy的安装:linux用户:在terminal下写入pip install cvxpy 回车

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

导入数据其中q1x包含100个训练数据的坐标

q1y.dat分别对应这一百个数据的坐标分类,将其缩放到-1 1两种标记

q1x = np.loadtxt("q1x.dat")
q1y = np.loadtxt("q1y.dat")

X=q1x
y=2*(q1y-0.5)
C=1
m=X.shape[0]
n=X.shape[1]

w = cp.Variable(n,1)
b = cp.Variable()
xi = cp.Variable(m,1)

objective = cp.Minimize(1/2*cp.norm(w)+C*sum(xi))
constrains=[cp.multiply(y,(X*w+b))>=1-xi,xi>=0]
prob = cp.Problem(objective,constrains)
result = prob.solve() 
w.value

yp1 = - (w.value[0]*xp + b - C)/w.value[1]; # margin boundary for support vectors for y=1
yp0 = - (w.value[0]*xp + b + C)/w.value[1];# margin boundary for support vectors for y=0
idx0 = np.where(q1y==0) 
idx1 = np.where(q1y==1)
# plt.plot(xp.value,yp.value)
# plt.scatter(q1x[:,0],q1x[:,1])
plt.plot(q1x[idx0, 0], q1x[idx0, 1],'gs'); 
# plt.legend('a')
plt.plot(q1x[idx1, 0], q1x[idx1, 1],'r^'); 
# plt.legend('b')
plt.plot(xp, yp.value,'b--'); 
# plt.legend('center')
plt.plot(xp, yp1.value,'r--');
# plt.legend('soft_up')
plt.plot(xp, yp0.value,'g--'); 

plt.title('decision boundary for a linear SVM classifier with C=1' )
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
# plt.legend('a','b','c','b','c')
# plt.plot(xp, yp0)
plt.show()

绘图结果如图所示:




附加:由于两个数据上传不便,用python世界打印出来附在这下面:

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.]


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值