在下面的代码部分中,我继续得到一个索引越界错误,我一辈子都不知道为什么(这个网站上的链接都没有帮助过我)。我要做的是将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
在中的第七次迭代(
i=6
)时产生错误在评估
^{pr2}$Func
问题是},但是
x_data.ix[i]
,因为传递给Func
的x_data
是{i=6
。在我想您忘了已经向
^{3}$Func
传递了一行,因此不应该进一步分割它,即定义同样尝试运行这个,迟早你会得到
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 1000.
,因此你需要增加maxfev
,也就是说相关问题 更多 >
编程相关推荐