Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<define_search_space>
import pandas as pd
import numpy as np
from glob import glob
import shutil
bestparams = {0:(343, 0.33300775159479323),
1:(377, 0.3937832899637623),
2:(381, 0.43248276195861485),
3:(368, 0.4567765830007994),
4:(279, 0.4364333328398183),
5:(301, 0.3130615907253661)}<define_variables>
low_thr = 0.08
high_thr = 0.95
folds = KFold(n_splits=5, shuffle=True, random_state=42)
 oof_preds = np.zeros(( X.shape[0],6))
sub_preds = np.zeros(( X_test.shape[0],6))<train_model>
pred_14cls = pd.read_csv('.. /input/vinbigdata-14-class-submission-lb0154/submission.csv')
 pred_2cls = pd.read_csv('.. /input/vinbigdata-2class-prediction/2-cls test pred.csv' )
for i in [0, 1, 2, 3, 4, 5]:
optimal_rounds, best_cv_score = bestparams[i]
print(i, optimal_rounds, best_cv_score)
 for n_fold,(trn_idx, val_idx)in enumerate(folds.split(X)) :
print(n_fold)
 trn_x, trn_y = X[feats].iloc[trn_idx], x[trn_idx]>i
val_x, val_y = X[feats].iloc[val_idx], x[val_idx]>i
clf = lgbm.train(lgbm_params,
lgbm.Dataset(trn_x,trn_y),
num_boost_round = optimal_rounds + 1,
verbose_eval=200)
 oof_preds[val_idx,i] = clf.predict(val_x, num_iteration=optimal_rounds + 1)
 sub_preds[:,i] += clf.predict(X_test[feats], num_iteration=optimal_rounds + 1)/ folds.n_splits
del clf
del trn_x, trn_y, val_x, val_y
gc.collect()<concatenate>
pred = pd.merge(pred_14cls, pred_2cls, on = 'image_id', how = 'left')
 pred.head()
off_preds_withbias = np.hstack([oof_preds,np.ones(shape=(oof_preds.shape[0],1)) ])
 sub_preds_withbias = np.hstack([sub_preds,np.ones(shape=(sub_preds.shape[0],1)) ] )<compute_test_metric>
pred['PredictionString'].value_counts().iloc[[0]]
params = np.linalg.lstsq(off_preds_withbias, y.target,rcond=-1)[0]<compute_test_metric>
def filter_2cls(row, low_thr=low_thr, high_thr=high_thr):
prob = row['target']
if prob<low_thr:
row['PredictionString'] = '14 1 0 0 1 1'
elif low_thr<=prob<high_thr:
row['PredictionString']+=f' 14 {prob} 0 0 1 1'
elif high_thr<=prob:
row['PredictionString'] = row['PredictionString']
else:
raise ValueError('Prediction must be from [0-1]')
 return row
trainpreds = np.dot(off_preds_withbias,params)
 print(mean_absolute_error(y.target,trainpreds))<save_to_csv>
sub = pred.apply(filter_2cls, axis=1)
 sub.head()
testpreds = np.dot(sub_preds_withbias,params)
 sub = pd.DataFrame({'seg_id':submission.seg_id, 'time_to_failure':testpreds})
 sub.to_csv('cut.csv',index=False )<set_options>
sub['PredictionString'].value_counts().iloc[[0]]
<load_from_csv><EOS> 
sub[['image_id', 'PredictionString']].to_csv('submission.csv',index = False )