我正在尝试使用libsvm在Java中实现SVM分类器。我收到以下错误:
Exception in thread "main" java.lang.NullPointerException
at libsvm.Kernel.dot(svm.java:213)
at libsvm.Kernel.kernel_function(svm.java:172)
at libsvm.SVC_Q.<init>(svm.java:1161)
at libsvm.svm.solve_c_svc(svm.java:1333)
at libsvm.svm.svm_train_one(svm.java:1510)
at libsvm.svm.svm_train(svm.java:2067)
我正在尝试在6个训练样例上实现二元分类器: 这是我的代码:
import libsvm.*;
import java.io.*;
import java.io.Reader;
import java.lang.Object;
public class SVMclassifier {
public SVMclassifier(){
svm_problem svmData=new svm_problem();
svmData.l=6;
double[] lab={1,0,1,1,0,0};
svmData.y=lab;
svm_node[][] values=new svm_node[6][3];
System.out.print("I am here");
svm_node AA=new svm_node();
AA.index=1;
AA.value=0.1;
values[0][1]=AA;
AA.index=1;
AA.value=0.9;
values[0][2]=AA;
AA.index=1;
AA.value=0.9;
values[1][0]=AA;
AA.index=2;
AA.value=0.1;
values[1][1]=AA;
AA.index=1;
AA.value=0.05;
values[2][0]=AA;
AA.index=2;
AA.value=0.95;
values[2][1]=AA;
AA.index=1;
AA.value=0.06;
values[3][0]=AA;
AA.index=2;
AA.value=0.90;
values[3][1]=AA;
AA.index=1;
AA.value=0.95;
values[4][0]=AA;
AA.index=2;
AA.value=0.05;
values[4][1]=AA;
AA.index=1;
AA.value=0.90;
values[5][0]=AA;
AA.index=2;
AA.value=0.06;
values[5][1]=AA;
svmData.x=values;
svm_model paras=new svm_model();
svm_parameter params =new svm_parameter();
int[] bb={1,1,1};
double[] cc={1,1,1};
System.out.println("I am here");
params.svm_type = svm_parameter.C_SVC;
params.kernel_type = svm_parameter.LINEAR;
params.degree = 3;
params.gamma = 0.5; // 1/num_features
params.coef0 = 0;
params.nu = 0.5;
params.cache_size = 100;
params.C = 1;
params.eps = 1e-3;
params.p = 0.1;
params.shrinking = 1;
params.probability = 0;
params.nr_weight = 0;
params.weight_label=bb;
params.weight=cc;
paras=svm.svm_train(svmData, params) ;
for(int i=0;i<=5;i=i+1){
svm_node[] v=values[i];
double u=svm.svm_predict(paras, v);
System.out.println(u);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SVMclassifier A=new SVMclassifier();
}
}
我是java和libsvm的新手。任何帮助都会受到欢迎。感谢