// In this example the k-means clustering method is used to cluster a given toy
// data set. In k-means clustering one tries to partition n observations into k
// clusters in which each observation belongs to the cluster with the nearest mean.
// The algorithm class constructor takes the number of clusters and a distance to
// be used as input. The distance used in this example is Euclidean distance.
// After training one can fetch the result of clustering by obtaining the cluster
// centers and their radiuses.
//import org.shogun.*;
//import org.jblas.*;
//import static org.shogun.Math.init_random;
using System;
public class clustering_kmeans_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int k = 3;
// already tried init_random(17)
Math.init_random(17);
double[,] fm_train = Load.load_numbers("../data/fm_train_real.dat");
RealFeatures feats_train = new RealFeatures(fm_train);
EuclideanDistance distance = new EuclideanDistance(feats_train, feats_train);
KMeans kmeans = new KMeans(k, distance);
kmeans.train();
double[,] out_centers = kmeans.get_cluster_centers();
kmeans.get_radiuses();
modshogun.exit_shogun();
}
}

Distance

../examples/documented/csharp_modular/distance_braycurtis_modular.cs

// 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();
}
}

../examples/documented/csharp_modular/distance_canberra_modular.cs

// 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();
}
}

../examples/documented/csharp_modular/distance_chebyshew_modular.cs

// 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();
}
}

../examples/documented/csharp_modular/distance_chisquare_modular.cs

// 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();
}
}

../examples/documented/csharp_modular/distance_cosine_modular.cs

// 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();
}
}

../examples/documented/csharp_modular/distance_euclidian_modular.cs

// 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_euclidean_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);
EuclideanDistance distance = new EuclideanDistance(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();
}
}

../examples/documented/csharp_modular/distance_geodesic_modular.cs

// 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();
}
}

// 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();
}
}

// 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();
}
}

// In this example, we demonstrate how to obtain string features
// by using a sliding window in a memory-efficient way. Instead of copying
// the string for each position of the sliding window, we only store a reference
// with respect to the complete string. This is particularly useful, when working
// with genomic data, where storing all explicitly copied strings in memory
// quickly becomes infeasible. In addition to a sliding window (of a particular
// length) over all position, we also support defining a custom position
// list.
using System;
public class features_string_sliding_window_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
String[] strings = new String[] {"AAAAAAAAAACCCCCCCCCCGGGGGGGGGGTTTTTTTTTT"};
StringCharFeatures f = new StringCharFeatures(strings, EAlphabet.DNA);
f.obtain_by_sliding_window(5,1);
DynamicIntArray positions = new DynamicIntArray();
positions.append_element(0);
positions.append_element(6);
positions.append_element(16);
positions.append_element(25);
//f.obtain_by_position_list(8,positions);
modshogun.exit_shogun();
}
}

Mkl

// In this example we show how to perform Multiple Kernel Learning (MKL)
// with the modular interface. First, we create a number of base kernels.
// These kernels can capture different views of the same features, or actually
// consider entirely different features associated with the same example
// (e.g. DNA sequences = strings AND gene expression data = real values of the same tissue sample).
// The base kernels are then subsequently added to a CombinedKernel, which
// contains a weight for each kernel and encapsulates the base kernels
// from the training procedure. When the CombinedKernel between two examples is
// evaluated it computes the corresponding linear combination of kernels according to their weights.
// We then show how to create an MKLClassifier that trains an SVM and learns the optimal
// weighting of kernels (w.r.t. a given norm q) at the same time.
// Finally, the example shows how to classify with a trained MKLClassifier.
//
using System;
public class mkl_binclass_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 1.0;
int mkl_norm = 2;
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 tfeats = new RealFeatures(traindata_real);
PolyKernel tkernel = new PolyKernel(10,3);
tkernel.init(tfeats, tfeats);
double[,] K_train = tkernel.get_kernel_matrix();
RealFeatures pfeats = new RealFeatures(testdata_real);
tkernel.init(tfeats, pfeats);
double[,] K_test = tkernel.get_kernel_matrix();
CombinedFeatures feats_train = new CombinedFeatures();
feats_train.append_feature_obj(new RealFeatures(traindata_real));
CombinedKernel kernel = new CombinedKernel();
kernel.append_kernel(new CustomKernel(K_train));
kernel.append_kernel(new PolyKernel(10,2));
kernel.init(feats_train, feats_train);
BinaryLabels labels = new BinaryLabels(trainlab);
MKLClassification mkl = new MKLClassification();
mkl.set_mkl_norm(1);
mkl.set_kernel(kernel);
mkl.set_labels(labels);
mkl.train();
CombinedFeatures feats_pred = new CombinedFeatures();
feats_pred.append_feature_obj(new RealFeatures(testdata_real));
CombinedKernel kernel2 = new CombinedKernel();
kernel2.append_kernel(new CustomKernel(K_test));
kernel2.append_kernel(new PolyKernel(10, 2));
kernel2.init(feats_train, feats_pred);
mkl.set_kernel(kernel2);
mkl.apply();
modshogun.exit_shogun();
}
}

