本文整理汇总了Python中hmm.HMM.from_events方法的典型用法代码示例。如果您正苦于以下问题:Python HMM.from_events方法的具体用法?Python HMM.from_events怎么用?Python HMM.from_events使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hmm.HMM
的用法示例。
在下文中一共展示了HMM.from_events方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: predict_crimes
# 需要导入模块: from hmm import HMM [as 别名]
# 或者: from hmm.HMM import from_events [as 别名]
def predict_crimes(cls, crimes, limit=16):
# Sort crimes in chronological order
crimes.sort(key=lambda crime: crime.date)
# Find the average duration between crimes
deltas = [later.date - now.date for now, later in zip(crimes, crimes[1:])]
delta = sum(deltas, timedelta()) / len(deltas)
# Only allow a granularity of 1 hour
delta = max(delta, timedelta(hours=1))
# Create a timeline that marks each of the given events, and also
# includes empty non-events at regular intervals (determined by `delta`)
# between the first and last timestamp
time, stop = crimes[0].date, crimes[-1].date
timeline = {crime.date : crime for crime in crimes}
while time <= stop:
timeline.setdefault(time, None)
time += delta
# Convert the padded timeline back to an ordered time sequence of events
events = [timeline[k] for k in sorted(timeline.keys())]
# Create an HMM to predict the regions in which crimes will take place
regions = [e.region if e is not None else None for e in events]
regions = iter(HMM.from_events(regions))
# Create a separate HMM for the crimes' descriptions
descs = [e.description for e in events if e is not None]
descs = iter(HMM.from_events(descs))
future = []
while len(future) < limit:
time += delta
region = next(regions)
if region is not None:
future.append({
'date': time.strftime(cls.DATE_FMT),
'region': region.to_dict(),
'description': next(descs)
})
return future