Exemple #1
0
 def test_predict_proba(self):
     """Test the 'predict_proba' class method."""
     training_dataset = gs.array([[0], [1], [2], [3]])
     labels = [0, 0, 1, 1]
     neigh = KNearestNeighborsClassifier(n_neighbors=self.n_neighbors,
                                         distance=self.distance)
     neigh.fit(training_dataset, labels)
     result = neigh.predict_proba([[0.9]])
     expected = gs.array([[2 / 3, 1 / 3]])
     self.assertAllClose(expected, result, atol=gs.atol)
Exemple #2
0
    def test_predict(self):
        """Test the 'predict' class method."""
        training_dataset = gs.array([[0], [1], [2], [3]])
        labels = [0, 0, 1, 1]

        neigh = KNearestNeighborsClassifier(n_neighbors=self.n_neighbors,
                                            metric=self.distance)
        neigh.fit(training_dataset, labels)
        result = neigh.predict([[1.1]])
        expected = gs.array([0])
        self.assertAllClose(expected, result)
Exemple #3
0
def main():
    """Plot the result of a KNN classification on the sphere."""
    sphere = Hypersphere(dim=2)
    sphere_distance = sphere.metric.dist

    n_labels = 2
    n_samples_per_dataset = 10
    n_targets = 200

    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)

    neigh = KNearestNeighborsClassifier(n_neighbors=2,
                                        distance=sphere_distance)
    neigh.fit(training_dataset, labels)
    target_labels = neigh.predict(target)

    plt.figure(0)
    ax = plt.subplot(111, projection="3d")
    plt.title("Training set")
    sphere_plot = visualization.Sphere()
    sphere_plot.draw(ax=ax)
    for i_label in range(n_labels):
        points_label_i = training_dataset[labels == i_label, ...]
        sphere_plot.draw_points(ax=ax, points=points_label_i)

    plt.figure(1)
    ax = plt.subplot(111, projection="3d")
    plt.title("Classification")
    sphere_plot = visualization.Sphere()
    sphere_plot.draw(ax=ax)
    for i_label in range(n_labels):
        target_points_label_i = target[target_labels == i_label, ...]
        sphere_plot.draw_points(ax=ax, points=target_points_label_i)

    plt.show()
Exemple #4
0
        labels.append(data[1])

    labels = np.array(labels)
    pca = PCA(n_components=2)
    features = pca.fit_transform(features)

    scaler = StandardScaler()
    features = scaler.fit_transform(features) * 3
    features = np.concatenate((features, np.zeros_like(features[:, 0:1])), axis=1)

    sphere = Hypersphere(dim=2)
    transformer = ToTangentSpace(sphere)
    base_point = np.array([0, 0, 1])
    s_points = transformer.inverse_transform(features, base_point)

    classifier = KNearestNeighborsClassifier()
    classifier.fit(s_points, labels)

    f_labels = []

    def loss_f(x):
        label = classifier.predict(x[np.newaxis, :])
        f_labels.append(label)
        return label == 0

    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, projection='3d')
    plot_sphere = Sphere()

    n_points = 10000
    f_points = plot_sphere.fibonnaci_points(n_points).swapaxes(0, 1)