import numpy as np
import matplotlib.pyplot as plt
dataSet = np.genfromtxt('ex2data2.txt',delimiter=',')
x_data = dataSet[:,:-1]
y_data = dataSet[:,-1]
def plot():
plt.scatter(x_data[y_data==0,0],x_data[y_data==0,1],c='r',marker='*',label='label0')
plt.scatter(x_data[y_data==1,0],x_data[y_data==1,1],c='b',marker='^',label='label1')
plt.legend()
plot()
plt.show()
from sklearn import svm
model = svm.SVC(kernel='rbf')
model.fit(x_data,y_data)
model.score(x_data,y_data)
'''
决策边界可视化
'''
x_min,x_max = x_data[:,0].min()-1,x_data[:,0].max()+1
y_min,y_max = x_data[:,1].min()-1,x_data[:,1].max()+1
xx,yy = np.meshgrid(np.arange(x_min,x_max,0.02),
np.arange(y_min,y_max,0.02))
'''
np.r_按行进行组合array(上下)
np.c_按列进行组合(左右)
例如:
a = np.array([1,2,3])
b = np.array([5,2,5])
np.r_[a,b]
array([1, 2, 3, 5, 2, 5])
np.c_[a,b]
array([[1, 5],
[2, 2],
[3, 5]])
'''
x_new = np.c_[xx.ravel(),yy.ravel()]
print(type(x_new))
z = model.predict(x_new)
z = z.reshape(xx.shape)
cs = plt.contourf(xx,yy,z)
plot()
plt.show()