不断获得 "only length-1 arrays can be converted to Python scalars"
keep getting "only length-1 arrays can be converted to Python scalars"
我一直收到错误 only length-1 arrays can be converted to Python scalars
。大多数人建议有时 numpy 与其他现有数学函数不兼容。但我将每个数学函数都更改为 np 函数。
错误状态:
Traceback (most recent call last): File "/Users/jimmy/Documents/2.py", line 20, in <module>
eu = mc_simulation(89,102,0.5,0.03,0.3,1000) File "/Users/jimmy/Documents/2.py", line 12, in mc_simulation
ST = s0 * exp((r - 0.5 * sigma ** 2) * T + sigma * a * z) TypeError: only length-1 arrays can be converted to Python scalars
我的代码:
from numpy import *
import numpy as np
from math import exp
def mc_simulation(s0, K, T, r, sigma, no_t):
random.seed(1000)
z = random.standard_normal(no_t)
ST = s0 * exp((r - 0.5 * sigma ** 2) * T + sigma * np.sqrt(T) * z)
payoff = maximum(ST - K, 0)
eu_call = exp(-r * T) * sum(payoff) / no_t
return eu_call
eu = mc_simulation(89,102,0.5,0.03,0.3,1000)
这里不需要math
。使用 numpy.exp
。此外,考虑养成不对导入使用 * 运算符的习惯。
import numpy as np
np.random.seed(1000)
def mc_simulation(s0, K, T, r, sigma, no_t):
z = np.random.standard_normal(no_t)
ST = s0 * np.exp((r - 0.5 * sigma ** 2) * T + sigma * np.sqrt(T) * z)
payoff = np.maximum(ST - K, 0)
eu_call = np.exp(-r * T) * np.sum(payoff) / no_t
return eu_call
print(mc_simulation(89,102,0.5,0.03,0.3,1000))
3.4054951916465099
关于您对 "why shouldn't I use the * operator" 的评论:关于为什么这会造成麻烦,有很多很好的讨论。但这里是官方 documentation 不得不说的:当你使用 from numpy import *
:
This imports all names except those beginning with an underscore (_).
In most cases Python programmers do not use this facility since it
introduces an unknown set of names into the interpreter, possibly
hiding some things you have already defined.
你自己的例子说明了这一点。如果您要使用:
from numpy import *
from math import *
两者都有一个 exp
函数,该函数作为 exp
导入到命名空间中。 Python 可能会不知道您要使用哪个 exp
,正如您在此处看到的,它们是完全不同的。如果您已经自己定义了一个 exp
函数,或者与这两个包中的任何函数共享一个名称的任何其他函数,这同样适用。
一般来说,要警惕您 运行 始终使用 from x import *
的任何教程。
我一直收到错误 only length-1 arrays can be converted to Python scalars
。大多数人建议有时 numpy 与其他现有数学函数不兼容。但我将每个数学函数都更改为 np 函数。
错误状态:
Traceback (most recent call last): File "/Users/jimmy/Documents/2.py", line 20, in <module>
eu = mc_simulation(89,102,0.5,0.03,0.3,1000) File "/Users/jimmy/Documents/2.py", line 12, in mc_simulation
ST = s0 * exp((r - 0.5 * sigma ** 2) * T + sigma * a * z) TypeError: only length-1 arrays can be converted to Python scalars
我的代码:
from numpy import *
import numpy as np
from math import exp
def mc_simulation(s0, K, T, r, sigma, no_t):
random.seed(1000)
z = random.standard_normal(no_t)
ST = s0 * exp((r - 0.5 * sigma ** 2) * T + sigma * np.sqrt(T) * z)
payoff = maximum(ST - K, 0)
eu_call = exp(-r * T) * sum(payoff) / no_t
return eu_call
eu = mc_simulation(89,102,0.5,0.03,0.3,1000)
这里不需要math
。使用 numpy.exp
。此外,考虑养成不对导入使用 * 运算符的习惯。
import numpy as np
np.random.seed(1000)
def mc_simulation(s0, K, T, r, sigma, no_t):
z = np.random.standard_normal(no_t)
ST = s0 * np.exp((r - 0.5 * sigma ** 2) * T + sigma * np.sqrt(T) * z)
payoff = np.maximum(ST - K, 0)
eu_call = np.exp(-r * T) * np.sum(payoff) / no_t
return eu_call
print(mc_simulation(89,102,0.5,0.03,0.3,1000))
3.4054951916465099
关于您对 "why shouldn't I use the * operator" 的评论:关于为什么这会造成麻烦,有很多很好的讨论。但这里是官方 documentation 不得不说的:当你使用 from numpy import *
:
This imports all names except those beginning with an underscore (_). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.
你自己的例子说明了这一点。如果您要使用:
from numpy import *
from math import *
两者都有一个 exp
函数,该函数作为 exp
导入到命名空间中。 Python 可能会不知道您要使用哪个 exp
,正如您在此处看到的,它们是完全不同的。如果您已经自己定义了一个 exp
函数,或者与这两个包中的任何函数共享一个名称的任何其他函数,这同样适用。
一般来说,要警惕您 运行 始终使用 from x import *
的任何教程。