所以我有两个变量平均值和服务值,由以下值组成:

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]

现在,我对变量平均值应用了poisson函数:
from scipy.stats import poisson
for m in mean:
    r_v=poisson(m)
    r_v1.append(r_v)

接下来我需要做的是得到所有平均值的pmfs之和(出现次数从0开始),并将它们与limit中的相应值进行比较。我需要得到pmfs之和大于或等于相应极限值的发生次数。例如,平均值=5.76,(pmf(0)+pmf(1)…+pmf(9))*100=93.2,这在9次发生的极限变量中超过90。
我运行了以下代码,看起来运行良好:
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

问题是,当我试图存储最后一个出现在round(sum(l),2)*100>=s处的z时,会出现以下错误:
ERROR:root:Line magic function `%z` not found.

有人能帮我纠正代码中的错误吗?谢谢。

最佳答案

%是调用IPython的交互式shell中的magic line函数的前缀,并且没有zline magic函数,因此会出现此错误。
在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.05227316084275148,
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.01487251059998144,
0.044617539179994441,
0.089235078359988937,
0.13385261753998332,
0.16062314104797995,
0.16062314104798009,
0.13767697804112569,
0.10325773353084421]),
(13,
[0.00012340980467956,
0.0011106882367801166,
0.0049980970655105232,
0.014994291196531574,
0.0337155192196056,
0.06072687934595293,
0.091090319018929264,
0.117161244529091,
0.13175564000952278,
0.13175564000952278,
0.11858007600857066,
0.097020062188830414,
0.07276504641622894]),
(四),
[0.0820849986238988,
0.20521249655974699,
0.2565156269968376,
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.1571729375644187,
0.12344930223720431,
0.084871395288077939,
0.051865852676047694])]

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

10-09 07:53