Esempio n. 1
0
    def test_hypersphere_kmedoids_fit(self):
        gs.random.seed(55)

        manifold = hypersphere.Hypersphere(2)
        metric = hypersphere.HypersphereMetric(2)

        data = manifold.random_von_mises_fisher(kappa=100, n_samples=200)

        kmedoids = RiemannianKMedoids(metric=metric, n_clusters=1)
        center = kmedoids.fit(data)

        self.assertTrue(manifold.belongs(center))
Esempio n. 2
0
def kmedoids_poincare_ball():
    """Run K-medoids on the Poincare ball."""
    n_samples = 20
    dim = 2
    n_clusters = 2
    manifold = PoincareBall(dim=dim)
    metric = manifold.metric

    cluster_1 = gs.random.uniform(low=0.5, high=0.6, size=(n_samples, dim))
    cluster_2 = gs.random.uniform(low=-0.2, high=0, size=(n_samples, dim))
    data = gs.concatenate((cluster_1, cluster_2), axis=0)

    kmedoids = RiemannianKMedoids(metric=metric, n_clusters=n_clusters, init="random")

    centroids = kmedoids.fit(data=data, max_iter=100)
    labels = kmedoids.predict(data=data)

    plt.figure(1)
    colors = ["red", "blue"]

    ax = visualization.plot(
        data,
        space="H2_poincare_disk",
        marker=".",
        color="black",
        point_type=manifold.point_type,
    )

    for i in range(n_clusters):
        ax = visualization.plot(
            data[labels == i],
            ax=ax,
            space="H2_poincare_disk",
            marker=".",
            color=colors[i],
            point_type=manifold.point_type,
        )

    ax = visualization.plot(
        centroids,
        ax=ax,
        space="H2_poincare_disk",
        marker="*",
        color="green",
        s=100,
        point_type=manifold.point_type,
    )

    ax.set_title("Kmedoids on Poincaré Ball Manifold")

    return plt
Esempio n. 3
0
    def test_hypersphere_kmedoids_predict(self):
        gs.random.seed(1234)
        dim = 2

        manifold = hypersphere.Hypersphere(dim)
        metric = hypersphere.HypersphereMetric(dim)

        data = manifold.random_von_mises_fisher(kappa=100, n_samples=200)

        kmedoids = RiemannianKMedoids(metric, n_clusters=5)
        centroids = kmedoids.fit(data, max_iter=100)
        result = kmedoids.predict(data)

        expected = gs.array([
            int(metric.closest_neighbor_index(x_i, centroids)) for x_i in data
        ])
        self.assertAllClose(expected, result)
def kmedoids_hypersphere():
    """Run K-medoids on the sphere."""
    n_samples = 50
    dim = 2
    n_clusters = 2
    manifold = Hypersphere(dim)
    metric = manifold.metric

    # Generate data on north pole
    cluster_1 = manifold.random_von_mises_fisher(kappa=50, n_samples=n_samples)

    # Generate data on south pole
    cluster_2 = manifold.random_von_mises_fisher(kappa=50, n_samples=n_samples)
    for point in cluster_2:
        point[2] = -point[2]

    data = gs.concatenate((cluster_1, cluster_2), axis=0)

    kmedoids = RiemannianKMedoids(metric=metric, n_clusters=n_clusters)
    centroids = kmedoids.fit(data)
    labels = kmedoids.predict(data)

    plt.figure(2)
    colors = ['red', 'blue']

    ax = visualization.plot(data, space='S2', marker='.', color='black')

    for i in range(n_clusters):
        if len(data[labels == i]) > 0:
            ax = visualization.plot(points=data[labels == i],
                                    ax=ax,
                                    space='S2',
                                    marker='.',
                                    color=colors[i])

    ax = visualization.plot(centroids,
                            ax=ax,
                            space='S2',
                            marker='*',
                            s=200,
                            color='green')

    ax.set_title('Kmedoids on Hypersphere Manifold')

    return plt
