试图清理最近邻居地块的边界

时间:2017-04-28 17:21:55

标签: python matplotlib scikit-learn nearest-neighbor

我在使用python测试之前用一些训练数据做了一个小小的情节。我的情节现在看起来像这样。训练数据来自傅里叶空间中的字母图像,我已将其掩盖以产生不同的字母值。

enter image description here

这些边界看起来不太理想,我不知道如何修复它们,以便红点和蓝点有自​​己独特的区域。这是我正在使用的代码:

X = np.matrix(X)
#knn = KNeighborsClassifier(n_neighbors=3)
y = [0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2]

h = 0.2  # step size in the mesh


# Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])

n_neighbors = 10
for weights in ['uniform', 'distance']:
    # we create an instance of Neighbours Classifier and fit the data.
    clf = KNeighborsClassifier(n_neighbors, weights=weights)
    clf.fit(X, y)


    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, x_max]x[y_min, y_max].
    x_min, x_max = X[:, 0].min() - 30, X[:, 0].max() + 20
    y_min, y_max = X[:, 1].min() - 5, X[:, 1].max() + 5
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.figure()
    plt.pcolormesh(xx, yy, Z, cmap=cmap_light)

    # Plot also the training points

    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.title("3-Class classification (k = %i, weights = '%s')"
              % (n_neighbors, weights))

plt.show()

是否需要找到不同的数据点来对我的数据进行分类?或者有没有办法改变这些决策边界的形成方式?任何帮助将不胜感激,如果我对某些事情过于模糊,请告诉我。提前谢谢!

0 个答案:

没有答案