我正在分析包含http请求日志的aws日志文件,我需要预测下一分钟的预期负载(请求数量)。然而,我发现有些时间跨度没有任何日志。在这种情况下,我是假设这些时间内的负载仅为0,还是需要进行某种插值?
time load
-----------------------------------
2018-11-07 09:45:00 40
2018-11-07 09:46:00 45
2018-11-07 09:47:00 34
2018-11-07 09:48:00 56
然后在接下来的两个小时内没有日志,然后再一次:
time load
-----------------------------------
2018-11-07 11:50:00 54
2018-11-07 11:51:00 34
2018-11-07 11:52:00 23
2018-11-07 11:53:00 21
比方说,当我将这个文件读给我的预测模型的pandas数据帧时,我是否要用0填充这2个小时的所有分钟?或者,有没有更好的智能方法来处理这种情况?
发布于 2019-02-02 01:08:17
我建议用-1来填充缺失的值。ML模型应该学会处理这种情况。当使用滑动平均值或其他插值方法填充这些值时,您将强制执行一个可能不能正确表示数据的函数。模型应该学会自己处理缺失值(并找到在测量值之间进行插值的最佳方法)。
这里我有一个例子,它是如何看起来的:该模型采用最后5个时间步来预测后续的未来时间戳。
import numpy as np
from sklearn.ensemble import RandomForestRegressor
import matplotlib.pylab as plt
timeline = np.array([40, 45, 50, 53, 54, None, None, None, 50, 43, 30,
20, 15, 14, 13, 14, 16, 21, 27, 35, 46, 59, 65, 70,
None, None, 74, 72, 70, 65, 56, 44, 32, 26, 21, 18,
17, 16, 16, 17, 23, None, 47, 60, 75, None, 105,
111, 116, 118, 119, 118, 112, 103, None, None,
60, 53, 51, 52, 55, 62, None, 75, 77, 76, 74, 63,
50, 35])
plt.figure()
plt.plot(timeline)
plt.xlabel("time_index")
plt.ylabel("requests")
plt.show()
timeline[timeline==None] = -1
def get_training_data(timeline, n_time_steps=5):
x = []
y = []
for i in range(n_time_steps, len(timeline)):
x.append(timeline[i-n_time_steps:i])
y.append(timeline[i])
return np.array(x), np.array(y)
x, y = get_training_data(timeline)
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
# train model
model.fit(x, y)
pred = model.predict([y[-5:]])[0]
print 'the prediction for the future timestamp is:', pred
对未来时间戳的预测是: 30.8
现在,如果您有同样有效的未知值:
model.predict(np.array([[10, 20, 30, -1, -1]]))
46.5
备注:
通常不是随机福雷斯特,而是递归神经网络(例如LSTM)用于这样的时间序列任务。但是,为了简单起见,我选择了一个更简单的模型。
发布于 2019-02-02 01:15:04
一种方法是用滚动均值填充缺失的日期。否则,如果您将模型与缺失日期的其他值(例如0
)进行拟合,则模型很可能还会考虑这些值以进行预测(假设无法预测哪些日期将有缺失值),这肯定会使预测结果变得更糟。
所以,假设你有:
time load
0 2018-11-07 09:45:00 40
1 2018-11-07 09:46:00 45
2 2018-11-07 09:47:00 34
3 2018-11-07 09:49:00 56
您可以首先使用.resample
对数据帧进行重采样,然后使用.rolling
填充缺失的值,该值将填充给定窗口长度的滚动平均值:
df.time = pd.to_datetime(df.time)
resampled = df.set_index('time').resample('Min').first()
fill = resampled.rolling(3,center=True,min_periods=1).mean()
resampled.fillna(fill)
load
time
2018-11-07 09:45:00 40.0
2018-11-07 09:46:00 45.0
2018-11-07 09:47:00 34.0
2018-11-07 09:48:00 45.0
2018-11-07 09:49:00 56.0
发布于 2019-02-04 13:46:05
使用tsclean(),它将自动处理缺失值和异常值。
https://stackoverflow.com/questions/54484003
复制相似问题