I have written a Java program using the WEKA library that 

1. trains a classification algorithm
2. runs predictions on an unlabeled data set using the trained algorithm
3. writes out the results to a .csv file

The problem is that it currently writes out the discrete classification results (i.e. which category the algorithm guesses that a row falls into). What I want is to write out the probability of a given class (e.g. if I am classifying rows as "spam" or "not spam" then I want the probability of spam to be the result).

My understanding is that to do this I need to use `distributionForInstance` instead of `classifyInstance` in my code. From [WEKA](https://weka.wikispaces.com/Use+WEKA+in+your+Java+code):

> 
> If you're interested in the distribution over all the classes, use the
>  method distributionForInstance(Instance). This method returns a double
>  array with the probability for each class.
> 
> 
> 

**The problem I am running into is that with `classifyInstance` I was dealing with the `double` data type and with `distributionForInstance` I am dealing with the `double[]` data type and apparently not adjusting my code correctly.**

Here is the working code that writes out the discreet predictions:

```
public class runPredictions {
public static void runPredictions(ArrayList al2) throws IOException, Exception{
    // Retrieve objects
    Instances newTest = (Instances) al2.get(0);
    Classifier clf = (Classifier) al2.get(1);

    // Print status
    System.out.println("Generating predictions...");

    // create copy
    Instances labeled = new Instances(newTest);

    // label instances
    for (int i = 0; i < newTest.numInstances(); i++) {
      double clsLabel = clf.classifyInstance(newTest.instance(i));
      labeled.instance(i).setClassValue(clsLabel);

    }
    System.out.println("Predictions complete! Writing output file to csv...");
    BufferedWriter outFile = new BufferedWriter(new FileWriter("C:/Users/hackr/Desktop/silverbullet_output.csv"));

    for (int i = 0; i < labeled.size(); i++)
    {
        outFile.write(labeled.get(i).toString());
        outFile.write("\n");
    }
    System.out.println("Output file written.");
    System.out.println("Completed successfully!");
    outFile.close();    
}    
}

```

Right now the code I'm working on has the following:

```
   for (int i = 0; i < labeled.size(); i++)

{
    double[] clsLabel = clf.distributionForInstance(newTest.instance(i));
    //outFile.write(labeled.get(i).toString());
    outFile.write(Double.toString(clsLabel[i]));
    outFile.write("\n");
}

```

and throws an

> 
> Index out of bounds
> 
> 
> 

error.

I also moved the creation of `clsLabel` because apparently it couldn't find the symbol anymore when the data type changed, unless I moved it to within the `for` loop.