简体   繁体   English

如何使用 numpy ZA3CBC3F9D0CE2F2C1554E1BD671 评估 Python 中的 function

[英]How to evaluate a function in Python using numpy arrays?

I have two arrays of randomly generated x's and y's and I want to evaluate each pair at some function.我有两个随机生成的 x 和 y 的 arrays,我想在某个 function 评估每一对。 How do I do that using numpy?如何使用 numpy 做到这一点? I tried the code below, but it does not work.我尝试了下面的代码,但它不起作用。

What I want is an array of func(x_i,y_i).我想要的是一个 func(x_i,y_i) 数组。

x = np.random.uniform(0, 1, 100)
y = np.random.uniform(0, 1, 100)

func = np.array((math.exp(-2 * x) * math.cos(2 * y)))

Any ideas?有任何想法吗?

Don't use math , use numpy .不要使用math ,使用numpy

import numpy as np

x = np.random.uniform(0, 1, 100)
y = np.random.uniform(0, 1, 100)

func = np.exp(-2 * x) * np.cos(2 * y)

>>> func.shape
    (100,)

That is, func[i] contains the result of your function using x[i] and y[i] as input.也就是说, func[i]包含使用x[i]y[i]作为输入的 function 的结果。

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

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