简体   繁体   English

如何通过Python插值3d时间序列数据?

[英]How can I interpolate 3d time series data by Python?

I have some 3d time series data. 我有一些3D时间序列数据。 But they have some missing values(np.nan). 但是它们缺少一些值(np.nan)。 So, I want to interpolate them. 因此,我想对它们进行插值。

Example: 例:

x = [0, nan, 2, 3, 4, 5]
y = [0, nan, 2, 3, 4, 5]
z = [3, nan, 5, 6, 7, 8]

# I want
interp(1) -> [1, 1, 4] 

How can I calculate? 我该如何计算?

I tried to interpolate x, y, and z for each individually by spline. 我试图通过样条曲线分别对x,y和z进行插值。 But I feel it is strange. 但是我觉得很奇怪。 Is it strange to interpolate individually or is it right mathematically or is there any solutions? 单独进行插值是否很奇怪,或者数学上是否正确,还是有解决方案?

In case calculating individually, it's very easy using scipy: 如果单独计算,使用scipy非常容易:

fx = interpolate.interp1d(time, x, kind="cubic")
fy = interpolate.interp1d(time, y, kind="cubic")
fz = interpolate.interp1d(time, z, kind="cubic")

Based on your comment it looks like you want to replace nan values with average of both sides (even though you've mentioned spline method). 根据您的评论,您似乎想用两侧的平均值替换nan值(即使您已经提到了样条线方法)。 So here's a simple function in case nan does not happen at the start and end. 因此,这是一个简单的功能,以防nan在开始和结束时均未发生。 import numpy as np 将numpy导入为np

x = np.array([0, np.nan, 2, 3, 4, 5])
y = np.array([0, np.nan, 2, 3, 4, 5])
z = np.array([3, np.nan, 5, 6, 7, 8])
def nan_mean(v):
    ind=np.where(np.isnan(v))[0]
    v[ind]=(v[ind+1]+v[ind-1])/2
nan_mean(x)
nan_mean(y)
nan_mean(z)

this returns your desired value 这将返回您想要的值

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

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