一、应用场景:实现把距离相对更近的样本聚为一类。
二、输入样本格式:不对样本对应经纬度。(经纬度为十进制)
三、实现过程:
1)、计算两样本间距离(使用公式为:haversine公式)
计算公式:
python实现代码:
from math import radians,cos,sin,asin,sqrt def haversine(lon1, lat1, lon2, lat2): # 经度1,纬度1,经度2,纬度2 (十进制度数) """ Calculate the great circle distance between two points on the earth (specified in decimal degrees) 计算两十进制经纬度之间的距离 """ # 将十进制度数转化为弧度 lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2]) # haversine公式 dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2 c = 2 * asin(sqrt(a)) r = 6371 # 地球平均半径,单位为公里 return c * r * 1000
2)、通过层次聚类算法对不同样本进行聚类
掉用了Python包:scipy.cluster.hierarchy
Python代码实现:
import scipy.cluster.hierarchy as sch #进行层次聚类: Z=sch.linkage(dist,method='average') #根据多大的距离进行分类 cluster= sch.fcluster(Z, t=500,criterion='distance')>、dist:为两两样本间的距离,例如有3个样本,则顺序为(d12,d13,d23)