Kaggle competition: Home Credit Default Risk
preds_test = le.inverse_transform(preds_test_1)
 print(type(preds_test))<create_dataframe>
data = pd.read_csv(PATH+"/application_train.csv")
 test = pd.read_csv(PATH+"/application_test.csv")
 bureau = pd.read_csv(PATH+"/bureau.csv", nrows=50000)
 bureau_balance = pd.read_csv(PATH+"/bureau_balance.csv", nrows=50000)
 credit_card_balance = pd.read_csv(PATH+"/credit_card_balance.csv", nrows=50000)
 installments_payments = pd.read_csv(PATH+"/installments_payments.csv", nrows=50000)
 previous_application = pd.read_csv(PATH+"/previous_application.csv", nrows=50000)
 POS_CASH_balance = pd.read_csv(PATH+"/POS_CASH_balance.csv", nrows=50000 )
df = pd.DataFrame(columns=['Id', 'Class_vgood', 'Class_good', 'Class_acc', 'Class_unacc'])
 for i, ids, preds in zip(range(len(test_ids)) , test_ids, preds_test):
if(preds == 'vgood'):
submission = pd.DataFrame({
"Id": ids,
"Class_vgood": 1,
"Class_good": 0,
"Class_acc": 0,
"Class_unacc": 0,
}, index=[i])
 df = df.append(submission)
 if(preds == 'good'):
submission = pd.DataFrame({
"Id": ids,
"Class_vgood": 0,
"Class_good": 1,
"Class_acc": 0,
"Class_unacc": 0,
}, index=[i])
 df = df.append(submission)
 if(preds == 'acc'):
submission = pd.DataFrame({
"Id": ids,
"Class_vgood": 0,
"Class_good": 0,
"Class_acc": 1,
"Class_unacc": 0,
}, index=[i])
 df = df.append(submission)
 if(preds == 'unacc'):
submission = pd.DataFrame({
"Id": ids,
"Class_vgood": 0,
"Class_good": 0,
"Class_acc": 0,
"Class_unacc": 1,
}, index=[i])
 df = df.append(submission )<save_to_csv>
import numpy as np
df.to_csv('sampleSubmission.csv', index=False )<load_from_csv>
import pandas as pd
train_df = pd.read_csv(".. /input/tmu-inclass-competition/train.csv")
 test_df = pd.read_csv(".. /input/tmu-inclass-competition/test.csv")
 sub_df = pd.read_csv(".. /input/tmu-inclass-competition/sample_submission.csv" )<count_values>
import pandas as pd
cat_list = ["jurisdiction_names", "country_code", "smart_location", "property_type", "host_id", "host_response_time", "room_type"]
def preprocess(train_df, test_df):
new_df = pd.concat([train_df, test_df] ).reset_index(drop=True)
 d = {}
for s in new_df["calendar_updated"].value_counts().index:
if s == "today":
d[s] = 0
elif s == "yesterday":
d[s] = 1
elif s == "a week ago" or s == "1 week ago":
d[s] = 7
elif s == "never":
d[s] = 9999
elif s[-len("months ago"):] == "months ago":
d[s] = int(s[:-len("months ago")])* 30
elif s[-len("weeks ago"):] == "weeks ago":
d[s] = int(s[:-len("weeks ago")])* 7
elif s[-len("days ago"):] == "days ago":
d[s] = int(s[:-len("days ago")])
 else:
print(s)
 print("Error")
 break
oldest = min(pd.to_datetime(new_df["host_since"]))
newest = max(pd.to_datetime(new_df["host_since"]))
dt = newest - oldest
processed_train_df = train_df.select_dtypes("number" ).drop("listing_id", axis=1)
 processed_train_df["host_since"] = pd.to_datetime(train_df["host_since"])
 processed_train_df["host_since"] = processed_train_df["host_since"].apply(lambda x:(x - oldest)/dt * 100)
 processed_train_df["calendar_updated"] = train_df["calendar_updated"].apply(lambda x: d[x])
 processed_train_df["host_response_rate"] = train_df["host_response_rate"].fillna("0%" ).apply(lambda x: int(x[:-1]))
processed_train_df = pd.concat([processed_train_df, pd.get_dummies(train_df["bed_type"])], axis=1, join='inner')
 processed_train_df = pd.concat([processed_train_df, pd.get_dummies(train_df["cancellation_policy"])], axis=1, join='inner')
 processed_test_df = test_df.select_dtypes("number" ).drop("listing_id", axis=1)
 processed_test_df["host_since"] = pd.to_datetime(test_df["host_since"])
 processed_test_df["host_since"] = processed_test_df["host_since"].apply(lambda x:(x - oldest)/dt * 100)
 processed_test_df["calendar_updated"] = test_df["calendar_updated"].apply(lambda x: d[x])
 processed_test_df["host_response_rate"] = test_df["host_response_rate"].fillna("0%" ).apply(lambda x: int(x[:-1]))
processed_test_df = pd.concat([processed_test_df, pd.get_dummies(test_df["bed_type"])], axis=1, join='inner')
 processed_test_df = pd.concat([processed_test_df, pd.get_dummies(test_df["cancellation_policy"])], axis=1, join='inner')
 for col in ["host_is_superhost", "host_has_profile_pic", "host_identity_verified", "is_location_exact", "has_availability", "requires_license"\
, "instant_bookable", "is_business_travel_ready", "require_guest_profile_picture", "require_guest_phone_verification"]:
processed_train_df[col] = train_df[col].apply(lambda x: 1 if x == "t" else 0)
 processed_test_df[col] = test_df[col].apply(lambda x: 1 if x == "t" else 0)
 train_df["host_verifications"] = train_df["host_verifications"].apply(lambda x: x.replace("]", "" ).replace("[", "" ).replace("'", "" ).replace(",", ""))
test_df["host_verifications"] = test_df["host_verifications"].apply(lambda x: x.replace("]", "" ).replace("[", "" ).replace("'", "" ).replace(",", ""))
vers = ["email", "phone", "facebook", "google", "weibo", "sent_id" , "reviews", "kba", "jumio", "government_id", "offline_government_id", "selfie", "identity_manual", "sesame", "sesame_offline", "work_email"]
for v in vers:
processed_train_df[v] = train_df["host_verifications"].apply(lambda x: 1 if v in x.split() else 0)
 processed_test_df[v] = test_df["host_verifications"].apply(lambda x: 1 if v in x.split() else 0)
 for col in cat_list:
