简体   繁体   English

如何使用numpy查找数组中某个部分的平均值

[英]how to find average of a section in an array using numpy

import numpy as np

temp_array = np.array(
  [[33.5, 35.3, 33.6, 33.6, 33.5, 33.9, 32.3, 33.2, 53.8, 54.6, 53.4, 54.2],
   [33.1, 34.2, 34.1, 34.3, 34.7, 31.3, 32.3, 33.4, 57.5, 55. , 53.5, 56.1],
   [35.3, 35.4, 35.6, 32.6, 33.2, 34.3, 32.8, 33.1, 54.7, 55.4, 54.6, 55.1],
   [34.2, 36.1, 33.5, 32.4, 32.1, 33.5, 34.5, 35. , 53.8, 56.9, 54.5, 54.7],
   [33.4, 33.8, 36.2, 33. , 35. , 34.2, 33.8, 33.8, 55.7, 55.2, 56. , 54.5],
   [34.3, 35.9, 34.4, 34.2, 53.5, 54.2, 55.7, 54. , 56.3, 54.4, 55.5, 53.8],
   [34.7, 35.4, 34.7, 33.1, 53.6, 54.5, 54.4, 55.5, 54.7, 55.4, 55.1, 55.6],
   [33.3, 34.3, 33.6, 33.1, 55.4, 55.7, 55.4, 55.4, 55.8, 55. , 55.3, 54.1],
   [33.7, 33.5, 37. , 34.9, 57.6, 54.2, 54.9, 54.6, 56. , 55.7, 55.1, 55.9],
   [34. , 35.1, 33.6, 34.5, 56.2, 55.3, 55.2, 54. , 54.1, 54.5, 54.4, 56. ]])


    
cell_shape = (5,4)

I want to find the average for the average the section for example the first section:我想找到该部分的平均值,例如第一部分:

[33.5, 35.3, 33.6, 33.6]
[33.1, 34.2, 34.1, 34.3]
[35.3, 35.4, 35.6, 32.6]
[34.2, 36.1, 33.5, 32.4]
[33.4, 33.8, 36.2, 33. ]

Second section:第二节:

[33.5, 33.9, 32.3, 33.2]
[34.7, 31.3, 32.3, 33.4]
[33.2, 34.3, 32.8, 33.1]
[32.1, 33.5, 34.5, 35. ]
[35. , 34.2, 33.8, 33.8] 

and etc.等等。

The current answers works fine, but they may be slow if you want to do this often on large arrays.当前的答案工作正常,但如果您想经常在大型阵列上执行此操作,它们可能会很慢。 The operation you want to do is called "strided convolutions using a mean/uniform kernel".您想要执行的操作称为“使用均值/均匀内核的跨步卷积”。 There exists many libraries to do said operation (eg PyTorch, scikit-image, or a more advanced numpy way using stride tricks ) a lot faster than using for-loops in Python.有许多库可以执行上述操作(例如 PyTorch、scikit-image或使用 stride 技巧的更高级的 numpy 方法),比在 Python 中使用 for 循环快得多。

Here is an example using skimage which avoids for-loops in Python:这是一个使用 skimage 的示例,它避免了 Python 中的 for 循环:

import numpy as np
from skimage.util.shape import view_as_windows

temp_array = np.array(
  [[33.5, 35.3, 33.6, 33.6, 33.5, 33.9, 32.3, 33.2, 53.8, 54.6, 53.4, 54.2],
   [33.1, 34.2, 34.1, 34.3, 34.7, 31.3, 32.3, 33.4, 57.5, 55. , 53.5, 56.1],
   [35.3, 35.4, 35.6, 32.6, 33.2, 34.3, 32.8, 33.1, 54.7, 55.4, 54.6, 55.1],
   [34.2, 36.1, 33.5, 32.4, 32.1, 33.5, 34.5, 35. , 53.8, 56.9, 54.5, 54.7],
   [33.4, 33.8, 36.2, 33. , 35. , 34.2, 33.8, 33.8, 55.7, 55.2, 56. , 54.5],
   [34.3, 35.9, 34.4, 34.2, 53.5, 54.2, 55.7, 54. , 56.3, 54.4, 55.5, 53.8],
   [34.7, 35.4, 34.7, 33.1, 53.6, 54.5, 54.4, 55.5, 54.7, 55.4, 55.1, 55.6],
   [33.3, 34.3, 33.6, 33.1, 55.4, 55.7, 55.4, 55.4, 55.8, 55. , 55.3, 54.1],
   [33.7, 33.5, 37. , 34.9, 57.6, 54.2, 54.9, 54.6, 56. , 55.7, 55.1, 55.9],
   [34. , 35.1, 33.6, 34.5, 56.2, 55.3, 55.2, 54. , 54.1, 54.5, 54.4, 56. ]])

cell_shape = (5,4)
sections: np.ndarray = view_as_windows(temp_array, cell_shape, cell_shape)
print(sections.mean((-2,-1)))

result:结果:

[[34.16  33.495 54.96 ]
 [34.365 54.965 55.135]]
sh = temp_array.shape
for y in range(0, sh[0], cell_shape[0]):
    for x in range(0, sh[1], cell_shape[1]):
        print(np.average(temp_array[y:y + cell_shape[0], x:x + cell_shape[1]]))

Prints:印刷:

34.16
33.49499999999999
54.96
34.365
54.964999999999996
55.135000000000005
for startx, endx in [ ( n * cell_shape[0], (n+1) * cell_shape[0] ) for n in range( temp_array.shape[0] // cell_shape[0] ) ]:
        for starty, endy in [ ( n * cell_shape[1], (n+1) * cell_shape[1] ) for n in range( temp_array.shape[1] // cell_shape[1] ) ]:
                np.average(temp_array[startx:endx, starty:endy])

Uses np slicing which is in the format, [startx:endx, starty:endy]使用格式为 [startx:endx, starty:endy] 的 np 切片

This only prints the arrays but making it an average is just a matter of changing it to np.mean这仅打印数组,但使其成为平均值只是将其更改为 np.mean 的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM