当前位置: 首页>>代码示例>>Python>>正文


Python HMM.from_events方法代码示例

本文整理汇总了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
开发者ID:kebertx,项目名称:crimgeoprofile,代码行数:43,代码来源:crime.py


注:本文中的hmm.HMM.from_events方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。