Ejemplo n.º 1
0
def draw_graph():
    dist = []
    dist2 = []
    win = []
    win2 = []
    data1 = []
    data0 = []
    tab = {}
    k = 4

    X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1)

    tab['data'] = X1
    tab['target'] = Y1

    knn = KNN(tab)
    nearest = knn.findKNearest(np.zeros(2), k)

    for i, e in enumerate(tab['data']):
        dist.append([((e[0]**2 + e[1]**2) ** 0.5), tab['target'][i], i])

    dist = sorted(dist, key=lambda x: x[0])

    for i, e in enumerate(dist):
        if e[1]:
            data1.append(e[2])
        else:
            data0.append(e[2])

    for i, e in enumerate(dist[:k]):
        if e[1]:
            win.append(e[2])
        else:
            win2.append(e[2])
        dist2.append(e[0:2])

    plt.plot(tab['data'][data0, 0], tab['data'][data0, 1], 'ro', mew=1.5, ms=1.5) #ms, mw length and width
    plt.plot(tab['data'][data1, 0], tab['data'][data1, 1], 'go', mew=1.5, ms=1.5)
    plt.plot(tab['data'][[win], 0], tab['data'][[win], 1], 'bo', mew=1.5, ms=1.5)
    plt.plot(tab['data'][[win2], 0], tab['data'][[win2], 1], 'bo', mew=1.5, ms=1.5)
    circle = plt.Circle((0, 0), dist[k-1][0], color='black', fill=False, linewidth=1)
    ax = plt.gca()
    ax.add_patch(circle)
    plt.ylim(-3, 3) #hardcode y length
    plt.xlim(-3, 3)
    plt.show()
Ejemplo n.º 2
0
    def test_find_nearest(self):
        tab = {}
        tab['data'] = np.array([[0.2, 0.31], [0.1, 0.4], [2, 3], [2, 2], [1, 4], [1.5, 2], [2, 1.4], [1.3, 3.1],
                                [-1, 2.3], [-1.4,-1.6], [-0.2, -0.3], [-0.9, 1.7], [2, -1.7], [0.5, -0.7], [1.2, -1.2]])

        tab['target'] = np.array([0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1])

        expectedNearestPoint = np.array([[-0.2, -0.3], [0.2, 0.31], [0.1, 0.4], [0.5, -0.7]])
        expectedNearestDist = [[((-0.2)**2 + (-0.3)**2) ** 0.5, 0], [((0.2)**2 + (0.31)**2) ** 0.5, 0],
                               [((0.1)**2 + (0.4)**2) ** 0.5, 0], [((0.5)**2 + (-0.7)**2) ** 0.5, 1]]

        k = 4

        knn = KNN(tab)
        nearest = knn.findKNearest(np.zeros(2), k)

        self.assertListEqual(nearest, expectedNearestDist)