libsvm回归java实现

nbnkbykc  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(183)

我正在用java中的libsvm回归库执行energyforecast。在预测测试结果时,它对测试数据的每个特征集都给出相同的预测。
下面是代码
我的数据就是这样

List trainData= new ArrayList<List<Double>>();
    List testData= new ArrayList<List<Double>>();
    List temp=null;
    List<Double> pred=null;

设置支持向量机参数

svm_parameter param = new svm_parameter();
        param.svm_type = svm_parameter.EPSILON_SVR;
        param.kernel_type = svm_parameter.RBF;
        param.C = 1.0;
        param.gamma = 1;
        param.cache_size=200;
        param.coef0=0.0;
        param.degree=3;

建立支持向量机模型

svm_problem prob = new svm_problem();
        int recordCount = trainData.size();
        prob.y = new double[recordCount];
        prob.l = recordCount;

        //getting number of features
        List features= (List) trainData.get(0);
        prob.x = new svm_node[recordCount][features.size()-1];

        for (int i = 0; i < recordCount; i++){            
            features= (List) trainData.get(i);
            prob.x[i] = new svm_node[features.size()-1];
            for (int j = 0; j < features.size()-1; j++){
                svm_node node = new svm_node();
                node.index = j;
                node.value = (double) features.get(j+1);
                prob.x[i][j] = node;
            }           
            prob.y[i] = (double) features.get(0);
        } 
        //training the model
        svm_model model = svm.svm_train(prob, param);

评估模型

pred= new ArrayList<Double>();

        for(int k = 0; k < testData.size(); k++){

            List<Double> fVector = testData.get(k);

            svm_node[] nodes = new svm_node[fVector.size()-1];
            for (int i = 0; i < fVector.size()-1; i++)
            {
               svm_node node = new svm_node();
                node.index = i;
                node.value = (double) fVector.get(i+1);
                nodes[i] = node;
            }
            pred.add(svm.svm_predict(model, nodes));
        }

打印结果

for (int i = 0; i < pred.size(); i++) 
{
   List ll= (List) testData.get(i);
   System.out.println("(Actual:" + ll.get(0) + " Prediction:" + pred.get(i) + ")");
}

这就是我得到的。。。

(Actual:24076.68821 Prediction:39655.281246792365)
(Actual:26737.617019999998 Prediction:39655.281246792365)
(Actual:30801.850919999997 Prediction:39655.281246792365)
(Actual:35218.41209 Prediction:39655.281246792365)
(Actual:36237.76992 Prediction:39655.281246792365)
(Actual:36066.1183 Prediction:39655.281246792365)
(Actual:34224.65958 Prediction:39655.281246792365)
(Actual:30959.14411 Prediction:39655.281246792365)
(Actual:26310.846380000003 Prediction:39655.281246792365)
(Actual:22635.94736 Prediction:39655.281246792365)
(Actual:20206.37281 Prediction:39655.281246792365)
(Actual:18753.80365 Prediction:39655.281246792365)
(Actual:18053.6531 Prediction:39655.281246792365)
(Actual:18214.27355 Prediction:39655.281246792365)
(Actual:20045.88932 Prediction:39655.281246792365)
(Actual:24695.85854 Prediction:39655.281246792365)
(Actual:25663.172580000002 Prediction:39655.281246792365)
(Actual:23554.40137 Prediction:39655.281246792365)
(Actual:23382.007169999997 Prediction:39655.281246792365)
(Actual:23912.54456 Prediction:39655.281246792365)
(Actual:24658.7333 Prediction:39655.281246792365)
(Actual:25330.670140000002 Prediction:39655.281246792365)

有人能帮我出什么问题吗..svm参数有问题吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题