通过循环和参数组合多个函数

2024-04-24 16:14:01 发布

您现在位置:Python中文网/ 问答频道 /正文

更新我的问题已经完全回答了,我已经用jarmod的答案将它应用到我的程序中,尽管代码看起来更整洁,它没有影响速度(当我的图形出现时(我使用matplotlib绘制此数据),我有点困惑为什么我的程序运行缓慢,以及如何提高速度(大约需要30秒,我知道这部分代码正在减慢它),我在第二个代码块中显示了我的真实代码。而且,速度很大程度上取决于我设定的距离,在短距离内,它安静而快速

我这里有一个示例代码,它显示了进行预测和提取值所需的计算。我使用for循环遍历标记为1-100的特定CSV文件范围。我返回每个月(1-12)的数字,以获得给定月份预测量的预测平均值。你知道吗

我的完整代码包括12个全年预测功能,但我觉得代码是低效的,因为功能非常相似,除了一个数字和读取csv文件这么多次减慢程序。你知道吗

有没有一种方法可以组合这些函数,或者添加另一个参数使其运行。我最担心的是,很难返回单独的数字并对它们进行分类。换言之,我希望理想情况下,所有12个月精度预测只有一个函数,我可能知道如何添加另一个参数和另一个循环序列,但不知道如何进行,或者是否可能。基本上,我想存储onemonthaccurary的所有值(它在当前文件之前进入文件并比较与当前文件关联的日期的预测值),然后存储twomonthaccurary的所有值,依此类推。。。所以我以后可以用这些变量作图和其他用途

import csv
import pandas as pd

def onemonthaccuracy(basefilenumber):
    basefileread = pd.read_csv(str(basefilenumber)+'.csv', encoding='latin-1')
    basefilevalue = basefileread.loc[basefileread['Customer'].str.contains('Customer A', na=False), 'Jun-16\nQty'] 

    onemonthread = pd.read_csv(str(basefilenumber-1)+'.csv', encoding='latin-1')
    onemonthvalue = onemonthread.loc[onemonthread['Customer'].str.contains('Customer A', na=False),'Jun-16\nQty']   

    onetotal = int(onemonthvalue)/int(basefilevalue)   

    return onetotal


def twomonthaccuracy(basefilenumber):
    basefileread = pd.read_csv(str(basefilenumber)+'.csv', encoding='Latin-1')
    basefilevalue = basefileread.loc[basefileread['Customer'].str.contains('Customer A', na=False), 'Jun-16\nQty']

    twomonthread = pd.read_csv(str(basefilenumber-2)+'.csv', encoding = 'Latin-1')
    twomonthvalue = twomonthread.loc[twomonthread['Customer'].str.contains('Customer A', na=False), 'Jun-16\nQty']

    twototal = int(twomonthvalue)/int(basefilevalue)    

    return twototal


onetotal = 0
twototal = 0
onetotallist = []
twototallist = []


for basefilenumber in range(24,36):
    onetotal += onemonthaccuracy(basefilenumber)
    twototal +=twomonthaccuracy(basefilenumber)
    onetotallist.append(onemonthaccuracy(i)) 
    twototallist.append(twomonthaccuracy(i))

onetotalpermonth = onetotal/12
twototalpermonth = twototal/12   
x = [1,2]
y = [onetotalpermonth, twototalpermonth]
z = [1,2]
w = [(onetotallist),(twototallist)]

for ze, we in zip(z, w):
    plt.scatter([ze] * len(we), we, marker='D', s=5)

plt.scatter(x,y)
plt.show()

这是我在我的程序中使用的真正的代码块,也许有什么我不知道的东西在减慢它?你知道吗

#other parts of code 
#StartRange = yearvalue+Value
#EndRange = endValue + endyearvalue
#Range = EndRange - StartRange
# Department
#more code.... 

def nmonthaccuracy(basefilenumber, n):
    basefileread = pd.read_csv(str(basefilenumber)+'.csv', encoding='Latin-1')
    baseheader = getfileheader(basefilenumber)
    basefilevalue = basefileread.loc[basefileread['Customer'].str.contains(Department, na=False), baseheader]

    nmonthread = pd.read_csv(str(basefilenumber-n)+'.csv', encoding = 'Latin-1')
    nmonthvalue = nmonthread.loc[nmonthread['Customer'].str.contains(Department, na=False), baseheader]

    return (1-(int(basefilevalue)/int(nmonthvalue))+1) if int(nmonthvalue) > int(basefilevalue) else int(nmonthvalue)/int(basefilevalue)  

N = 13
total = [0] * N
total_by_month_list  = [[] for _ in range(N)]
for basefilenumber in range(int(StartRange),int(EndRange)):
    for n in range(N):
    total[n] += nmonthaccuracy(basefilenumber, n)
    total_by_month_list[n].append(nmonthaccuracy(basefilenumber,n)) 

onetotal=total[1]/ Range
twototal=total[2]/ Range
threetotal=total[3]/ Range
fourtotal=total[4]/ Range
fivetotal=total[5]/ Range #... all the way to 12 

onetotallist=total_by_month_list[1]
twototallist=total_by_month_list[2]
threetotallist=total_by_month_list[3]
fourtotallist=total_by_month_list[4]
fivetotallist=total_by_month_list[5] #... all the way to 12 
# alot more code after this 

Tags: csv代码forreadbycustomerlistint
1条回答
网友
1楼 · 发布于 2024-04-24 16:14:01

像这样:

def nmonthaccuracy(basefilenumber, n):
    basefileread = pd.read_csv(str(basefilenumber)+'.csv', encoding='Latin-1')
    basefilevalue = basefileread.loc[basefileread['Customer'].str.contains('Lam DepT', na=False), 'Jun-16\nQty']

    nmonthread = pd.read_csv(str(basefilenumber-n)+'.csv', encoding = 'Latin-1')
    nmonthvalue = nmonthread.loc[nmonthread['Customer'].str.contains('Lam DepT', na=False), 'Jun-16\nQty']

    return int(nmonthvalue)/int(basefilevalue)    

N = 2
total_by_month = [0] * N
total_aggregate = 0

for basefilenumber in range(20,30):
    for n in range(N):
        a = nmonthaccuracy(basefilenumber, n)
        total_by_month[n] += a
        total_aggregate += a

如果您想知道以下代码的作用:

N = 2
total_by_month = [0] * N

它将N设置为所需的月数(2,但可以设置为12或其他值),然后创建一个total_by_month数组,可以存储N个结果,每个月一个。然后它将total_by_month初始化为全零(N零),这样每个N月总数都从零开始。你知道吗

相关问题 更多 >