Kaggle competition: UCI-HAR
def lag_feature(df, lags, col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id', col+'_lag_'+str(i)]
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df<merge>
X_train = pd.read_csv('.. /input/uci-har/UCI HAR Dataset for Kaggle/UCI HAR Dataset for Kaggle/train/X_train.txt',delim_whitespace=True,header=None ).to_numpy()
y_train = pd.read_csv('.. /input/uci-har/UCI HAR Dataset for Kaggle/UCI HAR Dataset for Kaggle/train/y_train.txt',delim_whitespace=True,header=None ).to_numpy().reshape(-1)
 X_test = pd.read_csv('.. /input/uci-har/UCI HAR Dataset for Kaggle/UCI HAR Dataset for Kaggle/test/X_test.txt',delim_whitespace=True,header=None ).to_numpy()
%%time
group = matrix.groupby(['date_block_num'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num'], how='left')
 matrix['date_avg_item_cnt'] = matrix['date_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_avg_item_cnt')
 matrix.drop(['date_avg_item_cnt'], axis=1, inplace=True )<categorify>
logistic = LogisticRegression(C=2.0)
 start = time.time()
logistic.fit(X_train,y_train)
 end = time.time()
print("Time Required to train: {}".format(end-start))
start = time.time()
y_pred = logistic.predict(X_test)
 end = time.time()
print("Time Required to test: {}".format(end-start))
%%time
group = matrix.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix['date_item_avg_item_cnt'] = matrix['date_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3,6,12], 'date_item_avg_item_cnt')
 matrix.drop(['date_item_avg_item_cnt'], axis=1, inplace=True )<categorify>
submit = pd.read_csv('.. /input/uci-har/sample_submit.csv' )
<merge><EOS> 
for i in range(len(y_pred)) :
submit['Label'][i]=y_pred[i]
submit=submit.astype(np.int32)
 submit.to_csv('submit.csv', mode='w', header= True, index= False )