本文介绍了在weka中添加新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将新实例添加到我创建的现有Instances对象中?
How can I add a new Instance to an existing Instances object that I created ?
以下是一个示例:
ArrayList<Attribute> atts = new ArrayList<Attribute>(2);
ArrayList<String> classVal = new ArrayList<String>();
classVal.add("A");
classVal.add("B");
atts.add(new Attribute("content",(ArrayList<String>)null));
atts.add(new Attribute("@@class@@",classVal));
Instances dataRaw = new Instances("TestInstances",atts,0);
我想在dataRaw中添加一个新实例。据我所知,我必须使用dataRaw.add(Instance i)....如果Instance类是一个接口,我怎样才能创建一个实例对象?
I want to add a new instance to dataRaw. As far as I know I have to use dataRaw.add(Instance i) .... How can I create an instance object if the Instance class is an interface ?
在此先感谢
推荐答案
让我们先来看一些亮点。
Let start with some highlights.
- weka将每个实例值存储为double []。因此,您创建double [] instanceValue1并向此数组添加值。
- 甚至字符串也存储在此double []中。使用以下代码添加字符串值:
- 实例是接口,它实现了由两个类组成,{SparseInstance,DenseInstance}。我们使用以下代码将我们的值数组添加到数据集:
以下是完整的运行示例:
Here is the complete running example:
import java.util.ArrayList;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
public class Program {
public static void main(String[] args) {
ArrayList<Attribute> atts = new ArrayList<Attribute>(2);
ArrayList<String> classVal = new ArrayList<String>();
classVal.add("A");
classVal.add("B");
atts.add(new Attribute("content",(ArrayList<String>)null));
atts.add(new Attribute("@@class@@",classVal));
Instances dataRaw = new Instances("TestInstances",atts,0);
System.out.println("Before adding any instance");
System.out.println("--------------------------");
System.out.println(dataRaw);
System.out.println("--------------------------");
double[] instanceValue1 = new double[dataRaw.numAttributes()];
instanceValue1[0] = dataRaw.attribute(0).addStringValue("This is a string!");
instanceValue1[1] = 0;
dataRaw.add(new DenseInstance(1.0, instanceValue1));
System.out.println("After adding a instance");
System.out.println("--------------------------");
System.out.println(dataRaw);
System.out.println("--------------------------");
double[] instanceValue2 = new double[dataRaw.numAttributes()];
instanceValue2[0] = dataRaw.attribute(0).addStringValue("This is second string!");
instanceValue2[1] = 1;
dataRaw.add(new DenseInstance(1.0, instanceValue2));
System.out.println("After adding second instance");
System.out.println("--------------------------");
System.out.println(dataRaw);
System.out.println("--------------------------");
}
}
它的输出如下:
Before adding any instance
--------------------------
@relation TestInstances
@attribute content string
@attribute @@class@@ {A,B}
@data
--------------------------
After adding a instance
--------------------------
@relation TestInstances
@attribute content string
@attribute @@class@@ {A,B}
@data
'This is a string!',A
--------------------------
After adding second instance
--------------------------
@relation TestInstances
@attribute content string
@attribute @@class@@ {A,B}
@data
'This is a string!',A
'This is second string!',B
--------------------------
这篇关于在weka中添加新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!