我正在为救护车编写关于繁忙概率等的统计计算。我有两个循环;第一个循环写入.csv非常好,我可以使用它并对其运行统计。第二个循环给我带来了麻烦。它与第一个循环几乎相同,但在打印时,它包含奇怪的值,如“Probability0”和dtype:float64”。我不知道它为什么这么做。 我正在Windows机器上运行python3.6 Anaconda发行版。 代码如下所示。同样,第二个循环正在打印意外的结果。 我的问题是:如何让第二个循环只打印计算出的值?你知道吗
# -*- coding: utf-8 -*-
# David Kulpanowski
# 15 September 2018
# Python 3.6 Anaconda distribution
# queueing theory calculations for ambulances and fire apparatus
import math
import pandas as pd
# declare variables
number_servers = 40
service_rate = 1.33333333
arrival_rate = 15
lambda_mu = arrival_rate / service_rate
k = 0
# I create a .csv file because when I create an array Python says it cannot append float values in arrays
probability0 = open('c:/temp/Probability0.csv', 'w')
# run the loop and calculate the values for p0
# This loop appears to run correctly
while k <= number_servers:
if(k == 0):
p0_value = 1.0
probability0.write('Probability0\n')
probability0.write(str(p0_value) + '\n')
elif(k == 1):
p0_value = lambda_mu
probability0.write(str(p0_value) + '\n')
elif(k == 2):
p0_value = lambda_mu *lambda_mu / k
probability0.write(str(p0_value) + '\n')
elif( k >= 3 & k <= number_servers):
p0_value = p0_value * lambda_mu / k
probability0.write(str(p0_value) + '\n')
k = k + 1
probability0.close()
# open the .csv and read the contents and display them on screen
df = pd.read_csv('c:/temp/Probability0.csv', sep=',')
print('The probability of 0 is:')
print(df.head(n = 40))
# declare the variables
servers_minus1 = number_servers - 1
magic_number0 = math.factorial(servers_minus1)
sum_probability0 = df.sum()
ls = lambda_mu / number_servers
magic_number1 = (math.pow(lambda_mu, number_servers)) / (magic_number0 * number_servers * (1 - ls))
L3 = 1 / (sum_probability0 + magic_number1)
k = 0
pn_value = 0
# create a .csv file to hold the data. Again, I am not able to make this work with arrays because there is some difficulty appending float values
# This loop is writing strange values and I don't know where they come from
# Where is "Probability0" coming from and "dtype: float64"
probabilityN = open('c:/temp/ProbabilityN.csv', 'w')
while k <= number_servers:
if(k == 0):
pn_value = L3
probabilityN.write('ProbabilityN\n')
probabilityN.write(str(pn_value) + '\n')
elif(k > 0):
pn_value = lambda_mu * pn_value / k
probabilityN.write(str(pn_value) + '\n')
k = k + 1
probabilityN.close()
# open the file and print to screen
df2 = pd.read_csv('c:/temp/ProbabilityN.csv', sep=',')
print('the probability of N is:')
print(df2.head(n=40))
########
# Notice the completely different output between the two csv files even though the loops
# are nearly identical.
# why is Python writing "Probability0" and "dtype: float64"
# By the way, the calculations appear correct when I verify them against a Microsoft Excel file
########
您正在将
pandas
系列作为字符串输出。注意,L3是pandas
系列。当你对发生的事情感到困惑时,使用pdb
(注意我在第52行添加了import pdb; pdb.set_trace()
)。pdb
的工作原理类似于gdb
:它是一个交互式调试器。我不打算在这里讨论所有的命令,但这里有一些输出:再往下走,我们可以看到:
所以,IIUC,你想输出
pn_value.values[0]
,而不是pn_value
。你知道吗嗯。你知道吗
相关问题 更多 >
编程相关推荐