2017-04-05 36 views
0

我有标记的数据元素的向量,如下:解析阵列成CSV使用的StringBuilder()头 - 问题与标题行

[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个“列”(成员索引)。

我的代码如下所示(以下两个块创建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,花斑癣,而维吉尼卡是标签。正如你可以看到它从第二行开始工作,但我无法弄清楚如何修复第一行。

回答

1

如果我正确理解您的问题,您将在内部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