def test_predict_poincare_ball_distance(self):
     """Test the 'predict' class method using the Poincare ball distance."""
     dim = 2
     space = PoincareBall(dim=dim)
     distance = space.metric.dist
     training_dataset = gs.array([[1 / 2, 1 / 4], [1 / 2, 0],
                                  [1 / 2, -1 / 4], [-1 / 2, 1 / 4],
                                  [-1 / 2, 0], [-1 / 2, -1 / 4]])
     labels = [0, 0, 0, 1, 1, 1]
     kde = KernelDensityEstimationClassifier(distance=distance,
                                             kernel='distance')
     kde.fit(training_dataset, labels)
     target_dataset = gs.array([[1 / 2, 1 / 5], [1 / 2, 0], [1 / 2, -1 / 5],
                                [-1 / 2, 1 / 5], [-1 / 2, 0],
                                [-1 / 2, -1 / 5]])
     result = kde.predict(target_dataset)
     expected = [0, 0, 0, 1, 1, 1]
     self.assertAllClose(expected, result)
 def test_predict_hypersphere_distance(self):
     """Test the 'predict' class method using the hypersphere distance."""
     dim = 2
     space = Hypersphere(dim=dim)
     distance = space.metric.dist
     training_dataset = gs.array([[1, 0, 0], [3**(1 / 2) / 2, 1 / 2, 0],
                                  [3**(1 / 2) / 2, -1 / 2, 0], [0, 0, 1],
                                  [0, 1 / 2, 3**(1 / 2) / 2],
                                  [0, -1 / 2, 3**(1 / 2) / 2]])
     labels = [0, 0, 0, 1, 1, 1]
     kde = KernelDensityEstimationClassifier(distance=distance)
     kde.fit(training_dataset, labels)
     target_dataset = gs.array([[2**(1 / 2) / 2, 2**(1 / 2) / 2, 0],
                                [0, 1 / 2, -3**(1 / 2) / 2],
                                [0, -1 / 2, -3**(1 / 2) / 2],
                                [-3**(1 / 2) / 2, 1 / 2, 0],
                                [-3**(1 / 2) / 2, -1 / 2, 0],
                                [0, 2**(1 / 2) / 2, 2**(1 / 2) / 2]])
     result = kde.predict(target_dataset)
     expected = [0, 0, 0, 1, 1, 1]
     self.assertAllClose(expected, result)
Example #3
0
 def test_predict_hyperboloid_distance(self):
     """Test the 'predict' class method using the hyperboloid distance."""
     dim = 2
     space = Hyperboloid(dim=dim)
     distance = space.metric.dist
     training_dataset_intrinsic = gs.array([
         [1 / 2, 1 / 4],
         [1 / 2, 0],
         [1 / 2, -1 / 4],
         [-1 / 2, 1 / 4],
         [-1 / 2, 0],
         [-1 / 2, -1 / 4],
     ])
     training_dataset = space.change_coordinates_system(
         training_dataset_intrinsic,
         from_coordinates_system="intrinsic",
         to_coordinates_system="extrinsic",
     )
     labels = [0, 0, 0, 1, 1, 1]
     kde = KernelDensityEstimationClassifier(distance=distance,
                                             kernel="distance")
     kde.fit(training_dataset, labels)
     target_dataset_intrinsic = gs.array([
         [1 / 2, 1 / 5],
         [1 / 2, 0],
         [1 / 2, -1 / 5],
         [-1 / 2, 1 / 5],
         [-1 / 2, 0],
         [-1 / 2, -1 / 5],
     ])
     target_dataset = space.change_coordinates_system(
         target_dataset_intrinsic,
         from_coordinates_system="intrinsic",
         to_coordinates_system="extrinsic",
     )
     result = kde.predict(target_dataset)
     expected = [0, 0, 0, 1, 1, 1]
     self.assertAllClose(expected, result)
Example #4
0
def main():
    """Plot a Kernel Density Estimation Classification on the sphere."""
    sphere = Hypersphere(dim=2)
    sphere_distance = sphere.metric.dist

    n_labels = 2
    n_samples_per_dataset = 10
    n_targets = 200
    radius = np.inf

    kernel = triangular_radial_kernel
    bandwidth = 3

    n_training_samples = n_labels * n_samples_per_dataset
    dataset_1 = sphere.random_von_mises_fisher(
        kappa=10,
        n_samples=n_samples_per_dataset)
    dataset_2 = - sphere.random_von_mises_fisher(
        kappa=10,
        n_samples=n_samples_per_dataset)
    training_dataset = gs.concatenate((dataset_1, dataset_2), axis=0)
    labels_dataset_1 = gs.zeros([n_samples_per_dataset], dtype=gs.int64)
    labels_dataset_2 = gs.ones([n_samples_per_dataset], dtype=gs.int64)
    labels = gs.concatenate((labels_dataset_1, labels_dataset_2))
    target = sphere.random_uniform(n_samples=n_targets)

    labels_colors = gs.zeros([n_labels, 3])
    labels_colors[0, :] = gs.array([0, 0, 1])
    labels_colors[1, :] = gs.array([1, 0, 0])

    kde = KernelDensityEstimationClassifier(
        radius=radius,
        distance=sphere_distance,
        kernel=kernel,
        bandwidth=bandwidth,
        outlier_label='most_frequent')
    kde.fit(training_dataset, labels)
    target_labels = kde.predict(target)
    target_labels_proba = kde.predict_proba(target)

    plt.figure(0)
    ax = plt.subplot(111, projection='3d')
    plt.title('Training set')
    sphere_plot = visualization.Sphere()
    sphere_plot.draw(ax=ax)
    colors = gs.zeros([n_training_samples, 3])
    for i_sample in range(n_training_samples):
        colors[i_sample, :] = labels_colors[labels[i_sample], :]
    sphere_plot.draw_points(ax=ax, points=training_dataset, c=colors)

    plt.figure(1)
    ax = plt.subplot(111, projection='3d')
    plt.title('Classification')
    sphere_plot = visualization.Sphere()
    sphere_plot.draw(ax=ax)
    colors = gs.zeros([n_targets, 3])
    for i_target in range(n_targets):
        colors[i_target, :] = labels_colors[target_labels[i_target], :]
    sphere_plot.draw_points(ax=ax, points=target, c=colors)

    plt.figure(2)
    ax = plt.subplot(111, projection='3d')
    plt.title('Probabilistic classification')
    sphere_plot = visualization.Sphere()
    sphere_plot.draw(ax=ax)
    colors = target_labels_proba @ labels_colors
    sphere_plot.draw_points(ax=ax, points=target, c=colors)

    plt.show()