Skip to content

Commit

Permalink
fix java modular examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed May 25, 2012
1 parent 43b5b5e commit 1de9b14
Show file tree
Hide file tree
Showing 32 changed files with 114 additions and 66 deletions.
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.List;
import java.io.Serializable;
import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_averaged_perceptron_modular{
static {
Expand All @@ -23,14 +24,14 @@ public static void main(String argv[]) {
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);
Labels labels = new Labels(trainlab);
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);
DoubleMatrix out_labels = perceptron.apply().get_labels();
DoubleMatrix out_labels = obtain_from_generic(perceptron.apply()).get_labels();

modshogun.exit_shogun();
}
Expand Down
Expand Up @@ -2,6 +2,8 @@
import org.jblas.*;
import static org.shogun.EAlphabet.DNA;

import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_domainadaptationsvm_modular {
static {
System.loadLibrary("modshogun");
Expand Down Expand Up @@ -38,15 +40,15 @@ public static void main(String argv[]) {

WeightedDegreeStringKernel kernel = new WeightedDegreeStringKernel(feats_train, feats_train, degree);
double label_train_dna[][] = {{-1,-1,-1,-1,-1,1,1,1,1,1}};
Labels labels = new Labels(new DoubleMatrix(label_train_dna));
BinaryLabels labels = new BinaryLabels(new DoubleMatrix(label_train_dna));

SVMLight svm = new SVMLight(C, kernel, labels);
svm.train();

DomainAdaptationSVM dasvm = new DomainAdaptationSVM(C, kernel, labels, svm, 1.0);
dasvm.train();

DoubleMatrix out = dasvm.apply(feats_test).get_labels();
DoubleMatrix out = obtain_from_generic(dasvm.apply(feats_test)).get_labels();
modshogun.exit_shogun();
}
}
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.MulticlassLabels.obtain_from_generic;

public class classifier_gaussiannaivebayes_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -18,11 +20,11 @@ public static void main(String argv[]) {
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);
Labels labels = new Labels(trainlab);
MulticlassLabels labels = new MulticlassLabels(trainlab);

GaussianNaiveBayes gnb = new GaussianNaiveBayes(feats_train, labels);
gnb.train();
DoubleMatrix out_labels = gnb.apply(feats_test).get_labels();
DoubleMatrix out_labels = obtain_from_generic(gnb.apply(feats_test)).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.MulticlassLabels.obtain_from_generic;

public class classifier_gmnpsvm_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -24,13 +26,13 @@ public static void main(String argv[]) {

GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);

Labels labels = new Labels(trainlab);
MulticlassLabels labels = new MulticlassLabels(trainlab);

GMNPSVM svm = new GMNPSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();
kernel.init(feats_train, feats_test);
DoubleMatrix out_labels = svm.apply(feats_test).get_labels();
DoubleMatrix out_labels = obtain_from_generic(svm.apply(feats_test)).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_gpbtsvm_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -24,13 +26,13 @@ public static void main(String argv[]) {

GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);

Labels labels = new Labels(trainlab);
BinaryLabels labels = new BinaryLabels(trainlab);

GPBTSVM svm = new GPBTSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();
kernel.init(feats_train, feats_test);
DoubleMatrix out_labels = svm.apply().get_labels();
DoubleMatrix out_labels = obtain_from_generic(svm.apply()).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.MulticlassLabels.obtain_from_generic;

public class classifier_knn_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -19,11 +21,11 @@ public static void main(String argv[]) {
RealFeatures feats_test = new RealFeatures(testdata_real);
EuclidianDistance distance = new EuclidianDistance(feats_train, feats_train);

Labels labels = new Labels(trainlab);
MulticlassLabels labels = new MulticlassLabels(trainlab);

KNN knn = new KNN(k, distance, labels);
knn.train();
DoubleMatrix out_labels = knn.apply(feats_test).get_labels();
DoubleMatrix out_labels = obtain_from_generic(knn.apply(feats_test)).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.MulticlassLabels.obtain_from_generic;

public class classifier_larank_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -24,13 +26,13 @@ public static void main(String argv[]) {

GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);

Labels labels = new Labels(trainlab);
MulticlassLabels labels = new MulticlassLabels(trainlab);

LaRank svm = new LaRank(C, kernel, labels);
svm.set_batch_mode(false);
svm.set_epsilon(epsilon);
svm.train();
DoubleMatrix out_labels = svm.apply(feats_train).get_labels();
DoubleMatrix out_labels = obtain_from_generic(svm.apply(feats_train)).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_lda_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -20,15 +22,15 @@ public static void main(String argv[]) {
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);

Labels labels = new Labels(trainlab);
BinaryLabels labels = new BinaryLabels(trainlab);

LDA lda = new LDA(gamma, feats_train, labels);
lda.train();

System.out.println(lda.get_bias());
System.out.println(lda.get_w().toString());
lda.set_features(feats_test);
DoubleMatrix out_labels = lda.apply().get_labels();
DoubleMatrix out_labels = obtain_from_generic(lda.apply()).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.LIBLINEAR_SOLVER_TYPE.L2R_L2LOSS_SVC_DUAL;
import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_liblinear_modular {
static {
Expand All @@ -23,15 +25,15 @@ public static void main(String argv[]) {
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);

Labels labels = new Labels(trainlab);
BinaryLabels labels = new BinaryLabels(trainlab);

LibLinear svm = new LibLinear(C, feats_train, labels);
svm.set_liblinear_solver_type(L2R_L2LOSS_SVC_DUAL);
svm.set_epsilon(epsilon);
svm.set_bias_enabled(true);
svm.train();
svm.set_features(feats_test);
DoubleMatrix out_labels = svm.apply().get_labels();
DoubleMatrix out_labels = obtain_from_generic(svm.apply()).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,5 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_libsvm_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -23,14 +26,14 @@ public static void main(String argv[]) {

GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);

Labels labels = new Labels(trainlab);
BinaryLabels labels = new BinaryLabels(trainlab);

LibSVM svm = new LibSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();

kernel.init(feats_train, feats_test);
DoubleMatrix out_labels = svm.apply().get_labels();
DoubleMatrix out_labels = obtain_from_generic(svm.apply()).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_libsvmoneclass_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -27,7 +29,7 @@ public static void main(String argv[]) {
svm.train();

kernel.init(feats_train, feats_test);
DoubleMatrix out_labels = svm.apply().get_labels();
DoubleMatrix out_labels = obtain_from_generic(svm.apply()).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_mpdsvm_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -24,14 +26,14 @@ public static void main(String argv[]) {

GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);

Labels labels = new Labels(trainlab);
BinaryLabels labels = new BinaryLabels(trainlab);

MPDSVM svm = new MPDSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();

kernel.init(feats_train, feats_test);
DoubleMatrix out_labels = svm.apply().get_labels();
DoubleMatrix out_labels = obtain_from_generic(svm.apply()).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.MulticlassLabels.obtain_from_generic;

public class classifier_multiclasslibsvm_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -24,14 +26,14 @@ public static void main(String argv[]) {

GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);

Labels labels = new Labels(trainlab);
MulticlassLabels labels = new MulticlassLabels(trainlab);

MulticlassLibSVM svm = new MulticlassLibSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();

kernel.init(feats_train, feats_test);
DoubleMatrix out_labels = svm.apply().get_labels();
DoubleMatrix out_labels = obtain_from_generic(svm.apply()).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,6 +1,8 @@
import org.shogun.*;
import org.jblas.*;

import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_perceptron_modular {
static {
System.loadLibrary("modshogun");
Expand All @@ -21,15 +23,15 @@ public static void main(String argv[]) {
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);

Labels labels = new Labels(trainlab);
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);
DoubleMatrix out_labels = perceptron.apply().get_labels();
DoubleMatrix out_labels = obtain_from_generic(perceptron.apply()).get_labels();
System.out.println(out_labels.toString());

modshogun.exit_shogun();
Expand Down
@@ -1,10 +1,12 @@
import org.shogun.*;
import org.jblas.*;
import static org.shogun.EAlphabet.DNA;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.shogun.EAlphabet.DNA;
import static org.shogun.BinaryLabels.obtain_from_generic;

public class classifier_svmlight_linear_term_modular {
static {
System.loadLibrary("modshogun");
Expand Down Expand Up @@ -43,7 +45,7 @@ public static void main(String argv[]) {

WeightedDegreeStringKernel kernel = new WeightedDegreeStringKernel(feats_train, feats_train, degree);
double label_train_dna[][] = {{-1,-1,-1,-1,-1,1,1,1,1,1}};
Labels labels = new Labels(new DoubleMatrix(label_train_dna));
BinaryLabels labels = new BinaryLabels(new DoubleMatrix(label_train_dna));

SVMLight svm = new SVMLight(C, kernel, labels);
svm.set_qpsize(3);
Expand All @@ -53,7 +55,7 @@ public static void main(String argv[]) {
svm.train();

kernel.init(feats_train, feats_test);
svm.apply().get_labels();
obtain_from_generic(svm.apply()).get_labels();

modshogun.exit_shogun();
}
Expand Down

0 comments on commit 1de9b14

Please sign in to comment.