Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<import_modules>
sns.set(style="whitegrid", color_codes=True)
 np.random.seed(sum(map(ord, "categorical")))
 print(os.listdir(".. /input"))
application_train = pd.read_csv(".. /input/application_train.csv")
 application_test = pd.read_csv(".. /input/application_test.csv")
 bureau = pd.read_csv(".. /input/bureau.csv")
 bureau_balance = pd.read_csv(".. /input/bureau_balance.csv")
 credit_card_balance = pd.read_csv(".. /input/credit_card_balance.csv")
 installments_payments = pd.read_csv(".. /input/installments_payments.csv")
 previous_application = pd.read_csv(".. /input/previous_application.csv")
 POS_CASH_balance = pd.read_csv(".. /input/POS_CASH_balance.csv" )
import seaborn as sns<count_missing_values>
sns.set(rc={'figure.figsize':(14.7,8.27)} )
dataset.isnull().sum()<define_variables>
bureau_cat = [f_ for f_ in bureau.columns if bureau[f_].dtype == 'object']
dummy_bureau = pd.get_dummies(bureau, columns=bureau_cat)
 dummy_bureau.head()
set_test = dataset.iloc[-8760:,:]<define_variables>
bureau_balance_cat = [f_ for f_ in bureau_balance.columns if bureau_balance[f_].dtype == 'object']
dummy_bureau_balance = pd.get_dummies(bureau_balance, columns=bureau_balance_cat)
 dummy_bureau_balance.head()
set_train = dataset.iloc[:-8760,:]<set_options>
avg_bureau_balance = dummy_bureau_balance.groupby('SK_ID_BUREAU' ).mean()
avg_bureau_balance.head()
set_train<set_options>
bureau_all = dummy_bureau.merge(right=avg_bureau_balance.reset_index() , how='left', on='SK_ID_BUREAU', suffixes=('', '_balance_'))
bureau_all.head()
set_test<count_missing_values>
bureau_per_curr = bureau_all[['SK_ID_CURR', 'SK_ID_BUREAU']].groupby('SK_ID_CURR' ).count()
bureau_per_curr.head(10)
 bureau_all['SK_ID_BUREAU'] = bureau_all['SK_ID_CURR'].map(bureau_per_curr['SK_ID_BUREAU'] )
droping_list_all=[]
for j in range(0,7):
if not dataset.iloc[:, j].notnull().all() :
droping_list_all.append(j)
 droping_list_all<data_type_conversions>
avg_bureau = bureau_all.groupby('SK_ID_CURR' ).mean()
avg_bureau.head(10 )
set_train = set_train.apply(pd.to_numeric )<data_type_conversions>
del avg_bureau_balance, dummy_bureau, dummy_bureau_balance, bureau_all
set_train.iloc[:,0]=set_train.iloc[:,0].fillna(set_train.iloc[:,0].mean() )<data_type_conversions>
credit_card_balance_cat = [f_ for f_ in credit_card_balance.columns if credit_card_balance[f_].dtype == 'object']
dummy_credit_card_balance = pd.get_dummies(credit_card_balance, columns=credit_card_balance_cat)
 dummy_credit_card_balance.head()
set_train.iloc[:,1]=set_train.iloc[:,1].fillna(set_train.iloc[:,1].mean() )<count_missing_values>
credit_card_per_curr = dummy_credit_card_balance[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
dummy_credit_card_balance['SK_ID_PREV'] = dummy_credit_card_balance['SK_ID_CURR'].map(credit_card_per_curr['SK_ID_PREV'])
 avg_credit_card = dummy_credit_card_balance.groupby('SK_ID_CURR' ).mean()
avg_credit_card.head()
set_train.isnull().sum()<drop_column>
del dummy_credit_card_balance
set_train = set_train[['Intensidad_Global', 'Medida_1', 'Medida_2', 'Medida_3', 'Poder_Reactivo_Global', 'Voltaje',
'Poder_Activo_Global']]
set_train<drop_column>
installments_payments_cat = [f_ for f_ in installments_payments.columns if installments_payments[f_].dtype == 'object']
dummy_installments_payments = pd.get_dummies(installments_payments, columns=installments_payments_cat)
 dummy_installments_payments.head()
set_test = set_test[['Intensidad_Global', 'Medida_1', 'Medida_2', 'Medida_3', 'Poder_Reactivo_Global', 'Voltaje',
'Poder_Activo_Global']]
set_test<normalization>
installments_per_curr = dummy_installments_payments[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
dummy_installments_payments['SK_ID_PREV'] = dummy_installments_payments['SK_ID_CURR'].map(installments_per_curr['SK_ID_PREV'])
 avg_installments = dummy_installments_payments.groupby('SK_ID_CURR' ).mean()
avg_installments.head()
values = set_train.values
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values )<prepare_x_and_y>
del dummy_installments_payments
values = scaled
n_train_time_start = 1920000
n_train_time = 2000000
train = values[n_train_time_start:n_train_time, :]
test = values[n_train_time:, :]
train_X, train_y = train[:, :-1], train[:, -1]
test_X, test_y = test[:, :-1], test[:, -1]
train_X = train_X.reshape(( train_X.shape[0], 1, train_X.shape[1]))
test_X = test_X.reshape(( test_X.shape[0], 1, test_X.shape[1]))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape )<compute_test_metric>
pos_cash_balance_cat = [f_ for f_ in POS_CASH_balance.columns if POS_CASH_balance[f_].dtype == 'object']
dummy_POS_CASH_balance = pd.get_dummies(POS_CASH_balance, columns=pos_cash_balance_cat)
 dummy_POS_CASH_balance.head()
