gpt4 book ai didi

python - 将最后一次迭代存储在循环中(python)

转载 作者:行者123 更新时间:2023-11-28 18:15:48 24 4
gpt4 key购买 nike

所以我有 2 个变量 mean 和 service 由以下值组成:

mean=[5.76,6.5,7,5,4.5,6,9,2.5,3,5.5]
limit=[90,80,95,96,98,85,82,75,90,91]

现在,我对变量 mean 应用了 poisson 函数:

from scipy.stats import poisson
for m in mean:
r_v=poisson(m)
r_v1.append(r_v)

接下来我需要做的是获取所有均值(出现次数从0开始)的pmfs之和,并将它们与limit中的相应值进行比较。我需要获取pmfs之和大于或等于limit中对应值的出现次数。例如,对于均值 =5.76,(pmf(0)+pmf(1)...+pmf(9))*100=93.2,在极限变量中超过 90 的次数有 9 次。我运行了以下似乎工作正常的代码:

for r,s in zip(r_v1, limit):
l=[]
z=0
while True:
y=r.pmf(z)
l.append(y)
z+=1
if round(sum(l),2)*100>=s:
break
Store=%z

问题是,当我尝试存储最后一次出现的 z 时 round(sum(l),2)*100>=s,我收到以下错误:

ERROR:root:Line magic function `%z` not found.

有人可以帮我纠正代码中的错误吗?非常感谢。

最佳答案

% 是在 IPython 的交互式 shell 中调用魔法线函数的前缀,并且没有 z 线魔法函数,因此你会得到这个错误。

在 IPython 中键入 %quickref,您将获得可用魔术函数或 readthedocs 的概览。 .


你的问题是你在一个步骤中尝试了太多。尝试获得一次输入的正确结果,然后在另一步中累积:

def calc(mean_, limit_):

def inner(m, s):
r = poisson(m)
l = []
z = 0
while True:
y = r.pmf(z)
l.append(y)
z += 1
if round(sum(l), 2) * 100 >= s:
return z, l

return [inner(*args) for args in zip(mean_, limit_)]

调用 calc(mean, limit) 给出:

[(10,  [0.0031511115984444414,   0.018150402807039979,   0.052273160084275148,   0.10036446736180832,   0.14452483300100394,   0.16649260761715656,   0.15983290331247035,   0.13151964615426115,   0.094694145231068075,   0.060604252947883679]), (10,  [0.0015034391929775724,   0.009772354754354215,   0.031760152951651209,   0.068813664728577653,   0.11182220518393866,   0.14536886673912017,   0.1574829389673803,   0.14623415761256733,   0.11881525306021086,   0.08581101609904139]), (12,  [0.00091188196555451624,   0.0063831737588816145,   0.022341108156085643,   0.052129252364199796,   0.091226191637349643,   0.1277166682922895,   0.14900277967433773,   0.14900277967433773,   0.1303774322150455,   0.10140466950059107,   0.070983268650413558,   0.045171170959354162]), (10,  [0.006737946999085467,   0.033689734995427337,   0.084224337488568321,   0.1403738958142805,   0.17546736976785063,   0.17546736976785068,   0.1462228081398754,   0.10444486295705395,   0.065278039348158651,   0.036265577415643714]), (10,  [0.011108996538242306,   0.049990484422090385,   0.11247858994970336,   0.168717884924555,   0.18980762054012446,   0.17082685848611215,   0.1281201438645839,   0.082362949627232548,   0.046329159165318316,   0.0231645795826592]), (9,  [0.0024787521766663585,   0.014872513059998144,   0.044617539179994441,   0.089235078359988937,   0.13385261753998332,   0.16062314104797995,   0.16062314104798009,   0.13767697804112569,   0.10325773353084421]), (13,  [0.00012340980408667956,   0.0011106882367801166,   0.0049980970655105232,   0.014994291196531574,   0.033737155192196056,   0.06072687934595293,   0.091090319018929264,   0.1171161244529091,   0.13175564000952278,   0.13175564000952278,   0.11858007600857066,   0.097020062188830414,   0.072765046641622894]), (4,  [0.0820849986238988,   0.20521249655974699,   0.25651562069968376,   0.21376301724973648]), (6,  [0.049787068367863944,   0.14936120510359185,   0.22404180765538775,   0.22404180765538775,   0.16803135574154085,   0.10081881344492458]), (10,  [0.0040867714384640666,   0.02247724291155237,   0.06181241800676901,   0.1133227663457432,   0.15581880372539689,   0.17140068409793663,   0.15711729375644187,   0.12344930223720431,   0.084871395288077939,   0.051865852676047694])]

关于python - 将最后一次迭代存储在循环中(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48483032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com