摩拜单车数据分析项目
有目标的去分析。
一般地理geohash 7 用的比较多 一般来说是长150米,宽110米
1.GeoHash将二维的经纬度转换成字符串,比如下图展示了北京9个区域的GeoHash字符串,分别是WX4ER,WX4G2、WX4G3等等,每一个字符串代表了某一矩形区域。也就是说,这个矩形区域内所有的点(经纬度坐标)都共享相同的GeoHash字符串,这样既可以保护隐私(只表示大概区域位置而不是具体的点),又比较容易做缓存,比如左上角这个区域内的用户不断发送位置信息请求餐馆数据,由于这些用户的GeoHash字符串都是WX4ER,所以可以把WX4ER当作key,把该区域的餐馆信息当作value来进行缓存,而如果不使用GeoHash的话,由于区域内的用户传来的经纬度是各不相同的,很难做缓存。
2.字符串越长,表示的范围越精确。如图所示,5位的编码能表示10平方千米范围的矩形区域,而6位编码能表示更精细的区域(约0.34平方千米)
3.字符串相似的表示距离相近,这样可以利用字符串的前缀匹配来查询附近的POI信息
有项目目标然后去分析。
train=train.sample(frac=0.5) #吧原数据集随机%50 进行抽样
s.weekday() #Monday is 0 and Sunday is 6
s.hour #取出小时
df["day"]=df["starttime"].apply(lambda s:srt(s)["10"]) #输出的格式的格式是2017-10-23
def processdata(df):
#time
df["weekday"]=df["starttime"].apply(lambda s: s.weekday())
df["hour"]=df["starttime"].apply(lambda s: s.hour())
df["day"]=df["starttime"].apply(lambda s:srt(s)["10"])
print("time process sucessful")
#geohash
df["star_loc_point"]=df["geohashed_start_loc"].apply(lambda s:geohash.decode(s))
df["end_loc_point"]=df["geohashed_end_loc"].apply(lambda s:geohash.decode(s))
df["start_neigh"]=df["geohashed_start_loc"].apply(lambda s:geohash.neighbors(s))
df["star_loc_point_g6"]=df["geohashed_start_loc"].apply(lambda s:s[:6])
df["end_loc_point_g6"]=df["geohashed_end_loc"].apply(lambda s:s[:6])
df["star_neighbor_g6"]=df["star_loc_point_g6"].apply(lambda s:geohash.neighbors(s))
#判断目的地是否在neihbors
def ingeobashneg(start_geohash,end_geohash,names):
names.append(start_geohash)
if end_geohash in names:
return 1
else:
return 0
#判断geohash6
df["inside"]=df.apply(lambda s:ingeobashneg(s["start_geohash"],s["end_geohash"],s["start_neigh"]),axis=1)
df["inside_6"]=df.apply(lambda s:ingeobashneg(s["star_loc_point_g6"],s["end_loc_point_g6"],s["star_neighbor_g6"]),axis=1)
print("geohash sucessful")
#通过经纬度计算距离的函数
def haversine(lon1, lat1, lon2, lat2):
"""
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
df["start_end_distance"] = df.apply(lambda s : haversine(s['start_lat_lng'][1],s['start_lat_lng'][0],s['end_lat_lng'][1],s['end_lat_lng'][0]),axis = 1)
print("Distance process successfully!!!")
return df
## 不同时间骑行的距离是否不一样呢
hour_group=train.groupby("hour")
hour_distance=hour_group["start_end_distance"].agg(np.mean).reset_index()
sns.barplot(x='hour',y='start_end_distance',data=hour_distance)