Lefteris Kyprianou
Lefteris Kyprianou

Reputation: 327

Taking the average from 3d array without zeros

I want to take the X(avg) from the axis= 1 of this matrix of shape(5,10,15) but without taking into account any zero vectors in the average. The average matrix must be of shape (5,15). If i try to implement the np.average(l,axis=1) function i am taking a matrix of shape (5,15) but the zero vectors is still inside.

[[[ 9 19  6  2 13  4 19 11 19 15 14  2 16  4  2]
  [13 11  3 14 12  4 10 11 11  6 10  3 12  1  1]
  [12  9 17  5  9 10 17  9 12  2 16  9 10  2 14]
  [17 17 15 19  7 18 18  5  9 11 14 14  3 10  2]
  [ 9  3 13 19 18 18 17  5 16  5  2 15 19 16 12]
  [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
  [ 9 11 10 12 12 12 10  9 18 16 15  2 11 17 12]
  [ 5 13 18 19 17 13  8  8  1  4  7  7 12 10 14]
  [ 9  3 13 19 18 18 17  5 16  5  2 15 19 16 12]
  [ 9 11 10 12 12 12 10  9 18 16 15  2 11 17 12]]

 [[ 5 13 18 19 17 13  8  8  1  4  7  7 12 10 14]
  [ 5 13 18 19 17 13  8  8  1  4  7  7 12 10 14]
  [18  7 16  9  8 14 19 15 12  3 15 15  8  7  4]
  [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
  [18  7 16  9  8 14 19 15 12  3 15 15  8  7  4]
  [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
  [16  5  3  6  8 16  9  2  7 12 16 14 19  4 17]
  [19  4 10  4 15 12  4  3  2  1 17 16  8  2  6]
  [ 9 11 10 12 12 12 10  9 18 16 15  2 11 17 12]
  [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]]

 [[ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
  [ 9  3 13 19 18 18 17  5 16  5  2 15 19 16 12]
  [ 2 11  6  4 13  6 11  5  3 15  9 13 13 18  9]
  [13  7 15 10  2  5  5  2 10  1 10 14 12  9  6]
  [ 9 19  6  2 13  4 19 11 19 15 14  2 16  4  2]
  [ 4 10 11 19 11  5  3 14 19 11  2  1 15  6 10]
  [11 12 16  9  8 16 19  8 12  1 19 16  1  8 17]
  [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
  [11 12 16  9  8 16 19  8 12  1 19 16  1  8 17]
  [ 9  3 13 19 18 18 17  5 16  5  2 15 19 16 12]]

 [[ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
  [11 12 16  9  8 16 19  8 12  1 19 16  1  8 17]
  [19  2  3  5  2 12  1  8 10  9 12 16 17 16 15]
  [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
  [13 11  3 14 12  4 10 11 11  6 10  3 12  1  1]
  [ 9 19  6  2 13  4 19 11 19 15 14  2 16  4  2]
  [12  9 17  5  9 10 17  9 12  2 16  9 10  2 14]
  [ 5 13 18 19 17 13  8  8  1  4  7  7 12 10 14]
  [18  7 16  9  8 14 19 15 12  3 15 15  8  7  4]
  [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]]

 [[ 9 19  6  2 13  4 19 11 19 15 14  2 16  4  2]
  [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
  [ 9 11 10 12 12 12 10  9 18 16 15  2 11 17 12]
  [19  2  3  5  2 12  1  8 10  9 12 16 17 16 15]
  [10 19  7  9  5  7  5  1 15  8 19  7 15 10 12]
  [12  9 17  5  9 10 17  9 12  2 16  9 10  2 14]
  [ 5 13 18 19 17 13  8  8  1  4  7  7 12 10 14]
  [13 11  3 14 12  4 10 11 11  6 10  3 12  1  1]
  [19  4  9 17 10 13 10 17 12 13  9  8 12 10 15]
  [ 9 11 10 12 12 12 10  9 18 16 15  2 11 17 12]]]

Upvotes: 1

Views: 226

Answers (2)

Jérôme Richard
Jérôme Richard

Reputation: 50488

You can use the weights of np.average to skip the zeros very easily:

np.average(v, axis=1, weights=v>0)

Output result:

array([[10.22222222, 10.77777778, 11.66666667, 13.44444444, 13.11111111,
        12.11111111, 14.        ,  8.        , 13.33333333,  8.88888889,
        10.55555556,  7.66666667, 12.55555556, 10.33333333,  9.        ],
       [12.85714286,  8.57142857, 13.        , 11.14285714, 12.14285714,
        13.42857143, 11.        ,  8.57142857,  7.57142857,  6.14285714,
        13.14285714, 10.85714286, 11.14285714,  8.14285714, 10.14285714],
       [ 8.5       ,  9.625     , 12.        , 11.375     , 11.375     ,
        11.        , 13.75      ,  7.25      , 13.375     ,  6.75      ,
         9.625     , 11.5       , 12.        , 10.625     , 10.625     ],
       [12.42857143, 10.42857143, 11.28571429,  9.        ,  9.85714286,
        10.42857143, 13.28571429, 10.        , 11.        ,  5.71428571,
        13.28571429,  9.71428571, 10.85714286,  6.85714286,  9.57142857],
       [11.66666667, 11.        ,  9.22222222, 10.55555556, 10.22222222,
         9.66666667, 10.        ,  9.22222222, 12.88888889,  9.88888889,
        13.        ,  6.22222222, 12.88888889,  9.66666667, 10.77777778]])

Upvotes: 2

winwin
winwin

Reputation: 1797

Count nonzero values by the axis, then sum by the axis, and then divide the count by the sum. It's the only way, since you want only the nonzeros.

Upvotes: 0

Related Questions