1、问题背景 使用scikit-learn库的SVM分类算法(RBF核)时,在使用该页面上提供的示例http://scikit-learn.org/stable/auto_examples/svm/plot_iris.html时,我们使用标准差为10的正态分布数据创建了自己的图形,而不是鸢尾花数据。生成的图形如下:
注意,RBF核图形与示例中的图形有很大不同。除红色和蓝色部分外,整个区域都被归类为黄色。换句话说,支持向量太多。尝试更改C和degree参数,但没有帮助。下面是用于生成此图形的代码。
代码语言:python代码运行次数:0复制import numpy as np
import pylab as pl
from sklearn import svm, datasets
FP_SIZE = 50
STD = 10
def gen(fp):
data = []
target = []
fp_count = len(fp)
# generate rssi reading for monitors / fingerprint points
# using scikit-learn data structure
for i in range(0, fp_count):
for j in range(0,FP_SIZE):
target.append(i)
data.append(np.around(np.random.normal(fp[i],STD)))
data = np.array(data)
target = np.array(target)
return data, target
fp = [[-30,-70],[-58,-30],[-60,-60]]
data, target = gen(fp)
# import some data to play with
# iris = datasets.load_iris()
X = data[:, :2] # we only take the first two features. We could
# avoid this ugly slicing by using a two-dim dataset
Y = target
h = .02 # step size in the mesh
# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0 # SVM regularization parameter
svc = svm.SVC(kernel='linear', C=C).fit(X, Y)
rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, Y)
poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, Y)
lin_svc = svm.LinearSVC(C=C).fit(X, Y)
# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
# title for the plots
titles = ['SVC with linear kernel',
'SVC with RBF kernel',
'SVC with polynomial (degree 3) kernel',
'LinearSVC (linear kernel)']
for i, clf in enumerate((svc, rbf_svc, poly_svc, lin_svc)):
# Plot the decision boundary. For that, we will asign a color to each
# point in the mesh [x_min, m_max]x[y_min, y_max].
pl.subplot(2, 2, i 1)
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
pl.contourf(xx, yy, Z, cmap=pl.cm.Paired)
pl.axis('off')
# Plot also the training points
pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)
pl.title(titles[i])
pl.show()
2、解决方案 您使用过除了点之外的任何其他正确性度量吗?通常需要使用网格搜索来运行SVM,特别是如果您有RBF时,C只负责正则化,如果您的数据一开始不稀疏,正则化作用很小。 您需要对gamma和C运行网格搜索,他们这里有一个很好的例子: http://scikit-learn.org/0.13/auto_examples/grid_search_digits.html#example-grid-search-digits-py 此外,他们的库已经照顾了交叉验证。 请记住,这些示例适用于玩具数据集,一旦您进入新数据集,就没有理由相信它会与示例中的数据集有任何相似之处。