我正在开发一个使用马尔可夫链的应用程序.
此代码的示例如下:
chain = MarkovChain(order=1)
train_seq = ["","hello","this","is","a","beautiful","world"]
for i, word in enum(train_seq):
chain.train(previous_state=train_seq[i-1],next_state=word)
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是迭代train_seq,但保留N个最后元素.
for states in unknown(train_seq,order=1):
# states should be a list of states, with states[-1] the newest word,
# and states[:-1] should be the previous occurrences of the iteration.
chain.train(*states)
Run Code Online (Sandbox Code Playgroud)
希望我的问题的描述足够明确
window会一次给你一些n物品iterable.
from collections import deque
def window(iterable, n=3):
it = iter(iterable)
d = deque(maxlen = n)
for elem in it:
d.append(elem)
yield tuple(d)
print [x for x in window([1, 2, 3, 4, 5])]
# [(1,), (1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)]
Run Code Online (Sandbox Code Playgroud)
如果你想要前几次相同数量的物品,
from collections import deque
from itertools import islice
def window(iterable, n=3):
it = iter(iterable)
d = deque((next(it) for Null in range(n-1)), n)
for elem in it:
d.append(elem)
yield tuple(d)
print [x for x in window([1, 2, 3, 4, 5])]
Run Code Online (Sandbox Code Playgroud)
会这样做的.
| 归档时间: |
|
| 查看次数: |
403 次 |
| 最近记录: |