gpt4 book ai didi

java - weka:将新实例添加到数据集

转载 作者:行者123 更新时间:2023-12-01 06:18:42 27 4
gpt4 key购买 nike

我有一个 weka 数据集:

@attribute uid numeric
@attribute itemid numeric
@attribute rating numeric
@attribute timestamp numeric

@data
196 242 3 881250949
186 302 3 891717742
22 377 1 878887116
196 51 5 881250949
244 51 2 880606923

如果我想添加一个像这样的新实例:

244 59  2   880606923

我该怎么做?

类似这样的东西?

Instances newData = arffLoader.getDataSet();
for (int i = 0; i < newData.numInstances(); i++) {
Instance one = newData.instance(i);
one.setDataset(data);
data.add(one);
}

最佳答案

尝试下面的代码。您需要做什么来为新值创建一个 double 组。使用 DenseInstance 类将它们添加到您的 Instances 对象中。

public static void main(String[] args) {


String dataSetFileName = "stackoverflowQuestion.arff";
Instances data = MyUtilsForWekaInstanceHelper.getInstanceFromFile(dataSetFileName);
System.out.println("Before adding");
System.out.println(data);


double[] instanceValue1 = new double[data.numAttributes()];
instanceValue1[0] = 244;
instanceValue1[1] = 59;
instanceValue1[2] = 2;
instanceValue1[3] = 880606923;

DenseInstance denseInstance1 = new DenseInstance(1.0, instanceValue1);

data.add(denseInstance1);

System.out.println("-----------------------------------------------------------");
System.out.println("After adding");
System.out.println(data);


public class MyUtilsForWekaInstanceHelper {

public static Instances getInstanceFromFile(String pFileName)
{
Instances data = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(pFileName));
data = new Instances(reader);
reader.close();
// setting class attribute
data.setClassIndex(data.numAttributes() - 1);
}
catch (Exception e) {
throw new RuntimeException(e);
}
return data;

}
}

输出如下。

Before adding
@relation stackoverflowQuestion

@attribute uid numeric
@attribute itemid numeric
@attribute rating numeric
@attribute timestamp numeric

@data
196,242,3,881250949
186,302,3,891717742
22,377,1,878887116
196,51,5,881250949
244,51,2,880606923
---------------------------------------------------------------------------------
After adding
@relation stackoverflowQuestion

@attribute uid numeric
@attribute itemid numeric
@attribute rating numeric
@attribute timestamp numeric

@data
196,242,3,881250949
186,302,3,891717742
22,377,1,878887116
196,51,5,881250949
244,51,2,880606923
244,59,2,880606923

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

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