How to perform local polynomial fitting in Python

I have 200k data points and I am trying to get the derivative of a fitted polynomial. I have divided my dataset into smaller ones every 0.5K, the data is voltage and temperature. My code 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')

      

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

+3


source to share


1 answer


Look at the Savicki-Golly filter for efficient local polynomial fitting.



It is implemented, for example, in scipy.signal.savgol_filter

. The derivative of the set polynomial can be obtained with an argument deriv=1

.

+2


source







All Articles