Kaggle competition: Home Credit Default Risk
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 device<load_pretrained>
application_train=pd.read_csv(r".. /input/application_train.csv")
 application_test=pd.read_csv(r".. /input/application_test.csv")
 bureau_balance=pd.read_csv(r".. /input/bureau_balance.csv")
 bureau=pd.read_csv(r".. /input/bureau.csv")
 credit_card_balance=pd.read_csv(r".. /input/credit_card_balance.csv")
 POS_cash=pd.read_csv(r".. /input/POS_CASH_balance.csv")
 bureau=pd.read_csv(r".. /input/bureau.csv")
 previous_application=pd.read_csv(r".. /input/previous_application.csv")
 install_payment=pd.read_csv(r".. /input/installments_payments.csv" )
os.mkdir('.. /Inputs')
 with zipfile.ZipFile(".. /input/padhai-hindi-vowel-consonant-classification/train.zip","r")as z:
z.extractall(".. /Inputs/")
 with zipfile.ZipFile(".. /input/padhai-hindi-vowel-consonant-classification/test.zip","r")as z:
z.extractall(".. /Inputs/" )<set_options>
total_null=application_train.isnull().sum().sort_values(ascending=False)
 percentage=(application_train.isnull().sum() /application_train.isnull().count() *100 ).sort_values(ascending=False)
 missing_train_data=pd.concat([total_null,percentage],axis=1,keys=["Total_null","Percentage"] )
transform = transforms.Compose([
transforms.ColorJitter() ,
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor() , ] )<split>
total_null=POS_cash.isnull().sum().sort_values(ascending=False)
 percentage=(POS_cash.isnull().sum() /POS_cash.isnull().count() *100 ).sort_values(ascending=False)
 missing_POS_data=pd.concat([total_null,percentage],axis=1,keys=["Total_null","Percentage"] )
batch_size = 60
full_data=VowelConsonantDataset(".. /Inputs/train",train=True,transform=transform)
 train_size = int(0.9 * len(full_data))
test_size = len(full_data)- train_size
train_data, validation_data = random_split(full_data, [train_size, test_size])
 train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True)
 validation_loader = torch.utils.data.DataLoader(validation_data, batch_size=batch_size, shuffle=True)
 test_data=VowelConsonantDataset(".. /Inputs/test",train=False,transform=transform)
 test_loader = torch.utils.data.DataLoader(test_data, batch_size=60,shuffle=False )<import_modules>
total_null=bureau.isnull().sum().sort_values(ascending=False)
 percentage=(bureau.isnull().sum() /bureau.isnull().count() *100 ).sort_values(ascending=False)
 missing_bureau_data=pd.concat([total_null,percentage],axis=1,keys=["Total_null","Percentage"])
 missing_bureau_data.head(15 )
from torchvision import models<choose_model_class>
total_null=bureau_balance.isnull().sum().sort_values(ascending=False)
 percentage=(bureau_balance.isnull().sum() /bureau_balance.isnull().count() *100 ).sort_values(ascending=False)
 missing_bureau_balance=pd.concat([total_null,percentage],axis=1,keys=["Total_null","Percentage"])
 missing_bureau_balance.head(15 )
class MyModel(nn.Module):
def __init__(self, num_classes1, num_classes2):
super(MyModel, self ).__init__()
self.model_snet = models.mobilenet_v2(pretrained=True)
 final_in_features = self.model_snet.classifier[1].in_features
mod_classifier = list(self.model_snet.classifier.children())[:-1]
self.model_snet.classifier = nn.Sequential(*mod_classifier)
 self.fc1 = nn.Linear(final_in_features, num_classes1,bias=True)
 torch.nn.init.xavier_uniform_(self.fc1.weight)
 torch.nn.init.zeros_(self.fc1.bias)
 self.fc2 = nn.Linear(final_in_features, num_classes2,bias=True)
 torch.nn.init.xavier_uniform_(self.fc2.weight)
 torch.nn.init.zeros_(self.fc2.bias)
 def forward(self, x):
x = self.model_snet(x)
 out1 = self.fc1(x)
 out2 = self.fc2(x)
 return out1, out2<choose_model_class>
total_null=previous_application.isnull().sum().sort_values(ascending=False)
 percentage=(previous_application.isnull().sum() /previous_application.isnull().count() *100 ).sort_values(ascending=False)
 missing_previous_application=pd.concat([total_null,percentage],axis=1,keys=["Total_null","Percentage"])
 missing_previous_application.head(15 )
net = MyModel(10,10 )<data_type_conversions>
total_null=install_payment.isnull().sum().sort_values(ascending=False)
 percentage=(install_payment.isnull().sum() /install_payment.isnull().count() *100 ).sort_values(ascending=False)
 missing_installment=pd.concat([total_null,percentage],axis=1,keys=["Total_null","Percentage"])
 missing_installment.head()
