考虑一些给定的序列和窗口长度,比如list
a = [13 * i + 1 for i in range(24)]
(所以
In [61]: a
Out[61]:
[1,
14,
27,
40,
...,
287,
300]
)
和窗口长度3。
我想采用这个序列的滑动窗口总和,但周期性地;即,计算长度为24 list
:
[sum([1, 14, 27]),
sum([14, 27, 40]),
...,
sum([287, 300, 1]),
sum([300, 1, 14])]
使用collections.deque
和Stupid Lambda Tricks,我能想到的最好的是
d = collections.deque(range(24))
d.rotate(1)
map(lambda _: d.rotate(-1) or sum(a[i] for i in list(d)[: 3]), range(24))
有什么不那么可怕吗?
答案 0 :(得分:3)
简单的
怎么样?a = [13 * i + 1 for i in range(24)]
w = 3
aa = a + a[:w]
print([sum(aa[i:i+w]) for i in range(len(a))])
请注意,如果窗口很大,有更好的方法来计算O(n)
中的滑动窗口总和(即每个元素的时间恒定,与窗口大小无关)。
我们的想法是进行扫描转换"用开头的所有元素之和替换每个元素(这需要单次传递)。
之后,在O(1)中用
计算从x0到x1的元素之和sum_table[x1] - sum_table[x0]
在代码中:
sum_table = [0]
for v in a:
sum_table.append(sum_table[-1] + v)
for v in a[:w]:
sum_table.append(sum_table[-1] + v)
print([sum_table[i+w] - sum_table[i] for i in range(len(a))])
答案 1 :(得分:2)
如果您不介意使用itertools,这是一个解决方案:
from itertools import cycle, islice
a_one = islice(cycle(a), 1, None)
a_two = islice(cycle(a), 2, None)
sums = [sum(t) for t in zip(a, a_one, a_two)]
你也可以根据窗口长度为此写一个抽象:
wlen = 3
a_rotations = (islice(cycle(a), i, None) for i in range(1, wlen))
sums = [sum(t) for t in zip(a, *a_rotations)]
这是另一种解决方案,就窗口长度而言是更具可扩展性的解决方案:
alen = len(a)
wlen = 3
sums = [sum(a[:wlen])]
for i in range(alen - 1):
sums.append(sums[i] - a[i] + a[i + wlen - alen])
另一种解决方案,有效地结合了这两个想法并借用了Stefan Pochmann解决方案中的变量保存理念:
from itertools import islice, cycle
wlen = 3
rotatediterator = islice(cycle(a), wlen, None)
sums = []
lastsum = sum(a[:wlen])
for addval, subval in zip(rotatediterator, a):
sums.append(lastsum)
lastsum += addval - subval
答案 2 :(得分:2)
这适用于使用islice和sum的任何registered
:
> set.seed(4)
> Year2011 <- sample(Year, size = 1000, replace = TRUE)
> Year2011 <- as.integer(Year2011)
> set.seed(5)
> Year2012 <- sample(Year, size = 1000, replace = TRUE)
> Year2012 <- as.integer(Year2012)
> Temp <- rnorm(1000, mean = 20, sd = 5)
> a <- data.frame(Year2011, Year2012, Temp)
> #Add blank vector registered to dataset
> a$registered <- c(0)
> #Fit linear model for variable casual in training set
> mod <- lm(casual ~ ., data = b)
> #Predict variable casual and insert in test set
> a$casual <- predict(mod, a)
或者在python3中使用yield:
n
或使用modulo:
from itertools import cycle, islice
def rolling_window(func, l, n):
for i in xrange(len(l)):
yield func(islice(cycle(l), i, n+i))
print(list(rolling_window(sum,l,3)))
[42, 81, 120, 159, 198, 237, 276, 315, 354, 393, 432, 471, 510, 549, 588, 627, 666, 705, 744, 783, 822, 861, 588, 315]
答案 3 :(得分:2)
快速方式(至少对于大窗口尺寸):
sums = []
s = sum(a[:3])
for i, n in enumerate(a, 3-len(a)):
sums.append(s)
s += a[i] - n
类似,但使用itertools.accumulate
:
acc = list(accumulate([0] + a + a))
print([acc[i+3] - acc[i] for i in range(len(a))])
或者喜欢Shashank,但负面指数:
sums = [sum(a[:3])]
for i in range(-len(a), -1):
sums.append(sums[-1] - a[i] + a[i+3])
这是一个简短而基本的,再次使用负面指数:
[a[i] + a[i+1] + a[i+2] for i in range(-len(a), 0)]
答案 4 :(得分:2)
在每个连续点,添加新的(a[i]
)并减去旧的(a[i-3]
)。要回绕,您可以链接范围。
s = [sum(a[:3])]
for i in itertools.chain(range(3,len(a)), range(3)) :
s.append( s[-1] + a[i] - a[i-3] )
答案 5 :(得分:1)
您可以map
使用zip
功能:
>>> new=a+a[:2]
>>> new
[1, 14, 27, 40, 53, 66, 79, 92, 105, 118, 131, 144, 157, 170, 183, 196, 209, 222, 235, 248, 261, 274, 287, 300, 1, 14]
>>> map(sum,zip(new,new[1:],new[2:]))
[42, 81, 120, 159, 198, 237, 276, 315, 354, 393, 432, 471, 510, 549, 588, 627, 666, 705, 744, 783, 822, 861, 588, 315]
>>>
请注意,我们创建了new
,将a
的前2个元素连接到a
的末尾,zip(new,new[1:],new[2:])
会为您提供所需的子集,然后就可以了使用map
函数在其上应用sum
函数:
>>> zip(new,new[1:],new[2:])
[(1, 14, 27), (14, 27, 40), (27, 40, 53), (40, 53, 66), (53, 66, 79), (66, 79, 92), (79, 92, 105), (92, 105, 118), (105, 118, 131), (118, 131, 144), (131, 144, 157), (144, 157, 170), (157, 170, 183), (170, 183, 196), (183, 196, 209), (196, 209, 222), (209, 222, 235), (222, 235, 248), (235, 248, 261), (248, 261, 274), (261, 274, 287), (274, 287, 300), (287, 300, 1), (300, 1, 14)]