weka 建立回归模型并输出打分结果

例子是从数据库中读取数据,并拆分成建模数据,测试数据,建立多层感知器模型,并对测试数据进行打分,输出打分结果

package weka.regression;

import java.io.File;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;

import weka.Utils;
import weka.classifiers.evaluation.Evaluation;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.core.Instance;
import weka.core.Instances;

public class RegressionTest {

    public static void main(String[] args) {
        RegressionTest regressionTest = new RegressionTest();
        Instances modelData = regressionTest.loadData();
        //数据拆分成建模数据和测试数据
        int trainSize = (int) Math.round(modelData.numInstances() * 0.80);
        int testSize = modelData.numInstances() - trainSize;
        modelData.setClassIndex(modelData.numAttributes() - 1);
        Instances train = new Instances(modelData, 0, trainSize);
        train.setClassIndex(train.numAttributes() - 1);
        MultilayerPerceptron model = regressionTest.trainModel(train);
        Instances test = new Instances(modelData, trainSize, testSize);
        test.setClassIndex(test.numAttributes() - 1);
        regressionTest.evaluate(model, test);

    }

    public MultilayerPerceptron trainModel(Instances train){
        MultilayerPerceptron model = new MultilayerPerceptron();
        try {
            model.buildClassifier(train);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(model);
        return model;
    }

    public Instances loadData(){
        String sql = "select * from qy_car_model";
        Instances modelData = null;
        try {
            modelData = Utils.loadDataSetFromOracle(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelData;
    }

    public void evaluate(MultilayerPerceptron model,Instances testData){
        Evaluation eval;// 构造评价器
        try {
            eval = new Evaluation(testData);
            eval.evaluateModel(model, testData);// 用测试数据集来评价m_classifier
            double sum = testData.numInstances(); // 测试语料实例数
            StringBuffer buf = new StringBuffer();
            buf.append("SPD,ALPHA,PEDAL,BRAKE,RECOV,STEER,TEMP,真实值,预测值\n");
            for (int i = 0; i < sum; i++) {
                Instance ins = testData.instance(i);
                for(int j=0;j<ins.numAttributes();j++){
                    buf.append(ins.value(j)).append(",");//输出每条数据
                }
                buf.append(model.classifyInstance(ins));
                buf.append("\n");
            }
            File file = new File("testApplyResult.csv");
            FileUtils.writeStringToFile(file , buf.toString(),"UTF-8");
            System.out.println(buf);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }


}
public static Instances loadDataSetFromOracle(String sql) throws Exception{
        InstanceQuery query = new InstanceQuery();
        query.setUsername("ywf");
        query.setPassword("ywf");
        File file = new File("DatabaseUtils.props.oracle");
        query.initialize(file);
        query.setQuery(sql);
        Instances data = query.retrieveInstances();
       return data;
    }
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页