题目
代码
import numpy
as np
from sklearn
.svm
import SVC
from sklearn
.preprocessing
import StandardScaler
import matplotlib
.pyplot
as plt
def svm_c(x
, y
):
svc
= SVC
(kernel
='linear')
svc
.fit
(x
, y
)
w
= svc
.coef_
[0]
a
= -w
[0] / w
[1]
return svc
, w
, a
if __name__
== '__main__':
x
= np
.array
([[1, 2], [2, 3], [3, 3], [2, 1], [3, 2]])
y
= np
.array
([[1], [1], [1], [-1], [-1]])
svc
, w
, a
= svm_c
(x
, y
)
xx
= np
.linspace
(0, 4)
yy
= a
* xx
- (svc
.intercept_
[0]) / w
[1]
plt
.plot
(xx
, yy
)
plt
.scatter
(x
[:3, 0], x
[:3, 1], color
='red')
plt
.scatter
(x
[3:, 0], x
[3:, 1], color
='blue')
plt
.show
()
结果展示
转载请注明原文地址: https://mac.8miu.com/read-484744.html