net = net.to(device )<categorify>
total_null=application_test.isnull().sum().sort_values(ascending=False)
 percentage=(application_test.isnull().sum() /application_test.isnull().count() *100 ).sort_values(ascending=False)
 missing_app_test=pd.concat([total_null,percentage],axis=1,keys=["Total_null","Percentage"])
 missing_app_test.head(15 )
def evaluation(dataloader):
total, correct = 0, 0
for data in dataloader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
 _, actual_v = torch.max(labels[:,0,:].data, 1)
 _, actual_c = torch.max(labels[:,1,:].data, 1)
 outputs_v,outputs_c = net(inputs)
 _, pred_v = torch.max(outputs_v.data, 1)
 _, pred_c = torch.max(outputs_c.data, 1)
 total += labels.size(0)
 correct_v =(pred_v == actual_v)*1
correct_c =(pred_c == actual_c)*1
correct_v[correct_v == 0] = 2
correct_c[correct_c == 0] = 3
correct +=(( correct_v==correct_c)).sum().item()
return 100 * correct / total<choose_model_class>
application_train.select_dtypes("object" ).nunique()
loss_fn = nn.CrossEntropyLoss()
plist = [
{'params': net.fc1.parameters() , 'lr': 5e-3},
{'params': net.fc2.parameters() , 'lr': 5e-3}
]
lr=0.01
opt = optim.SGD(net.parameters() ,lr=0.01,momentum=0.9,nesterov=True )<init_hyperparams>
lbl=LabelEncoder()
lbl_count=0
for i in application_train:
if application_train[i].dtype=='object':
if len(list(application_train[i].unique())) <= 2:
lbl.fit(application_train[i])
 application_train[i]=lbl.transform(application_train[i])
 application_test[i]=lbl.transform(application_test[i])
 lbl_count +=1
print('%d column were encoded.'%lbl_count)
%%time
loss_arr = []
loss_epoch_arr = []
max_epochs = 10
min_loss = 1000
best_model = None
for epoch in range(max_epochs):
for i, data in enumerate(train_loader, 0):
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
 labels_v = labels[:,0,:]
labels_c = labels[:,1,:]
_, actual_v = torch.max(labels_v.data, 1)
 _, actual_c = torch.max(labels_c.data, 1)
 opt.zero_grad()
outputs_v, outputs_c = net(inputs)
 loss_v = loss_fn(outputs_v, actual_v)
 loss_c = loss_fn(outputs_c, actual_c)
 loss = torch.add(loss_v,loss_c)
 loss.backward()
opt.step()
if min_loss > loss.item() :
min_loss = loss.item()
best_model = copy.deepcopy(net.state_dict())
 loss_arr.append(loss.item())
 del inputs, labels, outputs_v, outputs_c
torch.cuda.empty_cache()
loss_epoch_arr.append(loss.item())
 print('Epoch: %d/%d, Test acc: %0.2f, Train acc: %0.2f' %(epoch, max_epochs, evaluation(validation_loader), evaluation(train_loader)))
 net.load_state_dict(best_model)
 plt.plot(loss_epoch_arr)
 plt.plot(loss_arr)
 plt.show()<find_best_params>
application_train.select_dtypes("object" ).nunique()
net.eval()
plist=[]
fn_list=[]
for inputs_test, fn in test_loader:
inputs_test=inputs_test.to(device)
 out1,out2=net.forward(inputs_test)
 _,pred1=torch.max(out1,1)
 pred1=pred1.tolist()
_,pred2=torch.max(out2,1)
 pred2=pred2.tolist()
for x,y,z in zip(pred1,pred2,fn):
p="V"+str(x)+"_"+"C"+str(y)
 plist.append(p)
 fn_list.append(z )<prepare_output>
application_train=pd.get_dummies(application_train)
 application_test=pd.get_dummies(application_test)
 print("application_train feature shape:",application_train.shape)
 print("application_test feature shape:",application_test.shape )
submission = pd.DataFrame({"ImageId":fn_list, "Class":plist})
 submission.head()<save_to_csv>
train_target=application_train['TARGET']
application_train,application_test=application_train.align(application_test,axis=1,join='inner' )
submission.to_csv('submission.csv', index=False )<save_to_csv>
application_train['TARGET']=train_target
submission.to_csv('submission.csv', index=False )<import_modules>
application_train.isnull().sum()
import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier<load_from_csv>
application_train.isnull().sum()
traindf = pd.read_csv(".. /input/train.csv" )<drop_column>
prev_category = pd.get_dummies(previous_application)
 bureau_category = pd.get_dummies(bureau)
 pos_category = pd.get_dummies(POS_cash)
 credit_category= pd.get_dummies(credit_card_balance)
