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

I want to get the coefficients of a weighted linear regression of an x-y pair represented by two arrays in java. I have zeroed in on weka, but it is asking an 'Instances' class object in the 'LinearRegression' class. To create an 'Instances' class file, an ARFF file is needed which contains the data. I have come across solutions that use the FastVector class but that has now been deprecated in the latest weka version. How do I create an ARFF file for the x-y pair and the corresponding weights all represented by arrays in java?

Here's my code based on Baz's answer. It's giving an exception on the last line "lr.buildClassifier(newDataset)" - Thread [main] (Suspended (exception UnassignedClassException))

Capabilities.testWithFail(Instances) line: 1302 . Here's the code -

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);

}

解决方案

I think this might help you:

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);

Afterwards Instances is you dataset.

Note: The current version (3.6.8) of Weka did not complain, even though I used FastVector.

However, for the Developer version (3.7.7), use this:

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值