|
SHOGUN
v2.0.0
|
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);
BinaryLabels labels = new BinaryLabels(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 = BinaryLabels.obtain_from_generic(perceptron.apply()).get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example the Gaussian Naive Bayes algorithm used to classify
// toy data
using System;
public class classifier_gaussiannaivebayes_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");
double[] trainlab = Load.load_labels("../data/label_train_multiclass.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);
MulticlassLabels labels = new MulticlassLabels(trainlab);
GaussianNaiveBayes gnb = new GaussianNaiveBayes(feats_train, labels);
gnb.train();
double[] out_labels = MulticlassLabels.obtain_from_generic(gnb.apply(feats_test)).get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example a multi-class support vector machine is trained on a toy data
// set and the trained classifier is then used to predict labels of test
// examples. The training algorithm is based on BSVM formulation (L2-soft margin
// and the bias added to the objective function) which is solved by the Improved
// Mitchell-Demyanov-Malozemov algorithm. The training algorithm uses the Gaussian
// kernel of width 2.1 and the regularization constant C=1. The solver stops if the
// relative duality gap falls below 1e-5.
//
// For more details on the used SVM solver see
// V.Franc: Optimization Algorithms for Kernel Methods. Research report.
// CTU-CMP-2005-22. CTU FEL Prague. 2005.
// ftp://cmp.felk.cvut.cz/pub/cmp/articles/franc/Franc-PhD.pdf .
//
using System;
public class classifier_gmnpsvm_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 1.0;
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");
RealFeatures feats_train = new RealFeatures();
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
MulticlassLabels labels = new MulticlassLabels(trainlab);
GMNPSVM svm = new GMNPSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();
kernel.init(feats_train, feats_test);
double[] out_labels = MulticlassLabels.obtain_from_generic(svm.apply(feats_test)).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
// toy data set and the trained classifier is then used to predict labels of test
// examples. As training algorithm Gradient Projection Decomposition Technique
// (GPDT) is used with SVM regularization parameter C=1 and a Gaussian
// kernel of width 2.1. The solver returns an epsilon-precise (epsilon=1e-5) solution.
//
// For more details on GPDT solver see http://dm.unife.it/gpdt .
//
using System;
public class classifier_gpbtsvm_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 1.0;
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);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
BinaryLabels labels = new BinaryLabels(trainlab);
GPBTSVM svm = new GPBTSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();
kernel.init(feats_train, feats_test);
double[] out_labels = BinaryLabels.obtain_from_generic(svm.apply()).get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// This example shows usage of a k-nearest neighbor (KNN) classification rule on
// a toy data set. The number of the nearest neighbors is set to k=3 and the distances
// are measured by the Euclidean metric. Finally, the KNN rule is applied to predict
// labels of test examples.
using System;
public class classifier_knn_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int k = 3;
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");
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclideanDistance distance = new EuclideanDistance(feats_train, feats_train);
MulticlassLabels labels = new MulticlassLabels(trainlab);
KNN knn = new KNN(k, distance, labels);
knn.train();
double[] out_labels = MulticlassLabels.obtain_from_generic(knn.apply(feats_test)).get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example a multi-class support vector machine classifier is trained on a
// toy data set and the trained classifier is then used to predict labels of test
// examples. As training algorithm the LaRank algorithm is used with SVM
// regularization parameter C=1 and a Gaussian kernel of width 2.1 and a precision
// set to epsilon=1e-5.
//
// For more details on LaRank see
// Bordes, A. and Bottou, L. and Gallinari, P. and Weston, J.
// Solving MultiClass Support Vector Machines with LaRank. ICML 2007.
//
using System;
public class classifier_larank_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 1.0;
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");
RealFeatures feats_train = new RealFeatures();
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
MulticlassLabels labels = new MulticlassLabels(trainlab);
LaRank svm = new LaRank(C, kernel, labels);
svm.set_batch_mode(false);
svm.set_epsilon(epsilon);
svm.train();
double[] out_labels = MulticlassLabels.obtain_from_generic(svm.apply(feats_train)).get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example a two-class linear classifier based on the Linear Discriminant
// Analysis (LDA) is trained on a toy data set and then the trained classifier is
// used to predict test examples. The regularization parameter, which corresponds
// to a weight of a unitary matrix added to the covariance matrix, is set to
// gamma=3.
//
// For more details on the LDA see e.g.
// http://en.wikipedia.org/wiki/Linear_discriminant_analysis
using System;
public class classifier_lda_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int gamma = 3;
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);
BinaryLabels labels = new BinaryLabels(trainlab);
LDA lda = new LDA(gamma, feats_train, labels);
lda.train();
Console.WriteLine(lda.get_bias());
//Console.WriteLine(lda.get_w().toString());
foreach(double item in lda.get_w()) {
Console.Write(item);
}
lda.set_features(feats_test);
double[] out_labels = BinaryLabels.obtain_from_generic(lda.apply()).get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example a two-class linear support vector machine classifier is trained
// on a toy data set and the trained classifier is then used to predict labels of
// test examples. As training algorithm the LIBLINEAR solver is used with the SVM
// regularization parameter C=0.9 and the bias in the classification rule switched
// on and the precision parameters epsilon=1e-5.
//
// For more details on LIBLINEAR see
// http://www.csie.ntu.edu.tw/~cjlin/liblinear/
using System;
public class classifier_liblinear_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double C = 0.9;
double epsilon = 1e-3;
Math.init_random(17);
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);
BinaryLabels labels = new BinaryLabels(trainlab);
LibLinear svm = new LibLinear(C, feats_train, labels);
svm.set_liblinear_solver_type(LIBLINEAR_SOLVER_TYPE.L2R_L2LOSS_SVC_DUAL);
svm.set_epsilon(epsilon);
svm.set_bias_enabled(true);
svm.train();
svm.set_features(feats_test);
double[] out_labels = BinaryLabels.obtain_from_generic(svm.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);
BinaryLabels labels = new BinaryLabels(trainlab);
LibSVM svm = new LibSVM(C, kernel, labels);
svm.train();
double[] result = BinaryLabels.obtain_from_generic(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();
}
}
// In this example a two-class support vector machine classifier is trained on a
// toy 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 and the
// precision parameter epsilon=1e-5. The example also shows how to retrieve the
// support vectors from the train SVM model.
//
// For more details on LIBSVM solver see http://www.csie.ntu.edu.tw/~cjlin/libsvm/
using System;
public class classifier_libsvm_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 1.0;
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);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
BinaryLabels labels = new BinaryLabels(trainlab);
LibSVM svm = new LibSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();
kernel.init(feats_train, feats_test);
double[] out_labels = BinaryLabels.obtain_from_generic(svm.apply()).get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example a one-class support vector machine classifier is trained on a
// toy data set. The training algorithm finds a hyperplane in the RKHS which
// separates the training data from the origin. The one-class classifier is
// typically used to estimate the support of a high-dimesnional distribution.
// For more details see e.g.
// B. Schoelkopf et al. Estimating the support of a high-dimensional
// distribution. Neural Computation, 13, 2001, 1443-1471.
//
// In the example, the one-class SVM is trained by the LIBSVM solver with the
// regularization parameter C=1 and the Gaussian kernel of width 2.1 and the
// precision parameter epsilon=1e-5.
//
// For more details on LIBSVM solver see http://www.csie.ntu.edu.tw/~cjlin/libsvm/
using System;
public class classifier_libsvmoneclass_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 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();
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
LibSVMOneClass svm = new LibSVMOneClass(C, kernel);
svm.set_epsilon(epsilon);
svm.train();
kernel.init(feats_train, feats_test);
double[] out_labels = BinaryLabels.obtain_from_generic(svm.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
// toy data set and the trained classifier is used to predict labels of test
// examples. As training algorithm the Minimal Primal Dual SVM is used with SVM
// regularization parameter C=1 and a Gaussian kernel of width 1.2 and the
// precision parameter 1e-5.
//
// For more details on the MPD solver see
// Kienzle, W. and B. Schölkopf: Training Support Vector Machines with Multiple
// Equality Constraints. Machine Learning: ECML 2005, 182-193. (Eds.) Carbonell,
// J. G., J. Siekmann, Springer, Berlin, Germany (11 2005)
using System;
public class classifier_mpdsvm_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 1.0;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
// already tried double[,]
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);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
BinaryLabels labels = new BinaryLabels(trainlab);
MPDSVM svm = new MPDSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();
kernel.init(feats_train, feats_test);
// already tried double[,]
double[] out_labels = BinaryLabels.obtain_from_generic(svm.apply()).get_labels();
foreach (double item in out_labels)
Console.Write(item);
modshogun.exit_shogun();
}
}
using System;
public class classifier_multiclasslibsvm_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 1.0;
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");
RealFeatures feats_train = new RealFeatures();
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
MulticlassLabels labels = new MulticlassLabels(trainlab);
MulticlassLibSVM svm = new MulticlassLibSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();
kernel.init(feats_train, feats_test);
double[] out_labels = MulticlassLabels.obtain_from_generic(svm.apply()).get_labels();
foreach (double item in out_labels)
Console.Write(item);
modshogun.exit_shogun();
}
}
// This example shows usage of the Perceptron algorithm for training a two-class
// linear classifier, i.e. y = sign( <x,w>+b). The Perceptron algorithm works by
// iteratively passing though the training examples and applying the update rule on
// those examples which are misclassified by the current classifier. The Perceptron
// update rule reads
//
// w(t+1) = w(t) + alpha * y_t * x_t
// b(t+1) = b(t) + alpha * y_t
//
// where (x_t,y_t) is feature vector and label (must be +1/-1) of the misclassified example
// (w(t),b(t)) are the current parameters of the linear classifier
// (w(t+1),b(t+1)) are the new parameters of the linear classifier
// alpha is the learning rate; in this examples alpha=1
//
// The Perceptron algorithm iterates until all training examples are correctly
// classified or the prescribed maximal number of iterations, in this example
// max_iter=1000, is reached.
using System;
public class classifier_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");
// already tried double[][]
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);
BinaryLabels labels = new BinaryLabels(trainlab);
Perceptron perceptron = new Perceptron(feats_train, labels);
perceptron.set_learn_rate(learn_rate);
perceptron.set_max_iter(max_iter);
perceptron.train();
perceptron.set_features(feats_test);
// already tried double[][]
double[] out_labels = BinaryLabels.obtain_from_generic(perceptron.apply()).get_labels();
foreach (double item in out_labels)
Console.Write(item);
modshogun.exit_shogun();
}
}
// In this example an agglomerative hierarchical single linkage clustering method
// is used to cluster a given toy data set. Starting with each object being
// assigned to its own cluster clusters are iteratively merged. Here the clusters
// are merged that have the closest (minimum distance, here set via the Euclidean
// distance object) two elements.
using System;
public class clustering_hierarchical_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int merges = 3;
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);
Hierarchical hierarchical = new Hierarchical(merges, distance);
hierarchical.train();
double[] out_distance = hierarchical.get_merge_distances();
int[,] out_cluster = hierarchical.get_cluster_pairs();
modshogun.exit_shogun();
}
}
// 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();
}
}
// 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 converter_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(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 converter_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(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 converter_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(features);
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_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();
}
}
// 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();
}
}
// This example shows how to compute the Hamming Word Distance for string features.
using System;
public class distance_hammingword_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int order = 3;
int gap = 0;
bool reverse = false;
bool use_sign = 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_labels("../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();
HammingWordDistance distance = new HammingWordDistance(feats_train, feats_train, use_sign);
double[,] dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
double[,] dm_test = distance.get_distance_matrix();
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);
EuclideanDistance distance = new EuclideanDistance(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();
}
}
// In this example the Histogram algorithm object computes a histogram over all
// 16bit unsigned integers in the features.
using System;
public class distribution_histogram_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);
Histogram histo = new Histogram(feats);
histo.train();
double[] histogram = histo.get_histogram();
foreach(double item in histogram) {
Console.Write(item);
}
//int num_examples = feats.get_num_vectors();
//int num_param = histo.get_num_model_parameters();
//double[,] out_likelihood = histo.get_log_likelihood();
//double out_sample = histo.get_log_likelihood_sample();
modshogun.exit_shogun();
}
}
// In this example a hidden markov model with 3 states and 6 transitions is trained
// on a string data set. After calling the constructor of the HMM class specifying
// the number of states and transitions the model is trained. Via the Baum-Welch
// algorithm the optimal transition and emission probabilities are estimated. The
// best path, i.e. the path with highest probability given the model can then be
// calculated using get_best_path_state.
//import org.shogun.*;
//import org.jblas.*;
//import static org.shogun.EAlphabet.CUBE;
//import static org.shogun.BaumWelchViterbiType.BW_NORMAL;
public class distribution_hmm_modular {
public static void Main() {
bool reverse = false;
modshogun.init_shogun_with_defaults();
int N = 1;
int M = 512;
double pseudo = 1e-5;
int order = 3;
int gap = 0;
string[] fm_train_dna = Load.load_cubes("../data/fm_train_cube.dat");
StringCharFeatures charfeat = new StringCharFeatures(fm_train_dna, EAlphabet.CUBE);
StringWordFeatures feats = new StringWordFeatures(charfeat.get_alphabet());
feats.obtain_from_char(charfeat, order-1, order, gap, reverse);
HMM hmm = new HMM(feats, N, M, pseudo);
hmm.train();
hmm.baum_welch_viterbi_train(BaumWelchViterbiType.BW_NORMAL);
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);
}
int best_path = 0;
int best_path_state = 0;
for(int i = 0; i < num_examples; i++){
best_path += (int)hmm.best_path(i);
for(int j = 0; j < N; j++)
best_path_state += hmm.get_best_path_state(i, j);
}
double[] lik_example = hmm.get_log_likelihood();
double lik_sample = hmm.get_log_likelihood_sample();
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();
}
BinaryLabels ground_truth_labels = new BinaryLabels(ground_truth);
BinaryLabels predicted_labels = new BinaryLabels(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();
}
}
using System;
public class features_dense_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();
}
}
// Creates features similar to the feature space of the SNP kernel. Useful when
// working with linear methods.
//import org.shogun.*;
//import org.jblas.*;
//import static org.shogun.EAlphabet.SNP;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
public class features_snp_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
string filename = "../data/snps.dat";
StringByteFeatures sf = new StringByteFeatures(EAlphabet.SNP);
sf.load_ascii_file(filename, false, EAlphabet.SNP, EAlphabet.SNP);
SNPFeatures snps = new SNPFeatures(sf);
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();
}
}
// This creates a HashedWDFeatures object, i.e. an approximation to the Weighted
// Degree kernel feature space via hashes. These features can be particularly fast
// in linear SVM solvers.
using System;
public class features_string_hashed_wd_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int order = 3;
int start_order = 1;
int hash_bits = 2;
int from_order = order;
StringByteFeatures f = new StringByteFeatures(EAlphabet.RAWDNA);
HashedWDFeatures y = new HashedWDFeatures(f,start_order,order,from_order,hash_bits);
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();
}
}
// 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();
}
}
// This example demonstrates the use of the AUC Kernel.
using System;
public class kernel_auc_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 1.6;
double[,] train_real = Load.load_numbers("../data/fm_train_real.dat");
double[] trainlab = Load.load_labels("../data/label_train_twoclass.dat");
RealFeatures feats_train = new RealFeatures(train_real);
GaussianKernel subkernel = new GaussianKernel(feats_train, feats_train, width);
BinaryLabels labels = new BinaryLabels(trainlab);
AUCKernel kernel = new AUCKernel(0, subkernel);
kernel.setup_auc_maximization(labels);
double[,] km_train = kernel.get_kernel_matrix();
int numRows = km_train.GetLength(0);
int numCols = km_train.GetLength(1);
Console.Write("km_train:\n");
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
Console.Write(km_train[i,j] +" ");
}
Console.Write("\n");
}
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);
EuclideanDistance distance = new EuclideanDistance(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);
EuclideanDistance distance = new EuclideanDistance(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();
}
}
// This is an example for the initialization of a combined kernel, which is a weighted sum of
// in this case three kernels on real valued data. The sub-kernel weights are all set to 1.
//
//import org.shogun.*;
//import org.jblas.*;
//import static org.shogun.EAlphabet.DNA;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
using System;
public class kernel_combined_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int cardinality = 2;
int cache = 10;
double[,] traindata_real = Load.load_numbers("../data/fm_train_real.dat");
double[,] testdata_real = Load.load_numbers("../data/fm_test_real.dat");
String[] fm_train_dna = Load.load_dna("../data/fm_train_dna.dat");
String[] fm_test_dna = Load.load_dna("../data/fm_test_dna.dat");
RealFeatures subfeats_train = new RealFeatures(traindata_real);
RealFeatures subfeats_test = new RealFeatures(testdata_real);
CombinedKernel kernel= new CombinedKernel();
CombinedFeatures feats_train = new CombinedFeatures();
CombinedFeatures feats_test = new CombinedFeatures();
GaussianKernel subkernel = new GaussianKernel(cache, 1.1);
feats_train.append_feature_obj(subfeats_train);
feats_test.append_feature_obj(subfeats_test);
kernel.append_kernel(subkernel);
StringCharFeatures subkfeats_train = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringCharFeatures subkfeats_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
int degree = 3;
FixedDegreeStringKernel subkernel2= new FixedDegreeStringKernel(10, degree);
feats_train.append_feature_obj(subkfeats_train);
feats_test.append_feature_obj(subkfeats_test);
kernel.append_kernel(subkernel2);
subkfeats_train = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
subkfeats_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
LocalAlignmentStringKernel subkernel3 = new LocalAlignmentStringKernel(10);
feats_train.append_feature_obj(subkfeats_train);
feats_test.append_feature_obj(subkfeats_test);
kernel.append_kernel(subkernel3);
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();
modshogun.exit_shogun();
}
}
// This is an example for the initialization of the CommUlongString-kernel. This kernel
// sums over k-mere matches (k='order'). For efficient computing a preprocessor is used
// that extracts and sorts all k-mers. If 'use_sign' is set to one each k-mere is counted
// only once.
//import org.shogun.*;
//import org.jblas.*;
//import static org.shogun.EAlphabet.DNA;
using System;
public class kernel_comm_ulong_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int order = 3;
int gap = 0;
bool reverse = false;
bool use_sign = 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");
StringCharFeatures charfeat = new StringCharFeatures(EAlphabet.DNA);
charfeat.set_features(fm_train_dna);
StringUlongFeatures feats_train = new StringUlongFeatures(charfeat.get_alphabet());
feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse);
SortUlongString preproc = new SortUlongString();
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);
StringUlongFeatures feats_test = new StringUlongFeatures(charfeat.get_alphabet());
feats_test.obtain_from_char(charfeat_test, order-1, order, gap, reverse);
feats_test.add_preprocessor(preproc);
feats_test.apply_preprocessor();
CommUlongStringKernel kernel = new CommUlongStringKernel(feats_train, feats_train, use_sign);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
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);
EuclideanDistance distance = new EuclideanDistance();
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 FixedDegree String kernel takes as input two strings of same size and counts the number of matches of length d.
using System;
public class kernel_fixed_degree_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int degree = 4;
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 feats_train = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringCharFeatures feats_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
FixedDegreeStringKernel kernel = new FixedDegreeStringKernel(feats_train, feats_train, degree);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
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);
EuclideanDistance distance = new EuclideanDistance(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();
}
}
// This is an example for the initialization of the local alignment kernel on
// DNA sequences, where each column of the matrices of type char corresponds to
// one training/test example.
using System;
public class kernel_local_alignment_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
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 feats_train = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringCharFeatures feats_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
LocalAlignmentStringKernel kernel = new LocalAlignmentStringKernel(feats_train, feats_train);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
modshogun.exit_shogun();
}
}
// The LocalityImprovedString kernel is inspired by the polynomial kernel.
// Comparing neighboring characters it puts emphasize on local features.
//
// It can be defined as
// K({\bf x},{\bf x'})=\left(\sum_{i=0}^{T-1}\left(\sum_{j=-l}^{+l}w_jI_{i+j}({\bf x},{\bf x'})\right)^{d_1}\right)^{d_2},
// where
// I_i({\bf x},{\bf x'})=1
// if $x_i=x'_i and 0 otherwise.
//
using System;
public class kernel_locality_improved_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int length = 5;
int inner_degree = 5;
int outer_degree = 7;
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 feats_train = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringCharFeatures feats_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
LocalityImprovedStringKernel kernel = new LocalityImprovedStringKernel(feats_train, feats_train, length, inner_degree, outer_degree);
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 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);
EuclideanDistance distance = new EuclideanDistance(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();
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 match word string kernel is being computed for toy data
using System;
public class kernel_match_word_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int degree = 20;
double scale = 1.4;
int size_cache = 10;
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");
StringCharFeatures charfeat = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringWordFeatures feats_train = new StringWordFeatures(EAlphabet.DNA);
feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse);
StringCharFeatures charfeat_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
StringWordFeatures feats_test = new StringWordFeatures(EAlphabet.DNA);
feats_test.obtain_from_char(charfeat_test, order-1, order, gap, reverse);
MatchWordStringKernel kernel = new MatchWordStringKernel(size_cache, degree);
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();
modshogun.exit_shogun();
}
}
// In this example the multiquadric kernel is being computed for toy data.
using System;
public class kernel_multiquadric_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);
EuclideanDistance distance = new EuclideanDistance(feats_train, feats_train);
MultiquadricKernel kernel = new MultiquadricKernel(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();
foreach(double item in km_train) {
Console.Write(item);
}
foreach(double item in km_test) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// This is an example initializing the oligo string kernel which takes distances
// between matching oligos (k-mers) into account via a gaussian. Variable 'k' defines the length
// of the oligo and variable 'w' the width of the gaussian. The oligo string kernel is
// implemented for the DNA-alphabet 'ACGT'.
//
using System;
public class kernel_oligo_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int size_cache = 3;
int k = 1;
double width = 10;
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 feats_train = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringCharFeatures feats_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
OligoStringKernel kernel = new OligoStringKernel(size_cache, k, width);
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();
modshogun.exit_shogun();
}
}
// In this example the poly match string kernel is being computed for toy data.
using System;
public class kernel_poly_match_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
bool reverse = false;
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");
StringCharFeatures feats_train = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringCharFeatures feats_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
PolyMatchStringKernel kernel = new PolyMatchStringKernel(feats_train, feats_train, degree, true);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test=kernel.get_kernel_matrix();
modshogun.exit_shogun();
}
}
// This is an example for the initialization of the PolyMatchString kernel on string data.
// The PolyMatchString kernel sums over the matches of two stings of the same length and
// takes the sum to the power of 'degree'. The strings consist of the characters 'ACGT' corresponding
// to the DNA-alphabet. Each column of the matrices of type char corresponds to
// one training/test example.
using System;
public class kernel_poly_match_word_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
bool reverse = false;
int order = 3;
int gap = 0;
int degree = 2;
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);
PolyMatchWordStringKernel kernel = new PolyMatchWordStringKernel(feats_train, feats_train, degree, true);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test=kernel.get_kernel_matrix();
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 power kernel is being computed for toy data.
//import org.shogun.*;
//import org.jblas.*;
using System;
public class kernel_power_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double degree = 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);
EuclideanDistance distance = new EuclideanDistance(feats_train, feats_train);
PowerKernel kernel = new PowerKernel(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();
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 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);
EuclideanDistance distance = new EuclideanDistance(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);
BinaryLabels labels = new BinaryLabels(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);
BinaryLabels.obtain_from_generic(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);
EuclideanDistance distance = new EuclideanDistance(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);
EuclideanDistance distance = new EuclideanDistance(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);
EuclideanDistance distance = new EuclideanDistance(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();
}
}
// The WeightedCommWordString kernel may be used to compute the weighted
// spectrum kernel (i.e. a spectrum kernel for 1 to K-mers, where each k-mer
// length is weighted by some coefficient \f$\beta_k\f$) from strings that have
// been mapped into unsigned 16bit integers.
//
// These 16bit integers correspond to k-mers. To applicable in this kernel they
// need to be sorted (e.g. via the SortWordString pre-processor).
//
// It basically uses the algorithm in the unix "comm" command (hence the name)
// to compute:
//
// k({\bf x},({\bf x'})= \sum_{k=1}^K\beta_k\Phi_k({\bf x})\cdot \Phi_k({\bf x'})
//
// where \f$\Phi_k\f$ maps a sequence \f${\bf x}\f$ that consists of letters in
// \f$\Sigma\f$ to a feature vector of size \f$|\Sigma|^k\f$. In this feature
// vector each entry denotes how often the k-mer appears in that \f${\bf x}\f$.
//
// Note that this representation is especially tuned to small alphabets
// (like the 2-bit alphabet DNA), for which it enables spectrum kernels
// of order 8.
//
// For this kernel the linadd speedups are quite efficiently implemented using
// direct maps.
//
using System;
public class kernel_weighted_comm_word_string_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
int degree = 20;
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 feats_train = new StringCharFeatures(fm_train_dna, EAlphabet.DNA);
StringCharFeatures feats_test = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
WeightedDegreePositionStringKernel kernel = new WeightedDegreePositionStringKernel(feats_train, feats_train, degree);
double[,] km_train = kernel.get_kernel_matrix();
kernel.init(feats_train, feats_test);
double[,] km_test = kernel.get_kernel_matrix();
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 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();
}
}
// 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 toy data is being processed using the kernel PCA algorithm
// as described in
//
// Schölkopf, B., Smola, A. J., & Muller, K. R. (1999).
// Kernel Principal Component Analysis.
// Advances in kernel methods support vector learning, 1327(3), 327-352. MIT Press.
// Retrieved from http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.32.8744i
//
// A gaussian kernel is used for the processing.
using System;
public class preprocessor_kernelpca_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 2.0;
double threshold = 0.05;
double[,] data = Load.load_numbers("../data/fm_train_real.dat");
RealFeatures features = new RealFeatures(data);
GaussianKernel kernel = new GaussianKernel(features, features, width);
KernelPCA preprocessor = new KernelPCA(kernel);
preprocessor.init(features);
preprocessor.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
// 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();
}
}
// 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();
}
}
// In this example toy data is being processed using the
// Principal Component Analysis.
using System;
public class preprocessor_pca_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");
RealFeatures features = new RealFeatures(traindata_real);
PCA preproc = new PCA();
preproc.init(features);
preproc.apply_to_feature_matrix(features);
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 kernel matrix is computed for a given string data set. The
// CommUlongString kernel is used to compute the spectrum kernel from strings that
// have been mapped into unsigned 64bit integers. These 64bit integers correspond
// to k-mers. To be applicable in this kernel the mapped k-mers have to be sorted.
// This is done using the SortUlongString preprocessor, which sorts the indivual
// strings in ascending order. The kernel function basically uses the algorithm in
// the unix "comm" command (hence the name). Note that this representation enables
// spectrum kernels of order 8 for 8bit alphabets (like binaries) and order 32 for
// 2-bit alphabets like DNA. For this kernel the linadd speedups are implemented
// (though there is room for improvement here when a whole set of sequences is
// ADDed) using sorted lists.
using System;
public class preprocessor_sortulongstring_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);
StringUlongFeatures feats_train = new StringUlongFeatures(charfeat.get_alphabet());
feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse);
charfeat = new StringCharFeatures(fm_test_dna, EAlphabet.DNA);
StringUlongFeatures feats_test = new StringUlongFeatures(charfeat.get_alphabet());
feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse);
SortUlongString preproc = new SortUlongString();
preproc.init(feats_train);
feats_train.add_preprocessor(preproc);
feats_train.apply_preprocessor();
feats_test.add_preprocessor(preproc);
feats_test.apply_preprocessor();
CommUlongStringKernel kernel = new CommUlongStringKernel(feats_train, feats_train, false);
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 a kernel matrix is computed for a given string data set. The
// CommWordString kernel is used to compute the spectrum kernel from strings that
// have been mapped into unsigned 16bit integers. These 16bit integers correspond
// to k-mers. To be applicable in this kernel the mapped k-mers have to be sorted.
// This is done using the SortWordString preprocessor, which sorts the indivual
// strings in ascending order. The kernel function basically uses the algorithm in
// the unix "comm" command (hence the name). Note that this representation is
// especially tuned to small alphabets (like the 2-bit alphabet DNA), for which it
// enables spectrum kernels of order up to 8. For this kernel the linadd speedups
// are quite efficiently implemented using direct maps.
using System;
public class preprocessor_sortwordstring_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, reverse);
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, reverse);
SortWordString preproc = new SortWordString();
preproc.init(feats_train);
feats_train.add_preprocessor(preproc);
feats_train.apply_preprocessor();
feats_test.add_preprocessor(preproc);
feats_test.apply_preprocessor();
CommWordStringKernel kernel = new CommWordStringKernel(feats_train, feats_train, false);
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 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);
RegressionLabels labels = new RegressionLabels(trainlab);
KernelRidgeRegression krr = new KernelRidgeRegression(tau, kernel, labels);
krr.train(feats_train);
kernel.init(feats_train, feats_test);
double[] out_labels = RegressionLabels.obtain_from_generic(krr.apply()).get_labels();
foreach(double item in out_labels) {
Console.Write(item);
}
modshogun.exit_shogun();
}
}
// In this example a support vector regression algorithm is trained on a
// real-valued toy data set. The underlying library used for the SVR training is
// LIBSVM. The SVR is trained with regularization parameter C=1 and a gaussian
// kernel with width=2.1. The labels of both the train and the test data are
// fetched via svr.classify().get_labels().
//
// For more details on LIBSVM solver see http://www.csie.ntu.edu.tw/~cjlin/libsvm/ .
using System;
public class regression_libsvr_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 0.8;
int C = 1;
double epsilon = 1e-5;
double tube_epsilon = 1e-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 feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
GaussianKernel kernel= new GaussianKernel(feats_train, feats_train, width);
RegressionLabels labels = new RegressionLabels(trainlab);
LibSVR svr = new LibSVR(C, epsilon, kernel, labels);
svr.set_tube_epsilon(tube_epsilon);
svr.train();
kernel.init(feats_train, feats_test);
double[] out_labels = RegressionLabels.obtain_from_generic(svr.apply()).get_labels();
foreach (double item in out_labels)
Console.Write(out_labels);
modshogun.exit_shogun();
}
}
// In this example a support vector regression algorithm is trained on a
// real-valued toy data set. The underlying library used for the SVR training is
// SVM^light. The SVR is trained with regularization parameter C=1 and a gaussian
// kernel with width=2.1. The the label of both the train and the test data are
// fetched via svr.classify().get_labels().
//
// For more details on the SVM^light see
// T. Joachims. Making large-scale SVM learning practical. In Advances in Kernel
// Methods -- Support Vector Learning, pages 169-184. MIT Press, Cambridge, MA USA, 1999.
using System;
public class regression_svrlight_modular {
public static void Main() {
modshogun.init_shogun_with_defaults();
double width = 0.8;
int C = 1;
double epsilon = 1e-5;
double tube_epsilon = 1e-2;
int num_threads = 3;
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);
RegressionLabels labels = new RegressionLabels(trainlab);
SVRLight svr = new SVRLight(C, epsilon, kernel, labels);
svr.set_tube_epsilon(tube_epsilon);
//svr.parallel.set_num_threads(num_threads);
svr.train();
kernel.init(feats_train, feats_test);
double[] out_labels = RegressionLabels.obtain_from_generic(svr.apply()).get_labels();
foreach (double item in out_labels)
Console.Write(item);
modshogun.exit_shogun();
}
}