2016-05-07 36 views
1

我有一個數據集,包含幾年內每天的美國國庫券曲線。行=日期,列=特定國債的期限(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) 

該代碼對於第一次迭代正常工作,但隨後不斷重複數據幀(曲線)中第一行的校準。此外,它不會將參數存儲在校準數據幀的下一行中。我將第一個問題看作與該問題有關,而第二個問題是循環計數問題。

有什麼想法會出錯?我有一個Matlab背景,發現熊貓設置非常令人沮喪。

僅供參考我查閱了下面的鏈接無濟於事。

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

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

每傑森的評論下面我已經更新了代碼:

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 

修改後的代碼現在把適當的參數基礎上,校準數據幀的每一行中循環計數器。

*但是,它仍然不會移動到熊貓iterrows函數的曲線數據框的第二個(或後續行)。

+0

我在下面編輯了我的答案,以解決您的問題的部分問題。讓我知道這是否有幫助。 – user6275647

回答

2

每次調用calibration時,都設置爲i = 0。因此,當您撥打calibration.loc[i] = np.array(opt)時,正在寫入的內容是校準項目0。變量i實際上從來沒有任何東西,除了0這個函數。

在函數do_calibration_model1()中,聲明global i,然後在函數調用結束時將i加1。我不確定這個i計數器是用來完成的。也許你認爲在do_calibration_model1()中的i正在更新calibration()函數中i變量的值,但事實並非如此。鑑於calibration()中沒有global i聲明,此函數中的i是局部變量。

關於iterrows,我認爲你不需要循環遍歷曲線長度的嵌入循環。這裏有一個簡單的例子來告訴你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的值,以便它們適合您的callibration數據幀,與我在此處爲temp執行的操作相同:temp = {'sum': sum(row), 'mean': np.mean(row)}