Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:11:32+00:00 2026-06-13T01:11:32+00:00

I am trying to use the java bindings for libsvm: http://www.csie.ntu.edu.tw/~cjlin/libsvm/ I have implemented

  • 0

I am trying to use the java bindings for libsvm:

http://www.csie.ntu.edu.tw/~cjlin/libsvm/

I have implemented a ‘trivial’ example which is easily linearly separable in y. The data is defined as:

double[][] train = new double[1000][]; 
double[][] test = new double[10][];

for (int i = 0; i < train.length; i++){
    if (i+1 > (train.length/2)){        // 50% positive
        double[] vals = {1,0,i+i};
        train[i] = vals;
    } else {
        double[] vals = {0,0,i-i-i-2}; // 50% negative
        train[i] = vals;
    }           
}

Where the first ‘feature’ is the class and the training set is similarly defined.

To train the model:

private svm_model svmTrain() {
    svm_problem prob = new svm_problem();
    int dataCount = train.length;
    prob.y = new double[dataCount];
    prob.l = dataCount;
    prob.x = new svm_node[dataCount][];     

    for (int i = 0; i < dataCount; i++){            
        double[] features = train[i];
        prob.x[i] = new svm_node[features.length-1];
        for (int j = 1; j < features.length; j++){
            svm_node node = new svm_node();
            node.index = j;
            node.value = features[j];
            prob.x[i][j-1] = node;
        }           
        prob.y[i] = features[0];
    }               

    svm_parameter param = new svm_parameter();
    param.probability = 1;
    param.gamma = 0.5;
    param.nu = 0.5;
    param.C = 1;
    param.svm_type = svm_parameter.C_SVC;
    param.kernel_type = svm_parameter.LINEAR;       
    param.cache_size = 20000;
    param.eps = 0.001;      

    svm_model model = svm.svm_train(prob, param);

    return model;
}

Then to evaluate the model I use:

public int evaluate(double[] features) {
    svm_node node = new svm_node();
    for (int i = 1; i < features.length; i++){
        node.index = i;
        node.value = features[i];
    }
    svm_node[] nodes = new svm_node[1];
    nodes[0] = node;

    int totalClasses = 2;       
    int[] labels = new int[totalClasses];
    svm.svm_get_labels(_model,labels);

    double[] prob_estimates = new double[totalClasses];
    double v = svm.svm_predict_probability(_model, nodes, prob_estimates);

    for (int i = 0; i < totalClasses; i++){
        System.out.print("(" + labels[i] + ":" + prob_estimates[i] + ")");
    }
    System.out.println("(Actual:" + features[0] + " Prediction:" + v + ")");            

    return (int)v;
}

Where the passed array is a point from the testing set.

The results are always returning class 0.
With the exact results being:

(0:0.9882998314585194)(1:0.011700168541480586)(Actual:0.0 Prediction:0.0)
(0:0.9883952943701599)(1:0.011604705629839989)(Actual:0.0 Prediction:0.0)
(0:0.9884899803606306)(1:0.011510019639369528)(Actual:0.0 Prediction:0.0)
(0:0.9885838957058696)(1:0.011416104294130458)(Actual:0.0 Prediction:0.0)
(0:0.9886770466322342)(1:0.011322953367765776)(Actual:0.0 Prediction:0.0)
(0:0.9870913229268679)(1:0.012908677073132284)(Actual:1.0 Prediction:0.0)
(0:0.9868781382588805)(1:0.013121861741119505)(Actual:1.0 Prediction:0.0)
(0:0.986661444476744)(1:0.013338555523255982)(Actual:1.0 Prediction:0.0)
(0:0.9864411843906802)(1:0.013558815609319848)(Actual:1.0 Prediction:0.0)
(0:0.9862172999068877)(1:0.013782700093112332)(Actual:1.0 Prediction:0.0)

Can someone explain why this classifier is not working?
Is there a step I have messed up, or a step I am missing?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:11:34+00:00Added an answer on June 13, 2026 at 1:11 am

    it seems to me that your evaluate method is wrong. Should be something like this:

    public double evaluate(double[] features, svm_model model) 
    {
        svm_node[] nodes = new svm_node[features.length-1];
        for (int i = 1; i < features.length; i++)
        {
            svm_node node = new svm_node();
            node.index = i;
            node.value = features[i];
    
            nodes[i-1] = node;
        }
    
        int totalClasses = 2;       
        int[] labels = new int[totalClasses];
        svm.svm_get_labels(model,labels);
    
        double[] prob_estimates = new double[totalClasses];
        double v = svm.svm_predict_probability(model, nodes, prob_estimates);
    
        for (int i = 0; i < totalClasses; i++){
            System.out.print("(" + labels[i] + ":" + prob_estimates[i] + ")");
        }
        System.out.println("(Actual:" + features[0] + " Prediction:" + v + ")");            
    
        return v;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a java.net.HttpURLConnection to make an HTTP request to a server.
I am trying to use Astyanax for Cassandra with Java. I tried the example
I am trying to debug some C/Java bindings that use some custom refcounting/locking. I
I am trying to use Java's useDelimiter method on it's Scanner class to do
I am trying use a Java Uploader in a ROR app (for its ease
I'm really stuck trying to use java wrapper library for opencv's cvMatchTemplate. See this
I am trying to use Sun java webserver (port 8099) as a Reverse proxy
I am trying to use a Java project called Search in my another Web
I am trying to use the HBase Java API with Trinidad in a Rails
I'm trying to re-use a Java generic collection I wrote, looks a lot like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.