Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<define_variables>
import numpy as np
import pandas as pd
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold
epochs = 39
batch_size = 64<define_variables>
df = pd.read_csv(".. /input/train.csv" )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=0.10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<train_model>
def preprocess(df):

 def mk_categoricals(df, prefixes=None, subsets=None):

 def mk_category(dummies):
assert(dummies.sum(axis=1)<= 1 ).all()
nans = dummies.sum(axis=1)!= 1
if nans.any() :
dummies = dummies.assign(_na=nans.astype(int))
return dummies.idxmax(axis=1 ).astype('category')
 categoricals = pd.DataFrame()
if prefixes:
for prefix in prefixes:
columns = df.columns[df.columns.str.startswith(prefix)]
categoricals[prefix] = mk_category(df[columns])
 if subsets:
for feature_name, subset in subsets.items() :
categoricals[feature_name] = mk_category(df[subset])
 return categoricals
groupper = df.groupby('idhogar')
 interactions =(pd.DataFrame(dict(
 head_escolari=df.parentesco1 * df.escolari,
head_female=df.parentesco1 * df.female,
head_partner_escolari=df.parentesco2 * df.escolari)) 
.groupby(df.idhogar)
.max())
 my_features =(groupper.mean() [['escolari', 'age', 'hogar_nin',
'hogar_total', 'epared3', 'epared1',
'etecho3', 'etecho1', 'eviv3', 'eviv1',
'male',
'r4h1', 'r4h2', 'r4h3', 'r4m1', 'r4m2',
'r4m3',
'r4t1', 'r4t2', 'r4t3', 'v2a1', 'rooms',
'bedrooms',
'meaneduc',
'SQBdependency', 'rez_esc', 'refrig',
'tamviv', 'overcrowding']] 
.join(groupper.std() [['escolari', 'age']],
rsuffix='_std')
.join(groupper[['escolari', 'age']].min() , rsuffix="_min")
.join(groupper[['escolari', 'age']].max() , rsuffix="_max")
.join(groupper[['dis']].sum() , rsuffix="_sum")
.assign(child_rate=lambda x: x.hogar_nin / x.hogar_total,
wrf=lambda x: x.epared3 - x.epared1 +
x.etecho3 - x.etecho1 +
x.eviv3 - x.eviv1,
escolari_range=lambda x: x.escolari_max - x.escolari_min,
age_range=lambda x: x.age_max - x.age_min,
rent_per_individual=lambda x: x.v2a1 / x.r4t3,
rent_per_child=lambda x: x.v2a1 / x.r4t1,
rent_per_over65=lambda x: x.v2a1 / x.r4t3,
rent_per_room=lambda x: x.v2a1 / x.rooms,
rent_per_bedroom=lambda x: x.v2a1 / x.bedrooms,
rooms_per_individual=lambda x: x.rooms / x.r4t3,
rooms_per_child=lambda x: x.rooms / x.r4t1,
bedrooms_per_individual=lambda x: x.bedrooms / x.r4t3,
bedrooms_per_child=lambda x: x.bedrooms / x.r4t1,
years_schooling_per_individual=lambda x: x.escolari / x.r4t3,
years_schooling_per_adult=lambda x: x.escolari /(x.r4t3 - x.r4t1),
years_schooling_per_child=lambda x: x.escolari / x.r4t3 
)
.drop(['hogar_nin', 'hogar_total', 'epared3', 'epared1',
'etecho3', 'etecho1', 'eviv3', 'eviv1'],
axis=1)
.join(interactions)
.join(groupper[['computer', 'television',
'qmobilephone', 'v18q1']] 
.mean().sum(axis=1 ).rename('technics')) 
.assign(technics_per_individual=lambda x: x.technics / x.r4t3,
technics_per_child=lambda x: x.technics / x.r4t1)
.join(mk_categoricals(groupper.mean() ,
prefixes=['lugar', 'area', 'tipovivi',
'energcocinar',
'sanitario', 'pared', 'piso',
'abastagua'],
subsets={'electricity': ['public',
'planpri',
'noelec',
'coopele']})) 
)
 return my_features
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
steps_per_epoch=X_train.shape[0] // batch_size, callbacks=[annealer] )<save_to_csv>
X = preprocess(df)
 y = df.groupby('idhogar' ).Target.mean().astype(int)
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("sub.csv",index=False)
<load_from_csv>
clf = lgb.LGBMClassifier(class_weight='balanced', boosting_type='dart',
drop_rate=0.9, min_data_in_leaf=100,
max_bin=255,
n_estimators=500,
bagging_fraction=0.01,
min_sum_hessian_in_leaf=1,
importance_type='gain',
learning_rate=0.1,
max_depth=-1,
num_leaves=31 )
train = pd.read_csv(r".. /input/digit-recognizer/train.csv",dtype = np.float32)
 train_label = train.label.values
