Kaggle competition: Costa Rican Household Poverty Level Prediction
folds = train.copy()
mskf = MultilabelStratifiedKFold(n_splits=5)
 for f,(t_idx, v_idx)in enumerate(mskf.split(X=train, y=target)) :
folds.loc[v_idx, 'kfold'] = int(f)
 folds['kfold'] = folds['kfold'].astype(int)
 folds<categorify>
import pandas as pd
import torch
import numpy as np
import lightgbm as lgb
import xgboost as xgb
from sklearn import preprocessing
from xgboost.sklearn import XGBClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
from sklearn.svm import SVC
from sklearn.ensemble import VotingClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from imblearn.over_sampling import SMOTE
from sklearn.ensemble import ExtraTreesClassifier
from xgboost import plot_importance
import matplotlib.pyplot as plt
import matplotlib.pylab as plt1
class MoADataset:
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float),
'y' : torch.tensor(self.targets[idx, :], dtype=torch.float)
 }
return dct
class TestDataset:
def __init__(self, features):
self.features = features
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float)
 }
return dct 
<train_model>
data = pd.read_csv('.. /input/train.csv',dtype={'duration': int})
 data_test = pd.read_csv('.. /input/test.csv',dtype={'duration': int})
 subs = pd.read_csv('.. /input/sample_submission.csv',dtype={'duration': int})
 data_csv1 = data
def train_fn(model, optimizer, scheduler, loss_fn, dataloader, device):
model.train()
final_loss = 0
for data in dataloader:
optimizer.zero_grad()
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 loss.backward()
optimizer.step()
scheduler.step()
final_loss += loss.item()
final_loss /= len(dataloader)
 return final_loss
def valid_fn(model, loss_fn, dataloader, device):
model.eval()
final_loss = 0
valid_preds = []
for data in dataloader:
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 final_loss += loss.item()
valid_preds.append(outputs.sigmoid().detach().cpu().numpy())
 final_loss /= len(dataloader)
 valid_preds = np.concatenate(valid_preds)
 return final_loss, valid_preds
def inference_fn(model, dataloader, device):
model.eval()
preds = []
for data in dataloader:
inputs = data['x'].to(device)
 with torch.no_grad() :
outputs = model(inputs)
 preds.append(outputs.sigmoid().detach().cpu().numpy())
 preds = np.concatenate(preds)
 return preds 
<train_model>
def comb(arr,data_csv):
test = []
for type in arr:
test.append(data_csv[type])
 data_csv.drop([type], axis=1,inplace=True)
 array2 = np.array(test)
 max_p_row= array2.argmax(axis=0)
 return max_p_row
norent=data_csv1[data_csv1['v2a1'].isnull() ]
print(norent.shape)
 own_house=norent[norent['tipovivi1']==1]['Id'].count()
installments=norent[norent['tipovivi2']==1]['Id'].count()
rented=norent[norent['tipovivi3']==1]['Id'].count()
Precarious=norent[norent['tipovivi4']==1]['Id'].count()
other= norent[norent['tipovivi5']==1]['Id'].count()
num_list=[own_house,installments,rented,Precarious,other]
name_list=['own_house','installments','rented','Precarious','other']
plt.bar(range(len(num_list)) ,num_list,tick_label=name_list)
 plt.show()
life_condition={'wall_material':['paredblolad','paredzocalo','paredpreb','pareddes','paredmad','paredzinc','paredfibras','paredother'],
'floor':['pisomoscer','pisocemento','pisoother','pisonatur','pisonotiene','pisomadera'],
'roof':['techozinc','techoentrepiso','techocane','techootro'],
'water':['abastaguadentro','abastaguafuera','abastaguan1o'],
'electricity':['public','planpri','noelec','coopele'],
'sanitario':['sanitario1','sanitario2','sanitario3','sanitario5','sanitario6'],
'energcocinar':['energcocinar1','energcocinar2','energcocinar3','energcocinar4'],
'etecho':['etecho2','etecho3'],
'eviv':['eviv1','eviv2','eviv3'],
'estadocivil':['estadocivil1','estadocivil2','estadocivil3','estadocivil4','estadocivil5','estadocivil6','estadocivil7'],
'parentesco':['parentesco1','parentesco2','parentesco3','parentesco4','parentesco5','parentesco6','parentesco7','parentesco8','parentesco9','parentesco10','parentesco11','parentesco12'],
'instlevel':['instlevel1', 'instlevel2', 'instlevel3', 'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7', 'instlevel8', 'instlevel9'],
'tipovivi':['tipovivi1','tipovivi2','tipovivi3','tipovivi4','tipovivi5'],
'area':['area1',"area2"],
}
for life in(life_condition):
_type = comb(life_condition[life], data_x)
 data_x.insert(2,life, _type)
 for life in(life_condition):
