【发布时间】:2017-02-09 20:29:52
【问题描述】:
假设我有这个代码:
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 的测量值。
我想通过使用a 作为基础,在a 发生的特定时间找到缺失的b 值。
所以,最终的b 将始终具有a 的时间值和长度。(当时,我想以time.mktime(datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f").timetuple()) 为单位返回时间
所以,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)])
现在,我不知道该如何处理。
一个想法是首先执行interpas here并将b长度拉伸到与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))
那么,时间怎么处理呢?
【问题讨论】:
-
谢谢,你的问题现在很清楚了:)
标签: python numpy scipy interpolation