HOMEWORK 8 Taxis

Recording
Slides
Type
HW
完整的实战项目,预测出租车到达时间的。
 

特征工程技巧

如果和日期相关,可以通过观察不同日期的分布来移除到非常规日期的数据。
日期相关的特征:
def augment(t): """Augment a dataframe t with additional columns.""" u = t.copy() pickup_time = pd.to_datetime(t['pickup_datetime']) u.loc[:, 'hour'] = pickup_time.dt.hour u.loc[:, 'day'] = pickup_time.dt.weekday u.loc[:, 'weekend'] = (pickup_time.dt.weekday >= 5).astype(int) u.loc[:, 'period'] = np.digitize(pickup_time.dt.hour, [0, 6, 18]) u.loc[:, 'speed'] = speed(t) return u
位置划分的特征
对位置左边进行pca之后,按照pca第一维度的数值进行划分为3个区间。
注意,这里只能用train来计算pca_means ,因为实际工程上,test是根据train的数据来的。
# Find the first principal component D = train[["pickup_lon", "pickup_lat"]].values pca_means = np.mean(D, axis=0) X = D - pca_means u, s, vt = np.linalg.svd(X, full_matrices=False) def add_region(t): """Add a region column to t based on vt above.""" D = t[["pickup_lon", "pickup_lat"]].values assert D.shape[0] == t.shape[0], 'You set D using the incorrect table' # Always use the same data transformation used to compute vt X = D - pca_means first_pc = np.dot(X, vt.T[:, 0]) t.loc[:,'region'] = pd.qcut(first_pc, 3, labels=[0, 1, 2]) add_region(train) add_region(test)
通过划分,得到下图所示的三个区域
notion imagenotion image
对于不同时段中,某项数据的分布不相同的情况,可以手动的根据经验划分多个段,然后这个段就是一个新的特征。比如本题中,不同时段的速度分布不一致,所以手动的给时间划分了三个段,分别为早上,白天,晚上。另外,也可以给地点划分多个段。