gpt4 book ai didi

java - 使用 StringBuilder() 将数组解析为带标题的 CSV — 标题行问题

转载 作者:行者123 更新时间:2023-12-02 13:29:47 25 4
gpt4 key购买 nike

我有一个标记数据元素的 vector ,如下所示:

[label1: 1.1, label2: 2.43, label3: 0.5]

[label1: 0.1, label2: 2.0, label3: 1.0]

可以有任意数量的元素,其中每个元素本质上对应于一行数据。我正在尝试将其解析为带有列标题的 CSV,如下所示:

label1 label2 label3
1.1 2.43 0.5
0.1 2.0 1.0

我一直在使用 StringBuilder() 构造函数,并且更愿意坚持使用它,但如果有必要,我可以使用其他东西。

除了将标题与数字结果的第一行分开之外,我几乎已经完成了这项工作。

我有一个遍历数组元素(“行”)的外循环和一个遍历每个数组元素(“列”)的每个部分的内循环,在上面的示例中我们有 2 个“行”(元素)和3 个“列”(成员索引)。

我的代码如下所示(下面的 block 创建 CSV 并打印到屏幕):

StringBuilder builder  = new StringBuilder();

// Write predictions to file
for (int i = 0; i < labeled.size(); i++)
{
// Discreet prediction
double predictionIndex =
clf.classifyInstance(newTest.instance(i));

// Get the predicted class label from the predictionIndex.
String predictedClassLabel =
newTest.classAttribute().value((int) predictionIndex);

// Get the prediction probability distribution.
double[] predictionDistribution =
clf.distributionForInstance(newTest.instance(i));

// Print out the true predicted label, and the distribution
System.out.printf("%5d: predicted=%-10s, distribution=",
i, predictedClassLabel);

// Loop over all the prediction labels in the distribution.
for (int predictionDistributionIndex = 0;
predictionDistributionIndex < predictionDistribution.length;
predictionDistributionIndex++)
{
// Get this distribution index's class label.
String predictionDistributionIndexAsClassLabel =
newTest.classAttribute().value(
predictionDistributionIndex);

// Get the probability.
double predictionProbability =
predictionDistribution[predictionDistributionIndex];

System.out.printf("[%10s : %6.3f]",
predictionDistributionIndexAsClassLabel,
predictionProbability );
if(i == 0){
builder.append(predictionDistributionIndexAsClassLabel+",");

if(predictionDistributionIndex == predictionDistribution.length){
builder.append("\n");
}
}
// Add probabilities as rows
builder.append(predictionProbability+",");

}

System.out.printf("\n");
builder.append("\n");

}

目前的结果如下:

setosa,1.0,versicolor,0.0,virginica,0.0,
1.0,0.0,0.0,
1.0,0.0,0.0,
1.0,0.0,0.0,
1.0,0.0,0.0,
1.0,0.0,0.0,
1.0,0.0,0.0,

其中 setosa、versicolor 和 virginica 是标签。正如您所看到的,它从第二行开始工作,但我不知道如何修复第一行。

最佳答案

如果我正确理解你的问题,你将在 for 循环内部同时获取第一行的标签和值,因此在它们出现时进行附加。如果你想将标签分开,你可以对内循环部分进行一些更改,如下所示:

StringBuilder labelRow = new StringBuilder();

// Loop over all the prediction labels in the distribution.
for (int predictionDistributionIndex = 0;
predictionDistributionIndex < predictionDistribution.length;
predictionDistributionIndex++)
{
// Get this distribution index's class label.
String predictionDistributionIndexAsClassLabel =
newTest.classAttribute().value(
predictionDistributionIndex);

// Get the probability.
double predictionProbability =
predictionDistribution[predictionDistributionIndex];

System.out.printf("[%10s : %6.3f]",
predictionDistributionIndexAsClassLabel,
predictionProbability );
if(i == 0){
labelRow.append(predictionDistributionIndexAsClassLabel+",");

if(predictionDistributionIndex == predictionDistribution.length){
builder.append("\n");
}

}

// Add probabilities as rows
builder.append(predictionProbability+",");

}
if(i == 0){
builder.insert(0,labelRow.toString()+"\n");
}

它的作用是将标签收集在单独的StringBuilder中,稍后您可以将其插入到最终builder值的开头。

关于java - 使用 StringBuilder() 将数组解析为带标题的 CSV — 标题行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43239423/

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