1、训练数据集
2、不同gamma值训练结果
程序代码如下:from sklearn.svm import SVC
import matplotlib.pyplot as plt
import numpy as np
from plot_decision_regions import plot_decision_regions
#利用随机噪声得到一个异或数据集
np.random.seed(0)
X_xor = np.random.randn(200, 2)
y_xor = np.logical_xor(X_xor[:,0] >0 , X_xor[:,1]>0)
y_xor = np.where(y_xor, 1, -1)
plt.scatter(X_xor[y_xor==1, 0], X_xor[y_xor==1, 1], c='b', marker='x', label='l')
plt.scatter(X_xor[y_xor==-1, 0], X_xor[y_xor==-1, 1], c='r', marker='s', label='l')
plt.ylim(-3.0)
plt.legend()
plt.show()
#利用高斯核函数进行分类
svm = SVC(kernel='rbf', random_state=0, gamma=0.10, C=10.0)
svm.fit(X_xor, y_xor)
plot_decision_regions(X_xor, y_xor, classifier = svm)
plt.legend(loc='upper left')
plt.title('gamma = 0.1')
plt.show()
#
svm = SVC(kernel='rbf', random_state=0, gamma=1.0, C=10.0)
svm.fit(X_xor, y_xor)
plot_decision_regions(X_xor, y_xor, classifier = svm)
plt.legend(loc='upper left')
plt.title('gamma = 1.0')
plt.show()
补充:
#参考《Python 机器学习》,作者:Sebastian Raschaka, 机械工业出版社;
转载本文请联系原作者获取授权,同时请注明本文来自李军科学网博客。
链接地址:http://blog.sciencenet.cn/blog-3377553-1130717.html
上一篇:Python_机器学习_总结6:线性SVM分类Iris
下一篇:Python_机器学习_总结8:决策树