_type = comb(life_condition[life], test_data)
 test_data.insert(2,life, _type)
 kind = ['regular', 'borderline1', 'borderline2', 'svm']
sm = SMOTE(kind='svm')
 X_res, y_res = sm.fit_sample(data_x, data_y)
 columns = test_data.columns.values
X_res = pd.DataFrame(X_res,columns=columns)
 print(X_res)
 print(test_data)
class Model(nn.Module):
def __init__(self, num_features, num_targets, hidden_size):
super(Model, self ).__init__()
cha_1 = 256
cha_2 = 512
cha_3 = 512
cha_1_reshape = int(hidden_size/cha_1)
 cha_po_1 = int(hidden_size/cha_1/2)
 cha_po_2 = int(hidden_size/cha_1/2/2)* cha_3
self.cha_1 = cha_1
self.cha_2 = cha_2
self.cha_3 = cha_3
self.cha_1_reshape = cha_1_reshape
self.cha_po_1 = cha_po_1
self.cha_po_2 = cha_po_2
self.batch_norm1 = nn.BatchNorm1d(num_features)
 self.dropout1 = nn.Dropout(0.1)
 self.dense1 = nn.utils.weight_norm(nn.Linear(num_features, hidden_size))
self.batch_norm_c1 = nn.BatchNorm1d(cha_1)
 self.dropout_c1 = nn.Dropout(0.1)
 self.conv1 = nn.utils.weight_norm(nn.Conv1d(cha_1,cha_2, kernel_size = 5, stride = 1, padding=2, bias=False),dim=None)
 self.ave_po_c1 = nn.AdaptiveAvgPool1d(output_size = cha_po_1)
 self.batch_norm_c2 = nn.BatchNorm1d(cha_2)
 self.dropout_c2 = nn.Dropout(0.1)
 self.conv2 = nn.utils.weight_norm(nn.Conv1d(cha_2,cha_2, kernel_size = 3, stride = 1, padding=1, bias=True),dim=None)
 self.batch_norm_c2_1 = nn.BatchNorm1d(cha_2)
 self.dropout_c2_1 = nn.Dropout(0.3)
 self.conv2_1 = nn.utils.weight_norm(nn.Conv1d(cha_2,cha_2, kernel_size = 3, stride = 1, padding=1, bias=True),dim=None)
 self.batch_norm_c2_2 = nn.BatchNorm1d(cha_2)
 self.dropout_c2_2 = nn.Dropout(0.2)
 self.conv2_2 = nn.utils.weight_norm(nn.Conv1d(cha_2,cha_3, kernel_size = 5, stride = 1, padding=2, bias=True),dim=None)
 self.max_po_c2 = nn.MaxPool1d(kernel_size=4, stride=2, padding=1)
 self.flt = nn.Flatten()
self.batch_norm3 = nn.BatchNorm1d(cha_po_2)
 self.dropout3 = nn.Dropout(0.2)
 self.dense3 = nn.utils.weight_norm(nn.Linear(cha_po_2, num_targets))
def forward(self, x):
x = self.batch_norm1(x)
 x = self.dropout1(x)
 x = F.celu(self.dense1(x), alpha=0.06)
 x = x.reshape(x.shape[0],self.cha_1,
self.cha_1_reshape)
 x = self.batch_norm_c1(x)
 x = self.dropout_c1(x)
 x = F.relu(self.conv1(x))
x = self.ave_po_c1(x)
 x = self.batch_norm_c2(x)
 x = self.dropout_c2(x)
 x = F.relu(self.conv2(x))
x_s = x
x = self.batch_norm_c2_1(x)
 x = self.dropout_c2_1(x)
 x = F.relu(self.conv2_1(x))
x = self.batch_norm_c2_2(x)
 x = self.dropout_c2_2(x)
 x = F.relu(self.conv2_2(x))
x = x * x_s
x = self.max_po_c2(x)
 x = self.flt(x)
 x = self.batch_norm3(x)
 x = self.dropout3(x)
 x = self.dense3(x)
 return x<feature_engineering>
ans = model.predict(test_data)
 subs['Target'] = ans
print(subs)
 subs.to_csv('sample_submission.csv',index=False)