title: calculating averages across array indices and collecting results to a new array
body: <p>I'm trying to run the <code>averaged perceptron</code> which takes all the weight vectors created during each iteration and then, for instance, for the first index, it will take the average across all weights associated with index 1 in all of the iterations that preceded the converagance and use this average as the output value for the final weights result, i.e. this becomes the value for index 1 of the final output array.</p>
  
  <p>so if the weights are:</p>
  
  <p>iteration 1: <code>[5,10,20]</code>
  iteration 2: <code>[3,4,6]</code>
  iteration 3: <code>[2,9,12]</code></p>
  
  <p>resulting array: <code>[3.333,7.666,12.666]</code></p>
  
  <p>I tried to make code to perform this operation but what I get is this:</p>
  
  <pre><code>weights: 
  [4.356800000000003, 0.5011, 0.502, 0.2078, 0.8042,
  
  weights: 
  [7.956800000000005, 0.5011, 0.402, 0.1078, 0.8042,             
  
  average: 
  [7.956800000000005, 0.5011, 0.402, 0.1078, 0.8042,
  </code></pre>
  
  <p>It's failing to take the average but merely replicating the last weight. What is the cause of this?! It seems to be that I specified the average calculation correctly but aparently not. Where have I gone awry? </p>
  
  <p>The code looks like this: </p>
  
  <pre><code>  public static void perceptron( Table&lt; int[] , String , Integer &gt; train_freq_count_against_globo_dict,
                                   Table&lt; int[] , String , Integer &gt; test_freq_count_against_globo_dict,
                                   Set&lt;String&gt; GLOBO_DICT )
    {
  
         //store weights to be averaged. 
         Map&lt;Integer,double[]&gt; cached_weights = new HashMap&lt;Integer,double[]&gt;();  
  
        int globo_dict_size = GLOBO_DICT.size();
        int number_of_files__train = train_freq_count_against_globo_dict.size();
  
        double[] weights = new double[ globo_dict_size + 1 ];//one for bias
        for (int i = 0; i &lt; weights.length; i++) 
        {
          weights[i] = randomNumber(0,1);
        }     
  
        double[][] feature_matrix__train = new double[ number_of_files__train ][ globo_dict_size ];
        int[] outputs__train = new int [ number_of_files__train ];
  
        int z = 0;
        for ( Cell&lt; int[] , String , Integer &gt; cell: train_freq_count_against_globo_dict.cellSet() )
        {            
            int[] container_of_feature_vector = cell.getRowKey();
  
            for (int q = 0; q &lt; globo_dict_size; q++) 
            {
                 feature_matrix__train[z][q] = container_of_feature_vector[q];
             }
             outputs__train[z] = String.valueOf( cell.getColumnKey() ).equals(LABEL) ? 1 : 0;
  
             z++;
        }
  
        //LEARNING WEIGHTS
        double localError, globalError;
        int p, iteration, output;
  
        iteration = 0;
        do 
        {
            iteration++;
            globalError = 0;
            //loop through all instances (complete one epoch)
            for (p = 0; p &lt; number_of_files__train; p++) 
            {
                // calculate predicted class
                output = calculateOutput( theta, weights, feature_matrix__train, p, globo_dict_size );
                // difference between predicted and actual class values
                localError = outputs__train[p] - output;
                //update weights and bias
                for (int i = 0; i &lt; globo_dict_size; i++) 
                {
                    weights[i] += ( LEARNING_RATE * localError * feature_matrix__train[p][i] );
                }
                weights[ globo_dict_size ] += ( LEARNING_RATE * localError );
  
                //summation of squared error (error value for all instances)
                globalError += (localError*localError);
            }
  
            System.out.println("weights: ");
            System.out.println(Arrays.toString(weights));
            System.out.println();
  
            //store weights for averaging
            cached_weights.put( iteration , weights );
        } 
        while(globalError != 0 &amp;&amp; iteration&lt;=MAX_ITER);
  
        //compute averages
        double[] sums = new double[ globo_dict_size + 1 ];
        double[] averages = new double[ globo_dict_size + 1 ];
  
        for (Entry&lt;Integer, double[]&gt; entry : cached_weights.entrySet()) 
        {
             double[] value = entry.getValue();
             for(int pos=0; pos &lt; globo_dict_size + 1; pos++)
             {
                 sums[ pos ] +=  value[ pos ]; 
             }
         }
         for(int pos=0; pos &lt; globo_dict_size + 1; pos++)
         {
             averages[ pos ] = sums[ pos ] / cached_weights.size(); 
         }
  }
  </code></pre>