gpt4 book ai didi

java - 在 weka 中添加一个新实例

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:04:10 25 4
gpt4 key购买 nike

如何将新实例添加到我创建的现有实例对象?

这是一个例子:

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 类是一个接口(interface),我如何创建一个实例对象?

提前致谢

最佳答案

让我们从一些亮点开始。

  • weka 将每个实例值存储为 double[]。因此,您创建 double[] instanceValue1 并将值添加到此数组。
  • 甚至字符串都存储在这个 double[] 中。您使用以下代码添加字符串值:

instanceValue1[0] = dataRaw.attribute(0).addStringValue("This is a string!");

  • Instance 是接口(interface),由两个类{SparseInstance , DenseInstance} 实现。我们使用以下代码将值数组添加到数据集:

dataRaw.add(new DenseInstance(1.0, instanceValue1));

完整的运行示例:

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

关于java - 在 weka 中添加一个新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12118132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com