如何在Python中执行局部多项式拟合

时间:2022-04-04 01:36:18

I have 200k data points and I'm trying to obtain derivative of fitted polynomial. I divided my data set into smaller ones every 0.5 K, the data is Voltage vs Temperature. My code roughly looks like this:

我有200k数据点,我试图获得拟合多项式的导数。我每0.5 K将数据集分成较小的数据集,数据是电压与温度。我的代码大致如下所示:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
testset=pd.read_csv('150615H0.csv',sep='\t')
x=np.linspace(1,220,219)
ub=min(testset['T(K)'])
lb=min(testset['T(K)'])-1
q={i:testset[(testset['T(K)'] < ub+i) & (testset['T(K)'] > lb+i)] for i in x}
f={j:np.polyfit(q[j]['T(K)'],q[j]['Vol(V)'],4) for j in q}
fs={k:np.poly1d(f[k]) for k in f}
fsd={l:np.polyder(fs[l],1) for l in fs}
for kk in q:
    plt.plot(q[kk]['T(K)'],fsd[kk](q[kk]['T(K)']),color='blue',linewidth=2,label='fit')

Unsurprinsingly, the derivative is discontinuous and I don't like it. Is there any other way to fit polynomial locally and get continuous derivative at the same time ?

不出所料,衍生品是不连续的,我不喜欢它。有没有其他方法可以在本地拟合多项式并同时得到连续导数?

1 个解决方案

#1


2  

Have a look at the Savitzky-Gollay filter for an efficient local polynomial fitting.

查看Savitzky-Gollay滤波器以获得有效的局部多项式拟合。

It is implemented, for instance, in scipy.signal.savgol_filter. The derivative of the fitted polynomial can be obtained with the deriv=1 argument.

例如,它在scipy.signal.savgol_filter中实现。拟合多项式的导数可以用deriv = 1参数获得。

#1


2  

Have a look at the Savitzky-Gollay filter for an efficient local polynomial fitting.

查看Savitzky-Gollay滤波器以获得有效的局部多项式拟合。

It is implemented, for instance, in scipy.signal.savgol_filter. The derivative of the fitted polynomial can be obtained with the deriv=1 argument.

例如,它在scipy.signal.savgol_filter中实现。拟合多项式的导数可以用deriv = 1参数获得。