社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python DBSCAN可以将质心数据合并回dataframe

Ani • 6 年前 • 2292 次点击  

嗨,我正试图找到多个坐标的中心点,然后将得到的数据连接回主数据集,这就是我目前所掌握的

样本数据
coords1=pd.DataFrame({'picku纬度':[12.807895,12.82166,12.821675,12.821675,12.82168,12.821697,12.8217,12.8217,12.821718,12.821722,12.821722,12.821751,12.821771,12.821782,12.821782,12.821794,12.8218218218,12.821878,12.821873,12.821892,12.821892,12.821929,12.821921921929,12.821935,12.821935,12.821947,12.821947,12.821947,12.821979,12.821927,12.82.658981,77.660594,77.660634,77.657854,77.657992,77.659848,77.660243,77.659244,77.658826,77.660763,77.660614,77.659569,77.660678,77.659861,77.660629,77.660488,77.660537,77.657746,77.66077]})

import pandas as pd, numpy as np, matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from geopy.distance import great_circle
from shapely.geometry import MultiPoint


coords1= Lat_Long_pick[['pickup_latitude', 'pickup_longitude']]


coords = Lat_Long_pick.as_matrix(columns=['pickup_latitude', 'pickup_longitude'])

ms_per_radian = 6371.0088
epsilon = 0.00001 
db = DBSCAN(eps=epsilon, min_samples=1, algorithm='ball_tree', metric='haversine').fit(np.radians(coords))
cluster_labels = db.labels_
num_clusters = len(set(cluster_labels))
clusters = pd.Series([coords[cluster_labels == n] for n in range(num_clusters)])
print('Number of clusters: {}'.format(num_clusters))


def get_centermost_point(cluster):
    centroid = (MultiPoint(cluster).centroid.x, MultiPoint(cluster).centroid.y)
    centermost_point = min(cluster, key=lambda point: great_circle(point, centroid).m)
    return tuple(centermost_point)

centermost_points = clusters.map(get_centermost_point)


lats, lons = zip(*centermost_points)
rep_points = pd.DataFrame({'lon':lons, 'lat':lats})

rep_points.tail()


rs = rep_points.apply(lambda row: Lat_Long_pick[(Lat_Long_pick['pickup_latitude']==row['lat'])&(Lat_Long_pick['pickup_longitude']==row['lon'])].iloc[0], axis=1)

我现在如何加入rs回到Latúu Longúu pick或coords1

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50476
文章 [ 1 ]  |  最新文章 6 年前