train_image = train.loc[:,train.columns != "label"].values/255
train_image, valid_image, train_label, valid_label = train_test_split(train_image,
train_label,
test_size = 0.2,
random_state = 7)
 train_image = torch.from_numpy(train_image)
 train_label = torch.from_numpy(train_label ).type(torch.LongTensor)
 valid_image = torch.from_numpy(valid_image)
 valid_label = torch.from_numpy(valid_label ).type(torch.LongTensor)
 batch_size = 100
n_iters = 15000
num_epochs = n_iters /(len(train_image)/ batch_size)
 num_epochs = int(num_epochs)
 print('num_epochs',num_epochs)
 train = torch.utils.data.TensorDataset(train_image,train_label)
 valid = torch.utils.data.TensorDataset(valid_image,valid_label)
 train_loader = DataLoader(train, batch_size = batch_size, shuffle = False)
 valid_loader = DataLoader(valid, batch_size = batch_size, shuffle = False)
<choose_model_class>
df_test = pd.read_csv(".. /input/test.csv" ).set_index('Id')
 X_test = preprocess(df_test )
class res18model(nn.Module):
def __init__(self, num_classes=10):
super().__init__()
self.backbone = torchvision.models.resnet18(pretrained=True)
 self.backbone.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3,
bias=False)
 in_features = self.backbone.fc.in_features
self.logit = nn.Linear(in_features, num_classes)
 def forward(self, x):
batch_size, C, H, W = x.shape
x = self.backbone.conv1(x)
 x = self.backbone.bn1(x)
 x = self.backbone.relu(x)
 x = self.backbone.maxpool(x)
 x = self.backbone.layer1(x)
 x = self.backbone.layer2(x)
 x = self.backbone.layer3(x)
 x = self.backbone.layer4(x)
 x = F.adaptive_avg_pool2d(x,1 ).reshape(batch_size,-1)
 x = F.dropout(x, 0.25, self.training)
 x = self.logit(x)
 return x<set_options>
kf = StratifiedKFold(n_splits=5, shuffle=True)
 predicts = []
for train_index, test_index in kf.split(X, y):
print("
X_train, X_val = X.iloc[train_index], X.iloc[test_index]
y_train, y_val = y.iloc[train_index], y.iloc[test_index]
clf.fit(X_train, y_train, eval_set=[(X_val, y_val)],
early_stopping_rounds=20)
 predicts.append(clf.predict(X_test))
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device)
<choose_model_class>
predict_by_hh = pd.DataFrame(np.array(predicts ).mean(axis=0 ).round().astype(int),
columns=['Target'],
index=X_test.index )
model = res18model(num_classes=10)
 model.to(device)
 criterion = nn.CrossEntropyLoss()
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters() , lr=learning_rate, momentum=0.9 )<train_model>
predict = df_test.join(predict_by_hh, on='idhogar')[['Target']].astype(int )
<load_from_csv><EOS> 
predict.to_csv("output.csv" )