我是python新手,有一个简单的问题,我还没有找到答案。
假设我有一个c(t)的时间序列:

t_  c_
1   40
2   41
3   4
4   5
5   7
6   20
7   20
8   8
9   90
10  99
11  10
12  5
13  8
14  8
15  19

我现在想评估这个系列,关于c值在一定范围内持续了多长时间,以及这些时间段发生的频率。
因此,结果将包括三列:c(binned)、duration(binned)、frequency。转化为简单的例子,结果如下:
c_      Dt_  Freq_
0-50    8    1
50-100  2    1
0-50    5    1

你能给我个建议吗?
提前谢谢你,
乌尔里克
//编辑:
谢谢你的回复我的示例数据有点缺陷,因此我无法显示我的问题的一部分所以,这里有一个新的数据系列:
series=
t   c
1   1
2   1
3   10
4   10
5   10
6   1
7   1
8   50
9   50
10  50
12  1
13  1
14  1

如果我采用Christoph提出的准则:
bins = pd.cut(series['c'], [-1, 5, 100])
same_as_prev = (bins != bins.shift())
run_ids = same_as_prev.cumsum()
result = bins.groupby(run_ids).aggregate(["first", "count"])

我得到这样的结果:
first   count
(-1, 5]   2
(5, 100]  3
(-1, 5]   2
(5, 100]  3
(-1, 5]   3

但我更感兴趣的是:
c        length  freq
(-1, 5]    2      2
(-1, 5]    3      1
(5, 100]   3      2

我怎样才能做到这一点?我怎么能把它画成kde图呢?
最好的,
乌尔里克

最佳答案

用一个例子很好地问了一个问题:)
这是一种方法,很可能是不完整的,但它应该会帮助你一点。
由于数据的时间间隔是固定的,所以我不实现时间序列,而是将索引用作时间。因此,我将c转换为一个数组,并使用np.where()在容器中查找值。

import numpy as np

c = np.array([40, 41, 4, 5, 7, 20, 20, 8, 90, 99, 10, 5, 8, 8, 19])

bin1 = np.where((0 <= c) & (c <= 50))[0]
bin2 = np.where((50 < c) & (c <= 100))[0]

对于bin1,输出是array([ 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14], dtype=int64)对应于idx,其中c的值在bin中。
下一步是找到连续的idx。根据这个:
from itertools import groupby
from operator import itemgetter

data = bin1
for k, g in groupby(enumerate(data), lambda ix : ix[0] - ix[1]):
    print(list(map(itemgetter(1), g)))

# Output is:
#[0, 1, 2, 3, 4, 5, 6, 7]
#[10, 11, 12, 13, 14]

最后一步:按正确的顺序放置新的子箱,并跟踪哪个箱对应于哪个子箱因此,完整的代码如下所示:
import numpy as np
from itertools import groupby
from operator import itemgetter

c = np.array([40, 41, 4, 5, 7, 20, 20, 8, 90, 99, 10, 5, 8, 8, 19])

bin1 = np.where((0 <= c) & (c <= 50))[0]
bin2 = np.where((50 < c) & (c <= 100))[0]

# 1 and 2 for the range names.
bins = [(bin1, 1), (bin2, 2)]
subbins = list()

for b in bins:
    data = b[0]
    name = b[1] # 1 or 2
    for k, g in groupby(enumerate(data), lambda ix : ix[0] - ix[1]):
        subbins.append((list(map(itemgetter(1), g)), name))

subbins = sorted(subbins, key=lambda x: x[0][0])

输出:[([0, 1, 2, 3, 4, 5, 6, 7], 1), ([8, 9], 2), ([10, 11, 12, 13, 14], 1)]
然后,你只需要做你想要的统计:)

关于python - 评估频率,持续时间和时间序列值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55119880/

10-11 03:55
查看更多