插值到特定时间 [英] interpolate to specific time

查看:159
本文介绍了插值到特定时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码:

import numpy as np
import time
from datetime import datetime

class Measurements():
    def __init__(self, time_var, value):
        self.time_var = time_var
        self.value = value

a = np.array([ Measurements('30-01-2017 12:02:15.880922', 100),
               Measurements('30-01-2017 12:02:16.880922', 100),
               Measurements('30-01-2017 12:02:17.880922', 110),
               Measurements('30-01-2017 12:02:18.880922', 99),
               Measurements('30-01-2017 12:02:19.880922', 96)])


b = np.array([ Measurements('30-01-2017 12:02:15.123444', 10),
               Measurements('30-01-2017 12:02:18.880919', 12),
              ])

所以,我有5个来自a的测量值和2个来自b的测量值.

So, I have 5 measurements from a and 2 from b.

我希望以a为基础,在发生a的特定时间查找丢失的b值.

I want, by using the a as base, to find the missing b values at the specific time where a happens.

因此,最终的b将始终具有a时间值和长度.(对于该时间,我想到了使用time.mktime(datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f").timetuple())以秒为单位返回时间

So, the final b will always have the a time values and length.( for the time, I thought of taking the time.mktime(datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f").timetuple()) to return time in seconds

所以b将是:

np.array([ Measurements('30-01-2017 12:02:15.880922', MISSING_VALUE),
               Measurements('30-01-2017 12:02:16.880922', MISSING_VALUE),
               Measurements('30-01-2017 12:02:17.880922', MISSING_VALUE),
               Measurements('30-01-2017 12:02:18.880922', MISSING_VALUE),
               Measurements('30-01-2017 12:02:19.880922', MISSING_VALUE)])

现在,我不确定该如何处理.

Now, I am not sure how to deal with this.

一个想法是首先执行interp 此处所示,然后将b的长度拉伸为与a相等.

One thought is to execute first the interp as here and stretch the b length to be equal with a.

或使用interp1d(更灵活):

from scipy import interpolate

a = np.array([100, 123, 123, 118, 123])
b = np.array([12, 11, 14, 13])

b_interp = interpolate.interp1d(np.arange(b.size),b, kind ='cubic', assume_sorted=False)
b_new = b_interp(np.linspace(0, b.size-1, a.size))

但是,如何处理时间呢?

But then , how to deal with the time?

推荐答案

这是您的问题的解决方案:

Here is the solution of your problem :

  • 首先,如果使用三次插值,则a至少需要4个值,b至少需要4个值(scipy.interpolate.interp1dkind="cubic"不能正常工作)
  • 第二次,您无法使用scipy.interpolate.interp1d插值的数值不在您定义的范围内(b倍的范围)
  • first, if you use cubic interpolation, you need at least 4 values for a and 4 values for b (scipy.interpolate.interp1d with kind="cubic" is not working otherwise)
  • second, you can not interpolate values with scipy.interpolate.interp1d that are not in the range you define (the range of b times)

我稍微更改了您的初始代码以显示给您:

I changed a bit your initial code to show you :

time_a_full = ['30-01-2017 12:02:15.880922','30-01-2017 12:02:16.880922','30-01-2017 12:02:17.880922','30-01-2017 12:02:18.880922','30-01-2017 12:02:19.880922','30-01-2017 12:02:22.880922']
time_b_full = ['30-01-2017 12:02:15.123444','30-01-2017 12:02:16.880919','30-01-2017 12:02:18.880920', '30-01-2017 12:02:19.880922','30-01-2017 12:02:20.880922']

# Here I transform the time in seconds as suggested
time_a = np.array([time.mktime(datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f").timetuple()) for s in time_a_full])
time_b = np.array([time.mktime(datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f").timetuple()) for s in time_b_full])

values_a = np.array([100,100,110,99,96,95])
values_b = np.array([10,12,13,16,20])

# result of the linear interp with the numpy function
np.interp(time_a, time_b, values_b)

# result of the cubic interpolation
f = interpolate.interp1d(time_b,values_b, kind="cubic")
time_a[time_a<time_b.min()]=time_b.min() # use this to stay on range define by the times of b
time_a[time_a>time_b.max()]=time_b.max() # use this to stay on range define by the times of b
f(time_a)

这篇关于插值到特定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