使用scikit-learn的SVM分类算法(RBF核)时出现意外结果

我参考了这个页面上的示例 http://scikit-learn.org/stable/auto_examples/svm/plot_iris.html,使用标准差为10的正态分布数据替代了鸢尾花数据,创建了自己的图表。

我的图表结果如下所示:enter image description here

请注意,RBF核的图表与示例中的图表非常不同。除了红色和蓝色部分外,整个区域都被分类为黄色。换句话说,支持向量太多了。我尝试更改C和degree,但没有帮助。生成此图表的代码如下所示。

请注意,我需要使用RBF核,因为多项式核的运行速度明显慢于RBF核。

import numpy as npimport pylab as plfrom sklearn import svm, datasetsFP_SIZE = 50STD = 10def 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, targetfp = [[-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 datasetY = targeth = .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 vectorsC = 1.0  # SVM regularization parametersvc = 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 inx_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1xx, yy = np.meshgrid(np.arange(x_min, x_max, h),                     np.arange(y_min, y_max, h))# title for the plotstitles = ['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()

回答:

除了点上的结果外,你还使用了其他正确性测量方法吗?

通常SVM需要使用网格搜索来运行,特别是如果你使用RBF核,C值只会处理正则化,如果你的数据一开始就不稀疏,这将起到很小的作用。

你需要对gamma和C进行网格搜索,他们在这里有一个很好的示例:

http://scikit-learn.org/0.13/auto_examples/grid_search_digits.html#example-grid-search-digits-py

另外,他们的库已经处理了交叉验证。

请记住,这些示例对于玩具数据集来说很好,但一旦你使用新的数据集,就没有理由相信它会像示例中的数据集那样表现。

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注