#线性支持向量机
#代码:
from sklearn import datasets
x,y = datasets.make_classification(n_samples=100,n_features=2,n_redundant=0,n_classes=2,random_state=7816)
x.shape,y.shape
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(x[:,0],x[:,1],c=y,s=100)
plt.xlabel('x values')
plt.ylabel('y values')
import numpy as np
x = x.astype(np.float32)
y = y*2-1
from sklearn import model_selection as ms
x_train,x_test,y_train,y_test = ms.train_test_split(x,y,test_size=0.2,random_state=42)
print(x)
print(y)
x = x.astype(np.float32)
y = y*2-1
from sklearn import model_selection as ms
x_train,x_test,y_train,y_test = ms.train_test_split(x,y,test_size=0.2,random_state=42)
import cv2
svm = cv2.ml.SVM_create()
svm.setKernel(cv2.ml.SVM_LINEAR)
x,y = datasets.make_classification(n_samples=100,n_features=2,n_redundant=0,n_classes=2,random_state=7816)
x.shape,y.shape
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(x[:,0],x[:,1],c=y,s=100)
plt.xlabel('x values')
plt.ylabel('y values')
import numpy as np
x = x.astype(np.float32)
y = y*2-1
from sklearn import model_selection as ms
x_train,x_test,y_train,y_test = ms.train_test_split(x,y,test_size=0.2,random_state=42)
print(x)
print(y)
x = x.astype(np.float32)
y = y*2-1
from sklearn import model_selection as ms
x_train,x_test,y_train,y_test = ms.train_test_split(x,y,test_size=0.2,random_state=42)
import cv2
svm = cv2.ml.SVM_create()
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.train(x_train,cv2.ml.ROW_SAMPLE,y_train)
_,y_pred = svm.predict(x_test)
from sklearn import metrics
metrics.accuracy_score(y_test,y_pred)
def plot_decision_boundary(svm, X_test, y_test):
# create a mesh to plot in
h = 0.02 # step size in mesh
x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1
y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
X_hypo = np.c_[xx.ravel().astype(np.float32),
yy.ravel().astype(np.float32)]
_, zz = svm.predict(X_hypo)
zz = zz.reshape(xx.shape)
plt.contourf(xx, yy, zz, cmap=plt.cm.coolwarm, alpha=0.8)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, s=200)
plt.figure(figsize=(10, 6))
plot_decision_boundary(svm, x_test, y_test)
from sklearn import metrics
metrics.accuracy_score(y_test,y_pred)
def plot_decision_boundary(svm, X_test, y_test):
# create a mesh to plot in
h = 0.02 # step size in mesh
x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1
y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
X_hypo = np.c_[xx.ravel().astype(np.float32),
yy.ravel().astype(np.float32)]
_, zz = svm.predict(X_hypo)
zz = zz.reshape(xx.shape)
plt.contourf(xx, yy, zz, cmap=plt.cm.coolwarm, alpha=0.8)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, s=200)
plt.figure(figsize=(10, 6))
plot_decision_boundary(svm, x_test, y_test)
可视化决策边界结果:
#非线性支持向量机:
#代码:
kernels = [cv2.ml.SVM_LINEAR,cv2.ml.SVM_INTER,cv2.ml.SVM_SIGMOID,cv2.ml.SVM_RBF]
for idx,kernel in enumerate(kernels):
svm = cv2.ml.SVM_create()
svm.setKernel(kernel)
svm.train(x_train,cv2.ml.ROW_SAMPLE,y_train)
_,y_pred = svm.predict(x_test)
accuracy = metrics.accuracy_score(y_test,y_pred)
plt.subplot(2,2,idx+1)
plot_decision_boundary(svm,x_test,y_test)
plt.title('accuracy = %.2f' %accuracy)