This page lists ready to run shogun examples for the C# Modular interface.
To run the examples issue
gmcs path/to/shogun/interfaces/csharp_modular/*.cs name_of_example.cs LD_LIBRARY_PATH=path/to/libshogun:path/to/shogun/interfaces/csharp_modular mono name_of_example.exe
// In this example the Averaged Perceptron used to classify toy data.
using System;
public class classifier_averaged_perceptron_modular{
public static void Main() {
modshogun.init_shogun_with_defaults();
double learn_rate = 1.0;
int max_iter = 1000;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
double[] trainlab = Load.load_labels("../data/label_train_twoclass.dat");
RealFeatures feats_train = new RealFeatures();
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);
Labels labels = new Labels(trainlab);
AveragedPerceptron perceptron = new AveragedPerceptron(feats_train, labels);
perceptron.set_learn_rate(learn_rate);
perceptron.set_max_iter(max_iter);
perceptron.train();
perceptron.set_features(feats_test);
double[] out_labels = perceptron.apply().get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example a two-class support vector machine classifier is trained on a
// 2-dimensional randomly generated data set and the trained classifier is used to
// predict labels of test examples. As training algorithm the LIBSVM solver is used
// with SVM regularization parameter C=1 and a Gaussian kernel of width 2.1.
//
// For more details on LIBSVM solver see http://www.csie.ntu.edu.tw/~cjlin/libsvm/
using System;
public class classifier_libsvm_minimal_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int num = 1000;
double dist = 1.0;
double width = 2.1;
double C = 1.0;
Random RandomNumber = new Random();
double[,] traindata_real = new double[2, num * 2];
for (int i = 0; i < num; i ++) {
traindata_real[0, i] = RandomNumber.NextDouble() - dist;
traindata_real[0, i + num] = RandomNumber.NextDouble() + dist;
traindata_real[1, i] = RandomNumber.NextDouble() - dist;
traindata_real[1, i + num] = RandomNumber.NextDouble() + dist;
}
double[,] testdata_real = new double[2, num * 2];
for (int i = 0; i < num; i ++) {
testdata_real[0, i] = RandomNumber.NextDouble() - dist;
testdata_real[0, i + num] = RandomNumber.NextDouble() + dist;
testdata_real[1, i] = RandomNumber.NextDouble() - dist;
testdata_real[1, i + num] = RandomNumber.NextDouble() + dist;
}
double[] trainlab = new double[num * 2];
for (int i = 0; i < num; i ++) {
trainlab[i] = -1;
trainlab[i + num] = 1;
}
double[] testlab = new double[num * 2];
for (int i = 0; i < num; i ++) {
testlab[i] = -1;
testlab[i + num] = 1;
}
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
Labels labels = new Labels(trainlab);
LibSVM svm = new LibSVM(C, kernel, labels);
svm.train();
double[] result = svm.apply(feats_test).get_labels();
int err_num = 0;
for (int i = 0; i < num; i++) {
if (result[i] > 0) {
err_num += 1;
}
if (result[i+num] < 0) {
err_num += 1;
}
}
double testerr=err_num/(2*num);
Console.WriteLine(testerr);
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) controls the processing of the given data points, where a pairwise
// distance matrix is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// matrix between these two data sets is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CBrayCurtisDistance.html.
//
// Obviously, using the Bray Curtis distance is not limited to this showcase
// example.
using System;
public class distance_braycurtis_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
BrayCurtisDistance distance = new BrayCurtisDistance(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) controls the processing of the given data points, where a pairwise
// distance (dissimilarity ratio) matrix is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// matrix between these two data sets is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CCanberraMetric.html.
//
// Obviously, using the Canberra distance is not limited to this showcase
// example.
using System;
public class distance_canberra_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
CanberraMetric distance = new CanberraMetric(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) controls the processing of the given data points, where a pairwise
// distance (maximum of absolute feature dimension differences) matrix is
// computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// (maximum of absolute feature dimension differences) matrix between these
// two data sets is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CChebyshewMetric.html.
//
// Obviously, using the Chebyshew distance is not limited to this showcase
// example.
using System;
public class distance_chebyshew_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
ChebyshewMetric distance = new ChebyshewMetric(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) controls the processing of the given data points, where a pairwise
// distance matrix is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// matrix between these two data sets is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CChiSquareDistance.html.
//
// Obviously, using the ChiSquare distance is not limited to this showcase
// example.
using System;
public class distance_chisquare_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
ChiSquareDistance distance = new ChiSquareDistance(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) controls the processing of the given data points, where a pairwise
// distance matrix is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// matrix between these two data sets is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CCosineDistance.html.
//
// Obviously, using the Cosine distance is not limited to this showcase
// example.
using System;
public class distance_cosine_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
CosineDistance distance = new CosineDistance(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) controls the processing of the given data points, where a pairwise
// distance matrix is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// matrix between these two data sets is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CEuclidianDistance.html.
//
// Obviously, using the Euclidian distance is not limited to this showcase
// example.
using System;
public class distance_euclidian_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) controls the processing of the given data points, where a
// pairwise distance (shortest path on a sphere) matrix is computed
// by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// (shortest path on a sphere) matrix between these two data sets is
// computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CGeodesicMetric.html.
//
// Obviously, using the Geodesic distance is not limited to this showcase
// example.
using System;
public class distance_geodesic_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
GeodesicMetric distance = new GeodesicMetric(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) controls the processing of the given data points, where a pairwise
// distance (divergence measure based on the Kullback-Leibler divergence) matrix
// is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// (divergence measure based on the Kullback-Leibler divergence) matrix between
// these two data sets is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CJensenMetric.html.
//
// Obviously, using the Jensen-Shannon distance/divergence is not limited to
// this showcase example.
using System;
public class distance_jensen_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
JensenMetric distance = new JensenMetric(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// This example shows how to compute the Manahattan Distance for string features.
using System;
public class distance_manhattenword_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int order = 3;
int gap = 0;
bool reverse = false;
String[] fm_train_dna = Load.load_dna("../data/fm_train_dna.dat");
String[] fm_test_dna = Load.load_dna("../data/fm_test_dna.dat");
double[,] fm_test_real = Load.load_numbers("../data/fm_test_real.dat");
StringCharFeatures charfeat = new StringCharFeatures(EAlphabet.DNA);
charfeat.set_features(fm_train_dna);
StringWordFeatures feats_train = new StringWordFeatures(charfeat.get_alphabet());
feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse);
SortWordString preproc = new SortWordString();
preproc.init(feats_train);
feats_train.add_preprocessor(preproc);
feats_train.apply_preprocessor();
StringCharFeatures charfeat_test = new StringCharFeatures(EAlphabet.DNA);
charfeat_test.set_features(fm_test_dna);
StringWordFeatures feats_test = new StringWordFeatures(charfeat.get_alphabet());
feats_test.obtain_from_char(charfeat_test, order-1, order, gap, reverse);
feats_test.add_preprocessor(preproc);
feats_test.apply_preprocessor();
ManhattanWordDistance distance = new ManhattanWordDistance(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) and norm 'k' controls the processing of the given data points,
// where a pairwise distance matrix is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// matrix between these two data sets is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CMinkowskiMetric.html.
//
// Obviously, using the Minkowski metric is not limited to this showcase
// example.
using System;
public class distance_minkowski_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double k = 3;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
MinkowskiMetric distance = new MinkowskiMetric(feats_train, feats_train, k);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example an squared euclidian distance is being computed for toy data.
using System;
public class distance_normsquared_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
distance.set_disable_sqrt(true);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// An approach as applied below, which shows the processing of input data
// from a file becomes a crucial factor for writing your own sample applications.
// This approach is just one example of what can be done using the distance
// functions provided by shogun.
//
// First, you need to determine what type your data will be, because this
// will determine the distance function you can use.
//
// This example loads two stored matrices of real values from different
// files and initializes the matrices to 'RealFeatures'.
// Each column of the matrices corresponds to one data point.
//
// The distance initialized by two data sets (the same data set as shown in the
// first call) controls the processing of the given data points, where a pairwise
// distance (extended Jaccard coefficient) matrix is computed by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// The method call 'init'* binds the given data sets, where a pairwise distance
// (extended Jaccard coefficient) matrix between these two data sets is computed
// by 'get_distance_matrix'.
//
// The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
//
// *Note that the previous computed distance matrix can no longer be
// reaccessed by 'get_distance_matrix'.
//
// For more details see doc/classshogun_1_1CTanimotoDistance.html.
//
// Obviously, using the Tanimoto distance/coefficient is not limited to
// this showcase example.
using System;
public class distance_tanimoto_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
TanimotoDistance distance = new TanimotoDistance(feats_train, feats_train);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
foreach(double item in dm_train) {
Console.Write(item);
}
foreach(double item in dm_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// Trains an inhomogeneous Markov chain of order 3 on a DNA string data set. Due to
// the structure of the Markov chain it is very similar to a HMM with just one
// chain of connected hidden states - that is why we termed this linear HMM.
using System;
public class distribution_linearhmm_modular {
public static void Main() {
bool reverse = false;
modshogun.init_shogun_with_defaults();
int order = 3;
int gap = 4;
String[] fm_train_dna = Load.load_dna("../data/fm_train_dna.dat");
StringCharFeatures charfeat = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringWordFeatures feats = new StringWordFeatures(charfeat.get_alphabet());
feats.obtain_from_char(charfeat, order-1, order, gap, reverse);
LinearHMM hmm = new LinearHMM(feats);
hmm.train();
hmm.get_transition_probs();
int num_examples = feats.get_num_vectors();
int num_param = hmm.get_num_model_parameters();
for (int i = 0; i < num_examples; i++)
for(int j = 0; j < num_param; j++) {
hmm.get_log_derivative(j, i);
}
double[] out_likelihood = hmm.get_log_likelihood();
double out_sample = hmm.get_log_likelihood_sample();
modshogun.exit_shogun();
}
}
// In this example various (accuracy, error rate, ..) measures are being computed
// for the pair of ground truth toy data and random data.
using System;
public class evaluation_contingencytableevaluation_modular {
public static void Main(string[] argv) {
modshogun.init_shogun_with_defaults();
double[] ground_truth = Load.load_labels("../data/label_train_twoclass.dat");
Random RandomNumber = new Random();
double[] predicted = new double[ground_truth.Length];
for (int i = 0; i < ground_truth.Length; i++) {
predicted[i] = RandomNumber.NextDouble();
}
Labels ground_truth_labels = new Labels(ground_truth);
Labels predicted_labels = new Labels(predicted);
ContingencyTableEvaluation base_evaluator = new ContingencyTableEvaluation();
base_evaluator.evaluate(predicted_labels,ground_truth_labels);
AccuracyMeasure evaluator1 = new AccuracyMeasure();
double accuracy = evaluator1.evaluate(predicted_labels,ground_truth_labels);
ErrorRateMeasure evaluator2 = new ErrorRateMeasure();
double errorrate = evaluator2.evaluate(predicted_labels,ground_truth_labels);
BALMeasure evaluator3 = new BALMeasure();
double bal = evaluator3.evaluate(predicted_labels,ground_truth_labels);
WRACCMeasure evaluator4 = new WRACCMeasure();
double wracc = evaluator4.evaluate(predicted_labels,ground_truth_labels);
F1Measure evaluator5 = new F1Measure();
double f1 = evaluator5.evaluate(predicted_labels,ground_truth_labels);
CrossCorrelationMeasure evaluator6 = new CrossCorrelationMeasure();
double crosscorrelation = evaluator6.evaluate(predicted_labels,ground_truth_labels);
RecallMeasure evaluator7 = new RecallMeasure();
double recall = evaluator7.evaluate(predicted_labels,ground_truth_labels);
PrecisionMeasure evaluator8 = new PrecisionMeasure();
double precision = evaluator8.evaluate(predicted_labels,ground_truth_labels);
SpecificityMeasure evaluator9 = new SpecificityMeasure();
double specificity = evaluator9.evaluate(predicted_labels,ground_truth_labels);
Console.Write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}\n", accuracy, errorrate, bal, wracc, f1, crosscorrelation, recall, precision, specificity);
modshogun.exit_shogun();
}
}
// This examples demonstrates how to encode real-valued features in Shogun,
// using RealFeatures.
using System;
public class features_simple_real_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] matrix = new double[6, 3]{{1,2,3},{4,0,0},{0,0,0},{0,5,0},{0,0,6},{9,9,9}};
RealFeatures a = new RealFeatures(matrix);
a.set_feature_vector(new double[] {1, 4, 0, 0, 0, 9}, 0);
double[,] a_out = a.get_feature_matrix();
foreach(double item in a_out) {
Console.Write("{0} ", item);
}
modshogun.exit_shogun();
}
}
// This example demonstrates how to encode ASCII-strings (255 symbols) in shogun.
using System;
public class features_string_char_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
string [] strings = new string[6] { "hey","guys","i","am","a","string"};
StringCharFeatures f = new StringCharFeatures(strings, EAlphabet.RAWBYTE);
string [] r = f.get_features();
foreach(string item in r) {
Console.WriteLine(item);
}
modshogun.exit_shogun();
}
}
// In this example the ANOVA kernel is being computed for toy data.
using System;
public class kernel_anova_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int cardinality = 2;
int size_cache = 5;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
ANOVAKernel kernel = new ANOVAKernel(feats_train, feats_train, cardinality, size_cache);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
modshogun.exit_shogun();
}
}
// In this example the Cauchy kernel is being computed for toy data.
using System;
public class kernel_cauchy_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double sigma = 1.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
CauchyKernel kernel = new CauchyKernel(feats_train, feats_train, sigma, distance);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test=kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// This is an example for the initialization of the chi2-kernel on real data, where
// each column of the matrices corresponds to one training/test example.
using System;
public class kernel_chi2_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 1.4;
int size_cache = 10;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
Chi2Kernel kernel = new Chi2Kernel(feats_train, feats_train, width, size_cache);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test=kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// In this example the circular kernel is being computed for toy data.
using System;
public class kernel_circular_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double sigma = 1.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
CircularKernel kernel = new CircularKernel(feats_train, feats_train, sigma, distance);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test=kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// The constant kernel gives a trivial kernel matrix with all entries set to the same value
// defined by the argument 'c'.
//
using System;
public class kernel_const_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double c = 23;
DummyFeatures feats_train = new DummyFeatures(10);
DummyFeatures feats_test = new DummyFeatures(17);
ConstKernel kernel = new ConstKernel(feats_train, feats_train, c);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test=kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// This is an example for the initialization of the diag-kernel.
// The diag kernel has all kernel matrix entries but those on
// the main diagonal set to zero.
using System;
public class kernel_diag_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double diag = 23;
DummyFeatures feats_train = new DummyFeatures(10);
DummyFeatures feats_test = new DummyFeatures(17);
ConstKernel kernel = new ConstKernel(feats_train, feats_train, diag);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test=kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// With the distance kernel one can use any of the following distance metrics:
// BrayCurtisDistance()
// CanberraMetric()
// CanberraWordDistance()
// ChebyshewMetric()
// ChiSquareDistance()
// CosineDistance()
// Distance()
// EuclidianDistance()
// GeodesicMetric()
// HammingWordDistance()
// JensenMetric()
// ManhattanMetric()
// ManhattanWordDistance()
// MinkowskiMetric()
// RealDistance()
// SimpleDistance()
// SparseDistance()
// SparseEuclidianDistance()
// StringDistance()
// TanimotoDistance()
//
using System;
public class kernel_distance_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 1.7;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance();
DistanceKernel kernel = new DistanceKernel(feats_train, feats_test, width, distance);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
// Parse and Display km_train
Console.Write("dm_train:\n");
int numRows = dm_train.GetLength(0);
int numCols = dm_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(dm_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\ndm_test:\n");
numRows = dm_test.GetLength(0);
numCols = dm_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(dm_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// The well known Gaussian kernel (swiss army knife for SVMs) on dense real valued features.
using System;
public class kernel_gaussian_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 1.3;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
foreach(double item in km_train) {
Console.Write(item);
}
foreach(double item in km_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example the inverse multiquadic kernel is being computed for toy data.
using System;
public class kernel_inversemultiquadric_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double shift_coef = 1.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
InverseMultiQuadricKernel kernel = new InverseMultiQuadricKernel(feats_train, feats_test, shift_coef, distance);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// example on saving a kernel to a file
using System;
public class kernel_io_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 1.2;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_test, width);
double[,] km_train = kernel.get_kernel_matrix();
AsciiFile f=new AsciiFile("gaussian_train.ascii",'w');
kernel.save(f);
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
AsciiFile f_test=new AsciiFile("gaussian_train.ascii",'w');
kernel.save(f_test);
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// This is an example for the initialization of a linear kernel on real valued
// data using scaling factor 1.2.
using System;
public class kernel_linear_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double scale = 1.2;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
LinearKernel kernel = new LinearKernel(feats_train, feats_test);
kernel.set_normalizer(new AvgDiagKernelNormalizer(scale));
kernel.init(feats_train, feats_train);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// This is an example for the initialization of a linear kernel on word (2byte)
// data.
using System;
public class kernel_linear_word_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double scale = 1.2;
double[,] traindata_real = Load.load_numbers("../data/fm_train_word.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_word.dat");
short[,] traindata_word = new short[traindata_real.GetLength(0), traindata_real.GetLength(1)];
for (int i = 0; i < traindata_real.GetLength(0); i++){
for (int j = 0; j < traindata_real.GetLength(1); j++)
traindata_word[i, j] = (short)traindata_real[i, j];
}
short[,] testdata_word = new short[testdata_real.GetLength(0), testdata_real.GetLength(1)];
for (int i = 0; i < testdata_real.GetLength(0); i++){
for (int j = 0; j < testdata_real.GetLength(1); j++)
testdata_word[i, j] = (short)testdata_real[i, j];
}
WordFeatures feats_train = new WordFeatures(traindata_word);
WordFeatures feats_test = new WordFeatures(testdata_word);
LinearKernel kernel = new LinearKernel(feats_train, feats_test);
kernel.set_normalizer(new AvgDiagKernelNormalizer(scale));
kernel.init(feats_train, feats_train);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
foreach(double item in km_train) {
Console.Write(item);
}
foreach(double item in km_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example the log kernel (logarithm of the distance powered by degree plus one) is being computed for toy data.
using System;
public class kernel_log_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double degree = 2.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
WaveKernel kernel = new WaveKernel(feats_train, feats_test, degree, distance);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// This example initializes the polynomial kernel with real data.
// If variable 'inhomogene' is 'True' +1 is added to the scalar product
// before taking it to the power of 'degree'. If 'use_normalization' is
// set to 'true' then kernel matrix will be normalized by the square roots
// of the diagonal entries.
using System;
public class kernel_poly_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int degree = 4;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
PolyKernel kernel = new PolyKernel(feats_train, feats_train, degree, false);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// In this example the rational quadratic kernel is being computed for toy data.
using System;
public class kernel_rationalquadratic_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double shift_coef = 1.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
RationalQuadraticKernel kernel = new RationalQuadraticKernel(feats_train, feats_test, shift_coef, distance);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// The SalzbergWordString kernel implements the Salzberg kernel.
//
// It is described in
//
// Engineering Support Vector Machine Kernels That Recognize Translation Initiation Sites
// A. Zien, G.Raetsch, S. Mika, B. Schoelkopf, T. Lengauer, K.-R. Mueller
//
using System;
public class kernel_salzberg_word_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
bool reverse = false;
int order = 3;
int gap = 0;
String[] fm_train_dna = Load.load_dna("../data/fm_train_dna.dat");
String[] fm_test_dna = Load.load_dna("../data/fm_test_dna.dat");
StringCharFeatures charfeat = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringWordFeatures feats_train = new StringWordFeatures(charfeat.get_alphabet());
feats_train.obtain_from_char(charfeat, order-1, order, gap, false);
charfeat = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
StringWordFeatures feats_test = new StringWordFeatures(charfeat.get_alphabet());
feats_test.obtain_from_char(charfeat, order-1, order, gap, false);
Labels labels = new Labels(Load.load_labels("../data/label_train_dna.dat"));
PluginEstimate pie = new PluginEstimate();
pie.set_labels(labels);
pie.set_features(feats_train);
pie.train();
SalzbergWordStringKernel kernel = new SalzbergWordStringKernel(feats_train, feats_train, pie, labels);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
pie.set_features(feats_test);
pie.apply().get_labels();
double[,] km_test=kernel.get_kernel_matrix();
modshogun.exit_shogun();
}
}
// The standard Sigmoid kernel computed on dense real valued features.
using System;
public class kernel_sigmoid_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int size_cache = 10;
double gamma = 1.2;
double coef0 = 1.3;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
SigmoidKernel kernel = new SigmoidKernel(feats_train, feats_test, size_cache, gamma, coef0);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// In this example the spherical kernel is being computed for toy data.
using System;
public class kernel_spherical_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double sigma = 1.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
MultiquadricKernel kernel = new MultiquadricKernel(feats_train, feats_test, sigma, distance);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// In this example the spline kernel is being computed for toy data.
using System;
public class kernel_spline_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double sigma = 1.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
SplineKernel kernel = new SplineKernel(feats_train, feats_test);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// In this example the t-Student's kernel is being computed for toy data.
using System;
public class kernel_tstudent_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double degree = 2.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
TStudentKernel kernel = new TStudentKernel(feats_train, feats_test, degree, distance);
double[,] km_train = kernel.get_kernel_matrix();
distance.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// In this example the wave kernel is being computed for toy data.
using System;
public class kernel_wave_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double theta = 1.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);
WaveKernel kernel = new WaveKernel(feats_train, feats_test, theta, distance);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// In this example the wavelet kernel is being computed for toy data.
using System;
public class kernel_wavelet_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double theta = 1.0;
double dilation = 1.5;
double translation = 1.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
WaveletKernel kernel = new WaveletKernel(feats_train, feats_test, 10, dilation, translation);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// This examples shows how to create a Weighted Degree String Kernel from data
// and how to compute the kernel matrix from the resulting object.
using System;
public class kernel_weighted_degree_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int degree = 3;
string[] fm_train_dna = Load.load_dna("../data/fm_train_dna.dat");
string[] fm_test_dna = Load.load_dna("../data/fm_test_dna.dat");
foreach(string item in fm_train_dna) {
Console.WriteLine(item);
}
StringCharFeatures feats_train = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringCharFeatures feats_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
WeightedDegreeStringKernel kernel = new WeightedDegreeStringKernel(feats_train, feats_train, degree);
double [] w = new double[degree];
double sum = degree * (degree + 1)/2;
for (int i = 0; i < degree; i++) {
w[i] = (degree - i)/sum;
}
kernel.set_wd_weights(w);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
foreach(double item in km_train) {
Console.Write(item);
}
foreach(double item in km_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
modshogun.init_shogun_with_defaults();
GaussianKernel k = new GaussianKernel();
Console.WriteLine(k.get_width());
}
}
// In this example toy data is being processed using the Isomap algorithm
// as described in
//
// Silva, V. D., & Tenenbaum, J. B. (2003).
// Global versus local methods in nonlinear dimensionality reduction.
// Advances in Neural Information Processing Systems 15, 15(Figure 2), 721-728. MIT Press.
// Retrieved from http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.9.3407&rep=rep1&type=pdf
//
// Before applying to the data the landmark approximation is enabled with
// specified number of landmarks. The landmark approximation is described in
//
// Sparse multidimensional scaling using landmark points
// V De Silva, J B Tenenbaum (2004) Technology, p. 1-4
//
// After enabling the landmark approximation k parameter -- the number
// of neighbors in the k nearest neighbor graph -- is initialized.
using System;
public class preprocessor_classicisomap_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] data = Load.load_numbers("../data/fm_train_real.dat");
RealFeatures features = new RealFeatures(data);
Isomap preprocessor = new Isomap();
preprocessor.set_target_dim(1);
preprocessor.apply_to_feature_matrix(features);
modshogun.exit_shogun();
}
}
// In this example toy data is being preprocessed using the Locally Linear Embedding (LLE)
// algorithm as described in
//
// Saul, L. K., Ave, P., Park, F., & Roweis, S. T. (2001).
// An Introduction to Locally Linear Embedding. Available from, 290(5500), 2323-2326.
// Retrieved from: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.123.7319&rep=rep1&type=pdf
//
// The number of neighbors used during the linear reconstruction step of the algorithm is set
// before processing of the data.
using System;
public class preprocessor_locallylinearembedding_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] data = Load.load_numbers("../data/fm_train_real.dat");
RealFeatures features = new RealFeatures(data);
LocallyLinearEmbedding preprocessor = new LocallyLinearEmbedding();
preprocessor.set_target_dim(1);
preprocessor.apply_to_feature_matrix(features);
modshogun.exit_shogun();
}
}
// In this example toy data is being processed using the multidimensional
// scaling as described on p.261 (Section 12.1) of
//
// Borg, I., & Groenen, P. J. F. (2005).
// Modern multidimensional scaling: Theory and applications. Springer.
//
// Before processing the landmark approximation is disabled.
using System;
public class preprocessor_multidimensionalscaling_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double[,] data = Load.load_numbers("../data/fm_train_real.dat");
RealFeatures features = new RealFeatures(data);
MultidimensionalScaling mds = new MultidimensionalScaling();
mds.set_target_dim(1);
mds.set_landmark(false);
mds.apply_to_feature_matrix(features);
modshogun.exit_shogun();
}
}
// In this example a kernel matrix is computed for a given real-valued data set.
// The kernel used is the Chi2 kernel which operates on real-valued vectors. It
// computes the chi-squared distance between sets of histograms. It is a very
// useful distance in image recognition (used to detect objects). The preprocessor
// NormOne, normalizes vectors to have norm 1.
using System;
public class preprocessor_normone_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 1.4;
int size_cache = 10;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
NormOne preproc = new NormOne();
preproc.init(feats_train);
feats_train.add_preprocessor(preproc);
feats_train.apply_preprocessor();
feats_test.add_preprocessor(preproc);
feats_test.apply_preprocessor();
Chi2Kernel kernel = new Chi2Kernel(feats_train, feats_train, width, size_cache);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
using System;
public class preprocessor_prunevarsubmean_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 1.4;
int size_cache = 10;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
PruneVarSubMean preproc = new PruneVarSubMean();
preproc.init(feats_train);
feats_train.add_preprocessor(preproc);
feats_train.apply_preprocessor();
feats_test.add_preprocessor(preproc);
feats_test.apply_preprocessor();
Chi2Kernel kernel = new Chi2Kernel(feats_train, feats_train, width, size_cache);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
using System;
public class preprocessor_randomfouriergausspreproc_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 1.4;
int size_cache = 10;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
RandomFourierGaussPreproc preproc = new RandomFourierGaussPreproc();
preproc.init(feats_train);
feats_train.add_preprocessor(preproc);
feats_train.apply_preprocessor();
feats_test.add_preprocessor(preproc);
feats_test.apply_preprocessor();
Chi2Kernel kernel = new Chi2Kernel(feats_train, feats_train, width, size_cache);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
// Parse and Display km_train
Console.Write("km_train:\n");
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
// Parse and Display km_test
Console.Write("\nkm_test:\n");
numRows = km_test.GetLength(0);
numCols = km_test.GetLength(1);
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_test[i,j] +" ");
}
Console.Write("\n");
}
modshogun.exit_shogun();
}
}
// In this example a kernelized version of ridge regression (KRR) is trained on a
// real-valued data set. The KRR is trained with regularization parameter tau=1e-6
// and a gaussian kernel with width=0.8. The labels of both the train and the test
// data can be fetched via krr.classify().get_labels().
using System;
public class regression_krr_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 0.8;
double tau = 1e-6;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
double[] trainlab = Load.load_labels("../data/label_train_twoclass.dat");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
GaussianKernel kernel= new GaussianKernel(feats_train, feats_train, width);
Labels labels = new Labels(trainlab);
KRR krr = new KRR(tau, kernel, labels);
krr.train(feats_train);
kernel.init(feats_train, feats_test);
double[] out_labels = krr.apply().get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}