def root_mean_squared_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_pred - y_true)) )<import_modules>
pos_per_curr = dummy_POS_CASH_balance[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
dummy_POS_CASH_balance['SK_ID_PREV'] = dummy_POS_CASH_balance['SK_ID_CURR'].map(pos_per_curr['SK_ID_PREV'])
 avg_pos = dummy_POS_CASH_balance.groupby('SK_ID_CURR' ).mean()
avg_pos.head()
<compute_train_metric>
del dummy_POS_CASH_balance
yhat = model.predict(test_X)
 test_X = test_X.reshape(( test_X.shape[0], 6))
inv_yhat = np.concatenate(( yhat, test_X[:, -6:]), axis=1)
 inv_yhat = scaler.inverse_transform(inv_yhat)
 inv_yhat = inv_yhat[:,0]
test_y = test_y.reshape(( len(test_y), 1))
inv_y = np.concatenate(( test_y, test_X[:, -6:]), axis=1)
 inv_y = scaler.inverse_transform(inv_y)
 inv_y = inv_y[:,0]
rmse = np.sqrt(mean_squared_error(inv_y, inv_yhat))
print('Test RMSE: %.3f' % rmse )<data_type_conversions>
previous_application_cat = [f_ for f_ in previous_application.columns if previous_application[f_].dtype == 'object']
dummy_previous_application = pd.get_dummies(previous_application, columns=previous_application_cat)
 dummy_previous_application.head()
set_test = set_test.apply(pd.to_numeric )<count_missing_values>
previous_per_curr = dummy_previous_application[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
dummy_previous_application['SK_ID_PREV'] = dummy_previous_application['SK_ID_CURR'].map(previous_per_curr['SK_ID_PREV'])
 dummy_previous_application.head(10 )
set_test.isnull().sum()<predict_on_test>
avg_previous = dummy_previous_application.groupby('SK_ID_CURR' ).mean()
avg_previous.head()
set_test_to_predict = set_test[['Intensidad_Global', 'Medida_1', 'Medida_2', 'Medida_3', 'Poder_Reactivo_Global', 'Voltaje']]<set_options>
del dummy_previous_application
set_test_to_predict<normalization>
del previous_application
values = set_test_to_predict.values
scaler_test = MinMaxScaler(feature_range=(0, 1))
scaled_test = scaler_test.fit_transform(values )<train_model>
y = application_train['TARGET']
del application_train['TARGET']
to_predict = scaled_test
to_predict = to_predict.reshape(( to_predict.shape[0], 1, to_predict.shape[1]))<predict_on_test>
cat = [f for f in application_train.columns if application_train[f].dtype == 'object']
for f_ in cat:
application_train[f_], indexer = pd.factorize(application_train[f_])
 application_test[f_] = indexer.get_indexer(application_test[f_])
 for f_ in cat:
print('{}: {}'.format(f_, application_train[f_].unique()))
application_train.head()
ypredict = model.predict(to_predict )<concatenate>
application_train.isnull().sum().sort_values(ascending=False )
to_predict = to_predict.reshape(( to_predict.shape[0], 6))
inv_ypredict = np.concatenate(( to_predict[:, :], ypredict), axis=1 )<normalization>
application_train = application_train.merge(right=avg_bureau.reset_index() , how='left', on='SK_ID_CURR')
 application_test = application_test.merge(right=avg_bureau.reset_index() , how='left', on='SK_ID_CURR')
inv_ypredict = scaler.inverse_transform(inv_ypredict)
 ypredict_final = inv_ypredict[:,-1]<create_dataframe>
application_train = application_train.merge(right=avg_previous.reset_index() , how='left', on='SK_ID_CURR')
 application_test = application_test.merge(right=avg_previous.reset_index() , how='left', on='SK_ID_CURR')
prediction = pd.DataFrame(ypredict_final )<prepare_output>
application_train = application_train.merge(right=avg_pos.reset_index() , how='left', on='SK_ID_CURR')
 application_test = application_test.merge(right=avg_pos.reset_index() , how='left', on='SK_ID_CURR' )
set_test['Poder_Activo_Global'] = ypredict_final<set_options>
application_train = application_train.merge(right=avg_installments.reset_index() , how='left', on='SK_ID_CURR')
 application_test = application_test.merge(right=avg_installments.reset_index() , how='left', on='SK_ID_CURR' )
set_test<define_variables>
application_train = application_train.merge(right=avg_credit_card.reset_index() , how='left', on='SK_ID_CURR')
 application_test = application_test.merge(right=avg_credit_card.reset_index() , how='left', on='SK_ID_CURR' )
enviar = set_test[['Poder_Activo_Global']]<feature_engineering>
X = application_train
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 42 )
enviar['Fecha_Hora'] = enviar.index<drop_column>
train_data=lgb.Dataset(x_train,label=y_train)
 test_data=lgb.Dataset(x_test,label=y_test )
enviar = enviar.reset_index(drop=True)
 enviar = enviar[['Fecha_Hora', 'Poder_Activo_Global']]
enviar<save_to_csv>
params = {
'boosting_type': 'gbdt',
'objective': 'binary',
'metric': {'binary_logloss', 'auc'},
'metric_freq': 1,
'is_training_metric': True,
'max_bin': 255,
'learning_rate': 0.1,
'num_leaves': 63,
'tree_learner': 'serial',
'feature_fraction': 0.8,
'bagging_fraction': 0.8,
'bagging_freq': 5,
'min_data_in_leaf': 50,
'min_sum_hessian_in_leaf': 5,
'is_enable_sparse': True,
'use_two_round_loading': False,
'is_save_binary_file': False,
'num_machines': 1,
'verbose': 0,
'subsample_for_bin': 200000,
'min_child_samples': 20,
'min_child_weight': 0.001,
'min_split_gain': 0.0,
'colsample_bytree': 1.0,
'reg_alpha': 0.0,
'reg_lambda': 0.0
}
enviar.to_csv('Output.csv',index=False )<load_from_csv>
clf = lgb.train(params, train_data, 2000, valid_sets=test_data, early_stopping_rounds= 40, verbose_eval= 10 )
df_train_with_no_id = pd.read_csv('.. /input/train.csv')
 df_train_with_no_id=df_train_with_no_id.drop(['id'],1)
 X = np.array(df_train_with_no_id.drop(['diagnosis'],1))
y = np.array(df_train_with_no_id['diagnosis'])
 X.shape
model = linear_model.LogisticRegression()
model.fit(X,y)
 predictions = model.predict(X)
 model.score(X,y)
 validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, y, test_size=validation_size, random_state=seed)
 name='Logistic Regression'
