gpt4 book ai didi

python - pandas iterrows 和循环计数器的问题

转载 作者:行者123 更新时间:2023-12-01 03:58:06 24 4
gpt4 key购买 nike

我有一个数据集,其中包含几年来每天的美国国债曲线。行 = 日期,列 = 特定国债期限(3 个月、1 年、10 年等)

我有每天循环并校准利率模型参数的Python代码。我在通过 iterrows 和循环计数器循环每一行时遇到问题。目标是逐行将模型校准到每日曲线,将校准后的参数存储在数据框中,然后移至下一行并重复。

def do_calibration_model1():
global i
for index, row in curves.iterrows():
day = np.array(row) #the subsequent error_fxn uses this daily curve
calibration()
i += 1

def calibration():
i = 0
param = scipy.brute(error_fxn, bounds...., etc.)
opt = scipy.fmin(error_fxn, param, xtol..., ftol...)
calibration.loc[i] = np.array(opt) # store result of minimization (parameters for that day)

代码在第一次迭代时工作正常,但随后继续对数据帧(曲线)中的第一行重复校准。此外,它不会将参数存储在校准数据帧的下一行中。我认为第一个问题与 iterrows 有关,而第二个问题是循环计数器的问题。

对出了什么问题有什么想法吗?我有 Matlab 背景,发现 pandas 设置非常令人沮丧。

作为引用,我查阅了以下链接,但无济于事。

https://www.python.org/dev/peps/pep-0212/

http://nipunbatra.github.io/2015/06/pandas-iteration/

根据下面 Jason 的评论,我已将代码更新为:

def do_calibration_model1():
global i
for index, row in curves.iterrows():
for i in range(0,len(curves)):
day = np.array(row) #the subsequent error_fxn uses this daily curve
param = scipy.brute(error_fxn, bounds...., etc.)
opt = scipy.fmin(error_fxn, param, xtol..., ftol...)
calibration.loc[i] = np.array(opt) # store result of minimization (parameters for that day)
i += 1

修订后的代码现在根据循环计数器将适当的参数放置在校准数据帧的每一行中。

*但是,它仍然不会移动到 pandas iterrows 函数的曲线数据帧的第二个(或后续行)。

最佳答案

每次calibration被调用,你设置i = 0 。结果,当您调用 calibration.loc[i] = np.array(opt) ,写入的是校准项0。变量i在这个函数中,除了 0 之外,实际上从来没有任何东西。

在函数中do_calibration_model1() ,您声明 global i然后增加i在函数调用结束时加一。我不知道这是什么i计数器的目的是完成。也许您认为ido_calibration_model1()正在更新 i 的值calibration() 中的变量函数,但事实并非如此。鉴于没有 global i calibration()中的声明,i在这个函数中是一个局部变量。

关于iterrows ,我认为您不需要循环遍历曲线长度的嵌入式 for 循环。这是一个简单的示例,向您展示如何 iterrows作品:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])

new = pd.DataFrame({'sum': [],
'mean': []})
for index, row in df.iterrows():
temp = {'sum': sum(row), 'mean': np.mean(row)}
new = new.append(temp, ignore_index=True)

在上面,df看起来像这样:

          A         B         C         D
0 -2.197018 1.905543 0.773851 -0.006683
1 0.675442 0.818040 -0.561957 0.002737
2 -0.833482 0.248135 -1.159698 -0.302912
3 0.784216 -0.156225 -0.043505 -2.539486
4 -0.637248 0.034303 -1.405159 -1.590045
5 0.289257 -0.085030 -0.619899 -0.211158
6 0.804702 -0.838365 0.199911 0.210378
7 -0.031306 0.166793 -0.200867 1.343865

还有new通过iterrows填充的数据框循环看起来像这样:

       mean       sum
0 0.118923 0.475693
1 0.233566 0.934262
2 -0.511989 -2.047958
3 -0.488750 -1.954999
4 -0.899537 -3.598148
5 -0.156707 -0.626830
6 0.094157 0.376626
7 0.319621 1.278485

请注意,使用 append这里不需要使用 i计数器并简化了代码。

回到您的代码,我建议如下:

def do_calibration_model1():
callibration = pd.DataFrame({'a': [],
'b': []})
for index, row in curves.iterrows():
day = np.array(row)
param = scipy.brute(error_fxn, bounds...., etc.)
opt = scipy.fmin(error_fxn, param, xtol..., ftol...)
temp = {'a': ..., 'b': ...} # put opt values into dict
callibration = calibration.append(temp, ignore_index=True)
return callibration

在这一步callibration = pd.DataFrame({'a': [], 'b': []})您需要设置数据帧来摄取 opt 。之前,您转换了opt到 numpy 数组,但您需要排列 opt 的值所以它们适合您的校准数据框,就像我在这里对临时数据所做的那样:temp = {'sum': sum(row), 'mean': np.mean(row)} .

关于python - pandas iterrows 和循环计数器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37091869/

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