代码-Weka的LinearRegression类

时间:2022-04-29 17:25:18
package kit.weka;
import weka.classifiers.Evaluation;  
import weka.classifiers.functions.LinearRegression;  
import weka.core.Instance;  
import weka.core.Instances;  
import weka.core.converters.ConverterUtils.DataSource;  
public class LegressionTest 
{  
    public static void main(String[] args) throws Exception 
    {  
        // TODO Auto-generated method stub   
        DataSource train_data = new DataSource("d:\\Program Files\\Weka-3-7\\data\\train.arff");//读训练数据  
        DataSource test_data = new DataSource("d:\\Program Files\\Weka-3-7\\data\\test.arff");//读测试数据  
        Instances insTrain = train_data.getDataSet();  
        Instances insTest = test_data.getDataSet();  
        insTrain.setClassIndex(insTrain.numAttributes()-1);//设置训练集中,target的index   
        insTest.setClassIndex(insTest.numAttributes()-1);//设置测试集中,target的index   
        LinearRegression lr = new LinearRegression();//定义分类器的类型   
        lr.buildClassifier(insTrain);//训练分类器   
        Evaluation eval=new Evaluation(insTrain);  
        eval.evaluateModel(lr, insTest);//评估效果   
        System.out.println(eval.meanAbsoluteError());//计算MAE   
    }  
} 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }