i have vector of labeled data elements, this:
[label1: 1.1, label2: 2.43, label3: 0.5]
[label1: 0.1, label2: 2.0, label3: 1.0]
there can number of elements, each element corresponds row of data. i'm trying parse csv column headers, this:
label1 label2 label3 1.1 2.43 0.5 0.1 2.0 1.0
i've been working stringbuilder()
constructor , prefer stick it, if necessary can use else.
i've got working, except separating headers first row of numeric results.
i have outer loop traverses array elements ("rows") , inner loop traverses each piece of each array element ("columns"), in example above have 2 "rows" (elements) , 3 "columns" (member indexes).
my code looks (the block below both creates csv , prints screen):
stringbuilder builder = new stringbuilder(); // write predictions file (int = 0; < labeled.size(); i++) { // discreet prediction double predictionindex = clf.classifyinstance(newtest.instance(i)); // predicted class label predictionindex. string predictedclasslabel = newtest.classattribute().value((int) predictionindex); // prediction probability distribution. double[] predictiondistribution = clf.distributionforinstance(newtest.instance(i)); // print out true predicted label, , distribution system.out.printf("%5d: predicted=%-10s, distribution=", i, predictedclasslabel); // loop on prediction labels in distribution. (int predictiondistributionindex = 0; predictiondistributionindex < predictiondistribution.length; predictiondistributionindex++) { // distribution index's class label. string predictiondistributionindexasclasslabel = newtest.classattribute().value( predictiondistributionindex); // 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 rows builder.append(predictionprobability+","); } system.out.printf("\n"); builder.append("\n"); }
the results come out this:
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,
where setosa, versicolor, , virginica labels. can see works starting on second row, can't figure out how fix first row.
if understand question correctly, getting labels values first row simultaneously in inside loop , hence appending come. if want separate labels out, changes inner loop part below :
stringbuilder labelrow = new stringbuilder(); // loop on prediction labels in distribution. (int predictiondistributionindex = 0; predictiondistributionindex < predictiondistribution.length; predictiondistributionindex++) { // distribution index's class label. string predictiondistributionindexasclasslabel = newtest.classattribute().value( predictiondistributionindex); // 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 rows builder.append(predictionprobability+","); } if(i == 0){ builder.insert(0,labelrow.tostring()+"\n"); }
what collects labels in separate stringbuilder
, later can insert @ beginning of final builder
value.
Comments
Post a Comment