2017-08-06 32 views
-1

是否可以修改以下功能的代码以便允许在x中输入多个条目(即,我想一次查询多个点)?修改代码以允许在x中输入多个条目

def banana(x): 
    return exp(((-x[0]**2/200))-0.5*(x[1]+0.05*(x[0]**2) - 100*0.05)**2) 
+0

为什么不直接调用该函数多次:这是在用数字的二维数组(n x 2)numpy的代表? – jonrsharpe

+0

是的。在这种情况下,你最好使用numpy。 –

+0

是的,这是可能的。 – klutt

回答

0

而是改变功能的,可以考虑使用内置map功能的函数应用于集合中的所有元素。在此,banana的实现保持简单。以下是如何使用map函数的示例。

entries = [...] 
mapped_entries = map(banana, entries) 
0

使用numpy。看起来你的功能使用了一对数字(x[0]x[1])。

import numpy as np 

def apple(x): # Note that `x` is an array, not a tuple pair. 
    return np.exp((((-x[0]**2/200)) - 0.5 * (x[1] + 0.05 * x[0]**2 - 100 * 0.05) ** 2)) 

x = np.array([[1, 2, 3], [4, 5, 6]]) 
>>> apple(x) 
array([ 0.23427726, 0.36059494, 0.12857409]) 
相关问题