package LinearRegression;
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 Legression {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
DataSource train_data = new DataSource("E://train.arff");//读训练数据
DataSource test_data = new DataSource("E://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
}
}
利用Weka API进行线性回归
最新推荐文章于 2024-08-06 09:33:31 发布