为了账号安全,请及时绑定邮箱和手机立即绑定

概率分布模型

概率分布模型

30秒到达战场 2021-07-02 14:07:28
我正在尝试运行python在此数据科学站点上用作示例的脚本:https : //www.datascience.com/blog/introduction-to-bayesian-inference-learn-data-science-tutorialsimport numpy as npfrom scipy.misc import factorialimport matplotlib.pyplot as plt%matplotlib inlineplt.rcParams['figure.figsize'] = (16,7)def likelihood(theta, n, x):    """    likelihood function for a binomial distribution    n: [int] the number of experiments    x: [int] the number of successes    theta: [float] the proposed probability of success    """    return (factorial(n) / (factorial(x) * factorial(n - x))) \            * (theta ** x) * ((1 - theta) ** (n - x))# The number of impressions for our facebook-yellow-dress campaignn_impressions = 10# The number of clicks for our facebook-yellow-dress campaignn_clicks = 7# Observed click through ratectr = n_clicks / n_impressions# 0 to 1, all possible click through ratespossible_theta_values = map(lambda x: x/100., range(100))# Evaluate the likelihood function for possible click through rateslikelihoods = map(lambda theta: likelihood(theta, n, x)\                                , possible_theta_values)# Pick the best thetamle = possible_theta_values[np.argmax(likelihoods)]# Plotf, ax = plt.subplots(1)ax.plot(possible_theta_values, likelihoods)ax.axvline(mle, linestyle = "--")ax.set_xlabel("Theta")ax.set_ylabel("Likelihood")ax.grid()ax.set_title("Likelihood of Theta for New Campaign")plt.show()我正在使用 Spyder。我得到的错误包括:    %matplotlib inline    ^SyntaxError: invalid syntax也mle = possible_theta_values[np.argmax(likelihoods)]TypeError: 'map' object is not subscriptable和likelihoods = list(map(lambda theta: likelihood(theta, ctr, x)\NameError: name 'x' is not defined
查看完整描述

1 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

对代码进行少量修改以使其工作。我不得不转换的map可迭代对象名单,让你的代码工作,你提到的原因:TypeError: 'map' object is not subscriptable。我还必须明确定义x数组才能在计算likelihoods.


# 0 to 1, all possible click through rates

possible_theta_values = list(map(lambda x: x/100., range(100)))


n = n_impressions

x = n_clicks


# Evaluate the likelihood function for possible click through rates

likelihoods = list(map(lambda theta: likelihood(theta, n, x)\

                                , possible_theta_values))

//img1.sycdn.imooc.com//60ed48f50001186a10440485.jpg

查看完整回答
反对 回复 2021-07-13
  • 1 回答
  • 0 关注
  • 163 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信