我有以下时间序列
> y<- xts(1:10, Sys.Date()+1:10)
> y[c(1,2,5,9,10)] <- NA
> y
[,1]
2011-09-04 NA
2011-09-05 NA
2011-09-06 3
2011-09-07 4
2011-09-08 NA
2011-09-09 6
2011-09-10 7
2011-09-11 8
2011-09-12 NA
2011-09-13 NA
Run Code Online (Sandbox Code Playgroud)
一个直的na.locf给我这个:
> na.locf(y)
[,1]
2011-09-04 NA
2011-09-05 NA
2011-09-06 3
2011-09-07 4
2011-09-08 4
2011-09-09 6
2011-09-10 7
2011-09-11 8
2011-09-12 8
2011-09-13 8
Run Code Online (Sandbox Code Playgroud)
我怎么做到这一点?
[,1]
2011-09-04 NA
2011-09-05 NA
2011-09-06 3
2011-09-07 4
2011-09-08 4
2011-09-09 6
2011-09-10 7
2011-09-11 8
2011-09-12 NA
2011-09-13 NA
Run Code Online (Sandbox Code Playgroud)
我不希望最后一次观察结果除了最后一个非缺失值之外,即不会替换尾随的NA.非常感谢你的帮助!
根据我的数据(参见图片)称为GDP.我想知道如何在一个图表中绘制所有国家.我希望每个国家都有一个传奇,例如每行不同的颜色或每行不同的形状.
我知道如何绘制一个系列,例如:
ts.plot(GDP $ ALB)
但不知道如何用传奇绘制所有系列.
谢谢
抱歉,如果这很容易解决,但我无法理解它.
我有这个数据框:
> aaci
Date Plate.1 Plate.2 Plate.3 Plate.4 Plate.5 Plate.6 Plate.7 Plate.8 Average SE Species
1 2014-06-19 0.0000000 0.0000000 0.000000 0.000000 0.0000000 0.0000000 0.000000 0.0000000 0.000000 0.0000000 aa
7 2014-08-04 7.0057778 11.0000000 24.269333 10.439111 28.5724444 92.4604444 5.584000 55.5448889 29.359500 10.7354126 aa
13 2014-09-17 84.4075556 59.2493333 62.664444 38.147556 73.8417778 93.5208889 72.782667 94.5164444 72.391333 6.7116450 aa
19 2014-10-16 56.9840000 64.9733333 45.124444 38.817333 56.2031111 76.4613333 NA 85.4017778 60.566476 6.2339579 aa
25 2014-11-14 75.7146667 72.3604444 62.126222 20.095111 73.9520000 83.8688889 NA 61.5466667 64.237714 7.9248240 …
Run Code Online (Sandbox Code Playgroud) 我通常需要用给定的聚合函数(即求和,平均等)来总结具有不规则定时的时间序列.但是,我目前的解决方案似乎效率低,速度慢.
采取聚合功能:
function aggArray = aggregate(array, groupIndex, collapseFn)
groups = unique(groupIndex, 'rows');
aggArray = nan(size(groups, 1), size(array, 2));
for iGr = 1:size(groups,1)
grIdx = all(groupIndex == repmat(groups(iGr,:), [size(groupIndex,1), 1]), 2);
for iSer = 1:size(array, 2)
aggArray(iGr,iSer) = collapseFn(array(grIdx,iSer));
end
end
end
Run Code Online (Sandbox Code Playgroud)
请注意这两个array
和groupIndex
可2D.每个列array
都是要聚合的独立系列,但groupIndex
应将这些列合在一起(作为一行)以指定句点.
然后当我们给它带来一个不规则的时间序列时(注意第二个周期是一个基本周期更长),时间结果很差:
a = rand(20006,10);
b = transpose([ones(1,5) 2*ones(1,6) sort(repmat((3:4001), [1 5]))]);
tic; aggregate(a, b, @sum); toc
Elapsed time is 1.370001 seconds.
Run Code Online (Sandbox Code Playgroud)
使用分析器,我们可以发现该grpIdx
行大约占执行时间的1/4(.28秒),iSer
循环大约需要3/4(1.17秒)的总时间(1.48秒).
将此与期间无关紧要的情况相比较:
tic; cumsum(a); toc …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用LSTM进行多变量数据的时间序列预测.我有500个样本,15个维度.我想使用回顾10. LSTM层的输入形状是什么.那将会
(samples,look back,dimension) = (50000,10,15)
Run Code Online (Sandbox Code Playgroud)
要么
(samples,dimension, look back) = (50000,15,10)
Run Code Online (Sandbox Code Playgroud)
我正在使用Keras.
我有一个非常大的时间序列,我需要根据开头的某个任意值创建一个不同的时间序列,并在当前时间段内进行更改.在真实数据集中,此更改取决于数据框的其他变量,但出于MWE的目的,我按如下方式重新创建它:
initial_value <- 100
set.seed(123)
library(data.table)
df <- as.data.table(data.frame(num = c(1:10),change = rnorm(10)))
Run Code Online (Sandbox Code Playgroud)
新变量value
定义为上一期间的自身值加上change
当前期间的值.第一次观察中的值由任意选择确定initial_value
.如果没有限制value
,可以简单地创建
df <- df[, value0 := initial_value + cumsum(change)]
Run Code Online (Sandbox Code Playgroud)
这是非常快速的使用data.table
.然而,遗憾的是,change
也可能取决于前一时期的实际value
情况.具体来说,假设每当它达到102时,系列需要到达initial_value
下一个时段并在那里停留3个时段.因此,在以下数据框架中,我需要value
在上面生成的代码中创建变量value0
:
num change value0 value
1: 1 -0.56047565 99.43952 99.43952
2: 2 -0.23017749 99.20935 99.20935
3: 3 1.55870831 100.76806 100.76806
4: 4 0.07050839 100.83856 100.83856
5: 5 0.12928774 100.96785 100.96785
6: 6 1.71506499 102.68292 102.68292
7: 7 0.46091621 …
Run Code Online (Sandbox Code Playgroud) 我想在pandas滚动功能中设置center = True,对于时间序列:
import pandas as pd
series = pd.Series(1, index = pd.date_range('2014-01-01', '2014-04-01', freq = 'D'))
series.rolling('7D', min_periods=1, center=True, closed='left')
Run Code Online (Sandbox Code Playgroud)
但输出是:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-6-6b30c16a2d12> in <module>()
1 import pandas as pd
2 series = pd.Series(1, index = pd.date_range('2014-01-01', '2014-04-01', freq = 'D'))
----> 3 series.rolling('7D', min_periods=1, center=True, closed='left')
~\Anaconda3\lib\site-packages\pandas\core\generic.py in rolling(self, window, min_periods, freq, center, win_type, on, axis, closed)
6193 min_periods=min_periods, freq=freq,
6194 center=center, win_type=win_type,
-> 6195 on=on, axis=axis, closed=closed)
6196
6197 cls.rolling = …
Run Code Online (Sandbox Code Playgroud) 当运行plot()
数据帧或系列的方法时,python会抛出错误.错误的最后一行是NameError: name '_converter' is not defined
我正在使用Python 3.6,并且所有其他功能都按预期工作,因此不确定可能导致此问题的原因.
下面是导致问题的代码示例,下面是导致该错误的错误.
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot()
Run Code Online (Sandbox Code Playgroud)
返回的错误如下所示:
NameError Traceback (most recent call last)
<ipython-input-336-8fe4bd433d4d> in <module>()
----> 1 ts.plot()
2
3 plt.plot(ts)
c:\users\fguih\appdata\local\programs\python\python36\lib\site-packages\pandas\plotting\_core.py in __call__(self, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
2501 colormap=colormap, …
Run Code Online (Sandbox Code Playgroud) R有一个很好的库来检测时间序列中的异常,它的名字是tsoutliers,例如在这里讨论https://stats.stackexchange.com/questions/104882/detecting-outliers-in-time-series-ls-ao- TC-使用-tsoutliers封装功能于R-如何
python中有替代库吗?或者如何使用更多标准库(numpy,scipy,sklearn)找到时间序列异常值的简单方法?
不规则的时间序列data
存储在中pandas.DataFrame
。DatetimeIndex
已设置A。我需要索引中连续条目之间的时间差。
我以为会很简单
data.index.diff()
Run Code Online (Sandbox Code Playgroud)
但是得到了
AttributeError: 'DatetimeIndex' object has no attribute 'diff'
Run Code Online (Sandbox Code Playgroud)
我试过了
data.index - data.index.shift(1)
Run Code Online (Sandbox Code Playgroud)
但是得到了
ValueError: Cannot shift with no freq
Run Code Online (Sandbox Code Playgroud)
在执行此操作之前,我不想先推断或强制执行频率。时间序列中有很大的缺口,将会扩大到大量的时间nan
。关键是要首先找到这些差距。
那么,执行此看似简单的操作的干净方法是什么?
time-series ×10
r ×5
python ×4
pandas ×3
data-science ×1
data.table ×1
ggplot2 ×1
keras ×1
loops ×1
lstm ×1
matlab ×1
matplotlib ×1
rolling-sum ×1
xts ×1
zoo ×1