编码概率加权函数错误
error in coding probability weighting function
我有一个关于我的代码的问题(抱歉,如果它真的很简单或愚蠢,我以前从未编码过并尝试过这个项目)。我尝试 运行 为某个概率向量设置一个概率加权函数。但是我收到一个错误,我不知道如何解决。当我 运行 函数的参数先分开,然后在函数中调用它时,它起作用了,但是为什么它在函数本身中不起作用?
下面的代码
希望你能帮助我。
非常感谢!
import numpy as np
p = np.arange(0.01, 1, 0.01) # probalities equaly spread between 0 & 1 in steps of 0.01
alpha_1 = 0.5
alpha = np.zeros((1, 99)) + alpha_1 # vector of same length as p with all entries being 0.5
term_in_exopnential_of_weighting_function = - (- np.log(p))**alpha
w = np.exp(term_in_exopnential_of_weighting_function) # weighted probability function
# probability weighting function
#w(p)=np.exp(-(- np.log(p))**alpha)
# --> error, but why?`
看起来您想要做的是创建一个名为 w
的函数。在 Python 中,函数定义的语法是
def f(a, b, c):
# do something here
result = <something involving a, b, and c>
return result
然后您可以将函数调用为 f(1, 2, 3)
或 a
、b
和 c
.
的任何名称
在你举的例子中,我想也许你需要的是
>>> def w(alpha, p):
... return np.exp(-(- np.log(p))**alpha)
...
其中 >>>
是 Python 解释器输入提示。请注意,函数体必须缩进,这是解释器强制执行的。 ...
后面没有任何内容意味着我只是按下了 Enter
键而没有输入任何内容。根据 w
的定义,我得到以下结果,如您指定的 alpha
和 p
:
>>> w(alpha, p)
array([[0.116955 , 0.13836178, 0.15372645, 0.16627328, 0.17713938,
0.18687366, 0.19578782, 0.20407777, 0.21187567, 0.21927533,
0.2263461 , 0.23314086, 0.23970099, 0.24605961, 0.25224365,
0.25827542, 0.26417363, 0.26995416, 0.27563063, 0.28121487,
0.28671721, 0.29214677, 0.29751164, 0.30281908, 0.30807562,
0.31328717, 0.31845916, 0.32359653, 0.32870387, 0.33378542,
0.33884514, 0.34388676, 0.34891376, 0.35392947, 0.35893704,
0.36393949, 0.36893972, 0.37394054, 0.37894464, 0.38395468,
0.38897324, 0.39400286, 0.39904606, 0.40410531, 0.40918309,
0.41428186, 0.41940411, 0.42455233, 0.42972903, 0.43493677,
0.44017815, 0.44545582, 0.45077251, 0.45613099, 0.46153416,
0.466985 , 0.4724866 , 0.47804216, 0.48365505, 0.48932878,
0.49506703, 0.50087369, 0.50675283, 0.5127088 , 0.51874619,
0.52486989, 0.53108512, 0.53739747, 0.54381293, 0.55033797,
0.55697957, 0.56374529, 0.57064336, 0.57768275, 0.58487331,
0.59222586, 0.59975235, 0.60746605, 0.61538177, 0.62351608,
0.63188769, 0.64051783, 0.64943073, 0.65865431, 0.66822097,
0.67816868, 0.68854243, 0.69939617, 0.71079551, 0.72282159,
0.73557672, 0.74919294, 0.76384569, 0.77977671, 0.79733511,
0.81705854, 0.8398553 , 0.86750307, 0.90461 ]])
我有一个关于我的代码的问题(抱歉,如果它真的很简单或愚蠢,我以前从未编码过并尝试过这个项目)。我尝试 运行 为某个概率向量设置一个概率加权函数。但是我收到一个错误,我不知道如何解决。当我 运行 函数的参数先分开,然后在函数中调用它时,它起作用了,但是为什么它在函数本身中不起作用? 下面的代码
希望你能帮助我。 非常感谢!
import numpy as np
p = np.arange(0.01, 1, 0.01) # probalities equaly spread between 0 & 1 in steps of 0.01
alpha_1 = 0.5
alpha = np.zeros((1, 99)) + alpha_1 # vector of same length as p with all entries being 0.5
term_in_exopnential_of_weighting_function = - (- np.log(p))**alpha
w = np.exp(term_in_exopnential_of_weighting_function) # weighted probability function
# probability weighting function
#w(p)=np.exp(-(- np.log(p))**alpha)
# --> error, but why?`
看起来您想要做的是创建一个名为 w
的函数。在 Python 中,函数定义的语法是
def f(a, b, c):
# do something here
result = <something involving a, b, and c>
return result
然后您可以将函数调用为 f(1, 2, 3)
或 a
、b
和 c
.
在你举的例子中,我想也许你需要的是
>>> def w(alpha, p):
... return np.exp(-(- np.log(p))**alpha)
...
其中 >>>
是 Python 解释器输入提示。请注意,函数体必须缩进,这是解释器强制执行的。 ...
后面没有任何内容意味着我只是按下了 Enter
键而没有输入任何内容。根据 w
的定义,我得到以下结果,如您指定的 alpha
和 p
:
>>> w(alpha, p)
array([[0.116955 , 0.13836178, 0.15372645, 0.16627328, 0.17713938,
0.18687366, 0.19578782, 0.20407777, 0.21187567, 0.21927533,
0.2263461 , 0.23314086, 0.23970099, 0.24605961, 0.25224365,
0.25827542, 0.26417363, 0.26995416, 0.27563063, 0.28121487,
0.28671721, 0.29214677, 0.29751164, 0.30281908, 0.30807562,
0.31328717, 0.31845916, 0.32359653, 0.32870387, 0.33378542,
0.33884514, 0.34388676, 0.34891376, 0.35392947, 0.35893704,
0.36393949, 0.36893972, 0.37394054, 0.37894464, 0.38395468,
0.38897324, 0.39400286, 0.39904606, 0.40410531, 0.40918309,
0.41428186, 0.41940411, 0.42455233, 0.42972903, 0.43493677,
0.44017815, 0.44545582, 0.45077251, 0.45613099, 0.46153416,
0.466985 , 0.4724866 , 0.47804216, 0.48365505, 0.48932878,
0.49506703, 0.50087369, 0.50675283, 0.5127088 , 0.51874619,
0.52486989, 0.53108512, 0.53739747, 0.54381293, 0.55033797,
0.55697957, 0.56374529, 0.57064336, 0.57768275, 0.58487331,
0.59222586, 0.59975235, 0.60746605, 0.61538177, 0.62351608,
0.63188769, 0.64051783, 0.64943073, 0.65865431, 0.66822097,
0.67816868, 0.68854243, 0.69939617, 0.71079551, 0.72282159,
0.73557672, 0.74919294, 0.76384569, 0.77977671, 0.79733511,
0.81705854, 0.8398553 , 0.86750307, 0.90461 ]])