../examples/documented/csharp_modular/mkl_multiclass_modular.cs

// In this example we show how to perform Multiple Kernel Learning (MKL)
// with the modular interface for multi-class classification.
// First, we create a number of base kernels and features.
// These kernels can capture different views of the same features, or actually
// consider entirely different features associated with the same example
// (e.g. DNA sequences = strings AND gene expression data = real values of the same tissue sample).
// The base kernels are then subsequently added to a CombinedKernel, which
// contains a weight for each kernel and encapsulates the base kernels
// from the training procedure. When the CombinedKernel between two examples is
// evaluated it computes the corresponding linear combination of kernels according to their weights.
// We then show how to create an MKLMultiClass classifier that trains an SVM and learns the optimal
// weighting of kernels (w.r.t. a given norm q) at the same time. The main difference to the binary
// classification version of MKL is that we can use more than two values as labels, when training
// the classifier.
// Finally, the example shows how to classify with a trained MKLMultiClass classifier.
//
using System;
public class mkl_multiclass_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 1.0;
int mkl_norm = 2;
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_multiclass.dat");
CombinedKernel kernel = new CombinedKernel();
CombinedFeatures feats_train = new CombinedFeatures();
CombinedFeatures feats_test = new CombinedFeatures();
RealFeatures subkfeats1_train = new RealFeatures(traindata_real);
RealFeatures subkfeats1_test = new RealFeatures(testdata_real);
GaussianKernel subkernel = new GaussianKernel(10, width);
feats_train.append_feature_obj(subkfeats1_train);
feats_test.append_feature_obj(subkfeats1_test);
kernel.append_kernel(subkernel);
RealFeatures subkfeats2_train = new RealFeatures(traindata_real);
RealFeatures subkfeats2_test = new RealFeatures(testdata_real);
LinearKernel subkernel2 = new LinearKernel();
feats_train.append_feature_obj(subkfeats2_train);
feats_test.append_feature_obj(subkfeats2_test);
kernel.append_kernel(subkernel2);
RealFeatures subkfeats3_train = new RealFeatures(traindata_real);
RealFeatures subkfeats3_test = new RealFeatures(testdata_real);
PolyKernel subkernel3 = new PolyKernel(10, 2);
feats_train.append_feature_obj(subkfeats3_train);
feats_test.append_feature_obj(subkfeats3_test);
kernel.append_kernel(subkernel3);
kernel.init(feats_train, feats_train);
MulticlassLabels labels = new MulticlassLabels(trainlab);
MKLMulticlass mkl = new MKLMulticlass(C, kernel, labels);
mkl.set_epsilon(epsilon);
mkl.set_mkl_epsilon(epsilon);
mkl.set_mkl_norm(mkl_norm);
mkl.train();
kernel.init(feats_train, feats_test);
double[] outMatrix = MulticlassLabels.obtain_from_generic(mkl.apply()).get_labels();
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
// LogPlusOne adds one to a dense real-valued vector and takes the logarithm of
// each component of it. It is most useful in situations where the inputs are
// counts: When one compares differences of small counts any difference may matter
// a lot, while small differences in large counts don't. This is what this log
// transformation controls for.
using System;
public class preprocessor_logplusone_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);
LogPlusOne preproc = new LogPlusOne();
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();
foreach (double item in km_train)
Console.Write(item);
foreach (double item in km_test)
Console.Write(item);
modshogun.exit_shogun();
}
}