我正在嘗試使用Numexpr快速製作Vector Norm函數來與Numpy's進行比較。我嘗試以下方法:Numexpr錯誤:「a = global_dict [name]」
import numexpr as ne
import numpy as np
def L2_Norm(vector_in):
vector1 = ne.evaluate("abs(vector_in)")
vector2 = ne.evaluate("vector1**2")
vector3 = ne.evaluate("sum(vector2)")
vector_out = ne.evaluate("sqrt(vector3)")
return vector_out`
ve = np.arange(10)
L2_Norm(ve)
,我得到這個:
File "C:\Folder1\Folder2\src\test.py", line 11, in L2_Norm
vector3 = ne.evaluate("sum(vector2)")<br>
File "C:\Python27\lib\site-packages\numexpr\necompiler.py", line 701, in evaluate
a = global_dict[name]<br>
KeyError: 'a'
我基本上遵循同一步驟,其User Guide(這似乎是圍繞僅供參考)。我唯一的線索是這樣的:
umexpr's principal routine is this:
evaluate(ex, local_dict=None, global_dict=None, **kwargs)
where ex is a string forming an expression, like "2*a+3*b". The values for a and b will by default be taken from the calling function's frame (through the use of sys._getframe()). Alternatively, they can be specified using the local_dict or global_dict arguments, or passed as keyword arguments
......我真的不明白 - 我認爲作者保持簡單,因爲包簡單。我忽視了什麼?