Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<choose_model_class>
%matplotlib inline
print(os.listdir(".. /input"))
callbacks = [
EarlyStopping(monitor = 'loss', patience = 6),
ReduceLROnPlateau(monitor = 'loss', patience = 4)
 ]<train_model>
def dprint(*args, **kwargs):
print("[{}] ".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) + \
" ".join(map(str,args)) , **kwargs)
 id_name = 'Id'
target_name = 'Target'
model.fit(X_train_final,y_train_final,
batch_size = 64,
epochs = 100,
verbose = 1,
validation_data =(X_val,y_val),
callbacks = callbacks )<compute_test_metric>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
score = model.evaluate(X_val,y_val,verbose = 0)
 print('The loss on validation set is {0} and the accuracy is {1}'.format(round(score[0],3),round(score[1],3)) )<predict_on_test>
train['is_test'] = 0
test['is_test'] = 1
df_all = pd.concat([train, test], axis=0)
results = model.predict(X_test_normalised)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name='Label')
<save_to_csv>
dprint('Clean features...')
 cols = ['dependency']
for c in tqdm(cols):
x = df_all[c].values
strs = []
for i, v in enumerate(x):
try:
val = float(v)
 except:
strs.append(v)
 val = np.nan
x[i] = val
strs = np.unique(strs)
 for s in strs:
df_all[c + '_' + s] = df_all[c].apply(lambda x: 1 if x == s else 0)
 df_all[c] = x
df_all[c] = df_all[c].astype(float)
 dprint("Done." )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_result.csv",index=False )<load_from_csv>
dprint("Extracting features...")
 def extract_features(df):
df['bedrooms_to_rooms'] = df['bedrooms']/df['rooms']
df['rent_to_rooms'] = df['v2a1']/df['rooms']
df['rent_to_bedrooms'] = df['v2a1']/df['bedrooms']
df['tamhog_to_rooms'] = df['tamhog']/df['rooms']
df['tamhog_to_bedrooms'] = df['tamhog']/df['bedrooms']
df['r4t3_to_tamhog'] = df['r4t3']/df['tamhog']
df['r4t3_to_rooms'] = df['r4t3']/df['rooms']
df['r4t3_to_bedrooms'] = df['r4t3']/df['bedrooms']
df['rent_to_r4t3'] = df['v2a1']/df['r4t3']
df['v2a1_to_r4t3'] = df['v2a1']/(df['r4t3'] - df['r4t1'])
 df['hhsize_to_rooms'] = df['hhsize']/df['rooms']
df['hhsize_to_bedrooms'] = df['hhsize']/df['bedrooms']
df['rent_to_hhsize'] = df['v2a1']/df['hhsize']
df['qmobilephone_to_r4t3'] = df['qmobilephone']/df['r4t3']
df['qmobilephone_to_v18q1'] = df['qmobilephone']/df['v18q1']
extract_features(train)
 extract_features(test)
 dprint("Done." )
CFG = {
'model_arch': 'tf_efficientnet_b5_ns',
'img_size': 456,
'valid_bs': 32,
'num_workers': 4,
'device': 'cuda:0',
'tta': 3,
'used_epochs': [3,11,9,9,5],
'weights': [1,1,1,1,1]
}
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()<choose_model_class>
def encode_data(df):
yes_no_map = {'no': 0, 'yes': 1}
df['dependency'] = df['dependency'].replace(yes_no_map ).astype(np.float32)
 df['edjefe'] = df['edjefe'].replace(yes_no_map ).astype(np.float32)
 df['edjefa'] = df['edjefa'].replace(yes_no_map ).astype(np.float32)
 df['idhogar'] = LabelEncoder().fit_transform(df['idhogar'])
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x<create_dataframe>
dprint("Encoding Data.... ")
 encode_data(train)
 encode_data(test)
 dprint("Done..." )
test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
test_ds = CassavaDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_inference_transforms() , output_label=False)
 tst_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 device = torch.device(CFG['device'])
 model = CassvaImgClassifier(CFG['model_arch'], 5 ).to(device )<load_pretrained>
def do_features(df):
feats_div = [('children_fraction', 'r4t1', 'r4t3'), 
('working_man_fraction', 'r4h2', 'r4t3'), 
('all_man_fraction', 'r4h3', 'r4t3'), 
('human_density', 'tamviv', 'rooms'), 
('human_bed_density', 'tamviv', 'bedrooms'), 
('rent_per_person', 'v2a1', 'r4t3'), 
('rent_per_room', 'v2a1', 'rooms'), 
('mobile_density', 'qmobilephone', 'r4t3'), 
('tablet_density', 'v18q1', 'r4t3'), 
('mobile_adult_density', 'qmobilephone', 'r4t2'), 
('tablet_adult_density', 'v18q1', 'r4t2'),
]
feats_sub = [('people_not_living', 'tamhog', 'tamviv'), 
('people_weird_stat', 'tamhog', 'r4t3')]
for f_new, f1, f2 in feats_div:
df['fe_' + f_new] =(df[f1] / df[f2] ).astype(np.float32)
 for f_new, f1, f2 in feats_sub:
