项目作者: chen0040

项目描述 :
Package provides the direct java conversion of the origin libsvm C codes as well as a number of adapter to make it easier to program with libsvm on Java
高级语言: Java
项目地址: git://github.com/chen0040/java-libsvm.git
创建时间: 2017-05-02T04:14:17Z
项目社区:https://github.com/chen0040/java-libsvm

开源协议:MIT License

下载


java-libsvm

Package provides the direct java conversion of the origin libsvm

Build Status Coverage Status

Install

Add the following dependency to your POM file:

  1. <dependency>
  2. <groupId>com.github.chen0040</groupId>
  3. <artifactId>java-libsvm</artifactId>
  4. <version>1.0.4</version>
  5. </dependency>

Usage

The package use data frame as containers for training and testing data (Please refers to this link on how to create a data frame from file or from scratch)

One-class SVM

Below is the code to create and train a one-class SVM:

  1. OneClassSVM algorithm = new OneClassSVM();
  2. algorithm.fit(training_data)

Below is the code to predict if data point is an outlier:

  1. algorithm.isAnomaly(data_point)

SVR

Below is the code to create and train a SVR for regression modelling:

  1. SVR algorithm = new SVR();
  2. algorithm.fit(training_data)

Below is the code to perform data regression prediction:

  1. algorithm.isAnomaly(data_point)

BinarySVC

Below is the code to create and train a BinarySVC for binary classification:

  1. BinarySVC algorithm = new BinarySVC();
  2. algorithm.fit(training_data)

Below is the code to perform data binary classification:

  1. algorithm.isInClass(data_point)

OneVsOneSVC

Below is the code to create and train a OneVsOneSVC for multi-class classification:

  1. BinarySVC algorithm = new BinarySVC();
  2. algorithm.fit(training_data)

Below is the code to perform multi-class classification:

  1. algorithm.classify(data_point)

Data Format

The data format default is the DataFrame class, which can be used to load csv and libsvm format text file. Please refers to the unit test cases on how they can be used.

Sample codes

Sample code for OneClassSVM:

Below is a sample code example of the one-class SVM for the example below here:

scki-learn example for one-class

  1. import com.github.chen0040.data.frame.DataFrame;
  2. import com.github.chen0040.data.frame.DataQuery;
  3. import com.github.chen0040.data.frame.Sampler;
  4. import com.github.chen0040.svmext.oneclass.OneClassSVM;
  5. DataQuery.DataFrameQueryBuilder schema = DataQuery.blank()
  6. .newInput("c1")
  7. .newInput("c2")
  8. .newOutput("anomaly")
  9. .end();
  10. Sampler.DataSampleBuilder negativeSampler = new Sampler()
  11. .forColumn("c1").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? -2 : 2))
  12. .forColumn("c2").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? -2 : 2))
  13. .forColumn("anomaly").generate((name, index) -> 0.0)
  14. .end();
  15. Sampler.DataSampleBuilder positiveSampler = new Sampler()
  16. .forColumn("c1").generate((name, index) -> rand(-4, 4))
  17. .forColumn("c2").generate((name, index) -> rand(-4, 4))
  18. .forColumn("anomaly").generate((name, index) -> 1.0)
  19. .end();
  20. DataFrame trainingData = schema.build();
  21. trainingData = negativeSampler.sample(trainingData, 200);
  22. System.out.println(trainingData.head(10));
  23. DataFrame crossValidationData = schema.build();
  24. crossValidationData = negativeSampler.sample(crossValidationData, 40);
  25. DataFrame outliers = schema.build();
  26. outliers = positiveSampler.sample(outliers, 40);
  27. final double threshold = 0.5;
  28. OneClassSVM algorithm = new OneClassSVM();
  29. algorithm.set_gamma(0.1);
  30. algorithm.set_nu(0.1);
  31. algorithm.thresholdSupplier = () -> 0.0;
  32. algorithm.fit(trainingData);
  33. for(int i = 0; i < crossValidationData.rowCount(); ++i){
  34. boolean predicted = algorithm.isAnomaly(crossValidationData.row(i));
  35. logger.info("predicted: {}\texpected: {}", predicted, crossValidationData.row(i).target() > threshold);
  36. }
  37. for(int i = 0; i < outliers.rowCount(); ++i){
  38. boolean predicted = algorithm.isAnomaly(outliers.row(i));
  39. logger.info("outlier predicted: {}\texpected: {}", predicted, outliers.row(i).target() > threshold);
  40. }

Sample codes for SVR

Below is another complete sample code of the SVR to predict y = 4 + 0.5 x1 + 0.2 x2:

