Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Manifold Learning #1624

Merged
merged 2 commits into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions Orange/projection/manifold.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from Orange.distance import SklDistance, SpearmanDistance, PearsonDistance
from Orange.projection import SklProjector

__all__ = ["MDS", "Isomap", "LocallyLinearEmbedding"]
__all__ = ["MDS", "Isomap", "LocallyLinearEmbedding", "SpectralEmbedding",
"TSNE"]


class MDS(SklProjector):
__wraps__ = skl_manifold.MDS
name = 'mds'
name = 'MDS'

def __init__(self, n_components=2, metric=True, n_init=4, max_iter=300,
eps=0.001, n_jobs=1, random_state=None,
Expand Down Expand Up @@ -43,23 +44,61 @@ def fit(self, X, init=None, Y=None):

class Isomap(SklProjector):
__wraps__ = skl_manifold.Isomap
name = 'isomap'
name = 'Isomap'

def __init__(self, n_neighbors=5, n_components=2, eigen_solver='auto',
max_iter=None, path_method='auto',
tol=0, max_iter=None, path_method='auto',
neighbors_algorithm='auto', preprocessors=None):
super().__init__(preprocessors=preprocessors)
self.params = vars()


class LocallyLinearEmbedding(SklProjector):
__wraps__ = skl_manifold.LocallyLinearEmbedding
name = 'lle'
name = 'Locally Linear Embedding'

def __init__(self, n_neighbors=5, n_components=2, reg=0.001,
eigen_solver='auto', tol=1e-06 , max_iter=100,
eigen_solver='auto', tol=1e-06, max_iter=100,
method='standard', hessian_tol=0.0001,
modified_tol=1e-12, neighbors_algorithm='auto',
random_state=None, preprocessors=None):
super().__init__(preprocessors=preprocessors)
self.params = vars()


class SpectralEmbedding(SklProjector):
__wraps__ = skl_manifold.SpectralEmbedding
name = 'Spectral Embedding'

def __init__(self, n_components=2, affinity='nearest_neighbors', gamma=None,
random_state=None, eigen_solver=None, n_neighbors=None,
preprocessors=None):
super().__init__(preprocessors=preprocessors)
self.params = vars()


class TSNE(SklProjector):
__wraps__ = skl_manifold.TSNE
name = 't-SNE'

def __init__(self, n_components=2, perplexity=30.0, early_exaggeration=4.0,
learning_rate=1000.0, n_iter=1000, n_iter_without_progress=30,
min_grad_norm=1e-07, metric='euclidean', init='random',
random_state=None, method='barnes_hut', angle=0.5,
preprocessors=None):
super().__init__(preprocessors=preprocessors)
self.params = vars()

def __call__(self, data):
if self.params['metric'] is 'precomputed':
X, Y, domain = data, None, None
else:
data = self.preprocess(data)
X, Y, domain = data.X, data.Y, data.domain
distances = SklDistance, SpearmanDistance, PearsonDistance
if isinstance(self.params['metric'], distances):
X = self.params['metric'](X)
self.params['metric'] = 'precomputed'
clf = self.fit(X, Y=Y)
clf.domain = domain
return clf
58 changes: 57 additions & 1 deletion Orange/tests/test_manifold.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import unittest
import numpy as np

from Orange.projection import MDS, Isomap
from Orange.projection import (MDS, Isomap, LocallyLinearEmbedding,
SpectralEmbedding, TSNE)
from Orange.distance import Euclidean
from Orange.data import Table

Expand Down Expand Up @@ -45,3 +46,58 @@ def __isomap_test_helper(self, data, n_com):
isomap_fit = isomap_fit(data)
eshape = data.X.shape[0], n_com
self.assertEqual(eshape, isomap_fit.embedding_.shape)

def test_lle(self):
for i in range(1, 4):
self.__lle_test_helper(self.ionosphere, n_com=i)

def __lle_test_helper(self, data, n_com):
lle = LocallyLinearEmbedding(n_neighbors=5, n_components=n_com)
lle = lle(data)

ltsa = LocallyLinearEmbedding(n_neighbors=5, n_components=n_com,
method="ltsa")
ltsa = ltsa(data)

hessian = LocallyLinearEmbedding(n_neighbors=15, n_components=n_com,
method="hessian",
eigen_solver="dense")
hessian = hessian(data)

modified = LocallyLinearEmbedding(n_neighbors=5, n_components=n_com,
method="modified",
eigen_solver="dense")
modified = modified(data)

self.assertEqual((data.X.shape[0], n_com), lle.embedding_.shape)
self.assertEqual((data.X.shape[0], n_com), ltsa.embedding_.shape)
self.assertEqual((data.X.shape[0], n_com), hessian.embedding_.shape)
self.assertEqual((data.X.shape[0], n_com), modified.embedding_.shape)

def test_se(self):
for i in range(1, 4):
self.__se_test_helper(self.ionosphere, n_com=i)

def __se_test_helper(self, data, n_com):
se = SpectralEmbedding(n_components=n_com, n_neighbors=5)
se = se(data)
self.assertEqual((data.X.shape[0], n_com), se.embedding_.shape)

def test_tsne(self):
data = self.ionosphere[:50]
for i in range(1, 4):
self.__tsne_test_helper(data, n_com=i)

def __tsne_test_helper(self, data, n_com):
tsne_def = TSNE(n_components=n_com, metric='euclidean')
tsne_def = tsne_def(data)

tsne_euc = TSNE(n_components=n_com, metric=Euclidean)
tsne_euc = tsne_euc(data)

tsne_pre = TSNE(n_components=n_com, metric='precomputed')
tsne_pre = tsne_pre(Euclidean(data))

self.assertEqual((data.X.shape[0], n_com), tsne_def.embedding_.shape)
self.assertEqual((data.X.shape[0], n_com), tsne_euc.embedding_.shape)
self.assertEqual((data.X.shape[0], n_com), tsne_pre.embedding_.shape)
Loading