Kaggle competition: Costa Rican Household Poverty Level Prediction
fastai.__version__<define_variables>
from sklearn.metrics import accuracy_score
BATCH = 32
SIZE = 250<set_options>
df = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/train.csv')
 test = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/test.csv')
 df.shape, test.shape
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()<define_variables>
df_all = df.append(test)
 df_all.shape
path = Path('.. /input/imet-2019-fgvc6/' )<load_pretrained>
df_all['edjefa'].value_counts()
Path('models' ).mkdir(exist_ok=True)
 !cp '.. /input/resnet15216epochs/stage-1.pth' 'models/resnet152.pth'
def load_url(*args, **kwargs):
model_dir = Path('models')
 filename = 'resnet152.pth'
if not(model_dir/filename ).is_file() : raise FileNotFoundError
return torch.load(model_dir/filename)
 model_zoo.load_url = load_url<load_from_csv>
df_all['edjefe'].value_counts()
train_df = pd.read_csv(path/'train.csv')
 train_df.head()<load_from_csv>
mapeamento = {'yes': 1, 'no': 0}
df_all['edjefa'] = df_all['edjefa'].replace(mapeamento ).astype(int)
 df_all['edjefe'] = df_all['edjefe'].replace(mapeamento ).astype(int )
labels_df = pd.read_csv(path/'labels.csv')
 labels_df.head()<load_from_csv>
df_all['dependency'].value_counts()
test_df = pd.read_csv(path/'sample_submission.csv')
 test_df.head()<categorify>
df_all['dependency'] = df_all['dependency'].replace(mapeamento ).astype(float )
tfms = get_transforms(do_flip=True, flip_vert=False, max_rotate=0.10, max_zoom=1.5, max_warp=0.2, max_lighting=0.2,
xtra_tfms=[(symmetric_warp(magnitude=(-0,0), p=0)) ,])
 train, test = [ImageList.from_df(df, path=path, cols='id', folder=folder, suffix='.png')
 for df, folder in zip([train_df, test_df], ['train', 'test'])]
data =(train.split_by_rand_pct(0.1, seed=42)
.label_from_df(cols='attribute_ids', label_delim=' ')
.add_test(test)
.transform(tfms, size=SIZE, resize_method=ResizeMethod.PAD, padding_mode='border',)
.databunch(path=Path('.'), bs=BATCH ).normalize(imagenet_stats))<compute_test_metric>
df_all.isnull().sum().sort_values()
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, logit, target):
target = target.float()
max_val =(-logit ).clamp(min=0)
 loss = logit - logit * target + max_val + \ 
