简体   繁体   English

在整个参数空间/二维数组 Numpy 上应用 function

[英]Applying function over entire parameter space/2d array, Numpy

I'm in one of those weird places, where I know exactly what I want to do.我在那些奇怪的地方之一,在那里我确切地知道我想做什么。 I could easily code it up using for loops.我可以使用 for 循环轻松地对其进行编码。 but I'm trying to learn Numpy and I can't formulate how to solve this in Numpy.但我正在尝试学习 Numpy,但我无法在 Numpy 中制定如何解决这个问题。

I want to have a 2d array or parameter space.我想要一个二维数组或参数空间。 All values between 1200 and 1800, and all combinations therein. 1200 到 1800 之间的所有值,以及其中的所有组合。 So [1200, 1200], [1200, 1201], [1200, 1202].... [1201, 1200], [1201, 1201] etc.所以 [1200, 1200], [1200, 1201], [1200, 1202].... [1201, 1200], [1201, 1201] 等等。

I want to apply a function across this entire parameter space.我想在整个参数空间中应用 function。 The function uses a further 2 arrays, which are also values in 1200-1800 range. function 使用另外 2 个 arrays,它们也是 1200-1800 范围内的值。 But they are random values, so these 2 extra arrays are random values in the 1200-1800 range, so [1356, 1689, 1436, ...] and [1768, 1495, 1358, ...] etc. check_array1 and check_array2.但是它们是随机值,所以这两个额外的 arrays 是 1200-1800 范围内的随机值,所以 [1356, 1689, 1436, ...] 和 [1768, 1495, 1358, ...] 等 check_array1 和 check_array2 .

The function needs to move through the parameter space checking a condition, which is basically if x < check_array1 and y < check_array2 then 1 else 0 . function 需要通过参数空间检查一个条件,这基本上是if x < check_array1 and y < check_array2 then 1 else 0 Where x and y are the each specific point in the 2d parameter space.其中 x 和 y 是 2d 参数空间中的每个特定点。 It needs to check against every value combination in the check arrays.它需要检查检查 arrays 中的每个值组合。 Sum the total, do a comparison to another static value, and return the difference.将总数相加,与另一个 static 值进行比较,然后返回差值。

Each unique combination in the parameter space grid will then have a unique value associated with it based on how those specific x and y values from the parameter space compare to the 2 check arrays.然后,参数空间网格中的每个唯一组合将具有与其关联的唯一值,具体取决于参数空间中的那些特定 x 和 y 值与 2 检查 arrays 的比较。

Hopefully the above makes, I just can't figure out how to work this into a Numpy friendly problem.希望以上内容,我只是无法弄清楚如何将其转化为 Numpy 友好问题。 Sorry for the wall of text.对不起,文字墙。

Edit: I've written it in more basic Python to better illustrate what I'm trying to do.编辑:我用更基本的 Python 编写它,以更好地说明我正在尝试做的事情。

check1 = np.random.randint(1200, 1801, 300)
check2 = np.random.randint(1200, 1801, 300)

def check_this_double(i, j, check1, check2):
   total = 0
   for num in range(0, len(check1)):
       if ((i < check1[num]) or (j < check2[num])):
           total += 1           
   return total

outputs = {}
for i in range(1200, 1801):
   for j in range(1200, 1801):
    outputs[i,j] = check_this_double(i, j, check1, check2)

Edit 2: I believe I have it.编辑2:我相信我有它。 Following from Mountains code creating the p_space and then using np.vectorize on a normal Python fuction.从 Mountains 代码创建 p_space 然后在正常的 Python 函数上使用 np.vectorize 。

check1 = np.random.randint(1200, 1801, 300)
check2 = np.random.randint(1200, 1801, 300)

def calc(i, j):    
   total = np.where(np.logical_or(check1 < i, checks2 < j), 1, 0)
   return total.sum()

rate_calv_v = np.vectorize(rate_calc)

final = rate_calv_v(p_space[:, 0], p_space[:, 1])

Feels kind of like cheating:), there must be way to do it without np.vectorize.感觉有点像作弊:),必须有办法在没有 np.vectorize 的情况下做到这一点。 But this works for me I believe.但我相信这对我有用。

I don't fully understand the problem you are trying to solve.我不完全理解您要解决的问题。 I hope the following will give you a starting point on how numpy can be used.我希望以下内容可以让您了解如何使用 numpy。 I recommend going through a numpy introductory tutorial.我建议阅读 numpy 介绍性教程。

numpy boolean indexing and vector math can improve speed and reduce the need for loops. numpy boolean 索引和矢量数学可以提高速度并减少对循环的需求。 Here is my understanding of the first part of your questions.这是我对您问题第一部分的理解。

import numpy as np

xv, yv = np.meshgrid(np.arange(1200, 1801), np.arange(1200, 1801))
p_space = np.stack((xv, yv), axis=-1)  # the 2d array described

# print original values
print(p_space[0,:10,0])
print(p_space[0,-10:,0])

old_shape = p_space.shape
p_space = p_space.reshape(-1, 2)  # flatten the array for the compare

check1 = np.random.randint(1200, 1801, len(p_space))
check2 = np.random.randint(1200, 1801, len(p_space))

# you can used this to access and modify values that meet the condition
index_array = np.logical_and(p_space[:, 0] < check1, p_space[:, 1] < check2)

# do some sort of complex math
p_space[index_array] = p_space[index_array] / 2 + 10

# get the sum across the two columns
print(np.sum(p_space, axis=0))

p_space = p_space.reshape(old_shape)  # return to the grid shape

# print modified values
print(p_space[0,:10,0])  # likely to be changed based on checks
print(p_space[0,-10:,0])  # unlikely to be changed

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

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