-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)
是否可以修改以下功能的代碼以便允許在x
中輸入多個條目(即,我想一次查詢多個點)?修改代碼以允許在x中輸入多個條目
def banana(x):
return exp(((-x[0]**2/200))-0.5*(x[1]+0.05*(x[0]**2) - 100*0.05)**2)
而是改變功能的,可以考慮使用內置map
功能的函數應用於集合中的所有元素。在此,banana
的實現保持簡單。以下是如何使用map
函數的示例。
entries = [...]
mapped_entries = map(banana, entries)
使用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])
爲什麼不直接調用該函數多次:這是在用數字的二維數組(
n x 2
)numpy的代表? – jonrsharpe是的。在這種情況下,你最好使用numpy。 –
是的,這是可能的。 – klutt