traindf.drop(['Gender'], axis=1, inplace=True )<create_dataframe>
application_train=application_train.fillna(0)
 application_test=application_test.fillna(0 )
traindata = traindf.values<define_search_space>
application_test['is_test'] = 1
application_test['is_train'] = 0
application_train['is_test'] = 0
application_train['is_train'] = 1
Y = application_train['TARGET']
train_X = application_train.drop(['TARGET'], axis = 1)
 test_id = application_train['SK_ID_CURR']
test_X = application_test
data = pd.concat([train_X, test_X], axis=0 )
descriptores,clases = traindata[:,0:-1],traindata[:,-1]<define_variables>
prev_apps = previous_application[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
previous_application['SK_ID_PREV'] = previous_application['SK_ID_CURR'].map(prev_apps['SK_ID_PREV'])
 prev_apps_avg = previous_application.groupby('SK_ID_CURR' ).mean()
prev_apps_avg.columns = ['p_' + col for col in prev_apps_avg.columns]
data = data.merge(right=prev_apps_avg.reset_index() , how='left', on='SK_ID_CURR' )
clases = [1 if clase==True else 0 for clase in clases]<choose_model_class>
bureau_avg = bureau.groupby('SK_ID_CURR' ).mean()
bureau_avg['buro_count'] = bureau[['SK_ID_BUREAU','SK_ID_CURR']].groupby('SK_ID_CURR' ).count() ['SK_ID_BUREAU']
bureau_avg.columns = ['b_' + f_ for f_ in bureau_avg.columns]
data = data.merge(right=bureau_avg.reset_index() , how='left', on='SK_ID_CURR' )
knn = KNeighborsClassifier(n_neighbors=1 )<train_model>
install_pay= install_payment[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
install_payment['SK_ID_PREV'] = install_payment['SK_ID_CURR'].map(install_pay['SK_ID_PREV'])
 avg_inst = install_payment.groupby('SK_ID_CURR' ).mean()
avg_inst.columns = ['i_' + f_ for f_ in avg_inst.columns]
data = data.merge(right=avg_inst.reset_index() , how='left', on='SK_ID_CURR' )
knn.fit(descriptores,clases )<load_from_csv>
pos_cash = POS_cash[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
POS_cash['SK_ID_PREV'] = POS_cash['SK_ID_CURR'].map(pos_cash['SK_ID_PREV'])
 POS_avg = POS_cash.groupby('SK_ID_CURR' ).mean()
data = data.merge(right=POS_avg.reset_index() , how='left', on='SK_ID_CURR' )
testdf = pd.read_csv(".. /input/test.csv")
 testdf.drop(['Gender'], axis=1, inplace=True)
 testdata = testdf.values<predict_on_test>
credit_balns= credit_card_balance[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
credit_card_balance['SK_ID_PREV'] = credit_card_balance['SK_ID_CURR'].map(credit_balns['SK_ID_PREV'])
 avg_credit_bal = credit_card_balance.groupby('SK_ID_CURR' ).mean()
avg_credit_bal.columns = ['credit_bal_' + f_ for f_ in avg_credit_bal.columns]
data = data.merge(right=avg_credit_bal.reset_index() , how='left', on='SK_ID_CURR' )
predicciones = knn.predict(testdata )<define_variables>
ignore_features = ['SK_ID_CURR', 'is_train', 'is_test']
relevant_features = [col for col in data.columns if col not in ignore_features]
trainX = data[data['is_train'] == 1][relevant_features]
testX = data[data['is_test'] == 1][relevant_features]
predicciones = [True if prediccion==1 else False for prediccion in predicciones]<create_dataframe>
x_train, x_val, y_train, y_val = train_test_split(trainX, Y, test_size=0.2, random_state=18)
 lgb_train = lgb.Dataset(data=x_train, label=y_train)
 lgb_eval = lgb.Dataset(data=x_val, label=y_val )
soldf = pd.DataFrame(list(enumerate(predicciones)) )<rename_columns>
params = {'task': 'train', 'boosting_type': 'gbdt', 'objective': 'binary', 'metric': 'auc',
'learning_rate': 0.01, 'num_leaves': 48, 'num_iteration': 5000, 'verbose': 0 ,
'colsample_bytree':.8, 'subsample':.9, 'max_depth':7, 'reg_alpha':.1, 'reg_lambda':.1,
'min_split_gain':.01, 'min_child_weight':1}
model = lgb.train(params, lgb_train, valid_sets=lgb_eval, early_stopping_rounds=170, verbose_eval=200 )
soldf.columns = ['Id','Prediction']<save_to_csv>
preds = model.predict(testX)
 sub = application_test[['SK_ID_CURR']].copy()
sub['TARGET'] = preds
sub.to_csv('sub.csv', index= False)
 sub.head(10 )