今天来看一个回归问题——Kaggle竞赛Bike Sharing Demand,根据日期时间、天气、温度等特征,预测自行车的租借量。训练与测试数据集大概长这样:python
// train datetime,season,holiday,workingday,weather,temp,atemp,humidity,windspeed,casual,registered,count 2011-01-01 00:00:00,1,0,0,1,9.84,14.395,81,0,3,13,16 2011-01-01 01:00:00,1,0,0,1,9.02,13.635,80,0,8,32,40 // test datetime,season,holiday,workingday,weather,temp,atemp,humidity,windspeed 2011-01-20 00:00:00,1,0,1,1,10.66,11.365,56,26.0027 2011-01-20 01:00:00,1,0,1,1,10.66,13.635,56,
观察上面的数据,咱们能够发现:租借量等于注册用户租借量加上未注册用户租借量,即casual
+ registered
。评价指标是loss函数RMSLE (Root Mean Squared Logarithmic Error):函数
\[ \sqrt{\frac{1}{n} \sum_{i=1}^n (\log (p_i +1) - \log (a_i+1))^2 } \]测试
其中,\(p_i\)为预测的租借量,\(a_i\)为实际的租借量,\(n\)为样本数。实际上,RMSLE就是一个偏差函数。spa
日期时间放在一个string字段里,咱们须要解析出年、月、weekday、小时等,之因此没有选择天作特征,是由于weekday更具备周期性、表明性。code
import pandas as pd train = pd.read_csv("data/train.csv", parse_dates=[0], date_parser=lambda d: pd.datetime.strptime(d, '%Y-%m-%d %H:%M:%S')) train['year'] = train['datetime'].map(lambda d: d.year) train['month'] = train['datetime'].map(lambda d: d.month) train['hour'] = train['datetime'].map(lambda d: d.hour) train['weekday'] = train['datetime'].map(lambda d: d.weekday()) train['day'] = train['datetime'].map(lambda d: d.day)
为了方便计算,咱们categorical化部分特征:get
df['weather'] = df['weather'].astype('category') df['holiday'] = df['holiday'].astype('category') df['workingday'] = df['workingday'].astype('category') df['season'] = df['season'].astype('category') df['hour'] = df['hour'].astype('category')
选用的特征以下:string
features = ['season', 'holiday', 'workingday', 'weather', 'temp', 'atemp', 'humidity', 'windspeed', 'time', 'weekday', 'year']
之因此丢掉了month特征,是由于发现有过拟合。pandas
选用GBM来作回归,参数是经过grid_search挑出来的:it
booster = ensemble.GradientBoostingRegressor(n_estimators=500) param_grid = {'learning_rate': [0.1, 0.05, 0.01], 'max_depth': [10, 15, 20], 'min_samples_leaf': [3, 5, 10, 20], } gs_cv = GridSearchCV(booster, param_grid, n_jobs=4).fit(training[features], training['log-count']) # best hyperparameter setting print(gs_cv.best_params_) # {'learning_rate': 0.05, 'max_depth': 10, 'min_samples_leaf': 20}
该方法的RMSLE为0.43789。前面提到了租借量为casual
+ registered
之和,那么咱们能够把这二者看作类别,分别用GBM进行预测,而后相加后获得结果。结果的确将RMSLE下降到了0.41983。table
前面只用到了一种回归方法,那能不能将GBM与RF的结果合到一块儿呢?答案是能够的,经过赋权值0.5(即平均ensemble)的方式将两个结果组合起来,RMSLE下降到了0.37022。评价指标结果对好比下:
特征 | 回归 | RMSLE |
---|---|---|
+年、星期、小时 | GBM | 0.43789 |
GBM + GBM | 0.41983 | |
GBM RF ensemble | 0.37022 |
结论:ensemble真是个好方法,三个臭皮匠胜过诸葛亮。
[1] 阿波, kaggle上的自行车出租数量预测.
[2] Damien RJ, Forecasting Bike Sharing Demand.