无标题文章

D:\Anaconda2\python.exe D:/PyCharm/start/ML/SVM/SVM_CircleBorder.py

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

decision_function_shape=None, degree=3, gamma='auto', kernel='linear',

max_iter=-1, probability=False, random_state=None, shrinking=True,

tol=0.001, verbose=False)

0.6625

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

decision_function_shape=None, degree=3, gamma='auto', kernel='poly',

max_iter=-1, probability=False, random_state=None, shrinking=True,

tol=0.001, verbose=False)

0.7

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',

max_iter=-1, probability=False, random_state=None, shrinking=True,

tol=0.001, verbose=False)

0.9375

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

decision_function_shape=None, degree=3, gamma='auto', kernel='sigmoid',

max_iter=-1, probability=False, random_state=None, shrinking=True,

tol=0.001, verbose=False)

0.6625

本图中无分界线z,测试结果:

后加:

answer = clf.predict(np.c_[xx.ravel(), yy.ravel()])

z = answer.reshape(xx.shape)

plt.contourf(xx,yy,z,cmap=plt.cm.Paired,alpha=0.8)

无标题文章_第1张图片

代码:

====================================================

#coding:utf-8

importnumpyasnp

fromsklearnimportsvm

fromsklearn.cross_validationimporttrain_test_split

importmatplotlib.pyplotasplt

'''数据生成'''

h =0.1

x_min, x_max = -1,1

y_min, y_max = -1,1

xx, yy = np.meshgrid( np.arange(x_min, x_max, h),

np.arange(y_min, y_max, h))

n = xx.shape[0] * xx.shape[1]

x = np.array( [xx.T.reshape(n).T, xx.reshape(n)]).T# [  8.00000000e-01  -6.00000000e-01]

#print x  [ 20*20 ,2 ]

y = (x[:,0]*x[:,0] + x[:,1]*x[:,1] <0.8)#使Y分为两类,圈内圈外

#print  y  bool型 false/true

y.reshape(xx.shape)

x_train, x_test, y_train, y_test = train_test_split(x, y,test_size=0.2)

clf_linear = svm.SVC(kernel='linear').fit(x_train,y_train)

clf_poly = svm.SVC(kernel='poly',degree=3).fit(x_train,y_train)

clf_rbf = svm.SVC().fit(x_train,y_train)#radial basis function

clf_sigmoid = svm.SVC(kernel='sigmoid').fit(x_train,y_train)

titles = [

'LinearSVC(linear kernel)',

'SVC with polynomial(degree 3) kernel',

'SVC with RBF kernel',

'SVC with Sigmoid kernel'

]

for i,clf in enumerate( (clf_linear,clf_poly,clf_rbf,clf_sigmoid)):

print clf

answer = clf.predict(x_test)

print(np.mean( answer == y_test ))

print"\n"

plt.subplot(2,2,i+1)

plt.subplots_adjust(wspace=0.4,hspace=0.4)

#Put the result into a color plot

answer = clf.predict(np.c_[xx.ravel(), yy.ravel()])

z = answer.reshape(xx.shape)

plt.contourf(xx,yy,z,cmap=plt.cm.Paired,alpha=0.8)

plt.scatter(x_train[:,0], x_train[:,1],c=y_train,cmap=plt.cm.Paired)

plt.xlim(xx.min(),xx.max())

plt.ylim(yy.min(),yy.max())

plt.xticks(())

plt.yticks(())

plt.title(titles[i])

plt.show()

===================================================

results:

===================================================

D:\Anaconda2\python.exe D:/PyCharm/start/ML/SVM/SVM_CircleBorder.py

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

decision_function_shape=None, degree=3, gamma='auto', kernel='linear',

max_iter=-1, probability=False, random_state=None, shrinking=True,

tol=0.001, verbose=False)

0.5875

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

decision_function_shape=None, degree=3, gamma='auto', kernel='poly',

max_iter=-1, probability=False, random_state=None, shrinking=True,

tol=0.001, verbose=False)

0.65

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',

max_iter=-1, probability=False, random_state=None, shrinking=True,

tol=0.001, verbose=False)

0.975

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

decision_function_shape=None, degree=3, gamma='auto', kernel='sigmoid',

max_iter=-1, probability=False, random_state=None, shrinking=True,

tol=0.001, verbose=False)

0.5875


无标题文章_第2张图片

你可能感兴趣的:(无标题文章)