sample image for regression

  1. import com.github.chen0040.data.frame.DataFrame;
  2. import com.github.chen0040.data.frame.DataQuery;
  3. import com.github.chen0040.data.frame.Sampler;
  4. import com.github.chen0040.svmext.oneclass.SVR;
  5. DataQuery.DataFrameQueryBuilder schema = DataQuery.blank()
  6. .newInput("x1")
  7. .newInput("x2")
  8. .newOutput("y")
  9. .end();
  10. // y = 4 + 0.5 * x1 + 0.2 * x2
  11. Sampler.DataSampleBuilder sampler = new Sampler()
  12. .forColumn("x1").generate((name, index) -> randn() * 0.3 + index)
  13. .forColumn("x2").generate((name, index) -> randn() * 0.3 + index * index)
  14. .forColumn("y").generate((name, index) -> 4 + 0.5 * index + 0.2 * index * index + randn() * 0.3)
  15. .end();
  16. DataFrame trainingData = schema.build();
  17. trainingData = sampler.sample(trainingData, 200);
  18. System.out.println(trainingData.head(10));
  19. DataFrame crossValidationData = schema.build();
  20. crossValidationData = sampler.sample(crossValidationData, 40);
  21. SVR svr = new SVR();
  22. svr.fit(trainingData);
  23. for(int i = 0; i < crossValidationData.rowCount(); ++i){
  24. double predicted = svr.transform(crossValidationData.row(i));
  25. double actual = crossValidationData.row(i).target();
  26. System.out.println("predicted: " + predicted + "\texpected: " + actual);
  27. }

Sample code for BinarySVC

Below is another complete sample code of the BinarySVC for binary classification:

  1. import com.github.chen0040.data.frame.DataFrame;
  2. import com.github.chen0040.data.frame.DataQuery;
  3. import com.github.chen0040.data.frame.Sampler;
  4. import com.github.chen0040.svmext.classifiers.BinarySVC;
  5. DataQuery.DataFrameQueryBuilder schema = DataQuery.blank()
  6. .newInput("c1")
  7. .newInput("c2")
  8. .newOutput("anomaly")
  9. .end();
  10. Sampler.DataSampleBuilder negativeSampler = new Sampler()
  11. .forColumn("c1").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? -2 : 2))
  12. .forColumn("c2").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? -2 : 2))
  13. .forColumn("anomaly").generate((name, index) -> 0.0)
  14. .end();
  15. Sampler.DataSampleBuilder positiveSampler = new Sampler()
  16. .forColumn("c1").generate((name, index) -> rand(-4, 4))
  17. .forColumn("c2").generate((name, index) -> rand(-4, 4))
  18. .forColumn("anomaly").generate((name, index) -> 1.0)
  19. .end();
  20. DataFrame trainingData = schema.build();
  21. trainingData = negativeSampler.sample(trainingData, 200);
  22. trainingData = positiveSampler.sample(trainingData, 200);
  23. System.out.println(trainingData.head(10));
  24. DataFrame crossValidationData = schema.build();
  25. crossValidationData = negativeSampler.sample(crossValidationData, 40);
  26. crossValidationData = positiveSampler.sample(crossValidationData, 40);
  27. BinarySVC algorithm = new BinarySVC();
  28. algorithm.fit(trainingData);
  29. BinaryClassifierEvaluator evaluator = new BinaryClassifierEvaluator();
  30. for(int i = 0; i < crossValidationData.rowCount(); ++i){
  31. boolean predicted = algorithm.isInClass(crossValidationData.row(i));
  32. boolean actual = crossValidationData.row(i).target() > 0.5;
  33. evaluator.evaluate(actual, predicted);
  34. System.out.println("predicted: " + predicted + "\texpected: " + actual);
  35. }
  36. evaluator.report();

Sample codes for OneVsOneSVC

Below is another complete sample code of the OneVsOneSVC for multi-class classification:

  1. import com.github.chen0040.data.frame.DataFrame;
  2. import com.github.chen0040.data.frame.DataQuery;
  3. import com.github.chen0040.data.frame.Sampler;
  4. import com.github.chen0040.svmext.classifiers.OneVsOneSVC;
  5. InputStream irisStream = new FileInputStream("iris.data");
  6. DataFrame irisData = DataQuery.csv(",", false)
  7. .from(irisStream)
  8. .selectColumn(0).asNumeric().asInput("Sepal Length")
  9. .selectColumn(1).asNumeric().asInput("Sepal Width")
  10. .selectColumn(2).asNumeric().asInput("Petal Length")
  11. .selectColumn(3).asNumeric().asInput("Petal Width")
  12. .selectColumn(4).asCategory().asOutput("Iris Type")
  13. .build();
  14. TupleTwo<DataFrame, DataFrame> parts = irisData.shuffle().split(0.9);
  15. DataFrame trainingData = parts._1();
  16. DataFrame crossValidationData = parts._2();
  17. System.out.println(crossValidationData.head(10));
  18. OneVsOneSVC multiClassClassifier = new OneVsOneSVC();
  19. multiClassClassifier.fit(trainingData);
  20. ClassifierEvaluator evaluator = new ClassifierEvaluator();
  21. for(int i=0; i < crossValidationData.rowCount(); ++i) {
  22. String predicted = multiClassClassifier.classify(crossValidationData.row(i));
  23. String actual = crossValidationData.row(i).categoricalTarget();
  24. System.out.println("predicted: " + predicted + "\tactual: " + actual);
  25. evaluator.evaluate(actual, predicted);
  26. }
  27. evaluator.report();