kfold = model_selection.KFold(n_splits=10, random_state=seed)
 cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring='accuracy')
 msg = "%s: %f(%f)" %(name, cv_results.mean() , cv_results.std())
 print(msg)
 predictions = model.predict(X_validation)
 print(accuracy_score(Y_validation, predictions))
print(classification_report(Y_validation, predictions))<save_to_csv>
y_prediction=clf.predict(application_train)
datos_submit = pd.read_csv('.. /input/dataForSubmission.csv')
 ids=datos_submit['id']
datos_submit=datos_submit.drop(['id'],1)
 predictions=model.predict(datos_submit)
 predictions
resultados=DataFrame({'Id': ids, 'Predicted': predictions})
 resultados
resultados.to_csv('resultados_1.csv', index = False )<categorify>
score = roc_auc_score(y, y_prediction)
 print("Overall AUC: {:.3f}".format(score))
class VowelConsonantDataset(Dataset):
def __init__(self, file_path,train=True,transform=None):
self.transform = transform
self.file_path=file_path
self.train=train
self.file_names=[file for _,_,files in os.walk(self.file_path)for file in files]
self.len = len(self.file_names)
 if self.train:
self.classes_mapping=self.get_classes()
def __len__(self):
return len(self.file_names)
 def __getitem__(self, index):
file_name=self.file_names[index]
image_data=self.pil_loader(self.file_path+"/"+file_name)
 if self.transform:
image_data = self.transform(image_data)
 if self.train:
file_name_splitted=file_name.split("_")
 Y1 = self.classes_mapping[file_name_splitted[0]]
Y2 = self.classes_mapping[file_name_splitted[1]]
z1,z2=torch.zeros(10),torch.zeros(10)
 z1[Y1-10],z2[Y2]=1,1
label=torch.stack([z1,z2])
 return image_data, label
else:
return image_data, file_name
def pil_loader(self,path):
with open(path, 'rb')as f:
img = Image.open(f)
 return img.convert('RGB')
 def get_classes(self):
classes=[]
for name in self.file_names:
name_splitted=name.split("_")
 classes.extend([name_splitted[0],name_splitted[1]])
 classes=list(set(classes))
classes_mapping={}
for i,cl in enumerate(sorted(classes)) :
classes_mapping[cl]=i
return classes_mapping 
<set_options>
submit = clf.predict(application_test )
train_on_gpu = torch.cuda.is_available()<set_options>
application_test['TARGET'] = submit
application_test[['SK_ID_CURR', 'TARGET']].to_csv('submission.csv', index=False, float_format='%.8f' )