(( -max_val ).exp() +(-logit - max_val ).exp() ).log()
invprobs = F.logsigmoid(-logit *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
if len(loss.size())==2:
loss = loss.sum(dim=1)
 return loss.mean()<choose_model_class>
learn = cnn_learner(data, base_arch=models.resnet152, loss_func=FocalLoss() , metrics=fbeta, pretrained=False)
 learn.load('resnet152' )<find_best_params>
df_all.isnull().sum().sort_values()
learn.lr_find()
learn.recorder.plot()<train_model>
lr = 1e-2
learn.fit_one_cycle(10, slice(1e-5, 1e-3))<set_options>
feats = [c for c in df_all.columns if c not in ['Id', 'idhogar', 'Target','edjefe', 'edjefa']]
learn.save('stage-1', return_path=True)
 learn.export()<predict_on_test>
rf.fit(train[feats], train['Target'] )
valid_preds = learn.get_preds(DatasetType.Valid)
 best_thr = find_best_fixed_threshold(*valid_preds )<categorify>
accuracy_score(train['Target'], rf.predict(train[feats]))
test_preds = learn.TTA(ds_type=DatasetType.Test)
 test_df.attribute_ids = join_preds(test_preds, best_thr)
 test_df.head()<save_to_csv>
preds_val = rf.predict(train[feats] )
test_df.to_csv('submission.csv', index=False )<import_modules>
import scikitplot as skplt
import numpy as np
import pandas as p
from matplotlib import pyplot as plt
import seaborn as sns
from math import sqrt
from sklearn import metrics<load_from_csv>
rf2 = XGBClassifier(n_estimators=200, learning_rate=0.09, random_state=42)
df_base0 = p.read_csv('.. /input/public-solutions/base0_0.2211.csv',names=["item_id","deal_probability0"], skiprows=[0],header=None)
 df_base1 = p.read_csv('.. /input/public-solutions/base1_0.2212.csv',names=["item_id","deal_probability1"], skiprows=[0],header=None)
 df_base2 = p.read_csv('.. /input/public-solutions/base2_0.2212.csv',names=["item_id","deal_probability2"], skiprows=[0],header=None)
 df_base3 = p.read_csv('.. /input/public-solutions/base3_0.2213.csv',names=["item_id","deal_probability3"], skiprows=[0],header=None)
 df_base4 = p.read_csv('.. /input/public-solutions/base4_0.2215.csv',names=["item_id","deal_probability4"], skiprows=[0],header=None)
 df_base5 = p.read_csv('.. /input/public-solutions/base5_0.2219.csv',names=["item_id","deal_probability5"], skiprows=[0],header=None)
 df_base6 = p.read_csv('.. /input/public-solutions/base6_0.2220.csv',names=["item_id","deal_probability6"], skiprows=[0],header=None)
 df_base7 = p.read_csv('.. /input/public-solutions/base7_0.2222.csv',names=["item_id","deal_probability7"], skiprows=[0],header=None)
 df_base8 = p.read_csv('.. /input/public-solutions/base8_0.2224.csv',names=["item_id","deal_probability8"], skiprows=[0],header=None)
 df_base9 = p.read_csv('.. /input/public-solutions/base9_0.2226.csv',names=["item_id","deal_probability9"], skiprows=[0],header=None)
 df_base10 = p.read_csv('.. /input/public-solutions/base10_0.2227.csv',names=["item_id","deal_probability10"], skiprows=[0],header=None)
 df_base11 = p.read_csv('.. /input/public-solutions/base11_0.2228.csv',names=["item_id","deal_probability11"], skiprows=[0],header=None)
 df_base12 = p.read_csv('.. /input/public-solutions/base12_0.2230.csv',names=["item_id","deal_probability12"], skiprows=[0],header=None)
 df_base13 = p.read_csv('.. /input/public-solutions/base13_0.2232.csv',names=["item_id","deal_probability13"], skiprows=[0],header=None)
 df_base14 = p.read_csv('.. /input/public-solutions/base14_0.2237.csv',names=["item_id","deal_probability14"], skiprows=[0],header=None)
 df_base15 = p.read_csv('.. /input/public-solutions/base15_0.2237.csv',names=["item_id","deal_probability15"], skiprows=[0],header=None)
 df_base16 = p.read_csv('.. /input/public-solutions/base16_0.2238.csv',names=["item_id","deal_probability16"], skiprows=[0],header=None)
 df_base17 = p.read_csv('.. /input/public-solutions/base17_0.2239.csv',names=["item_id","deal_probability17"], skiprows=[0],header=None)
 df_base18 = p.read_csv('.. /input/public-solutions/base18_0.2246.csv',names=["item_id","deal_probability18"], skiprows=[0],header=None)
 df_base19 = p.read_csv('.. /input/public-solutions/base19_0.2247.csv',names=["item_id","deal_probability19"], skiprows=[0],header=None)
 df_base20 = p.read_csv('.. /input/public-solutions/base20_0.2249.csv',names=["item_id","deal_probability20"], skiprows=[0],header=None)
 df_base21 = p.read_csv('.. /input/public-solutions/base21_0.2255.csv',names=["item_id","deal_probability21"], skiprows=[0],header=None)
 df_base22 = p.read_csv('.. /input/public-solutions/base22_0.2255.csv',names=["item_id","deal_probability22"], skiprows=[0],header=None)
 df_base23 = p.read_csv('.. /input/public-solutions/base23_0.2269.csv',names=["item_id","deal_probability23"], skiprows=[0],header=None )<merge>
rf2.fit(train[feats], train['Target'])
 accuracy_score(train['Target'], rf2.predict(train[feats]))
df_base = p.merge(df_base0,df_base1,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base2,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base3,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base4,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base5,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base6,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base7,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base8,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base9,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base10,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base11,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base12,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base13,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base14,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base15,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base16,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base17,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base18,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base19,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base20,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base21,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base22,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base23,how='inner',on='item_id' )<compute_test_metric>
rf3 = GradientBoostingClassifier(n_estimators=200, learning_rate=1.0, max_depth=1, random_state=42)
M = np.zeros([df_base.iloc[:,1:].shape[1],df_base.iloc[:,1:].shape[1]])
 for i in np.arange(M.shape[1]):
for j in np.arange(M.shape[1]):
M[i,j] = sqrt(metrics.mean_squared_error(df_base.iloc[:,i+1], df_base.iloc[:,j+1]))<merge>
rf3.fit(train[feats], train['Target'])
 accuracy_score(train['Target'], rf3.predict(train[feats]))
df_base = p.merge(df_base0,df_base1,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base2,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base14,how='inner',on='item_id')
 df_base = p.merge(df_base,df_base18,how='inner',on='item_id' )<compute_test_metric>
rf = RandomForestClassifier(max_depth=None, random_state=42, n_jobs=4, n_estimators=700,
min_impurity_decrease=1e-3, min_samples_leaf=2,
verbose=0, class_weight='balanced' )
M = np.zeros([df_base.iloc[:,1:].shape[1],df_base.iloc[:,1:].shape[1]])
 for i in np.arange(M.shape[1]):
for j in np.arange(M.shape[1]):
M[i,j] = sqrt(metrics.mean_squared_error(df_base.iloc[:,i+1], df_base.iloc[:,j+1]))<feature_engineering>
rf.fit(train[feats], train['Target'])
 accuracy_score(train['Target'], rf.predict(train[feats]))
df_base['deal_probability'] = df_base.iloc[:,1:].mean(axis=1 )<save_to_csv>
rf.fit(train[feats], train['Target'] )
df_base[['item_id','deal_probability']].to_csv("best_public_blend.csv",index=False )<set_options>
test['Target'] = rf.predict(test[feats] ).astype(int )
color = sns.color_palette()
%matplotlib inline
pd.options.mode.chained_assignment = None
pd.options.display.max_columns = 999
print(os.listdir(".. /input")) 
<load_from_csv>
test['Target'].value_counts(normalize=True )
train_df = pd.read_csv(".. /input/train.csv", parse_dates=["activation_date"])
 test_df = pd.read_csv(".. /input/test.csv", parse_dates=["activation_date"])
 print("Train file rows and columns are : ", train_df.shape)
 print("Test file rows and columns are : ", test_df.shape)
 testdex = test_df.index
traindex = train_df.index<feature_engineering>
test[['Id', 'Target']].to_csv('submission.csv', index=False )