数据背景
数据前处理
1、数据类型归类->减少内存占用
if df[cols[i]].min() > np.iinfo(np.int8).min and df[cols[i]].max() < np.iinfo(np.int8).max:df[cols[i]] = df[cols[i]].astype(np.int8)
2、信息整合->合并三张表单
pd.merge(df, calendar, on='d', how='left')
3、数据探索->统计性指标:不同区域、不同产品价格均值、时间热力图
df.groupby(['store_id','cat_id','item_id'],as_index=False)['sell_price'].mean().dropna()
特征工程
1、category encoding
for i,type in enumerate(types):if type.name == 'category':df[cols[i]] = df[cols[i]].cat.codes
2、特征构建->频数特征、过去一段时间统计特征
df['iteam_sold_avg'] = df.groupby('item_id')['sold'].transform('mean').astype(np.float16)
df['rolling_sold_mean'] = df.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform(lambda x: x.rolling(window=7).mean()).astype(np.float16)
建立模型
df.to_pickle('data.pkl')
del df
gc.collect();
data = pd.read_pickle('data.pkl')model = LGBMRegressor(n_estimators=1000,learning_rate=0.3,subsample=0.8,colsample_bytree=0.8,max_depth=8,num_leaves=50,min_child_weight=300)
参考文献
https://www.kaggle.com/anshuls235/time-series-forecasting-eda-fe-modelling