df['fe_' + f_new] =(df[f1] - df[f2] ).astype(np.float32)
 aggs_num = {'age': ['min', 'max', 'mean'],
'escolari': ['min', 'max', 'mean']
}
aggs_cat = {'dis': ['mean']}
for s_ in ['estadocivil', 'parentesco', 'instlevel']:
for f_ in [f_ for f_ in df.columns if f_.startswith(s_)]:
aggs_cat[f_] = ['mean', 'count']
for name_, df_ in [('18', df.query('age >= 18')) ]:
df_agg = df_.groupby('idhogar' ).agg({**aggs_num, **aggs_cat} ).astype(np.float32)
 df_agg.columns = pd.Index(['agg' + name_ + '_' + e[0] + "_" + e[1].upper() for e in df_agg.columns.tolist() ])
 df = df.join(df_agg, how='left', on='idhogar')
 del df_agg
df.drop([f_ for f_ in df.columns if f_.startswith('SQB')or f_ == 'agesq'], axis=1, inplace=True)
 df.drop(['Id', 'idhogar'], axis=1, inplace=True)
 df.drop(['hhsize', 'female', 'area2'], axis=1, inplace=True)
 return df
val_preds = []
tst_preds = []
for fold, epoch in enumerate(CFG['used_epochs']):
model.load_state_dict(torch.load('.. /input/b5-weight/{}_fold_{}_{}'.format(CFG['model_arch'], fold, epoch)))
 with torch.no_grad() :
for _ in range(CFG['tta']):
tst_preds += [CFG['weights'][fold]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, tst_loader, device)]
tst_preds = np.sum(tst_preds, axis=0)
 del model
torch.cuda.empty_cache()<save_to_csv>
dprint("Do_feature Engineering.... ")
 train = do_features(train)
 test = do_features(test)
 dprint("Done.... " )
test['label'] = np.argmax(tst_preds, axis=1)
 test.to_csv('submission.csv', index=False)
 test.head()<set_options>
dprint("Fill Na value.... ")
 train = train.fillna(0)
 test = test.fillna(0)
 dprint("Done.... " )
warnings.filterwarnings("ignore")
 enable_pandas_profilling = False
enable_tpot = False<load_from_csv>
cols_to_drop = [
id_name,
target_name,
]
X = train.drop(cols_to_drop, axis=1, errors='ignore')
 y = train[target_name].values
df_features = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/features.csv.zip')
 df_train = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/train.csv.zip')
 df_stores = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/stores.csv')
 df_test = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/test.csv.zip')
 df_sample_submission = pd.read_csv('.. /input/walmart-recruiting-store-sales-forecasting/sampleSubmission.csv.zip' )<merge>
gc.collect()
df_feat_stores = df_features.merge(df_stores, how='inner', on='Store' )<create_dataframe>
def get_lgb_model() :
lgb_model = lgb.LGBMClassifier(max_depth=-1, learning_rate=0.2, objective='multiclass',
silent=True, metric='None',
n_jobs=4, n_estimators=5000, class_weight='balanced',
colsample_bytree= 0.93,
min_child_samples= 56, num_leaves= 19,
subsample= 0.84)
 return lgb_model
pd.DataFrame({'Type_Feat_Store': df_feat_stores.dtypes,'Type_Train': df_train.dtypes, 'Type_Test': df_test.dtypes} )<data_type_conversions>
dprint("Prepared Model.....")
 lgb_model = get_lgb_model()
lgb_model.fit(X, y)
 dprint("Done Model.....")
 dprint("Predict Test Value.....")
 target_lgb = lgb_model.predict(test)
 dprint("Done Prediction....." )
df_feat_stores.Date = pd.to_datetime(df_feat_stores.Date)
 df_train.Date = pd.to_datetime(df_train.Date)
 df_test.Date = pd.to_datetime(df_test.Date )<count_unique_values>
len(df_feat_stores.Date.unique())/52<feature_engineering>
sub = pd.read_csv(".. /input/sample_submission.csv" )
df_feat_stores['Week'] = df_feat_stores.Date.dt.week
df_feat_stores['Year'] = df_feat_stores.Date.dt.year<merge>
sub['Target'] = target_lgb
<drop_column><EOS> 
sub.to_csv("cat_boost.csv", index= False )