2017-08-06 33 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]) 
相關問題