Kaggle competition: Costa Rican Household Poverty Level Prediction
def process_data(data):
data = pd.get_dummies(data, columns=['cp_time','cp_dose'])
 return data<define_variables>
import pandas as pd
import numpy as np
import xgboost as xgb
from xgboost import plot_importance
from sklearn import svm, neighbors
from collections import Counter
from imblearn.over_sampling import SMOTE
from sklearn.model_selection import train_test_split
from sklearn.ensemble import VotingClassifier, RandomForestClassifier
import matplotlib.pyplot as plt
feature_cols = [c for c in process_data(folds ).columns if c not in target_cols]
feature_cols = [c for c in feature_cols if c not in ['kfold','sig_id']]
len(feature_cols )<define_variables>
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 = data[data['parentesco1'] == 1]
DEVICE =('cuda' if torch.cuda.is_available() else 'cpu')
 EPOCHS = 25
BATCH_SIZE = 128
LEARNING_RATE = 1e-3
WEIGHT_DECAY = 1e-5
NFOLDS = 5
EARLY_STOPPING_STEPS = 10
EARLY_STOP = False
num_features=len(feature_cols)
 num_targets=len(target_cols)
 hidden_size=4096*4 
<prepare_x_and_y>
data_x,data_y = datas_plit(data)
 test_data = datas_plit(data_test)
 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
data_csv1 = data
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'],
'elimbasu':['elimbasu1','elimbasu2','elimbasu3','elimbasu4','elimbasu5','elimbasu6'],
'floor':['pisomoscer','pisocemento','pisoother','pisonatur','pisonotiene','pisomadera'],
'roof':['techozinc','techoentrepiso','techocane','techootro'],
'water':['abastaguadentro','abastaguafuera','abastaguano'],
'electricity':['public','planpri','noelec','coopele'],
'sanitario':['sanitario1','sanitario2','sanitario3','sanitario5','sanitario6'],
'energcocinar':['energcocinar1','energcocinar2','energcocinar3','energcocinar4'],
'epared':['epared1','epared2','epared3'],
'etecho':['etecho1','etecho2','etecho3'],
'eviv':['eviv1','eviv2','eviv3'],
'sex':['male','female'],
'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'],
'lugar':['lugar1','lugar2','lugar3','lugar4','lugar5','lugar6'],
'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 )
def run_training(fold, seed):
seed_everything(seed)
 train = process_data(folds)
 test_ = process_data(test)
 trn_idx = train[train['kfold'] != fold].index
val_idx = train[train['kfold'] == fold].index
train_df = train[train['kfold'] != fold].reset_index(drop=True)
 valid_df = train[train['kfold'] == fold].reset_index(drop=True)
 x_train, y_train = train_df[feature_cols].values, train_df[target_cols].values
x_valid, y_valid = valid_df[feature_cols].values, valid_df[target_cols].values
train_dataset = MoADataset(x_train, y_train)
 valid_dataset = MoADataset(x_valid, y_valid)
 trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_features=num_features,
num_targets=num_targets,
hidden_size=hidden_size, 
)
 model.to(DEVICE)
 optimizer = torch.optim.Adam(model.parameters() , lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
 scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.01, div_factor=1e3, max_lr=1e-2, epochs=EPOCHS, steps_per_epoch=len(trainloader))
loss_fn = nn.BCEWithLogitsLoss()
early_stopping_steps = EARLY_STOPPING_STEPS
early_step = 0
oof = np.zeros(( len(train), target.iloc[:, 1:].shape[1]))
best_loss = np.inf
for epoch in range(EPOCHS):
train_loss = train_fn(model, optimizer,scheduler, loss_fn, trainloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, train_loss: {train_loss}")
 valid_loss, valid_preds = valid_fn(model, loss_fn, validloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, valid_loss: {valid_loss}")
 if valid_loss < best_loss:
best_loss = valid_loss
oof[val_idx] = valid_preds
torch.save(model.state_dict() , f"FOLD{fold}_.pth")
 elif(EARLY_STOP == True):
early_step += 1
if(early_step >= early_stopping_steps):
break
x_test = test_[feature_cols].values
testdataset = TestDataset(x_test)
 testloader = torch.utils.data.DataLoader(testdataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_features=num_features,
num_targets=num_targets,
hidden_size=hidden_size, 
)
 model.load_state_dict(torch.load(f"FOLD{fold}_.pth"))
model.to(DEVICE)
 predictions = np.zeros(( len(test_), target.iloc[:, 1:].shape[1]))
predictions = inference_fn(model, testloader, DEVICE)
 return oof, predictions 
<categorify>
model = xgb.XGBClassifier(max_depth=4, learning_rate=0.6, n_estimators=500,scale_pos_weight=5, silent=True, objective='multi:softmax')
 model.fit(X_res, y_res)
def run_k_fold(NFOLDS, seed):
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for fold in range(NFOLDS):
oof_, pred_ = run_training(fold, seed)
 predictions += pred_ / NFOLDS
oof += oof_
return oof, predictions<categorify>
ans = model.predict(X_res)
 y_test = data_y.values
cnt1 = 0
cnt2 = 0
for i in range(len(y_test)) :
if ans[i] == y_test[i]:
cnt1 += 1
else:
cnt2 += 1
print("Accuracy: %.2f %% " %(100 * cnt1 /(cnt1 + cnt2)))
<merge><EOS> 
ans = model.predict(test_data)
 subs['Target'] = ans
print(subs)
 subs.to_csv('sample_submission.csv',index=False)