How to sum all the arrays inside a list of arrays?

Aizzaac picture Aizzaac · Oct 24, 2016 · Viewed 17.9k times · Source

I am working with the confusion matrix. So for each loop I have an array (confusion matrix). As I am doing 10 loops, I end up with 10 arrays. I want to sum all of them. So I decided that for each loop I am going to store the arrays inside a list --I do not know whether it is better to store them inside an array.

And now I want to add each array which is inside the list.

So If I have:

    5 0 0       1 1 0
    0 5 0       2 4 0
    0 0 5       2 0 5

The sum will be:

    6 1 0
    2 9 0 
    2 0 10

This is a picture of my confusion matrices and my list of arrays: enter image description here

This is my code:

   list_cm.sum(axis=0)

Answer

AChampion picture AChampion · Oct 24, 2016

Just sum the list:

>>> sum([np.array([[5,0,0],[0,5,0],[0,0,5]]), np.array([[1,1,0],[2,4,0],[2,0,5]])])
array([[ 6,  1,  0],
       [ 2,  9,  0],
       [ 2,  0, 10]])