// 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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class clustering_kmeans_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
int k = 3;
init_random(17);
DoubleMatrix 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();
DoubleMatrix out_centers = kmeans.get_cluster_centers();
kmeans.get_radiuses();
modshogun.exit_shogun();
}
}

Distance

../examples/documented/java_modular/distance_braycurtis_modular.java

// 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.
import org.shogun.*;
import org.jblas.*;
public class distance_braycurtis_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix 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);
DoubleMatrix dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
DoubleMatrix dm_test = distance.get_distance_matrix();
System.out.println(dm_train.toString());
System.out.println(dm_test.toString());
modshogun.exit_shogun();
}
}

../examples/documented/java_modular/distance_canberra_modular.java

// 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.
import org.shogun.*;
import org.jblas.*;
public class distance_canberra_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix 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);
DoubleMatrix dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
DoubleMatrix dm_test = distance.get_distance_matrix();
System.out.println(dm_train.toString());
System.out.println(dm_test.toString());
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.
import org.shogun.*;
import org.jblas.*;
public class distance_chebyshew_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix 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);
DoubleMatrix dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
DoubleMatrix dm_test = distance.get_distance_matrix();
System.out.println(dm_train.toString());
System.out.println(dm_test.toString());
modshogun.exit_shogun();
}
}

../examples/documented/java_modular/distance_chisquare_modular.java

// 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.
import org.shogun.*;
import org.jblas.*;
public class distance_chisquare_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix 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);
DoubleMatrix dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
DoubleMatrix dm_test = distance.get_distance_matrix();
System.out.println(dm_train.toString());
System.out.println(dm_test.toString());
modshogun.exit_shogun();
}
}

../examples/documented/java_modular/distance_cosine_modular.java

// 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.
import org.shogun.*;
import org.jblas.*;
public class distance_cosine_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix 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);
DoubleMatrix dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
DoubleMatrix dm_test = distance.get_distance_matrix();
System.out.println(dm_train.toString());
System.out.println(dm_test.toString());
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.
import org.shogun.*;
import org.jblas.*;
public class distance_geodesic_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix 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);
DoubleMatrix dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
DoubleMatrix dm_test = distance.get_distance_matrix();
System.out.println(dm_train.toString());
System.out.println(dm_test.toString());
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.
import org.shogun.*;
import org.jblas.*;
public class distance_jensen_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix 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);
DoubleMatrix dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
DoubleMatrix dm_test = distance.get_distance_matrix();
System.out.println(dm_train.toString());
System.out.println(dm_test.toString());
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.
import org.shogun.*;
import org.jblas.*;
public class distance_minkowski_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
double k = 3;
DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix 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);
DoubleMatrix dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
DoubleMatrix dm_test = distance.get_distance_matrix();
System.out.println(dm_train.toString());
System.out.println(dm_test.toString());
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.
import org.shogun.*;
import org.jblas.*;
public class distance_tanimoto_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix 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);
DoubleMatrix dm_train = distance.get_distance_matrix();
distance.init(feats_train, feats_test);
DoubleMatrix dm_test = distance.get_distance_matrix();
System.out.println(dm_train.toString());
System.out.println(dm_test.toString());
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.
import org.shogun.*;
import org.jblas.*;
import static org.shogun.EAlphabet.DNA;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class features_string_sliding_window_modular {
static {
System.loadLibrary("modshogun");
}
public static void main(String argv[]) {
modshogun.init_shogun_with_defaults();
String[] strings = new String[] {"AAAAAAAAAACCCCCCCCCCGGGGGGGGGGTTTTTTTTTT"};
StringCharFeatures f = new StringCharFeatures(strings, 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();
}
}