如何在ndarray中找到数字层的总和

aer*_*ite 7 python numpy multidimensional-array

我有一个10*10阵列.我需要找到所有图层的总和.下图将清楚我的问题:

在此输入图像描述

我怎么能这么容易?

Jai*_*ime 7

只是为了添加另一个答案......虽然从外部方格中减去内部方块的一般想法可能是最好的方法,但如果你想要性能,所提出的实现都不会做得太好,因为它们会一遍又一遍地添加相同的数字.为了加快计算速度,您可以使用图像处理中的内容称为积分图像:

>>> 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]])
Run Code Online (Sandbox Code Playgroud)

从这个辅助数组中,您可以计算任意矩形子阵列的区域,添加两个条目并减去另外两个,例如:

>>> 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
Run Code Online (Sandbox Code Playgroud)

有了这个,您可以轻松计算您的总和,例如:

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]
Run Code Online (Sandbox Code Playgroud)


xnx*_*xnx 2

这是一个解决方案,无需重复求和相同元素,也无需中间数组(只是暴力索引的乐趣),适用于奇数或偶数的平方乘数n组:nn

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))
Run Code Online (Sandbox Code Playgroud)

生产:

[170, 122, 85, 62, 17]
[67, 31, 7]
Run Code Online (Sandbox Code Playgroud)