Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dr-spe' of git://github.com/iglesias/shogun
- Loading branch information
Showing
9 changed files
with
746 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
examples/undocumented/libshogun/converter_stochasticproximityembedding.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
29 changes: 29 additions & 0 deletions
29
examples/undocumented/python_modular/converter_stochasticproximityembedding_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
107
examples/undocumented/python_modular/graphical/converter_spe_helix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.