Java 如何在不为WEKA中的实例创建ARFF文件的情况下对该实例进行预测?
上学期我有一个项目,当我得到一组关于汽车的数据时,我必须建立一个模型,并使用该模型根据用户输入的数据进行预测(它涉及一个GUI等等)。教授介绍了Weka,但只介绍了它的GUI形式。我正在重新创建项目,但这次是使用Weka库。以下是所讨论的课程:Java 如何在不为WEKA中的实例创建ARFF文件的情况下对该实例进行预测?,java,weka,c4.5,Java,Weka,C4.5,上学期我有一个项目,当我得到一组关于汽车的数据时,我必须建立一个模型,并使用该模型根据用户输入的数据进行预测(它涉及一个GUI等等)。教授介绍了Weka,但只介绍了它的GUI形式。我正在重新创建项目,但这次是使用Weka库。以下是所讨论的课程: public class TreeModel { private J48 model = new J48(); private String[] options = new String[1]; private DataSource source; pr
public class TreeModel {
private J48 model = new J48();
private String[] options = new String[1];
private DataSource source;
private Instances data;
private Evaluation eval;
// Constructor
public TreeModel(String file) throws Exception {
source = new DataSource(file);
// By default, the options are set to produce unpruned tree '-U'
options[0] = "-U";
data = source.getDataSet();
model.setOptions(options);
}
// Overloaded constructor allowing you to choose options for the model
public TreeModel(String file, String[] options) throws Exception {
DataSource source = new DataSource(file);
data = source.getDataSet();
model.setOptions(options);
}
// Builds the decision tree
public void buildDecisionTree() throws Exception {
data.setClassIndex(data.numAttributes() - 1);
model.buildClassifier(data);
}
/*
* Uses cross validation technique to calculate the accuracy.
* Gives a more respected accuracy that is more likely to hold
* with instances not in the dataset.
*/
public void crossValidatedEvaluation(int folds) throws Exception {
eval = new Evaluation(data);
eval.crossValidateModel(model, data, folds, new Random());
System.out.println("The model predicted "+eval.pctCorrect()+" percent of the data correctly.");
}
/*
* Evaluates the accuracy of a decision tree when using all available data
* This should be looked at with skepticism (less interpretable)
*/
public void evaluateModel() throws Exception {
eval = new Evaluation(data);
eval.evaluateModel(model, data);
System.out.println("The model predicted "+eval.pctCorrect()+" percent of the data correctly.");
}
/*
* Returns a prediction for a particular instance. Will take in an instance
* as a parameter.
*/
public String getPrediction() throws Exception {
DataSource predFile = new DataSource("./predict.arff");
Instances pred = predFile.getDataSet();
Instance predic = pred.get(0);
pred.setClassIndex(pred.numAttributes() - 1);
double classify = model.classifyInstance(predic);
pred.instance(0).setClassValue(classify);
return pred.instance(0).stringValue(6);
}
// Returns source code version of the model (warning: messy code)
public String getModelSourceCode() throws Exception {
return model.toSource("DecisionTree");
}
}
在我的getPrediction()方法中,我有一个简单的示例,用于获取ARFF文件中实例的预测。问题是,我不知道如何初始化一个实例对象,然后将我想要进行预测的数据放在该实例中。我已经浏览了实例类的文档,但乍一看没有看到任何内容。有没有办法手动将数据放入实例中,或者我需要将预测数据转换为ARFF文件?此代码片段应该可以帮助您构建自己的实例集,而无需ARFF文件。下面我将展示如何从具有两个属性的数组中创建一组新实例;经纬度
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.FastVector;
import weka.core.Instances;
public class AttTest {
public static void main(String[] args) throws Exception
{
double[] one={0,1,2,3};
double[] two={3,2,1,0};
double[][] both=new double[2][4];
both[0]=one;
both[1]=two;
Instances to_use=AttTest.buildArff(both);
System.out.println(to_use.toString());
}
public static Instances buildArff(double[][] array) throws Exception
{
FastVector atts = new FastVector();
atts.addElement(new Attribute("lat")); //latitude
atts.addElement(new Attribute("lon")); //longitude
// 2. create Instances object
Instances test = new Instances("location", atts, 0);
// 3. fill with data
for(int s1=0; s1 < array[0].length; s1=s1+1)
{
double vals[] = new double[test.numAttributes()];
vals[0] = array[0][s1];
vals[1] = array[1][s1];
test.add(new DenseInstance(1.0, vals));
}
return(test);
}
}
导入weka.core.Attribute;
导入weka.core.DenseInstance;
导入weka.core.FastVector;
导入weka.core.Instances;
公开课测验{
公共静态void main(字符串[]args)引发异常
{
双[]一={0,1,2,3};
double[]two={3,2,1,0};
double[]两者均=新的double[2][4];
两者[0]=1;
两个[1]=两个;
要使用的实例=AttTest.buildArff(两者);
System.out.println(使用.toString());
}
公共静态实例buildArff(双[][]数组)引发异常
{
FastVector atts=新的FastVector();
atts.addElement(新属性(“lat”);//纬度
atts.addElement(新属性(“lon”);//经度
//2.创建实例对象
实例测试=新实例(“位置”,附件,0);
//3.填写数据
对于(int s1=0;s1