对于所有点,如何找到2D numpy数组中的点大于参考数组中对应点的百分比?

时间:2022-10-17 21:30:57

Python newbie here. Given a list (mylist) of 2D numpy arrays, and a reference 2D numpy array (refarr), I'm looking for a way to create an array (percentage_array) in which each point (i,j) is the percentage of corresponding (i,j) points in the mylist arrays that are greater than [i,j] in refarr. I could do this by looping through all the points in the array and though the list, e.g.:

Python新手在这里。给定2D numpy数组的列表(mylist)和参考2D numpy数组(refarr),我正在寻找一种创建数组(percentage_array)的方法,其中每个点(i,j)是对应的百分比( i,j)在refarr中大于[i,j]的mylist数组中的点。我可以通过遍历数组中的所有点并通过列表来完成此操作,例如:

percentage_array = numpy.empty(arr.shape)
for i in range(arr.shape[0]):
    for j in range(arr.shape[1]):
        t = 0 
        f = 0
        for arr in mylist:
            if arr[i,j] > refarr[i,j]:
                t += 1 # counting the number of arrays for which [i,j] is true
            elif arr[i,j] <= refarr[i,j]:
                f += 1 # counting the number of arrays for which [i,j] is false
        percentage_array[i,j] = t/(t+f) # fraction of arrays which have 
                                        # arr[i,j] > refarr[i,j]

...but this is neither quick nor elegant (I'm dealing with large amounts of data). Are there better ways to do this?

......但这既不快也不优雅(我正在处理大量数据)。有没有更好的方法来做到这一点?

1 个解决方案

#1


1  

You can create a 3d array with

您可以使用创建3d数组

a = np.array(myList)

Then you can compare this array to your original one using broadcasting:

然后你可以使用广播将这个数组与你原来的数组进行比较:

a < refarr # gives a 3D array of booleans because refarr is broadcasted to a 3D array

and to count the percentage of values where the condition is met, you average on the first axis:

要计算满足条件的值的百分比,您可以在第一个轴上进行平均:

(a < refarr[None, :, :]).mean(axis = 0)

The main drawback of this approach is that you have to create an array a which may be big. Otherwise I'd consider treating arrays one by one.

这种方法的主要缺点是你必须创建一个可能很大的数组。否则我会考虑逐个处理数组。

#1


1  

You can create a 3d array with

您可以使用创建3d数组

a = np.array(myList)

Then you can compare this array to your original one using broadcasting:

然后你可以使用广播将这个数组与你原来的数组进行比较:

a < refarr # gives a 3D array of booleans because refarr is broadcasted to a 3D array

and to count the percentage of values where the condition is met, you average on the first axis:

要计算满足条件的值的百分比,您可以在第一个轴上进行平均:

(a < refarr[None, :, :]).mean(axis = 0)

The main drawback of this approach is that you have to create an array a which may be big. Otherwise I'd consider treating arrays one by one.

这种方法的主要缺点是你必须创建一个可能很大的数组。否则我会考虑逐个处理数组。