索引器错误:在scipy函数中使用pandas.ix索引越界错误

2024-09-26 18:04:55 发布

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

在下面的代码部分中,我继续得到一个索引越界错误,我一辈子都不知道为什么(这个网站上的链接都没有帮助过我)。我要做的是将scipy曲线拟合函数(Func)应用于x_数据和y_数据中的每一行数据,以获得a、b、c和d拟合参数。谁能看出我做错了什么吗?谢谢!在

import pandas as pd
from scipy.optimize import curve_fit
import numpy as np
import math

#my function
def Func(x_data,a,b,c,d):
    return np.multiply((np.multiply((a+cols), b)),
           (x_data.ix[i]/math.pi))+np.multiply((c+cols), d)

#create dummy dataframe
index=pd.date_range('2017-01-01','2017-03-01', freq='D')
columns=['1.234','2.345','3.456','4.567','5.678','6.789']

data1 = np.random.rand(60,6)
data2 = np.random.rand(60,6)

x_data=pd.DataFrame(data1, index=index, columns=columns)
y_data=pd.DataFrame(data2, index=index, columns=columns)

#values for function
cols=np.array(x_data.columns.values).astype(np.float)   

#Apply function to each row of x_data and y_data and append to empty array
data_popt=[]
data_perr=[]
for i, j in zip(range(0, len(x_data)), range(0, len(y_data))):
    popt, pcov = curve_fit(Func,x_data.ix[i],y_data.ix[j])
    data_popt.append(popt)
    perr=np.sqrt(np.diag(pcov))
    data_perr.append(perr)
    index=x_data.index
    columns1=['a','b','c','d']
    columns2=['a_err','b_err','c_err','d_err']
data_popt=pd.DataFrame(data_popt, index=index, columns=columns1)
data_perr=pd.DataFrame(data_perr, index=index, columns=columns2)

另外,我使用的是pythonv2.7.12和pandas/scipyv0.18.1


Tags: columns数据importdataframedataindexnpfunction
1条回答
网友
1楼 · 发布于 2024-09-26 18:04:55

在中的第七次迭代(i=6)时产生错误

popt, pcov = curve_fit(Func,x_data.ix[i],y_data.ix[j])

在评估Func

^{pr2}$

问题是x_data.ix[i],因为传递给Funcx_data是{},但是i=6。在

我想您忘了已经向Func传递了一行,因此不应该进一步分割它,即定义

^{3}$

同样尝试运行这个,迟早你会得到RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 1000.,因此你需要增加maxfev,也就是说

popt, pcov = curve_fit(Func,x_data.ix[i],y_data.ix[j], maxfev=10000)

相关问题 更多 >

    热门问题