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


Python MeanShift.score_samples方法代码示例

本文整理汇总了Python中sklearn.cluster.MeanShift.score_samples方法的典型用法代码示例。如果您正苦于以下问题:Python MeanShift.score_samples方法的具体用法?Python MeanShift.score_samples怎么用?Python MeanShift.score_samples使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.cluster.MeanShift的用法示例。


在下文中一共展示了MeanShift.score_samples方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: analysis

# 需要导入模块: from sklearn.cluster import MeanShift [as 别名]
# 或者: from sklearn.cluster.MeanShift import score_samples [as 别名]
def analysis(lon, lat, day=None, hour=None, distance=1, method=1):
    global LOAD_TAXI_DF, taxi_df

    if not LOAD_TAXI_DF:
        filename = file_path('nyc_data.csv')
        taxi_df = pd.read_csv(filename, parse_dates=['pickup_datetime', 'dropoff_datetime'])
        LOAD_TAXI_DF = True

    columns = ['medallion', 'pickup_datetime', 'dropoff_datetime',
                'pickup_latitude', 'pickup_longitude',
                'dropoff_latitude', 'dropoff_longitude']
    taxi_df = taxi_df[columns]

    target_lat, target_lon = float(lat) , float(lon)
    target_day = datetime.now().weekday() if not day else int(day)
    target_hour = datetime.now().hour if not hour else int(hour)

    width = 0.010 * float(distance)

    lat_max, lon_max = target_lat + width, target_lon - width
    lat_min, lon_min = target_lat - width, target_lon + width 

    hour_day_df = taxi_df[ (taxi_df.pickup_datetime.dt.hour==target_hour) & (taxi_df.pickup_datetime.dt.day==target_day)][['pickup_latitude', 'pickup_longitude']]
    hour_day_df = hour_day_df[(hour_day_df['pickup_latitude'] > lat_min) & (hour_day_df['pickup_latitude'] < lat_max)]
    hour_day_df = hour_day_df[(hour_day_df['pickup_longitude'] < lon_min) & (hour_day_df['pickup_longitude'] > lon_max)]

    if method == 'MeanShift':
        clf = MeanShift().fit(hour_day_df.values)
        centers = clf.cluster_centers_
    elif method == 'KernelDensity':
        clf = KernelDensity(kernel='gaussian', bandwidth=0.005).fit(hour_day_df.values)
        score = clf.score_samples(hour_day_df.values)
        index_score = np.argsort(score)
        centers = hour_day_df[index_score < 20].values
    else:
        clf = AffinityPropagation().fit(hour_day_df.values)
        centers = clf.cluster_centers_

    markers = [{'lon':str(center[0]), 'lat':str(center[1]) } for center in centers]
    return markers
开发者ID:brenden17,项目名称:NYC-taxi,代码行数:42,代码来源:views.py


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