java—在weka中添加新示例

bjg7j2ky  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(433)

如何将新示例添加到我创建的现有示例对象中?
举个例子:

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)。。。。如果示例类是接口,如何创建示例对象?
提前谢谢

57hvy0tb

57hvy0tb1#

让我们从一些亮点开始。
weka将每个示例值存储为double[]。因此,创建双[]instancevalue1并将值添加到此数组。
偶字符串存储在这个double[]中。使用以下代码添加字符串值:
instancevalue1[0]=dataraw.attribute(0).addstringvalue(“这是一个字符串!”);
示例是接口,它由两个类实现,{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
--------------------------

相关问题