使用自定义函数进行插值

2024-09-26 18:17:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧(df),我想每天按组插入/缩小数据。我知道如何使用带有“interpolate”的pandas进行插值,但是插值的类型受到scipy.interpolate.interp1d的限制,但是我想使用Whittaker平滑器。有没有一种方法可以让我拥有这样的东西

df.groupby(['ID']).apply(lambda x: x.resample('1D').first().interpolate(method = my-custom-function-Whittaker_smoother))



df= pd.DataFrame({"ID":[1, 2 , 3 , 4, 1, 2 , 3 , 4, 1, 2 , 3 , 4], 
                        "date":['2015-04-09', '2015-04-09', '2015-04-09', '2015-04-09', '2015-06-03', '2015-06-03', '2015-06-03', '2015-06-03', '2015-06-08', '2015-06-08', '2015-06-08', '2015-06-08'], 
                        "V_1n":[0.2, 0.5, 0.8, 0.4, 0.9, 0.5, 3.0, 5.0, 0.0, 5.0, 0.0, 0.4]})
df['date'] = pd.to_datetime(df['date'], format="%Y-%m-%d") 
df.set_index('date', inplace= True)
df_interpolation = df.groupby(['ID']).apply(lambda x: x.resample('1D').first().interpolate())

我已准备好我的功能:

import scipy as sp
import scipy.sparse
import scipy.linalg
from scipy.sparse.linalg import cg

def Whittaker_smoother(y, lmda):
  m = len(y)
  E = sp.sparse.identity(m)
  d1 = -1 * np.ones((m),dtype='d')
  d2 = 3 * np.ones((m),dtype='d')
  d3 = -3 * np.ones((m),dtype='d')
  d4 = np.ones((m),dtype='d')
  D = sp.sparse.diags([d1,d2,d3,d4],[0,1,2,3], shape=(m-3, m), format="csr")
  z = sp.sparse.linalg.cg(E + lmda * (D.transpose()).dot(D), y)

  return z[0]

从这里开始:https://gist.github.com/zmeri/3c43d3b98a00c02f81c2ab1aaacc3a49


Tags: 数据importiddfdatenponesscipy

热门问题