简体   繁体   English

python3-获取600 x 600 numpy数组中nxn数组的平均值

[英]python3 - get average of n x n array within a 600 x 600 numpy array

I am writing a method which takes 4 arguments: 我正在写一个带有4个参数的方法:

  1. im: a 600 x 600 numpy array filled with values ranging from 0 - 1000 im:600 x 600 numpy数组,其填充范围是0-1000
  2. pos_1: x index in im pos_1:IM中的x索引
  3. pos_2: y index in im pos_2:y中的y索引
  4. grid_size: size of a sub-grid created given user input, used for averaging grid_size:给定用户输入创建的子网格的大小,用于平均

I want to return the average in the nxn grid around the location of [pos_1, pos_2]. 我想在[pos_1,pos_2]位置附近的nxn网格中返回平均值。 Grid_size will be 5, 7 or 9. Grid_size将为5、7或9。

Eg. 例如。 If grid_size was 5, pos_1 was 20 and pos_2 was 150, I would sum the values in a 5 x 5 grid centred around im[20,150] divided by 25 (5 x 5). 如果grid_size为5,则pos_1为20,而pos_2为150,我将求和以im [20,150]除以25(5 x 5)为中心的5 x 5网格中的值之和。 The current implementation is: 当前的实现是:

def calc_density(im, pos_1, pos_2, grid_size):
    grid_sum = 0
    if(pos_1 < 600 - (grid_size - 1)/2):
        if(pos_1 > (grid_size - 1)/2):
            if(pos_2 < 600 - (grid_size - 1)/2):
                if(pos_1 > (grid_size - 1)/2):
                    for i in range(grid_size):
                        for j in range(grid_size):
                            grid_sum = grid_sum + im[(pos_1 - (grid_size - 1)/2) + i, (pos_2 - (grid_size - 1)/2) + j]

This method should work for any case where [pos_1, pos_2] does not fall within (grid_size - 1)/2 from the edge of im. 此方法适用于[pos_1,pos_2]不在im的边缘(grid_size-1)/ 2范围内的情况。 If it does then the averaging nxn grid will fall outside of im. 如果这样做,则平均nxn网格将落在im之外。 If this happens then I am hoping to drop those positions which fall out of im and find the average of those which do fall in the nxn grid. 如果发生这种情况,那么我希望删除那些落在im之外的位置,并找到那些落在nxn网格中的平均值。

I can only imagine doing this with a lot of 'if' cases. 我只能想象在很多“如果”的情况下这样做。 Is there a better way to do this? 有一个更好的方法吗?

Use numpy facilities for a more readable and more efficient way: 使用numpy工具以更易读和更有效的方式:

def density(arr,x0,y0,size):
    xb=x0-size//2
    yb=y0-size//2
    return arr[xb:,yb:][:size,:size].mean()

This correctly manages borders in the sense you want : 这可以按照您想要的方式正确管理边界:

In [46]: density(arr,603,603,9)
Out[46]: 743.0

In [47]: arr
Out[47]: 
array([[ 73, 197, 311, ..., 952, 477, 138],
       [751,  93, 291, ..., 983, 167, 599],
       [ 54, 666, 380, ..., 456, 466, 754],
       ..., 
       [186, 737, 829, ..., 929,  28, 923],
       [136, 408, 193, ..., 844, 649, 927],
       [477, 411, 458, ...,  64, 173, 743]])

暂无
暂无

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

相关问题 可视化 3x3 numpy 数组并将其保存为新的形状数组:400x600 - Visualize 3x3 numpy array and save it as new array of shape: 400x600 如何将大小为 N 的 numpy 列数组乘以 python 中大小为 N 的行数组以获得 NXN 矩阵? - how to multiply a numpy column array of size N to row array of size N in python to get N X N matrix? ValueError:an = 600数组的布尔值索引过多(浮点型) - ValueError: too many boolean indices for a n=600 array (float) nxk阵列的平均值和RMSE - Average and RMSE of n x k array 我有一个 numpy 数组,形状为 480x600,numpy 复数,有一种方法可以将 append 放在一个空数组中,里面有更多这些吗? - I have a numpy array with the shape of 480x600, numpy complex numbers, there is a way to append it in a empty array which has more of these inside? 我在 Pandas 中有一个 30 x 20 矩阵,我想将其转换为 600 x 1 列数组。 如果没有 Numpy,这可能吗? - I have a 30 x 20 matrix in Pandas that I'd like to convert into a 600 x 1 column array. Is that possible to do without Numpy? N x 2 阵列上的 numpy.arange - numpy.arange on an N x 2 array 创建形式为[1,x,x ^ 2,…,x ^ n]的numpy数组 - Create a numpy array of the form [1,x,x^2,…,x^n] NumPy中的len(n)x len(m)数组 - len(n) x len(m) array in NumPy 通过将 function 应用于 nx 1 numpy 数组中的元素对,numpy 中的 nxn 矩阵 - n x n matrix in numpy by applying function to pairs of elements in n x 1 numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM