from scipy.spatial.distance import cdist
lonlat1=market[[‘lat’,‘lng’]]
lonlat2=df[[‘lat’,‘lng’]]
distance_tt=cdist(lonlat1,lonlat2,metric=‘euclidean’)
distance_tt
#批发市场距离各门店的距离 单位:百公里
dibet=pd.DataFrame(distance_tt)
market[‘500米内门店数量’]=dibet.applymap(lambda x: x<0.005).apply(sum,axis=1)
mar_count=market.sort_values(by=‘500米内门店数量’,ascending=False)
#在批发市场500米内的门店数量
mar_count[‘500米内门店数量’].sum()/df.shape[0]
‘0.3764705882352941’
#有近40%的便利店聚集的批发市场附近!异常!
dibet.applymap(lambda x: x<0.005)
df.总流水
copu=dibet.applymap(lambda x: x<0.01).values*(df.总流水.values)
copu
money=pd.DataFrame(copu)
money[‘聚合流水’]=money.apply(sum,axis=1)
money
mar_count[‘流水’]=money[‘聚合流水’]
from folium.plugins import HeatMap
#绘制热力图
num=df.shape[0]
arr=[[df.lat[i],df.lng[i],df[‘总流水’][i]] for i in range(num)]
heat_map = folium.Map(location=[30.381241,120.114774], tiles=‘OpenStreetMap’,zoom_start=50, control_scale=True)
HeatMap(
data=arr
,radius=35
# ,gradient={0.4: ‘blue’, 0.65: ‘lime’, 1: ‘red’}
).add_to(heat_map)
#绘制聚合图
incidents = folium.plugins.MarkerCluster().add_to(heat_map)
arr2=[[df.lat[i],df.lng[i],df[‘门店名称’][i],df[‘总流水’][i]] for i in range(num)]
for i in arr2:
incidents.add_child(
folium.Marker(
location=(i[0],i[1])
,tooltip=(i[2],i[3])
)
).add_to(heat_map)
for name,row in mar_count.iterrows():
heat_map.add_child(
folium.Marker(
(row[“lat”], row[“lng”]),
popup=row[‘名称’],
icon=folium.Icon(icon_color=‘red’)
)
)
#输出
heat_map.save(r’heatmap_cluster_ms_addmarkets.html’)