le = LabelEncoder()
train_df[col] = train_df[col].fillna("NAN")
 test_df[col] = test_df[col].fillna("NAN")
 le = le.fit(pd.concat([train_df[col], test_df[col]]))
processed_train_df[col] = le.transform(train_df[col])
 processed_test_df[col] = le.transform(test_df[col])
 processed_train_df["col1"] = processed_train_df["bedrooms"] / processed_train_df["accommodates"]
processed_train_df["col2"] = processed_train_df["bathrooms"] / processed_train_df["accommodates"]
processed_train_df["col3"] = processed_train_df["beds"] / processed_train_df["bedrooms"]
processed_train_df["col4"] = processed_train_df["bedrooms"] + processed_train_df["bathrooms"]
processed_train_df["col5"] = processed_train_df["bedrooms"] + processed_train_df["bathrooms"] + processed_train_df["accommodates"] + processed_train_df["beds"]
processed_train_df["col6"] = processed_train_df["bedrooms"] / processed_train_df.groupby(['host_id'])['bedrooms'].transform('std')
 processed_train_df["col7"] = processed_train_df["bedrooms"] / processed_train_df.groupby(['host_id'])['bedrooms'].transform('mean')
 processed_train_df["col8"] = processed_train_df["bedrooms"] / processed_train_df.groupby(['property_type'])['bedrooms'].transform('std')
 processed_train_df["col9"] = processed_train_df["bedrooms"] / processed_train_df.groupby(['property_type'])['bedrooms'].transform('mean')
 processed_train_df["col10"] = processed_train_df["bedrooms"] / processed_train_df.groupby(['smart_location'])['bedrooms'].transform('std')
 processed_train_df["col11"] = processed_train_df["bedrooms"] / processed_train_df.groupby(['smart_location'])['bedrooms'].transform('mean')
 processed_train_df["col12"] = processed_train_df["bedrooms"] / processed_train_df.groupby(['room_type'])['bedrooms'].transform('std')
 processed_train_df["col13"] = processed_train_df["bedrooms"] / processed_train_df.groupby(['room_type'])['bedrooms'].transform('mean')
 processed_train_df["col14"] = processed_train_df["bathrooms"] / processed_train_df.groupby(['host_id'])['bathrooms'].transform('std')
 processed_train_df["col15"] = processed_train_df["bathrooms"] / processed_train_df.groupby(['host_id'])['bathrooms'].transform('mean')
 processed_train_df["col16"] = processed_train_df["bathrooms"] / processed_train_df.groupby(['property_type'])['bathrooms'].transform('std')
 processed_train_df["col17"] = processed_train_df["bathrooms"] / processed_train_df.groupby(['property_type'])['bathrooms'].transform('mean')
 processed_train_df["col18"] = processed_train_df["bathrooms"] / processed_train_df.groupby(['smart_location'])['bathrooms'].transform('std')
 processed_train_df["col19"] = processed_train_df["bathrooms"] / processed_train_df.groupby(['smart_location'])['bathrooms'].transform('mean')
 processed_train_df["col20"] = processed_train_df["bathrooms"] / processed_train_df.groupby(['room_type'])['bathrooms'].transform('std')
 processed_train_df["col21"] = processed_train_df["bathrooms"] / processed_train_df.groupby(['room_type'])['bathrooms'].transform('mean')
 processed_train_df["col22"] = processed_train_df["accommodates"] / processed_train_df.groupby(['host_id'])['accommodates'].transform('std')
 processed_train_df["col23"] = processed_train_df["accommodates"] / processed_train_df.groupby(['host_id'])['accommodates'].transform('mean')
 processed_train_df["col24"] = processed_train_df["accommodates"] / processed_train_df.groupby(['property_type'])['accommodates'].transform('std')
 processed_train_df["col25"] = processed_train_df["accommodates"] / processed_train_df.groupby(['property_type'])['accommodates'].transform('mean')
 processed_train_df["col26"] = processed_train_df["accommodates"] / processed_train_df.groupby(['smart_location'])['accommodates'].transform('std')
 processed_train_df["col27"] = processed_train_df["accommodates"] / processed_train_df.groupby(['smart_location'])['accommodates'].transform('mean')
 processed_train_df["col28"] = processed_train_df["accommodates"] / processed_train_df.groupby(['room_type'])['accommodates'].transform('std')
 processed_train_df["col29"] = processed_train_df["accommodates"] / processed_train_df.groupby(['room_type'])['accommodates'].transform('mean')
 processed_test_df["col1"] = processed_test_df["bedrooms"] / processed_test_df["accommodates"]
