简体   繁体   English

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

[英]How to perform local polynomial fitting in Python

I have 200k data points and I'm trying to obtain derivative of fitted polynomial. 我有200k数据点,我试图获得拟合多项式的导数。 I divided my data set into smaller ones every 0.5 K, the data is Voltage vs Temperature. 我每0.5 K将数据集分成较小的数据,数据是电压与温度。 My code roughly looks like this: 我的代码大致如下所示:

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 ? 有没有其他方法可以在本地拟合多项式并同时得到连续导数?

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 . 例如,它在scipy.signal.savgol_filter The derivative of the fitted polynomial can be obtained with the deriv=1 argument. 拟合多项式的导数可以用deriv=1参数获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM