Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python解释数学函数_Python - Fatal编程技术网

用Python解释数学函数

用Python解释数学函数,python,Python,我有一个问题,我正在研究,我必须产生一个函数,它反映了给定的数学函数: 概率=e^beta/1+e^beta。到目前为止,我生成的代码在输入整数时可以工作,但我需要使用该函数来计算数组的概率 到目前为止,我的代码是: 导入数学 e=数学 def可能性(β): 对于范围内的i(beta): 回报率(e**(β)/(1+e**(β))) beta_候选者=np.随机.均匀(-5,5,50) 可能性候选=可能性(贝塔候选) 每当我运行代码时,都会遇到一个错误:只有整数标量数组才能转换为标量索引 In

我有一个问题,我正在研究,我必须产生一个函数,它反映了给定的数学函数:
概率=e^beta/1+e^beta
。到目前为止,我生成的代码在输入整数时可以工作,但我需要使用该函数来计算数组的概率

到目前为止,我的代码是:

导入数学
e=数学
def可能性(β):
对于范围内的i(beta):
回报率(e**(β)/(1+e**(β)))
beta_候选者=np.随机.均匀(-5,5,50)
可能性候选=可能性(贝塔候选)
每当我运行代码时,都会遇到一个错误:只有整数标量数组才能转换为标量索引

In [3]: import math

In [4]: e = math.e

In [5]: def likelihood(beta):
   ...:     return [e**i/(1+e**i) for i in beta]
   ...:

In [7]: likelihood_candidate = likelihood(beta_candidate)
由于您的beta_候选对象是numpy数组,因此您只需执行矢量化numpy操作:

l = np.exp(beta_candidate)/(1+np.exp(beta_candidate))

您的函数接受一个整数。您正在尝试向它提供一个列表。考虑使用类似列表的理解,函数在第一次迭代后停止,因为函数返回。