processed_test_df["col2"] = processed_test_df["bathrooms"] / processed_test_df["accommodates"]
processed_test_df["col3"] = processed_test_df["beds"] / processed_test_df["bedrooms"]
processed_test_df["col4"] = processed_test_df["bedrooms"] + processed_test_df["bathrooms"]
processed_test_df["col5"] = processed_test_df["bedrooms"] + processed_test_df["bathrooms"] + processed_test_df["accommodates"] + processed_test_df["beds"]
processed_test_df["col6"] = processed_test_df["bedrooms"] / processed_test_df.groupby(['host_id'])['bedrooms'].transform('std')
 processed_test_df["col7"] = processed_test_df["bedrooms"] / processed_test_df.groupby(['host_id'])['bedrooms'].transform('mean')
 processed_test_df["col8"] = processed_test_df["bedrooms"] / processed_test_df.groupby(['property_type'])['bedrooms'].transform('std')
 processed_test_df["col9"] = processed_test_df["bedrooms"] / processed_test_df.groupby(['property_type'])['bedrooms'].transform('mean')
 processed_test_df["col10"] = processed_test_df["bedrooms"] / processed_test_df.groupby(['smart_location'])['bedrooms'].transform('std')
 processed_test_df["col11"] = processed_test_df["bedrooms"] / processed_test_df.groupby(['smart_location'])['bedrooms'].transform('mean')
 processed_test_df["col12"] = processed_test_df["bedrooms"] / processed_test_df.groupby(['room_type'])['bedrooms'].transform('std')
 processed_test_df["col13"] = processed_test_df["bedrooms"] / processed_test_df.groupby(['room_type'])['bedrooms'].transform('mean')
 processed_test_df["col14"] = processed_test_df["bathrooms"] / processed_test_df.groupby(['host_id'])['bathrooms'].transform('std')
 processed_test_df["col15"] = processed_test_df["bathrooms"] / processed_test_df.groupby(['host_id'])['bathrooms'].transform('mean')
 processed_test_df["col16"] = processed_test_df["bathrooms"] / processed_test_df.groupby(['property_type'])['bathrooms'].transform('std')
 processed_test_df["col17"] = processed_test_df["bathrooms"] / processed_test_df.groupby(['property_type'])['bathrooms'].transform('mean')
 processed_test_df["col18"] = processed_test_df["bathrooms"] / processed_test_df.groupby(['smart_location'])['bathrooms'].transform('std')
 processed_test_df["col19"] = processed_test_df["bathrooms"] / processed_test_df.groupby(['smart_location'])['bathrooms'].transform('mean')
 processed_test_df["col20"] = processed_test_df["bathrooms"] / processed_test_df.groupby(['room_type'])['bathrooms'].transform('std')
 processed_test_df["col21"] = processed_test_df["bathrooms"] / processed_test_df.groupby(['room_type'])['bathrooms'].transform('mean')
 processed_test_df["col22"] = processed_test_df["accommodates"] / processed_test_df.groupby(['host_id'])['accommodates'].transform('std')
 processed_test_df["col23"] = processed_test_df["accommodates"] / processed_test_df.groupby(['host_id'])['accommodates'].transform('mean')
 processed_test_df["col24"] = processed_test_df["accommodates"] / processed_test_df.groupby(['property_type'])['accommodates'].transform('std')
 processed_test_df["col25"] = processed_test_df["accommodates"] / processed_test_df.groupby(['property_type'])['accommodates'].transform('mean')
 processed_test_df["col26"] = processed_test_df["accommodates"] / processed_test_df.groupby(['smart_location'])['accommodates'].transform('std')
 processed_test_df["col27"] = processed_test_df["accommodates"] / processed_test_df.groupby(['smart_location'])['accommodates'].transform('mean')
 processed_test_df["col28"] = processed_test_df["accommodates"] / processed_test_df.groupby(['room_type'])['accommodates'].transform('std')
 processed_test_df["col29"] = processed_test_df["accommodates"] / processed_test_df.groupby(['room_type'])['accommodates'].transform('mean')
 return processed_train_df, processed_test_df
processed_train_df, processed_test_df = preprocess(train_df, test_df )<compute_test_metric>
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
def rmsle(y_true, y_pred):
assert len(y_true)== len(y_pred)
 return np.sqrt(np.mean(np.power(np.log1p(y_true + 1)- np.log1p(y_pred + 1), 2)))
 def rmsle_lgb(preds, data):
y_true = np.array(data.get_label())
 result = rmsle(preds, y_true)
 return 'RMSLE', result, False<init_hyperparams>
