2017-04-05 33 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