Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 632
Aizzaac Avatar asked Oct 24 '16 15:10

Aizzaac


1 Answers

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]])
like image 200
AChampion Avatar answered Sep 21 '22 02:09

AChampion