繁体   English   中英

需要帮助来定义一个函数,然后将其添加到Python中的矩阵中

[英]Need help defining a function and then adding it to a matrix in Python

import pandas as pd
import numpy as np
import math

matrix2 = pd.DataFrame({"C": [1,2,3,4,5,6,7,8,9,10],"Wq": 
[0,0,0,0,0,0,0,0,0,0]})

x = 20
y = 4
Rho = x/y

def f(c):
    Pinv = Rho**c / (math.factorial(c)*(1-(Rho/c)))
    for i in range(1,(c-1)):
        Pinv = Pinv + (Rho**i)/math.factorial(i)
        P = 1/Pinv
        Lq = (Rho**(c+1))*P/(math.factorial(c-1)*(c-Rho)**2)
        Wq = 60*Lq/x
        Ls = Lq + Rho
        Ws = 60*Ls/x
        print (Lq,"is queue length and",Wq,"is the waiting time")

当我仅通过运行f(c)行来运行此函数时,将得到以下输出:

(0, 'is queue length and', 0, 'is the waiting time')
(0, 'is queue length and', 0, 'is the waiting time')
(0, 'is queue length and', 0, 'is the waiting time')
(0, 'is queue length and', 0, 'is the waiting time')
(0, 'is queue length and', 0, 'is the waiting time')

我不确定是什么导致了此问题,并且我希望得到10条回复。 是什么导致此函数产生此输出? 然后,我还想取Wq's并将其放在上面定义的矩阵中,以代替零。 最好的方法是什么?

matrix3 = matrix2.groupby("C").agg({"Wq":f})
print matrix3

      Wq
C       
1   None
2   None
3   None
4   None
5   None
6   None
7   None
8   None
9   None
10  None

当我尝试将公式结果放入矩阵中时,没有得到有用的结果。 我怎样才能解决这个问题?

我正在python 2中运行所有这些。

好吧,您获得至少10行响应的唯一方法是,由于该行,您的c值为12

for i in range(1,(c-1))

此外,我没有得到您显示的0

这是结果的转储(在简单地复制粘贴代码之后),并为cmd行中的c提供从1到12的不同值。 注意,当c为5时,您的公式有问题。

(mypython3) dhcp65:numpy$ python test.py 1

    (mypython3) dhcp65:numpy$ python test.py 2

    (mypython3) dhcp65:numpy$ python test.py 3
    -2.9761904761904763 is queue length and -8.928571428571429 is the waiting time

    (mypython3) dhcp65:numpy $ python test.py 4
    -5.2521008403361344 is queue length and -15.756302521008402 is the waiting time
    -6.009615384615384 is queue length and -18.028846153846153 is the waiting time

    (mypython3) dhcp65:numpy $ python test.py 5
    Traceback (most recent call last):
      File "test.py", line 24, in 
        f(int(sys.argv[1]))
      File "test.py", line 14, in f
        Pinv = Rho**c / (math.factorial(c)*(1-(Rho/c)))
    ZeroDivisionError: float division by zero

    (mypython3) dhcp65:numpy $ python test.py 6
    4.815100154083203 is queue length and 14.44530046224961 is the waiting time
    4.407616361071931 is queue length and 13.222849083215795 is the waiting time
    3.8627935723114946 is queue length and 11.588380716934484 is the waiting time
    3.345824411134903 is queue length and 10.037473233404707 is the waiting time

    (mypython3) dhcp65:numpy $ python test.py 7
    2.2890418986229126 is queue length and 6.867125695868738 is the waiting time
    1.8902734091458986 is queue length and 5.670820227437696 is the waiting time
    1.4649353084567782 is queue length and 4.394805925370335 is the waiting time
    1.1433484560222449 is queue length and 3.4300453680667347 is the waiting time
    0.93753750150006 is queue length and 2.81261250450018 is the waiting time

    (mypython3) dhcp65:numpy $ python test.py 8
    1.3964108888769726 is queue length and 4.189232666630918 is the waiting time
    0.9936154247268749 is queue length and 2.980846274180625 is the waiting time
    0.6710213266682127 is queue length and 2.013063980004638 is the waiting time
    0.47731202306982656 is queue length and 1.4319360692094798 is the waiting time
    0.3703888075021216 is queue length and 1.1111664225063649 is the waiting time
    0.31212295546979246 is queue length and 0.9363688664093773 is the waiting time

    (mypython3) dhcp65:numpy $ python test.py 9
    0.8847197434341055 is queue length and 2.6541592303023167 is the waiting time
    0.5112330581454396 is queue length and 1.5336991744363189 is the waiting time
    0.3000920346259153 is queue length and 0.9002761038777459 is the waiting time
    0.19791668777778001 is queue length and 0.5937500633333401 is the waiting time
    0.14764612856259476 is queue length and 0.4429383856877843 is the waiting time
    0.12185386956418662 is queue length and 0.3655616086925598 is the waiting time
    0.10833589410497427 is queue length and 0.3250076823149228 is the waiting time

    (mypython3) dhcp65:numpy $ python test.py 10
    0.5184106276667043 is queue length and 1.5552318830001128 is the waiting time
    0.2352163689517404 is queue length and 0.7056491068552211 is the waiting time
    0.12312049182007154 is queue length and 0.36936147546021464 is the waiting time
    0.07715736942997864 is queue length and 0.2314721082899359 is the waiting time
    0.05618316899681491 is queue length and 0.16854950699044474 is the waiting time
    0.045806579847684686 is queue length and 0.13741973954305406 is the waiting time
    0.04046793101001221 is queue length and 0.12140379303003664 is the waiting time
    0.03772029861440367 is queue length and 0.11316089584321103 is the waiting time

    (mypython3) dhcp65:numpy $ python test.py 11
    0.25803510585367956 is queue length and 0.7741053175610386 is the waiting time
    0.09466070260014806 is queue length and 0.2839821078004442 is the waiting time
    0.04605807492586729 is queue length and 0.13817422477760186 is the waiting time
    0.028053393466307388 is queue length and 0.08416018039892217 is the waiting time
    0.020169055643224576 is queue length and 0.06050716692967373 is the waiting time
    0.016341721496619808 is queue length and 0.04902516448985943 is the waiting time
    0.014391085106937635 is queue length and 0.04317325532081291 is the waiting time
    0.013391996810371766 is queue length and 0.0401759904311153 is the waiting time
    0.012894663180405287 is queue length and 0.038683989541215866 is the waiting time

    (mypython3) dhcp65:numpy $ python test.py 12
    0.1062534238423277 is queue length and 0.3187602715269831 is the waiting time
    0.03396725862528196 is queue length and 0.10190177587584588 is the waiting time
    0.01591819213810855 is queue length and 0.04775457641432565 is the waiting time
    0.009565024222502846 is queue length and 0.028695072667508537 is the waiting time
    0.006836488342528061 is queue length and 0.020509465027584182 is the waiting time
    0.005523461315570017 is queue length and 0.016570383946710048 is the waiting time
    0.004857127226798642 is queue length and 0.014571381680395925 is the waiting time
    0.004516585037388037 is queue length and 0.013549755112164111 is the waiting time
    0.004347254980977975 is queue length and 0.013041764942933926 is the waiting time
    0.004267263574222837 is queue length and 0.01280179072266851 is the waiting time

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM