LIBSVM Support Vector Machine regression example _java

Source: Internet
Author: User

LIBSVM Support Vector machine algorithm package for the basic use, here is demonstrated support vector regression machine

Copy Code code as follows:

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileReader;
Import java.util.ArrayList;
Import java.util.List;

Import LIBSVM.SVM;
Import Libsvm.svm_model;
Import Libsvm.svm_node;
Import Libsvm.svm_parameter;
Import Libsvm.svm_problem;

Public class SVM {
 public static void Main (string[] args) {
  //defines the training set point a{10.0, 10.0}, and Point b{-10.0. -10.0}, corresponding to lable {1.0, -1.0}
  List<Double> label = new Arraylist<double> ();
  List<svm_node[]> nodeset = new arraylist<svm_node[]> ();
  getdata (nodeset, label, "File/train.txt");

  int datarange=nodeset.get (0). length;
  svm_node[][] datas = new Svm_node[nodeset.size ()][datarange]; The vector table of the training set
  for (int i = 0; i < datas.length; i++) {
   for (int j = 0; J < Datarang E J + +) {
    datas[i][j] = nodeset.get (i) [j];
&NBSP;&NBSP;&NBSP}
  }
  double[] lables = new double[label.size ()];//a,b corresponding lable
  for (int i = 0; i < lables.length; i++) {
   lables[i] = Label.get (i);
  .}

Defining Svm_problem Objects
Svm_problem problem = new Svm_problem ();
PROBLEM.L = Nodeset.size (); Number of vectors
problem.x = datas; Training set Vector table
PROBLEM.Y = lables; The corresponding lable array

Defining Svm_parameter Objects
Svm_parameter param = new Svm_parameter ();
Param.svm_type = Svm_parameter. Epsilon_svr;
Param.kernel_type = Svm_parameter. LINEAR;
param.cache_size = 100;
Param.eps = 0.00001;
Param. C = 1.9;
Classification model of training SVM
System.out.println (Svm.svm_check_parameter (problem, param));
If there is no problem with the parameter, the Svm.svm_check_parameter () function returns NULL, otherwise the error description is returned.
Svm_model model = Svm.svm_train (problem, param);
Svm.svm_train () Training the SVM classification model

Get test data
list<double> TestLabel = new arraylist<double> ();
list<svm_node[]> testnodeset = new arraylist<svm_node[]> ();
GetData (Testnodeset, TestLabel, "file/test.txt");

svm_node[][] Testdatas = new Svm_node[testnodeset.size ()][datarange]; Vector Tables for training sets
for (int i = 0; i < testdatas.length; i++) {
for (int j = 0; J < DataRange; J + +) {
TESTDATAS[I][J] = Testnodeset.get (i) [j];
}
}
double[] Testlables = new double[testlabel.size ()]; A,b corresponds to the lable
for (int i = 0; i < testlables.length; i++) {
Testlables[i] = Testlabel.get (i);
}

Lable of predictive test data
Double err = 0.0;
for (int i = 0; i < testdatas.length; i++) {
Double truevalue = testlables[i];
System.out.print (TrueValue + "");
Double predictvalue = svm.svm_predict (model, testdatas[i]);
System.out.println (Predictvalue);
Err + math.abs (predictvalue-truevalue);
}
System.out.println ("err=" + err/datas.length);
}

public static void GetData (List<svm_node[]> nodeset, list<double> label,
String filename) {
try {

FileReader FR = new FileReader (new File (filename));
BufferedReader br = new BufferedReader (FR);
String line = null;
while (line = Br.readline ())!= null) {
string[] Datas = Line.split (",");
svm_node[] vector = new Svm_node[datas.length-1];
for (int i = 0; i < datas.length-1; i++) {
Svm_node node = new Svm_node ();
Node.index = i + 1;
Node.value = double.parsedouble (Datas[i]);
Vector[i] = node;
}
Nodeset.add (vector);
Double lablevalue = double.parsedouble (datas[datas.length-1]);
Label.add (Lablevalue);
}
catch (Exception e) {
E.printstacktrace ();
}

}
}

Training data, final column target value

Copy Code code as follows:

17.6,17.7,17.7,17.7,17.8
17.7,17.7,17.7,17.8,17.8
17.7,17.7,17.8,17.8,17.9
17.7,17.8,17.8,17.9,18
17.8,17.8,17.9,18,18.1
17.8,17.9,18,18.1,18.2
17.9,18,18.1,18.2,18.4
18,18.1,18.2,18.4,18.6
18.1,18.2,18.4,18.6,18.7
18.2,18.4,18.6,18.7,18.9
18.4,18.6,18.7,18.9,19.1
18.6,18.7,18.9,19.1,19.3

Test data

Copy Code code as follows:

18.7,18.9,19.1,19.3,19.6
18.9,19.1,19.3,19.6,19.9
19.1,19.3,19.6,19.9,20.2
19.3,19.6,19.9,20.2,20.6
19.6,19.9,20.2,20.6,21
19.9,20.2,20.6,21,21.5
20.2,20.6,21,21.5,22


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.