Esempio n. 5
0
def main():
    """Learning Poincaré graph embedding.

    Learns Poincaré Ball embedding by using Riemannian
    gradient descent algorithm. Then K-means is applied
    to learn labels of each data sample.
    """
    gs.random.seed(1234)

    karate_graph = load_karate_graph()

    hyperbolic_embedding = HyperbolicEmbedding()

    embeddings = hyperbolic_embedding.embed(karate_graph)

    colors = {1: 'b', 2: 'r'}
    group_1 = mpatches.Patch(color=colors[1], label='Group 1')
    group_2 = mpatches.Patch(color=colors[2], label='Group 2')

    circle = visualization.PoincareDisk(point_type='ball')

    _, ax = plt.subplots(figsize=(8, 8))
    ax.axes.xaxis.set_visible(False)
    ax.axes.yaxis.set_visible(False)
    circle.set_ax(ax)
    circle.draw(ax=ax)
    for i_embedding, embedding in enumerate(embeddings):
        x_coords = embedding[0]
        y_coords = embedding[1]
        pt_id = i_embedding
        plt.scatter(
            x_coords, y_coords,
            c=colors[karate_graph.labels[pt_id][0]],
            s=150
        )
        ax.annotate(pt_id, (x_coords, y_coords))

    plt.tick_params(
        which='both')
    plt.title('Poincare Ball Embedding of the Karate Club Network')
    plt.legend(handles=[group_1, group_2])
    plt.show()

    n_clusters = 2

    kmeans = RiemannianKMeans(
        metric=hyperbolic_embedding.manifold.metric,
        n_clusters=n_clusters,
        init='random',
        mean_method='frechet-poincare-ball')

    centroids = kmeans.fit(X=embeddings, max_iter=100)
    labels = kmeans.predict(X=embeddings)

    colors = ['g', 'c', 'm']
    circle = visualization.PoincareDisk(point_type='ball')
    _, ax2 = plt.subplots(figsize=(8, 8))
    circle.set_ax(ax2)
    circle.draw(ax=ax2)
    ax2.axes.xaxis.set_visible(False)
    ax2.axes.yaxis.set_visible(False)
    group_1_predicted = mpatches.Patch(
        color=colors[0], label='Predicted Group 1')
    group_2_predicted = mpatches.Patch(
        color=colors[1], label='Predicted Group 2')
    group_centroids = mpatches.Patch(
        color=colors[2], label='Cluster centroids')

    for _ in range(n_clusters):
        for i_embedding, embedding in enumerate(embeddings):
            x_coords = embedding[0]
            y_coords = embedding[1]
            pt_id = i_embedding
            if labels[i_embedding] == 0:
                color = colors[0]
            else:
                color = colors[1]
            plt.scatter(
                x_coords, y_coords,
                c=color,
                s=150
            )
            ax2.annotate(pt_id, (x_coords, y_coords))

    for _, centroid in enumerate(centroids):
        x_coords = centroid[0]
        y_coords = centroid[1]
        plt.scatter(
            x_coords, y_coords,
            c=colors[2],
            marker='*',
            s=150,
        )

    plt.title('K-means applied to Karate club embedding')
    plt.legend(handles=[group_1_predicted, group_2_predicted, group_centroids])
    plt.show()

    kmedoid = RiemannianKMedoids(
        metric=hyperbolic_embedding.manifold.metric,
        n_clusters=n_clusters,
        init='random')

    centroids = kmedoid.fit(data=embeddings, max_iter=100)
    labels = kmedoid.predict(data=embeddings)

    colors = ['g', 'c', 'm']
    circle = visualization.PoincareDisk(point_type='ball')
    _, ax2 = plt.subplots(figsize=(8, 8))
    circle.set_ax(ax2)
    circle.draw(ax=ax2)
    ax2.axes.xaxis.set_visible(False)
    ax2.axes.yaxis.set_visible(False)
    group_1_predicted = mpatches.Patch(
        color=colors[0], label='Predicted Group 1')
    group_2_predicted = mpatches.Patch(
        color=colors[1], label='Predicted Group 2')
    group_centroids = mpatches.Patch(
        color=colors[2], label='Cluster centroids')

    for _ in range(n_clusters):
        for i_embedding, embedding in enumerate(embeddings):
            x_coords = embedding[0]
            y_coords = embedding[1]
            pt_id = i_embedding
            if labels[i_embedding] == 0:
                color = colors[0]
            else:
                color = colors[1]
            plt.scatter(
                x_coords, y_coords,
                c=color,
                s=150
            )
            ax2.annotate(pt_id, (x_coords, y_coords))

    for _, centroid in enumerate(centroids):
        x_coords = centroid[0]
        y_coords = centroid[1]
        plt.scatter(
            x_coords, y_coords,
            c=colors[2],
            marker='*',
            s=150,
        )

    plt.title('K-Medoids applied to Karate club embedding')
    plt.legend(handles=[group_1_predicted, group_2_predicted, group_centroids])
    plt.show()