如何将曲线拟合到包含NaN值的excel文件的数据中



我是python的初学者。我有一个excel文件。这个excel文件显示了参数的每日值。这个excel文件包含了四年的数据。有些值是NaN。这是我的excel文件的一个示例。

Date     f11
0        0.002
1        0.004
2          -
.
.
.
1625       -
1626    -0.001
1627    -0.0013

我想通过模型找到f11数据的拟合曲线。这是我的代码。

df =pd.read_excel ('final-all-filters.xlsx')
x = df['Date']
y = df['f11'].dropna(how='all', axis=0)
def model(x, b, m, c, d, f, g, h, i):
return b+m*x+ c*np.sin(x/180)+d*np.cos(x/180)+h*np.sin(x/180)+i*np.cos(x/180)+ f/g * np.exp (681-x)* np.heaviside(x-681, 681)
popt,pcov = curve_fit (model, x, y, p0=[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05 ], maxfev = 10000)
print (popt)

当我运行这段代码时,我面对的错误是

ValueError: operands could not be broadcast together with shapes (1639,) (1632,)

我应该提到,当我用随机值填充NaN单元格时,模型可以正确工作。

您只是从y值中删除NaN。您应该首先从Dataframe中删除受影响的行,然后为xy值创建系列。

试题:

fixed = df.dropna()
x = fixed['Date']
y = fixed['f11']
def model(x, b, m, c, d, f, g, h, i):
return b+m*x+ c*np.sin(x/180)+d*np.cos(x/180)+h*np.sin(x/180)+i*np.cos(x/180)+ f/g * np.exp (681-x)* np.heaviside(x-681, 681)
popt,pcov = curve_fit (model, x, y, p0=[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05 ], maxfev = 10000)
print (popt)

编辑:

一旦您从没有NaNs的数据中创建了模型,您就可以使用它来填充缺失的数据。

您已经从Y中删除了nan,但您没有从x中删除相应的索引。您必须这样做才能使模型运行。

最新更新