Kaggle competition: Santander Customer Transaction Prediction
backbone = backbones.resnet_fpn.resnext50_32x4d(pretrained=False)
 model = faster_rcnn.model(backbone=backbone, num_classes=len(class_map))
checkpoint_path = '.. /input/vinbigdata-eda-faster-rcnn-icevision-training/frcnn-best-model-fold0-epoch=58.ckpt'
checkpoint = torch.load(checkpoint_path, map_location=torch.device(DEVICE))
new_state_dict = OrderedDict()
for key, value in checkpoint['state_dict'].items() :
if key.startswith('model.'):
new_key = key[6:]
new_state_dict[new_key] = value
else:
new_state_dict[key] = value
model.load_state_dict(new_state_dict, strict=False )<predict_on_test>
%matplotlib inline
infer_dl = faster_rcnn.infer_dl(infer_ds, batch_size=1, shuffle=False)
 _, preds = faster_rcnn.predict_dl(model=model, infer_dl=infer_dl, detection_threshold=0.5 )<categorify>
train_df=pd.read_csv(".. /input/train.csv")
 test_df=pd.read_csv(".. /input/test.csv")
 sample_df=pd.read_csv(".. /input/sample_submission.csv" )
for idx, pred in tqdm(enumerate(preds)) :
if len(pred['labels'])== 0:
continue
image_idx = dict(test_rs[idx])['imageid']
image_id = submission_df.iloc[image_idx]['image_id']
original_width, original_height = test_df.loc[test_df['image_id'] == image_id][['width', 'height']].values[0]
s = ''
for label, score, bbox in zip(pred['labels'], pred['scores'], pred['bboxes']):
bbox = vars(bbox)
 xmin = int(bbox['xmin']*(original_width/size))
ymin = int(bbox['ymin']*(original_height/size))
xmax = int(bbox['xmax']*(original_width/size))
ymax = int(bbox['ymax']*(original_height/size))
values_list = [label, np.round(score, 2), xmin, ymin, xmax, ymax]
s += ' '.join(map(str, values_list))
s += ' '
submission_df.iloc[image_idx]['PredictionString'] = s.strip()<save_to_csv>
train_df.target.value_counts()
submission_df.to_csv('submission.csv', index=False )<import_modules>
null_data = train_df[train_df.isnull().any(axis=1)]
null_data
os.environ['KMP_DUPLICATE_LIB_OK']='True'
curdoc().theme = 'light_minimal'
output_notebook()
PATH = "/kaggle/input/competitive-data-science-predict-future-sales/"<load_from_csv>
upper = train_df_corr.abs().where(np.triu(np.ones(train_df_corr.abs().shape), k=1 ).astype(np.bool))
correlated = [column for column in upper.columns if any(upper[column] > 0.2)]
correlated
items_df = pd.read_csv(f"{PATH}items.csv")
 shops_df = pd.read_csv(f"{PATH}shops.csv")
 sales_train_df = pd.read_csv(f"{PATH}sales_train.csv")
 test_df = pd.read_csv(f"{PATH}test.csv")
 sample_submission_df = pd.read_csv(f"{PATH}sample_submission.csv")
 item_categories_df = pd.read_csv(f"{PATH}item_categories.csv" )<feature_engineering>
param = {
'num_leaves': 10,
'max_bin': 119,
'min_data_in_leaf': 11,
'learning_rate': 0.02,
'min_sum_hessian_in_leaf': 0.00245,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': 0.05,
'lambda_l1': 4.972,
'lambda_l2': 2.276,
'min_gain_to_split': 0.65,
'max_depth': 14,
'save_binary': True,
'seed': 1337,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False,
}
sales_train_df.loc[sales_train_df.shop_id == 0, 'shop_id'] = 57
test_df.loc[test_df.shop_id == 0, 'shop_id'] = 57
sales_train_df.loc[sales_train_df.shop_id == 1, 'shop_id'] = 58
test_df.loc[test_df.shop_id == 1, 'shop_id'] = 58
sales_train_df.loc[sales_train_df.shop_id == 10, 'shop_id'] = 11
test_df.loc[test_df.shop_id == 10, 'shop_id'] = 11<feature_engineering>
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']
item_to_category = dict(zip(items_df.item_id, items_df.item_category_id))
sales_train_df["item_category_id"] = sales_train_df["item_id"].apply(lambda x: item_to_category[x])
 sales_train_df.head()<data_type_conversions>
%%time
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=2019)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
feature_importance_df = pd.DataFrame()
start = time.time()
for fold_,(trn_idx, val_idx)in enumerate(skf.split(train_df.values, target.values)) :
print("fold n°{}".format(fold_))
trn_data = lgb.Dataset(train_df.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train_df.iloc[val_idx][features], label=target.iloc[val_idx])
 num_round = 10000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 100)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][features], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(test_df[features], num_iteration=clf.best_iteration)/ 5
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )
<groupby><EOS> 
sub_df = pd.DataFrame({"ID_code":test_df["ID_code"].values})
 sub_df["target"] = predictions
sub_df.to_csv("lgb_submission.csv", index=False )