Java 使用StringBuilder()将数组解析为带有标题的CSV--标题行问题
我有一个标记数据元素的向量,如下所示:Java 使用StringBuilder()将数组解析为带有标题的CSV--标题行问题,java,arrays,csv,stringbuilder,Java,Arrays,Csv,Stringbuilder,我有一个标记数据元素的向量,如下所示: 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, [label1:1.1,label2:2.43,label3:0.5] [label1:0.1,label2:2.0,label3:1.0] 可以有任意数量的元素,其中每个元素基本上对应于一行数据。我试图将其解析为带有列
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,
[label1:1.1,label2:2.43,label3:0.5]
[label1:0.1,label2:2.0,label3:1.0]
可以有任意数量的元素,其中每个元素基本上对应于一行数据。我试图将其解析为带有列标题的CSV,如下所示:
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,
我一直在使用StringBuilder()
构造函数,希望继续使用它,但如果必要,我可以使用其他方法
除了从第一行数字结果中分离标题外,我几乎已经实现了这一点
我有一个遍历数组元素(“行”)的外循环和一个遍历每个数组元素(“列”)的每个部分的内循环,在上面的示例中,我们有2个“行”(元素)和3个“列”(成员索引)
我的代码如下所示(下面的块创建CSV并打印到屏幕):
其中setosa、versicolor和VIGINICA是标签。正如您所看到的,它从第二行开始工作,但我不知道如何修复第一行。如果我正确理解您的问题,您将在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 labelRow=new StringBuilder();
//在分布中的所有预测标签上循环。
对于(int-predictionDistributionIndex=0;
predictionDistributionIndex
它所做的是在一个单独的StringBuilder
中收集标签,稍后您可以将其插入最终builder
值的开头
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");
}