Skip to content

Commit

Permalink
Merge branch 'dr-spe' of git://github.com/iglesias/shogun
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Apr 4, 2012
2 parents 21ae0ec + a985a56 commit 07e97f4
Show file tree
Hide file tree
Showing 9 changed files with 746 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/undocumented/libshogun/Makefile
Expand Up @@ -70,6 +70,7 @@ TARGETS = basic_minimal \
converter_neighborhoodpreservingembedding \
converter_linearlocaltangentspacealignment \
converter_localitypreservingprojections \
converter_stochasticproximityembedding \
serialization_basic_tests \
library_cover_tree \
kernel_machine_train_locked \
Expand Down
@@ -0,0 +1,62 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Fernando José Iglesias García
* Copyright (C) 2012 Fernando José Iglesias García
*/

#include <shogun/base/init.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/converter/StochasticProximityEmbedding.h>

using namespace shogun;

int main()
{
init_shogun_with_defaults();

int N = 100;
int dim = 3;

// Generate toy data
SGMatrix< float64_t > matrix(dim, N);
for (int i=0; i<N*dim; i++)
matrix[i] = i;

const int32_t feature_cache = 0;
CSimpleFeatures< float64_t >* features = new CSimpleFeatures< float64_t >( feature_cache );
features->set_feature_matrix(matrix.matrix, dim, N);
SG_REF(features);

// Create embedding and set parameters for global strategy
CStochasticProximityEmbedding* spe = new CStochasticProximityEmbedding();
spe->set_target_dim(2);
spe->set_strategy(SPE_GLOBAL);
spe->set_nupdates(40);
SG_REF(spe);

// Apply embedding with global strategy
CSimpleFeatures< float64_t >* embedding = spe->embed(features);
SG_REF(embedding);

// Set parameters for local strategy
spe->set_strategy(SPE_LOCAL);
spe->set_k(12);

// Apply embedding with local strategy
SG_UNREF(embedding);
embedding = spe->embed(features);
SG_REF(embedding);

// Free memory
SG_UNREF(embedding);
SG_UNREF(spe);
SG_UNREF(features);

exit_shogun();

return 0;
}
@@ -0,0 +1,29 @@
from tools.load import LoadMatrix
lm = LoadMatrix()

data = lm.load_numbers('../data/fm_train_real.dat')

parameter_list = [[data, 12]]

def converter_stochasticproximityembedding_modular(data, k):
from shogun.Features import RealFeatures
from shogun.Converter import StochasticProximityEmbedding, SPE_GLOBAL, SPE_LOCAL

features = RealFeatures(data)

converter = StochasticProximityEmbedding()
converter.set_target_dim(1)
converter.set_nupdates(40)
# Embed with local strategy
converter.set_k(k)
converter.set_strategy(SPE_LOCAL)
converter.embed(features)
# Embed with global strategy
converter.set_strategy(SPE_GLOBAL)
converter.embed(features)

return features

if __name__=='__main__':
print 'StochasticProximityEmbedding'
converter_stochasticproximityembedding_modular(*parameter_list[0])
107 changes: 107 additions & 0 deletions examples/undocumented/python_modular/graphical/converter_spe_helix.py
@@ -0,0 +1,107 @@
"""
Shogun demo
Fernando J. Iglesias Garcia
This example shows the use of dimensionality reduction methods, mainly
Stochastic Proximity Embedding (SPE), although Isomap is also used for
comparison. The data selected to be embedded is an helix. Two different methods
of SPE (global and local) are applied showing that the global method outperforms
the local one in this case. Actually the results of local SPE are fairly poor
for this input. Finally, the reduction achieved with Isomap is better than the
two previous ones, more robust against noise. Isomap exploits the
parametrization of the input data.
"""

import math
import mpl_toolkits.mplot3d as mpl3
import numpy as np
import pylab
import util

from shogun.Features import RealFeatures
from shogun.Converter import StochasticProximityEmbedding, SPE_GLOBAL
from shogun.Converter import SPE_LOCAL, Isomap

# Number of data points
N = 500

# Generate helix
t = np.linspace(1, N, N).T / N
t = t*2*math.pi
X = np.r_[ [ ( 2 + np.cos(8*t) ) * np.cos(t) ],
[ ( 2 + np.cos(8*t) ) * np.sin(t) ],
[ np.sin(8*t) ] ]

# Bi-color helix
labels = np.round( (t*1.5) ) % 2

y1 = labels == 1
y2 = labels == 0

# Plot helix

fig = pylab.figure()

fig.add_subplot(2, 2, 1, projection = '3d')

pylab.plot(X[0, y1], X[1, y1], X[2, y1], 'ro')
pylab.plot(X[0, y2], X[1, y2], X[2, y2], 'go')

pylab.title('Original 3D Helix')

# Create features instance
features = RealFeatures(X)

# Create Stochastic Proximity Embedding converter instance
converter = StochasticProximityEmbedding()

# Set target dimensionality
converter.set_target_dim(2)
# Set strategy
converter.set_strategy(SPE_GLOBAL)

# Compute SPE embedding
embedding = converter.embed(features)

X = embedding.get_feature_matrix()

fig.add_subplot(2, 2, 2)

pylab.plot(X[0, y1], X[1, y1], 'ro')
pylab.plot(X[0, y2], X[1, y2], 'go')

pylab.title('SPE with global strategy')

# Compute a second SPE embedding with local strategy
converter.set_strategy(SPE_LOCAL)
converter.set_k(12)
embedding = converter.embed(features)

X = embedding.get_feature_matrix()

fig.add_subplot(2, 2, 3)

pylab.plot(X[0, y1], X[1, y1], 'ro')
pylab.plot(X[0, y2], X[1, y2], 'go')

pylab.title('SPE with local strategy')

# Compute Isomap embedding (for comparison)
converter = Isomap()
converter.set_target_dim(2)
converter.set_k(6)

embedding = converter.embed(features)

X = embedding.get_feature_matrix()

fig.add_subplot(2, 2, 4)

pylab.plot(X[0, y1], X[1, y1], 'ro')
pylab.plot(X[0, y2], X[1, y2], 'go')

pylab.title('Isomap')

pylab.connect('key_press_event', util.quit)
pylab.show()
2 changes: 2 additions & 0 deletions src/interfaces/modular/Converter.i
Expand Up @@ -22,6 +22,7 @@
%rename(LocalityPreservingProjections) CLocalityPreservingProjections;
%rename(MultidimensionalScaling) CMultidimensionalScaling;
%rename(Isomap) CIsomap;
%rename(StochasticProximityEmbedding) CStochasticProximityEmbedding;

%newobject shogun::CEmbeddingConverter::apply;
%newobject shogun::*::embed_kernel;
Expand All @@ -42,4 +43,5 @@
%include <shogun/converter/LocalityPreservingProjections.h>
%include <shogun/converter/MultidimensionalScaling.h>
%include <shogun/converter/Isomap.h>
%include <shogun/converter/StochasticProximityEmbedding.h>

1 change: 1 addition & 0 deletions src/interfaces/modular/Converter_includes.i
Expand Up @@ -14,4 +14,5 @@
#include <shogun/converter/LocalityPreservingProjections.h>
#include <shogun/converter/MultidimensionalScaling.h>
#include <shogun/converter/Isomap.h>
#include <shogun/converter/StochasticProximityEmbedding.h>
%}

0 comments on commit 07e97f4

Please sign in to comment.