Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the sum of number layer in ndarray

I have a 10*10 array. I need to find the sum of all layer. Following figure will clear my question:

enter image description here

How can i do this easily?

like image 857
Shahriar Avatar asked Dec 24 '14 15:12

Shahriar


People also ask

How to find the sum of an array of numbers?

1 reduce () ¶. You can use the reduce () method to find the sum of an array of numbers. The 0 in is the default value. 2 for loop ¶ 3 forEach loop ¶ 4 The reduce () method ¶. The reduce () method invokes a reducer function provided on each element of the array and results in single output value.

How to sum array elements consisting of not a number (NaNs)?

Note: using numpy.sum on array elements consisting Not a Number (NaNs) elements gives an error, To avoid this we use numpy. nansum () the parameters are similar to the former except the latter doesn’t support where and initial. Returns the cumulative sum of the elements in the given array.

How to sum a NumPy array in Python?

How to sum a numpy array? 1 # arr is a numpy array. 2 # sum of all values arr.sum() 3 # sum of each row (for 2D array) arr.sum(axis=1) 4 # sum of each column (for 2D array) arr.sum(axis=0) 5 # sum along a specific axis, n arr.sum(axis=n) You can also specify the axis to sum the numpy array along with the axis parameter (see the examples ...

How to return the cumulative sum of elements in an array?

Returns the cumulative sum of the elements in the given array. Syntax: numpy. prod (array_name, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)


2 Answers

Just to add another answer... While the general idea of subtracting inner squares from outer ones is probably the best approach, if you wanted performance none of the implementations presented would do too good, since they keep adding the same numbers over and over. To speed this calculation up, you could use what in image processing is called an integral image:

>>> int_arr = np.cumsum(np.cumsum(arr, axis=0), axis=1)
>>> int_arr
array([[  8,   8,  15,  19,  22,  22,  22,  29,  35,  44],
       [ 16,  17,  24,  34,  40,  44,  46,  54,  64,  79],
       [ 24,  30,  46,  62,  76,  80,  82,  91, 104, 119],
       [ 27,  35,  59,  84, 103, 115, 123, 137, 156, 178],
       [ 33,  48,  77, 103, 123, 135, 148, 163, 191, 213],
       [ 41,  65,  98, 129, 158, 177, 190, 205, 233, 259],
       [ 48,  81, 120, 158, 191, 217, 235, 251, 286, 316],
       [ 54,  95, 136, 180, 214, 249, 275, 296, 333, 364],
       [ 63, 105, 147, 194, 235, 276, 310, 331, 376, 414],
       [ 65, 115, 163, 213, 260, 306, 340, 364, 410, 456]])

From this helper array you can compute the area of any rectangular subarray adding two entries and subtracting two others, e.g.:

>>> np.sum(arr[1:-1, 1:-1])
286
>>> int_arr[-2,-2] + int_arr[0, 0] - int_arr[-2, 0] - int_arr[0, -2]
286

With this, you could compute your sums easily as, e.g.:

sums = [int_arr[-1, -1]]
top = 0
bot = len(arr) - 2
while top < bot:
    new_sum = (int_arr[bot, bot] + int_arr[top, top] -
               int_arr[top, bot] - int_arr[bot, top])
    sums[-1] -= new_sum
    sums.append(new_sum)
    top += 1
    bot -= 1

>>> sums
[170, 122, 85, 62, 17]
like image 194
Jaime Avatar answered Oct 08 '22 11:10

Jaime


Here's a solution without repeated summation of the same elements and without an intermediate array (just brute-force indexing fun) which works for square n by n arrays for odd or even n:

import numpy as np

def sum_shells(a):
    n = len(a)
    no2 = n // 2
    shell_sums = []
    for i in range(no2):
        shell_sums.append(np.sum(a[i,i:n-i]) + np.sum(a[n-i-1,i:n-i]) +
                          np.sum(a[i+1:n-i-1,i]) + np.sum(a[i+1:n-i-1,n-i-1]))
    if n % 2:
        shell_sums.append(a[no2, no2])
    return shell_sums

a = np.array([
  [8, 0, 7, 4, 3, 0, 0, 7, 6, 9],
  [8, 1, 0, 6, 3, 4, 2, 1, 4, 6],
  [8, 5, 9, 6, 8, 0, 0, 1, 3, 0],
  [3, 2, 8, 9, 5, 8, 6, 5, 6, 7],
  [6, 7, 5, 1, 1, 0, 5, 1, 9, 0],
  [8, 9, 4, 5, 9, 7, 0, 0, 0, 4],
  [7, 9, 6, 7, 4, 7, 5, 1, 7, 4],
  [6, 8, 2, 6, 1, 9, 8, 5, 2, 1],
  [9, 1, 1, 3, 7, 6, 8, 0, 8, 7],
  [2, 8, 6, 3, 6, 5, 0, 3, 1, 8] ])

b = np.array([[9, 5, 8, 6, 5],
              [1, 1, 0, 5, 1],
              [5, 9, 7, 0, 0],
              [7, 4, 7, 5, 1],
              [9, 5, 0, 2, 3] ])
print(sum_shells(a))
print(sum_shells(b))

Produces:

[170, 122, 85, 62, 17]
[67, 31, 7]
like image 21
xnx Avatar answered Oct 08 '22 12:10

xnx