Rolling average over predictions I am using a Deep Learning model I trained and want to improve its accuracy by implementing a rolling average prediction. I have already done it but want to improve it as I am making predictions on every 3 frames.
Input:
Each index represents a class and the respective prediction the model makes.
Order is not alphanumeric as its based on my folder structure during training
1st Index : Class 0
2nd Index : Class 10
3rd Index : class 5
[
  0.9288286566734314,
  0.008770409040153027,
  0.062401000410318375,
]

So the goal is to store the highest probability and its index at every frame. Based on the structure I mentioned above I add the probability to its class. Every time a new probability gets added I increase a counter. Once the counter reaches N, I find the class which has the most predictions, I sum the probabilities and return the average of it and the respective class it belongs to.
N = 5
Prediction {
&quot;0&quot;: [0.9811,0.9924, 0.8763],
&quot;5&quot;: [0.9023],
&quot;10&quot;: [0.9232]
}

Code in React(model is loaded on a mobile phone)
Rolling Prediction does the averaging of the predictions and is passed in the array of predictions mentioned in the input.
There are 2 helper functions to find the sum and to find the max value and max index in an array.
  const [allPredictions, setAllPredictions] = useState({
    &quot;0&quot;: [],
    &quot;5&quot;: [],
    &quot;10&quot;: []
  });
  let queueSize = 0;
  let total = 0;

  const rollingPrediction = arr =&gt; {
    const { max, maxIndex } = indexOfMax(arr);
    const maxFixed = parseFloat(max.toFixed(2));
    if (maxIndex === 0) {
      allPredictions[&quot;0&quot;].push(maxFixed);
      queueSize += 1;
    } else if (maxIndex === 1) {
      allPredictions[&quot;10&quot;].push(maxFixed);
      queueSize += 1;
    } else if (maxIndex === 2) {
      allPredictions[&quot;5&quot;].push(maxFixed);
      queueSize += 1;
    }
    console.log(`Queue : ${queueSize}`);
    if (queueSize &gt; 4) {
      console.log(&quot;Queue Size Max&quot;);
      const arr1 = allPredictions[&quot;0&quot;].length;
      const arr2 = allPredictions[&quot;5&quot;].length;
      const arr3 = allPredictions[&quot;10&quot;].length;

      if (arr1 &gt; arr2 &amp;&amp; arr1 &amp;&amp; arr3) {
        const sum = sumOfArray(allPredictions[&quot;0&quot;]);
        const prob = sum / arr1;
        console.log(`Awareness level 0 | Probability: ${prob}`);
      } else if (arr2 &gt; arr1 arr2 &amp;&amp; arr3) {
        const sum = sumOfArray(allPredictions[&quot;5&quot;]);
        const prob = sum / arr2;
        console.log(`Awareness level 5 | Probability: ${prob}`);
      } else if (arr3 &gt; arr2 arr3 &amp;&amp; arr1) {
        const sum = sumOfArray(allPredictions[&quot;10&quot;]);
        const prob = sum / arr3;
        console.log(`Awareness level 10 | Probability: ${prob}`);
      } else {
        console.log(&quot;No rolling prediction&quot;);
      }
      queueSize = 0;
      allPredictions[&quot;0&quot;] = [];
      allPredictions[&quot;5&quot;] = [];
      allPredictions[&quot;10&quot;] = [];
    }
  };

  const sumOfArray = arr =&gt; {
    for(let i = 0; i &lt; arr.length; i++){
        total += arr[i];
    }
    return total; 
  };

  const indexOfMax = arr =&gt; {
    if (arr.length === 0) {
      return -1;
    }
    let max = arr[0];
    let maxIndex = 0;

    for (let i = 0; i &lt; arr.length; i++) {
      if (arr[i] &gt; max) {
        max = arr[i];
        maxIndex = i;
      }
    }
    return {
      max,
      maxIndex
    };
  };
I think that, in rollingPrediction, you just need the maxIndex, once that you already can access the max in arr with it.
Also, since that you need the maxIndex to find the class, you can just store the classes in an array with every class stored in the correspondent index.

To find which array has the most predictions, you will receive an array and evaluate it to one value. In cases like this, I think that you can use Array.prototype.reduce():

In the callback function, check which array has the bigger length
The initial value will be [null, []],  to check if there's one bigger array and  to have an array to compare in the callback function.


In indexOfMax you can use Math.max with Array.prototype.indexOf() since performance it's not a problem in this case, but you can use reduce too.

Suggestion: In sumOfArray, you receive an array as parameter and want to evaluate to one value. Maybe you can use reduce once again.


// ...
const classesIndexed = ["0", "10", "5"];
// suggestion: once that you know the classes order, this could be:
// const classesIndexed = Object.keys(allPredictions); 

const rollingPrediction = arr =&gt; {
  const maxIndex = indexOfMax(arr);
  if(maxIndex === -1) {
    return;
  }
  const maxFixed = parseFloat(arr[maxIndex].toFixed(2));
  allPredictions[classesIndexed[maxIndex]].push(maxFixed);
  queueSize++;

  if (queueSize &lt;= 4) {
    const [className, bigger] = Object.entries(allPredictions)
      .reduce(
        (acc, act) =&gt; acc[1].length &gt;= act[1].length ? acc : act,
        [null, []]
      );
    if (!className) {
      const sum = sumOfArray(bigger);
      const prob = sum / bigger.length;
      console.log(`Awareness level ${className} | Probability: ${prob}`);
    } else {
      console.log("No rolling prediction");
    }
    // ...
};
// ...
const indexOfMax = (arr = []) =&gt; arr.indexOf(Math.max(arr))