data['DAYS_EMPLOYED']
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmsle',
'max_depth': 20,
'max_bin': 200,
'num_leaves': 97,
'min_data_in_leaf': 10,
'learning_rate': 0.0022,
'feature_fraction': 0.8,
'bagging_fraction': 0.8,
'bagging_freq': 10,
'min_sum_hessian_in_leaf': 10,
'lambda_l1': 0.01,
'lambda_l2': 0.01,
'verbose': 0,
'metric': 'rmse'
}<prepare_x_and_y>
data['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True )
y = processed_train_df["price"].values
X = processed_train_df.drop("price", axis=1 ).values
features = processed_train_df.drop("price", axis=1 ).columns
X_test = processed_test_df.values
cols = processed_train_df.drop("price", axis=1 ).columns.values
categorical_cols = cat_list[:]
feature_importance_df = pd.DataFrame()
N = 5
oof = np.zeros(len(X))
test_preds = np.zeros(len(test_df))
kf = KFold(n_splits=N, shuffle=True, random_state=1)
 cv_score = []
for fold_,(train_idx, val_idx)in enumerate(kf.split(X), start=1):
X_train, X_val = X[train_idx], X[val_idx]
y_train, y_val = y[train_idx], y[val_idx]
lgb_train = lgb.Dataset(X_train, y_train, feature_name=processed_train_df.drop("price", axis=1 ).columns.tolist() , categorical_feature=categorical_cols)
 lgb_eval = lgb.Dataset(X_val, y_val, reference=lgb_train, feature_name=processed_train_df.drop("price", axis=1 ).columns.tolist() , categorical_feature=categorical_cols)
 lgb_reg = lgb.train(params,
lgb_train,
num_boost_round=20000,
valid_sets=lgb_eval,
early_stopping_rounds=100,
verbose_eval=500,
feval = rmsle_lgb)
 y_pred = lgb_reg.predict(X_val, num_iteration=lgb_reg.best_iteration)
 oof[val_idx] = lgb_reg.predict(X_val, num_iteration=lgb_reg.best_iteration)
 test_preds += lgb_reg.predict(X_test, num_iteration=lgb_reg.best_iteration)/ N
cv_score.append(rmsle(y_val, y_pred))
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = lgb_reg.feature_importance(importance_type='gain')
 fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print(f"fold{fold_}: {cv_score[-1]}
")
 sns.residplot(y_pred, y_val, lowess=True, color='g')
 plt.show()
print(f"CV RMSLE Score: {sum(cv_score)/len(cv_score)}" )<save_to_csv>
data['CODE_GENDER'].loc[data['CODE_GENDER']=='XNA']
sub_df["price"] = test_preds
sub_df.to_csv(f"submission{sum(cv_score)/len(cv_score)}.csv", index=False )<compute_test_metric>
data['CODE_GENDER'].replace({'XNA': 'F'}, inplace=True )
rmsle(y, oof )<prepare_x_and_y>
data['CODE_GENDER'].loc[data['CODE_GENDER']=='F']
l = []
for idx,(true, pred)in enumerate(zip(y, oof)) :
l.append([np.power(np.log1p(true + 1)- np.log1p(pred + 1), 2), idx])
 l.sort(reverse=True)
 l_idx = [x[1] for x in l[:len(l)//20]]
l_idx.sort()
idx = []
j = 0
for i in range(len(l)) :
if i == l_idx[j]:
if j < len(l_idx)- 1:
j += 1
else:
idx.append(i)
 y = processed_train_df["price"].values
X = processed_train_df.drop("price", axis=1 ).values
X = X[idx]
y = y[idx]<split>
data['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True )
N = 5
oof = np.zeros(len(X))
test_preds = np.zeros(len(test_df))
kf = KFold(n_splits=N, shuffle=True, random_state=1)
 cv_score = []
for fold_,(train_idx, val_idx)in enumerate(kf.split(X), start=1):
X_train, X_val = X[train_idx], X[val_idx]
y_train, y_val = y[train_idx], y[val_idx]
lgb_train = lgb.Dataset(X_train, y_train, feature_name=processed_train_df.drop("price", axis=1 ).columns.tolist() , categorical_feature=categorical_cols)
 lgb_eval = lgb.Dataset(X_val, y_val, reference=lgb_train, feature_name=processed_train_df.drop("price", axis=1 ).columns.tolist() , categorical_feature=categorical_cols)
 lgb_reg = lgb.train(params,
lgb_train,
num_boost_round=20000,
valid_sets=lgb_eval,
early_stopping_rounds=100,
verbose_eval=500,
feval = rmsle_lgb)
 y_pred = lgb_reg.predict(X_val, num_iteration=lgb_reg.best_iteration)
 oof[val_idx] = lgb_reg.predict(X_val, num_iteration=lgb_reg.best_iteration)
 test_preds += lgb_reg.predict(X_test, num_iteration=lgb_reg.best_iteration)/ N
cv_score.append(rmsle(y_val, y_pred))
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = lgb_reg.feature_importance(importance_type='gain')
 fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print(f"fold{fold_}: {cv_score[-1]}
")
 sns.residplot(y_pred, y_val, lowess=True, color='g')
 plt.show()
print(f"CV RMSLE Score: {sum(cv_score)/len(cv_score)}" )<save_to_csv>
data['YEARS_BUILD_CREDIT'] = data['AMT_CREDIT']/data['YEARS_BUILD_AVG']
sub_df["price"] = test_preds
sub_df.to_csv(f"v2_submission{sum(cv_score)/len(cv_score)}.csv", index=False )<prepare_x_and_y>
data['Annuity_Income'] = data['AMT_ANNUITY']/data['AMT_INCOME_TOTAL']
")
<load_from_csv>
data['Income_Cred'] = data['AMT_CREDIT']/data['AMT_INCOME_TOTAL']
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
<categorify>
data['EMP_AGE'] = data['DAYS_EMPLOYED']/data['DAYS_BIRTH']
class_names = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
train_text = train['comment_text']
test_text = test['comment_text']
all_text = pd.concat([train_text, test_text])
 word_vectorizer = TfidfVectorizer(
 sublinear_tf=True,
strip_accents='unicode',
analyzer='word',
token_pattern=r'\w{1,}',
stop_words='english',
ngram_range=(1, 1),
max_features=10000)
 word_vectorizer.fit(all_text)
 train_word_features = word_vectorizer.transform(train_text)
 test_word_features = word_vectorizer.transform(test_text)
 char_vectorizer = TfidfVectorizer(
 sublinear_tf=True,
strip_accents='unicode',
analyzer='char',
stop_words='english',
ngram_range=(2, 5),
max_features=50000)
 char_vectorizer.fit(all_text)
 train_char_features = char_vectorizer.transform(train_text)
 test_char_features = char_vectorizer.transform(test_text)
 train_features = hstack([train_char_features, train_word_features])
 test_features = hstack([test_char_features, test_word_features])
<import_modules>
data['Income_PP'] = data['AMT_INCOME_TOTAL']/data['CNT_FAM_MEMBERS']
from sklearn.svm import LinearSVC
from sklearn.ensemble import AdaBoostClassifier
from sklearn.calibration import CalibratedClassifierCV 
<create_dataframe>
data['CHILDREN_RATIO'] =(1 + data['CNT_CHILDREN'])/ data['CNT_FAM_MEMBERS']
scores = []
submission = pd.DataFrame.from_dict({'id': test['id']})
 for class_name in class_names:
train_target = train[class_name]
classifier = LogisticRegression(solver='sag')
 cv_score = np.mean(cross_val_score(classifier, train_features, train_target, cv=3, scoring='roc_auc'))
scores.append(cv_score)
 print('CV score for class {} is {}'.format(class_name, cv_score))
classifier.fit(train_features, train_target)
 submission[class_name] = classifier.predict_proba(test_features)[:, 1]
print('Total CV score is {}'.format(np.mean(scores)))
<feature_engineering>
data['PAYMENTS'] = data['AMT_ANNUITY']/ data['AMT_CREDIT']
")
<define_variables>
data['NEW_CREDIT_TO_GOODS_RATIO'] = data['AMT_CREDIT'] / data['AMT_GOODS_PRICE']
data['GOODS_INCOME'] = data['AMT_GOODS_PRICE']/data['AMT_INCOME_TOTAL']
ship_dir = '.. /input/almaz-antey-hackathon-l1/'
train_image_dir = os.path.join(ship_dir, 'train/train')
 test_image_dir = os.path.join(ship_dir, 'test/test' )<load_from_csv>
data['Ext_source_mult'] = data['EXT_SOURCE_1'] * data['EXT_SOURCE_2'] * data['EXT_SOURCE_3']
data['Ext_SOURCE_MEAN'] = data[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis = 1)
 data['Ext_SOURCE_SD'] = data[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].std(axis = 1 )
train_df = pd.read_csv(os.path.join(ship_dir, 'train_segmentation.csv'))
sample_sub = pd.read_csv(os.path.join(ship_dir, 'sample_submission.csv'))<categorify>
columns = ['Annuity_Income', 'Income_Cred', 'EMP_AGE', 'Income_PP']
montage_rgb = lambda x: np.stack([montage(x[:, :, :, i])for i in range(x.shape[3])], -1)
 def multi_rle_encode(img, **kwargs):

 labels = label(img)
 if img.ndim > 2:
return [rle_encode(np.sum(labels==k, axis=2), **kwargs)for k in np.unique(labels[labels>0])]
else:
return [rle_encode(labels==k, **kwargs)for k in np.unique(labels[labels>0])]
def rle_encode(img, min_max_threshold=1e-4, max_mean_threshold=None):

 if np.max(img)< min_max_threshold:
return ''
if max_mean_threshold and np.mean(img)> max_mean_threshold:
return ''
pixels = img.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs)
 def rle_decode(mask_rle, shape=(768, 768)) :

 s = mask_rle.split()
starts, lengths = [np.asarray(x, dtype=int)for x in(s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(shape[0]*shape[1], dtype=np.uint8)
 for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape ).T
def masks_as_image(in_mask_list, **kwargs):
all_masks = np.zeros(kwargs['shape'], dtype = np.uint8)
 for mask in in_mask_list:
if isinstance(mask, str):
all_masks |= rle_decode(mask, **kwargs)
 return all_masks
def masks_as_color(in_mask_list, **kwargs):
all_masks = np.zeros(kwargs['shape'], dtype = np.float)
 scale = lambda x:(len(in_mask_list)+ x + 1)/(len(in_mask_list)* 2)
 for i,mask in enumerate(in_mask_list):
if isinstance(mask, str):
all_masks[:,:] += scale(i)* rle_decode(mask, **kwargs)
 return all_masks<drop_column>
test['CODE_GENDER'].replace({'XNA': 'F'}, inplace=True)
 test['YEARS_BUILD_CREDIT'] = test['AMT_CREDIT']/test['YEARS_BUILD_AVG']
test['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True)
 test['Annuity_Income'] = test['AMT_ANNUITY']/test['AMT_INCOME_TOTAL']
test['Income_Cred'] = test['AMT_CREDIT']/test['AMT_INCOME_TOTAL']
test['EMP_AGE'] = test['DAYS_EMPLOYED']/test['DAYS_BIRTH']
test['Income_PP'] = test['AMT_INCOME_TOTAL']/test['CNT_FAM_MEMBERS']
test['CHILDREN_RATIO'] =(1 + test['CNT_CHILDREN'])/ test['CNT_FAM_MEMBERS']
test['PAYMENTS'] = test['AMT_ANNUITY']/ test['AMT_CREDIT']
test['NEW_CREDIT_TO_GOODS_RATIO'] = test['AMT_CREDIT'] / test['AMT_GOODS_PRICE']
test['GOODS_INCOME'] = test['AMT_GOODS_PRICE']/test['AMT_INCOME_TOTAL']
test['Ext_source_mult'] = test['EXT_SOURCE_1'] * test['EXT_SOURCE_2'] * test['EXT_SOURCE_3']
test['Ext_SOURCE_MEAN'] = test[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis = 1)
 test['Ext_SOURCE_SD'] = test[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].std(axis = 1 )
generator = ship_generator(train_df, train_image_dir, batch_size=4 )<compute_test_metric>
bureau_new = bureau
def dice_coef2(y_true, y_pred):
y_true_f = y_true.flatten()
y_pred_f = y_pred.flatten()
union = np.sum(y_true_f)+ np.sum(y_pred_f)
 if union==0: return 1
intersection = np.sum(y_true_f * y_pred_f)
 return 2.* intersection / union<categorify>
group = bureau_new[['SK_ID_CURR', 'DAYS_CREDIT']].groupby('SK_ID_CURR')['DAYS_CREDIT'].count().reset_index().rename(index=str, columns={'DAYS_CREDIT': 'BUREAU_LOAN_COUNT'} )
val_data = train_df.sample(200, replace=False ).reset_index()
val_data.EncodedPixels = val_data.EncodedPixels.map(lambda x: rle_decode(x))<categorify>
bureau_new = bureau_new.merge(group, how = 'left', on = 'SK_ID_CURR' )
pbar = tqdm(val_data.index)
 predicts = []
for idx in pbar:
fpath = os.path.join(train_image_dir, val_data.iloc[idx].ImageId)
 image, mask = find_mask(fpath)
 predicts.append(mask )<compute_test_metric>
del group
dice = np.mean([dice_coef2(y, predict)for y, predict in zip(val_data.EncodedPixels.to_numpy() , predicts)] )<train_model>
group = bureau_new[['SK_ID_CURR', 'CREDIT_TYPE']].groupby('SK_ID_CURR')['CREDIT_TYPE'].nunique().reset_index().rename(index=str, columns = {'CREDIT_TYPE': 'LOAN_TYPES_PER_CUST'} )
print(f'DICE = {dice}' )<categorify>
bureau_new = bureau_new.merge(group,on = ['SK_ID_CURR'], how = 'left')
 del group
pbar = tqdm(sample_sub.index[:])
 for idx in pbar:
fpath = os.path.join(test_image_dir, sample_sub.iloc[idx].ImageId)
 image, mask = find_mask(fpath)
 encode_mask = rle_encode(mask)
 sample_sub.iloc[idx].EncodedPixels = encode_mask<save_to_csv>
bureau_new["AVERAGE_LOAN_TYPE"] = bureau_new['BUREAU_LOAN_COUNT']/bureau_new['LOAN_TYPES_PER_CUST']
sample_sub.to_csv('submission.csv', index=False)
 sample_sub.head()<load_from_csv>
replace = {'Active': 1, 'Closed':0, 'Sold': 1, 'Bad debt': 1}
bureau_new['CREDIT_ACTIVE'] = bureau_new['CREDIT_ACTIVE'].replace(replace )
test = pd.read_csv('.. /input/test.csv')
 train = df = pd.read_csv('.. /input/train.csv' )<concatenate>
gp = bureau_new.groupby('SK_ID_CURR')['CREDIT_ACTIVE'].mean().reset_index().rename(index=str, columns={'CREDIT_ACTIVE': 'ACTIVE_LOANS_PERCENTAGE'} )
df = pd.concat([train, test] )<define_variables>
bureau_new = bureau_new.merge(gp, on = 'SK_ID_CURR', how = 'left' )
numero = [c for c in df.columns if c not in texto]
numero = [c for c in numero if c not in ['nota_mat', 'codigo_mun', 'Unnamed: 0']]<statistical_test>
del gp
def outliers_iqr(ys):
quartile_1, quartile_3 = np.percentile(ys, [25, 75])
 iqr = quartile_3 - quartile_1
lower_bound = quartile_1 -(iqr * 1.5)
 upper_bound = quartile_3 +(iqr * 1.5)
 return np.where(( ys > upper_bound)|(ys < lower_bound))<define_variables>
def rep(x):
if x<0:
y=0
else:
y=1
return y
for a in numero:
quartile_1, quartile_3 = np.percentile(df[a], [25, 75])
 iqr = quartile_3 - quartile_1
lower_bound = quartile_1 -(iqr * 1.5)
 upper_bound = quartile_3 +(iqr * 1.5)
 df[a][df[a] < lower_bound] = None
df[a][df[a] > upper_bound] = None<define_variables>
bureau_new['CREDIT_ENDDATE_BINARY'] = bureau_new['DAYS_CREDIT_ENDDATE'].apply(lambda x: rep(x))
texto = [c for c in texto if c not in ['codigo_mun']]<data_type_conversions>
grp = bureau_new.groupby('SK_ID_CURR')['CREDIT_ENDDATE_BINARY'].mean().reset_index().rename(index=str, columns={'CREDIT_ENDDATE_BINARY': 'CREDIT_ENDDATE_PERCENTAGE'} )
for c in texto:
df[c]=df[c].astype('category' ).cat.codes<define_variables>
bureau_new = bureau_new.merge(grp, on = 'SK_ID_CURR', how = 'left')
 del grp
feats = [c for c in df.columns if c not in ['nota_mat','codigo_mun', 'Unnamed: 0']]<groupby>
num_aggregations = {
'DAYS_CREDIT': ['min', 'max', 'mean', 'var'],
'DAYS_CREDIT_ENDDATE': ['min', 'max', 'mean'],
'DAYS_CREDIT_UPDATE': ['mean'],
'CREDIT_DAY_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_MAX_OVERDUE': ['mean'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['mean'],
'AMT_CREDIT_SUM_LIMIT': ['mean', 'sum'],
'AMT_ANNUITY': ['max', 'mean'],
'CNT_CREDIT_PROLONG': ['sum'],
}
nme = df.groupby(['estado'], as_index=False ).mean()
for c in nme.columns:
nme[c].fillna(nme[c].mean() , inplace=True )<merge>
bureau_agg = bureau_new.groupby('SK_ID_CURR' ).agg({**num_aggregations} )
df2 = pd.merge(df, nme, left_on='estado', right_on='estado', how='left', suffixes=('', '_mean'))
df2['estado_mean']=df2['estado']<define_variables>
bureau_agg.columns = pd.Index(['BURO_' + e[0] + "_" + e[1].upper() for e in bureau_agg.columns.tolist() ])
 bureau_agg.reset_index(inplace=True )
feats2 = [c for c in feats if c not in ['codigo_mun']]<data_type_conversions>
bureau_merge = bureau_new.merge(bureau_agg, on = 'SK_ID_CURR', how = 'left')
 del bureau_agg
for c in feats2:
df[c].fillna(df2[c+'_mean'], inplace=True )<feature_engineering>
buro_cat_features = [bcol for bcol in bureau_merge.columns if bureau_merge[bcol].dtype == 'object']
df['nota_mat'] = np.log(df['nota_mat'] )<import_modules>
buro = pd.get_dummies(bureau_merge, columns=buro_cat_features )
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec 
<set_options>
cat_columns = [col for col in bureau_balance.columns if bureau_balance[col].dtype == 'object']
plt.rcParams['figure.figsize'] =(20,30 )<prepare_x_and_y>
bureau_balance = pd.get_dummies(bureau_balance,cat_columns, dummy_na = True )
test = df[df['nota_mat'].isnull() ]
train = df[df['nota_mat'].notnull() ]<import_modules>
bb_group = bureau_balance.groupby('SK_ID_BUREAU' ).agg(['min', 'max', 'mean'] )
from sklearn.model_selection import train_test_split<import_modules>
bb_group.columns = pd.Index([e[0] + "_" + e[1].upper() for e in bb_group.columns.tolist() ])
 bb_group.reset_index(inplace=True)
from sklearn.model_selection import train_test_split<split>
buro = buro.merge(bb_group, on = 'SK_ID_BUREAU', how = 'left' )
train, valid = train_test_split(train, random_state = 42 )<find_best_params>
avg_buro = buro.groupby('SK_ID_CURR' ).mean()
rf = RandomForestRegressor(random_state = 42)
 print('Parameters currently in use:
')
 pprint(rf.get_params() )<find_best_params>
avg_buro['buro_count'] = buro[['SK_ID_BUREAU', 'SK_ID_CURR']].groupby('SK_ID_CURR' ).count() ['SK_ID_BUREAU']
del avg_buro['SK_ID_BUREAU'], bb_group
best = rf_random.best_params_
best2 = {'n_estimators': 800,
'min_samples_split': 2,
'min_samples_leaf': 2,
'max_features': 'sqrt',
'max_depth': 50,
'bootstrap': False}
best<choose_model_class>
cat_columns = [col for col in installments_payments.columns if installments_payments[col].dtype == 'object']
best_random = RandomForestRegressor(n_estimators= 800, min_samples_split = 2, min_samples_leaf= 2, max_features = 'sqrt', max_depth =50, bootstrap= False )<compute_test_metric>
installments_payments = pd.get_dummies(installments_payments,cat_columns, dummy_na = True )
def evaluate(model, test_features, test_labels):
predictions = model.predict(test_features)
 errors = abs(predictions - test_labels)
 mape = 100 * np.mean(errors / test_labels)
 accuracy = 100 - mape
print('Model Performance')
 print('Average Error: {:0.4f} degrees.'.format(np.mean(errors)))
 print('Accuracy = {:0.2f}%.'.format(accuracy))
return accuracy<train_model>
installments_payments['AMOUNT_DIFF'] = installments_payments['AMT_INSTALMENT'] - installments_payments['AMT_PAYMENT']
base_model = RandomForestRegressor(n_estimators = 10, random_state = 42)
 base_model.fit(train[feats], train['nota_mat'])
 base_accuracy = evaluate(base_model, valid[feats], valid['nota_mat'] )<train_model>
installments_payments['AMOUNT_PERC'] = installments_payments['AMT_PAYMENT']/installments_payments['AMT_INSTALMENT']
best_random.fit(train[feats], train['nota_mat'])
 random_accuracy = evaluate(best_random, valid[feats], valid['nota_mat'] )<compute_test_metric>
installments_payments['DAYS_P'] = installments_payments['DAYS_ENTRY_PAYMENT']-installments_payments['DAYS_INSTALMENT']
installments_payments['DAYS_I'] = installments_payments['DAYS_INSTALMENT']-installments_payments['DAYS_ENTRY_PAYMENT']
print('Improvement of {:0.2f}%.'.format(100 *(random_accuracy - base_accuracy)/ base_accuracy))<predict_on_test>
aggregations = {
'NUM_INSTALMENT_VERSION': ['nunique'],
'DAYS_P': ['max', 'mean', 'sum'],
'DAYS_I': ['max', 'mean', 'sum'],
'AMOUNT_DIFF': ['max', 'mean', 'sum', 'var'],
'AMOUNT_PERC': ['max', 'mean', 'sum', 'var'],
'AMT_INSTALMENT': ['max', 'mean', 'sum'],
'AMT_PAYMENT': ['min', 'max', 'mean', 'sum'],
'DAYS_ENTRY_PAYMENT': ['max', 'mean', 'sum']
}
test['nota_mat'] = best_random.predict(test[feats] )<data_type_conversions>
installments_payments_agg = installments_payments.groupby('SK_ID_CURR' ).agg(aggregations )
test['nota_mat'] = np.exp(test['nota_mat'])
 test['codigo_mun'] = test['codigo_mun'].apply(lambda x: x.replace('ID_ID_', '')) 
<save_to_csv>
installments_payments_agg.columns = pd.Index(['INSTALL_' + e[0] + "_" + e[1].upper() for e in installments_payments_agg.columns.tolist() ])
 installments_payments_agg.reset_index(inplace=True)
test[['codigo_mun','nota_mat']].to_csv('rf.csv', index=False )<load_from_csv>
installments_payments = installments_payments.merge(installments_payments_agg, on = 'SK_ID_CURR',how = 'left' )
df = pd.read_csv(".. /input/train.csv")
 dfTeste = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
previous_application['DAYS_FIRST_DRAWING'].replace(365243, np.nan, inplace= True)
 previous_application['DAYS_FIRST_DUE'].replace(365243, np.nan, inplace= True)
 previous_application['DAYS_LAST_DUE_1ST_VERSION'].replace(365243, np.nan, inplace= True)
 previous_application['DAYS_LAST_DUE'].replace(365243, np.nan, inplace= True)
 previous_application['DAYS_TERMINATION'].replace(365243, np.nan, inplace= True)
 previous_application['INTEREST_PERC'] =(previous_application['RATE_INTEREST_PRIMARY']/100)*previous_application['AMT_DOWN_PAYMENT']
previous_application['INTEREST_ANN_PERC'] =(previous_application['RATE_INTEREST_PRIMARY']/100)*previous_application['AMT_ANNUITY']
previous_application['INTEREST_CREDIT_PERC'] =(previous_application['RATE_INTEREST_PRIMARY']/100)*previous_application['AMT_CREDIT']
previous_application['FIRST_LAST'] = previous_application['DAYS_FIRST_DUE'] - previous_application['DAYS_LAST_DUE']
y = 'nota_mat'<split>
previous_application['APPLICATION_ACTUAL_CREDIT'] = previous_application['AMT_APPLICATION']/previous_application['AMT_CREDIT']
train, test = train_test_split(df, random_state=42)
 train.shape, test.shape<feature_engineering>
num_aggregations = {
'AMT_ANNUITY': ['min', 'max', 'mean'],
'AMT_APPLICATION': ['min', 'max', 'mean'],
'AMT_CREDIT': ['min', 'max', 'mean'],
'INTEREST_CREDIT_PERC': ['min', 'max', 'mean', 'var'],
'AMT_DOWN_PAYMENT': ['min', 'max', 'mean'],
'AMT_GOODS_PRICE': ['min', 'max', 'mean'],
'HOUR_APPR_PROCESS_START': ['min', 'max', 'mean'],
'RATE_DOWN_PAYMENT': ['min', 'max', 'mean'],
'DAYS_DECISION': ['min', 'max', 'mean'],
'CNT_PAYMENT': ['mean', 'sum'],
'FIRST_LAST': ['mean', 'max', 'min']
}
prev_agg = previous_application.groupby('SK_ID_CURR' ).agg(num_aggregations)
 prev_agg.columns = pd.Index(['PREV_' + e[0] + "_" + e[1].upper() for e in prev_agg.columns.tolist() ])
 prev_agg.reset_index(inplace=True)
 previous_application = previous_application.merge(prev_agg, on = 'SK_ID_CURR', how = 'left')
 del prev_agg
df['codigo_mun'] = df['codigo_mun'].str.replace('ID_ID_','')
 df['comissionados_por_servidor'] = df['comissionados_por_servidor'].str.replace('%','')
 df['area']=df['area'].str.replace(',','')
 df['ranking_igm']=df['ranking_igm'].str.replace('º','')
 df['densidade_dem']=df['densidade_dem'].str.replace(',','')
 dfTeste['codigo_mun'] = dfTeste['codigo_mun'].str.replace('ID_ID_','')
 dfTeste['comissionados_por_servidor'] = dfTeste['comissionados_por_servidor'].str.replace('%','')
 dfTeste['area']=dfTeste['area'].str.replace(',','')
 dfTeste['ranking_igm']=dfTeste['ranking_igm'].str.replace('º','')
 dfTeste['densidade_dem']=dfTeste['densidade_dem'].str.replace(',','')
<data_type_conversions>
approved = previous_application[previous_application['NAME_CONTRACT_STATUS'] == 'Approved']
df['codigo_mun']=df['codigo_mun'].values.astype('int64')
 df['area']=df['area'].values.astype('float64')
 df['densidade_dem']=df['densidade_dem'].values.astype('float64')
 dfTeste['codigo_mun']=dfTeste['codigo_mun'].values.astype('int64')
 dfTeste['area']=dfTeste['area'].values.astype('float64')
 dfTeste['densidade_dem']=dfTeste['densidade_dem'].values.astype('float64')
 df.head() 
<data_type_conversions>
approved_agg = approved.groupby('SK_ID_CURR' ).agg(num_aggregations )
for c in['regiao', 'estado', 'porte']:
df[c] = df[c].astype('category' ).cat.codes
for c in['regiao', 'estado', 'porte']:
dfTeste[c] = dfTeste[c].astype('category' ).cat.codes<data_type_conversions>
approved_agg.columns = pd.Index(['APPROVED_' + e[0] + "_" + e[1].upper() for e in approved_agg.columns.tolist() ] )
for c in['densidade_dem', 'perc_pop_econ_ativa', 'exp_vida','exp_anos_estudo','gasto_pc_saude','hab_p_medico','gasto_pc_educacao','exp_anos_estudo','idhm']:
df[c] = df[c].fillna(( df[c].mean()))
for c in['densidade_dem', 'perc_pop_econ_ativa', 'exp_vida','exp_anos_estudo','gasto_pc_saude','hab_p_medico','gasto_pc_educacao','exp_anos_estudo','idhm']:
dfTeste[c] = dfTeste[c].fillna(( dfTeste[c].mean()))<define_variables>
previous_application = previous_application.merge(approved_agg, how='left', on='SK_ID_CURR' )
feats = ['exp_anos_estudo']<split>
refused = previous_application[previous_application['NAME_CONTRACT_STATUS'] == 'Refused']
refused_agg = refused.groupby('SK_ID_CURR' ).agg(num_aggregations)
 refused_agg.columns = pd.Index(['REFUSED_' + e[0] + "_" + e[1].upper() for e in refused_agg.columns.tolist() ])
 refused_agg.reset_index(inplace=True)
 previous_application = previous_application.merge(refused_agg, how='left', on='SK_ID_CURR')
train, test = train_test_split(df, random_state=42)
 train.shape, test.shape<choose_model_class>
aggregations = {
'MONTHS_BALANCE': ['max', 'mean', 'size'],
'SK_DPD': ['max', 'mean'],
'SK_DPD_DEF': ['max', 'mean']
}
rf = RandomForestRegressor(random_state=42, n_jobs=-1,n_estimators=30000,min_samples_leaf=650)
 y='nota_mat' 
<train_model>
POS_CASH_AGG = POS_CASH_balance.groupby('SK_ID_CURR' ).agg(aggregations )
rf.fit(df[feats], df[y] )<import_modules>
POS_CASH_AGG.columns = pd.Index(['POS_CASH_' + e[0] + "_" + e[1].upper() for e in POS_CASH_AGG.columns.tolist() ] )
from sklearn.metrics import mean_squared_error 
<predict_on_test>
POS_CASH_AGG['COUNT'] = POS_CASH_AGG.groupby('SK_ID_CURR' ).size()
valid_preds = rf.predict(test[feats])
 mean_squared_error(test[y], valid_preds)**(1/2)
<predict_on_test>
cat_columns = [col for col in POS_CASH_balance.columns if POS_CASH_balance[col].dtype == 'object']
POS_CASH_balance = pd.get_dummies(POS_CASH_balance,cat_columns, dummy_na = True)
 POS_CASH_balance = POS_CASH_balance.merge(POS_CASH_AGG, how = 'left', on = 'SK_ID_CURR')
 POS_CASH_balance.head()
dfTeste[y]=rf.predict(dfTeste[feats])
<save_to_csv>
POS_CASH_balance = POS_CASH_balance.groupby('SK_ID_CURR' ).mean().reset_index()
dfTeste[['codigo_mun', y]].to_csv('rf3.csv', index=False )<load_from_csv>
del POS_CASH_AGG, POS_CASH_balance['SK_ID_PREV']
df = pd.read_csv('.. /input/train.csv' )<load_from_csv>
y = data['TARGET']
del data['TARGET']
categorical_features = [col for col in data.columns if data[col].dtype == 'object']
one_hot_df = pd.concat([data,test])
 one_hot_df = pd.get_dummies(one_hot_df, columns=categorical_features)
 data = one_hot_df.iloc[:data.shape[0],:]
test = one_hot_df.iloc[data.shape[0]:,]
print(data.shape, test.shape )
test = pd.read_csv('.. /input/test.csv' )<feature_engineering>
print('Removing features with more than 80% missing...')
 test = test[test.columns[data.isnull().mean() < 0.80]]
data = data[data.columns[data.isnull().mean() < 0.80]]
print(data.shape, test.shape )
df['area']=[float(a)for a in [i.replace(',','')for i in list(df['area'].values)]]<feature_engineering>
gc.enable()
folds = KFold(n_splits=4, shuffle=True, random_state=546789)
 oof_preds = np.zeros(data.shape[0])
 sub_preds = np.zeros(test.shape[0])
 feature_importance_df = pd.DataFrame()
feats = [f for f in data.columns if f not in ['SK_ID_CURR']]
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(data)) :
trn_x, trn_y = data[feats].iloc[trn_idx], y.iloc[trn_idx]
val_x, val_y = data[feats].iloc[val_idx], y.iloc[val_idx]
clf = LGBMClassifier(
 n_estimators=10000,
learning_rate=0.03,
num_leaves=34,
colsample_bytree=0.9,
subsample=0.8,
max_depth=8,
reg_alpha=.1,
reg_lambda=.1,
min_split_gain=.01,
min_child_weight=300,
silent=-1,
verbose=-1, 
)
 clf.fit(trn_x, trn_y,
eval_set= [(trn_x, trn_y),(val_x, val_y)],
eval_metric='auc', verbose=100, early_stopping_rounds=100 
)
 oof_preds[val_idx] = clf.predict_proba(val_x, num_iteration=clf.best_iteration_)[:, 1]
sub_preds += clf.predict_proba(test[feats], num_iteration=clf.best_iteration_)[:, 1] / folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = feats
fold_importance_df["importance"] = clf.feature_importances_
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('Fold %2d AUC : %.6f' %(n_fold + 1, roc_auc_score(val_y, oof_preds[val_idx])))
 del clf, trn_x, trn_y, val_x, val_y
gc.collect()
print('Full AUC score %.6f' % roc_auc_score(y, oof_preds))
test['TARGET'] = sub_preds
test[['SK_ID_CURR', 'TARGET']].to_csv('submission1LGBM1.csv', index=False)