如何在java中从数组创建ARFF文件?

我想得到java中两个数组所代表的xy对的加权线性回归系数。 我已经注意到了weka,但它在’LinearRegression’类中询问了一个’Instances’类对象。 要创建“Instances”类文件,需要包含数据的ARFF文件。 我遇到过使用FastVector类的解决方案,但现在已经在最新的weka版本中弃用了。 如何为xy对创建一个ARFF文件,并在java中用数组表示相应的权重?

这是我的代码基于Baz的答案。 它在最后一行“lr.buildClassifier(newDataset)”上给出一个例外 – Thread [main](Suspended(exceptionUnassignedClassException))
Capabilities.testWithFail(Instances)行:1302。 这是代码 –

public static void test() throws Exception { double[][] data = {{4058.0, 4059.0, 4060.0, 214.0, 1710.0, 2452.0, 2473.0, 2474.0, 2475.0, 2476.0, 2477.0, 2478.0, 2688.0, 2905.0, 2906.0, 2907.0, 2908.0, 2909.0, 2950.0, 2969.0, 2970.0, 3202.0, 3342.0, 3900.0, 4007.0, 4052.0, 4058.0, 4059.0, 4060.0}, {19.0, 20.0, 21.0, 31.0, 103.0, 136.0, 141.0, 142.0, 143.0, 144.0, 145.0, 146.0, 212.0, 243.0, 244.0, 245.0, 246.0, 247.0, 261.0, 270.0, 271.0, 294.0, 302.0, 340.0, 343.0, 354.0, 356.0, 357.0, 358.0}}; int numInstances = data[0].length; ArrayList atts = new ArrayList(); List instances = new ArrayList(); for(int dim = 0; dim < 2; dim++) { Attribute current = new Attribute("Attribute" + dim, dim); if(dim == 0) { for(int obj = 0; obj < numInstances; obj++) { instances.add(new SparseInstance(numInstances)); } } for(int obj = 0; obj < numInstances; obj++) { instances.get(obj).setValue(current, data[dim][obj]); //instances.get(obj).setWeight(weights[obj]); } atts.add(current); } Instances newDataset = new Instances("Dataset", atts, instances.size()); for(Instance inst : instances) newDataset.add(inst); LinearRegression lr = new LinearRegression(); lr.buildClassifier(newDataset); } 

我想这可能会对你有所帮助:

 FastVector atts = new FastVector(); List instances = new ArrayList(); for(int dim = 0; dim < numDimensions; dim++) { // Create new attribute / dimension Attribute current = new Attribute("Attribute" + dim, dim); // Create an instance for each data object if(dim == 0) { for(int obj = 0; obj < numInstances; obj++) { instances.add(new SparseInstance(numDimensions)); } } // Fill the value of dimension "dim" into each object for(int obj = 0; obj < numInstances; obj++) { instances.get(obj).setValue(current, data[dim][obj]); } // Add attribute to total attributes atts.addElement(current); } // Create new dataset Instances newDataset = new Instances("Dataset", atts, instances.size()); // Fill in data objects for(Instance inst : instances) newDataset.add(inst); 

之后Instances就是数据集。

注意 :即使我使用了FastVector ,Weka的当前版本(3.6.8)也没有抱怨。

但是,对于Developer版本(3.7.7),请使用:

 ArrayList atts = new ArrayList(); List instances = new ArrayList(); for(int dim = 0; dim < numDimensions; dim++) { Attribute current = new Attribute("Attribute" + dim, dim); if(dim == 0) { for(int obj = 0; obj < numInstances; obj++) { instances.add(new SparseInstance(numDimensions)); } } for(int obj = 0; obj < numInstances; obj++) { instances.get(obj).setValue(current, data[dim][obj]); } atts.add(current); } Instances newDataset = new Instances("Dataset", atts, instances.size()); for(Instance inst : instances) newDataset.add(inst); 

您想构造一个Instances对象,该类重写toString()以ARFF格式输出。 如果不推荐使用FastVector,您可以使用Vector 。