Skip to content

Commit

Permalink
tSNE: Add tests that check for embedding correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlin-policar committed Sep 12, 2018
1 parent 5a5aa93 commit 4ccefcd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
48 changes: 48 additions & 0 deletions Orange/tests/test_manifold.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import unittest

import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier

from Orange.data import Table
from Orange.distance import Euclidean
Expand All @@ -12,6 +14,9 @@
from Orange.projection.manifold import torgerson


np.random.seed(42)


class TestManifold(unittest.TestCase):
@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -189,3 +194,46 @@ def test_continue_optimization_inplace(self):

# The embeddings in the table should match the embedding object
np.testing.assert_equal(new_model.embedding.X, new_model.embedding_)

def test_bh_correctness(self):
knn = KNeighborsClassifier(n_neighbors=5)

# Set iterations to 0 so we check that the initialization is fairly random
tsne = TSNE(early_exaggeration_iter=0, n_iter=0, perplexity=30,
negative_gradient_method='bh', initialization='random')
model = tsne(self.iris)

# Evaluate KNN on the random initialization
knn.fit(model.embedding_, self.iris.Y)
predicted = knn.predict(model.embedding_)
self.assertTrue(accuracy_score(predicted, self.iris.Y) < 0.6)

# 100 iterations should be enough for iris
model.optimize(n_iter=100, inplace=True)

# Evaluate KNN on the tSNE embedding
knn.fit(model.embedding_, self.iris.Y)
predicted = knn.predict(model.embedding_)
self.assertTrue(accuracy_score(predicted, self.iris.Y) > 0.95)

def test_fft_correctness(self):
knn = KNeighborsClassifier(n_neighbors=5)

# Set iterations to 0 so we check that the initialization is fairly random
tsne = TSNE(early_exaggeration_iter=0, n_iter=0, perplexity=30,
negative_gradient_method='fft', initialization='random')
model = tsne(self.iris)

# Evaluate KNN on the random initialization
knn.fit(model.embedding_, self.iris.Y)
predicted = knn.predict(model.embedding_)
self.assertTrue(accuracy_score(predicted, self.iris.Y) < 0.6)

# 100 iterations should be enough for iris
model.optimize(n_iter=100, inplace=True)

# Evaluate KNN on the tSNE embedding
knn.fit(model.embedding_, self.iris.Y)
predicted = knn.predict(model.embedding_)
self.assertTrue(accuracy_score(predicted, self.iris.Y) > 0.95)

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
from scipy import sparse

from Orange.data import Table
from Orange.data import Table, Domain, ContinuousVariable, DiscreteVariable
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.unsupervised.owmanifoldlearning import OWManifoldLearning

Expand Down

0 comments on commit 4ccefcd

Please sign in to comment.