Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<find_best_params>
import numpy as np
import pandas as pd
CV_rf.best_params_<train_model>
from tqdm.notebook import tqdm
import random
import gc
import time
model_fit = RandomForestClassifier(random_state=42, bootstrap= True, criterion= 'entropy', n_estimators= 800 )<train_model>
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold
from sklearn.ensemble import ExtraTreesClassifier
%time model_fit = model_fit.fit(X_train, y_train)
 result = model_fit.predict(X_test )<compute_test_metric>
import lightgbm as lgb
accuracy_score(y_test, result )<train_model>
gc.enable()
%time model_final = model_fit.fit(train_data_features, train_y )<load_from_csv>
train_data = pd.read_csv('/kaggle/input/home-credit-default-risk/application_train.csv',
na_values=['XNA', 'XAP'], na_filter=True)
 test_data = pd.read_csv('/kaggle/input/home-credit-default-risk/application_test.csv',
na_values=['XNA', 'XAP'], na_filter=True )
test = pd.read_csv('.. /input/fakenewsvortexbsb/sample_submission.csv', sep=';', error_bad_lines=False, quoting=3);<categorify>
train_counts = train_data.count().sort_values() /len(train_data)
 test_counts = test_data.count().sort_values() /len(test_data )
clean_test_review=[]
for i in range(0,num_reviews):
clean_test_review.append(review_to_words(test['Manchete'][i]))<categorify>
cols = set(train_counts[(train_counts < 1)&(train_counts > 0.99)].index)- set(test_counts[(test_counts < 1)&(test_counts > 0.9)].index )
test_data_features = vectorizer.transform(clean_test_review)
 test_data_features=test_data_features.toarray()<predict_on_test>
train_data.dropna(subset=cols, inplace=True )
result_test = model_final.predict(test_data_features )<save_to_csv>
train_target = train_data[['SK_ID_CURR', 'TARGET']]
minha_sub = pd.DataFrame({'index': test.index, 'Category': result_test})
 minha_sub.to_csv('submission.csv', index=False )<import_modules>
submit = test_data[['SK_ID_CURR']]
import pandas as pd
import numpy as np
import sklearn
import matplotlib.pyplot as plt
from sklearn import ensemble, neighbors
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_log_error, make_scorer
from sklearn.feature_selection import f_regression
import os<load_from_csv>
train_data.drop(columns=['TARGET'], inplace=True )
test_d = pd.read_csv('.. /input/test.csv',index_col='Id')
 train_d = pd.read_csv('.. /input/train.csv', index_col='Id' )<concatenate>
test_data['IS_TRAIN'] = 0
train_data['IS_TRAIN'] = 1
test_d['median_house_value'] = np.nan
data = train_d.append(test_d, sort=False )<feature_engineering>
application_data = train_data.append(test_data )
train_d['median_age_sum'] = train_d.groupby('median_age')['median_house_value'].transform('sum')
 ages = train_d['median_age'].value_counts()
for i, row in train_d.iterrows() :
train_d.loc[i,'median_age_freq'] = ages[train_d.loc[i,'median_age']]<feature_engineering>
del(train_data)
 del(test_data )
data['rooms_not_bedrooms'] = np.subtract(data['total_rooms'],data['total_bedrooms'])
 data['bedrooms_per_house'] = np.divide(data['total_bedrooms'],data['households'])
 data['rooms_not_bedrooms_per_house'] = np.divide(data['total_bedrooms'],data['households'])
 data['pop_per_house'] = np.divide(data['population'],data['households'])
 data['pop_per_bedroom'] = np.divide(data['population'],data['total_bedrooms'])
 data['pop_per_room'] = np.divide(data['population'],data['total_bedrooms'])
 data['income_per_pop'] = np.divide(data['median_income'],data['pop_per_house'] )<feature_engineering>
appl_counts = application_data.count().sort_values() /len(application_data )
ages_disc=[]
for i,row in data.iterrows() :
if row['median_age'] < 16:
ages_disc.append(0)
 elif row['median_age'] < 36:
ages_disc.append(1)
 elif row['median_age'] < 52:
ages_disc.append(2)
 else:
ages_disc.append(3)
 data['age_class'] = ages_disc<data_type_conversions>
appl_counts[(appl_counts < 0.6)]
corr_superior = correlacao.where(np.triu(np.ones(correlacao.shape), k=1 ).astype(np.bool))
drop = [column for column in corr_superior.columns if any(abs(corr_superior[column])> 0.9)]
drop<filter>
appl_counts = application_data.count().sort_values() /len(application_data )
correlacao.loc[correlacao['total_bedrooms'].abs() > 0.9, correlacao['total_bedrooms'].abs() > 0.9]<filter>
cols = list(set(appl_counts[(appl_counts < 0.6)].index)- set(['EXT_SOURCE_1', 'OWN_CAR_AGE']))
correlacao.loc[correlacao['rooms_not_bedrooms'].abs() > 0.9, correlacao['rooms_not_bedrooms'].abs() > 0.9]<filter>
le = LabelEncoder()
for col in application_data.select_dtypes('object'):
if len(application_data[col].unique())<= 2:
le.fit(application_data[col])
 application_data[col] = le.transform(application_data[col] )
correlacao.loc[correlacao['rooms_not_bedrooms_per_house'].abs() > 0.9, correlacao['rooms_not_bedrooms_per_house'].abs() > 0.9]<filter>
application_data = pd.get_dummies(application_data, dummy_na=True )
correlacao.loc[correlacao['pop_per_bedroom'].abs() > 0.9, correlacao['pop_per_bedroom'].abs() > 0.9]<drop_column>
appl_counts = application_data.count().sort_values() /len(application_data )
data.drop(['rooms_not_bedrooms_per_house','pop_per_room','pop_per_house','total_rooms','median_age'], axis = 1, inplace = True )<feature_engineering>
appl_counts[(appl_counts < 1)]
normcols = list(data.columns)
 [normcols.remove(x)for x in ['latitude','longitude','median_house_value']]
fit_norm = data.dropna()
fit_norm = fit_norm[normcols]
means = {}
stds = {}
for c in normcols:
means[c] = fit_norm[c].mean()
stds[c] = fit_norm[c].std()
fit_norm = data.copy()
for c in normcols:
fit_norm.loc[:,c] = fit_norm.loc[:,c].subtract(means[c] ).divide(stds[c])
 fit_norm['latitude'] = data['latitude']
fit_norm['longitude'] = data['longitude']
fit_norm['median_house_value'] = data['median_house_value']
fit_norm.head()<compute_test_metric>
train_data = application_data[application_data.IS_TRAIN == 1].merge(train_target, how='left', on='SK_ID_CURR' )
msle = make_scorer(mean_squared_log_error )<train_on_grid>
del(train_data )
knn = KNeighborsRegressor(n_neighbors=10)
 scores = cross_val_score(knn,
fit_norm.dropna().drop('median_house_value', axis = 1),
fit_norm.dropna().loc[:,'median_house_value'],
cv=10,
scoring = msle)
 scores = np.sqrt(scores)
 display(scores)
 print(f'MSLE = {round(scores.mean() , 4)}, std = {round(scores.std() , 4)}' )<find_best_model_class>
bureau = pd.read_csv('.. /input/home-credit-default-risk/bureau.csv',
na_values=['XNA', 'XAP'], na_filter=True )
scores_array = []
scores_var = []
for i in range(21):
knn = KNeighborsRegressor(n_neighbors=10, metric='wminkowski', p=2, metric_params={'w': np.divide([i/20]*2+[1-i/20]*9,15)})
 scores = cross_val_score(knn,
fit_norm.dropna().drop('median_house_value', axis = 1),
fit_norm.dropna().loc[:,'median_house_value'],
cv=10,
scoring = msle)
 scores = np.sqrt(scores)
 scores_array.append(scores.mean())
 scores_var.append(scores.std() )<drop_column>
application_data['IS_IN_BUREAU'] = 0
test_data = fit_norm.loc[fit_norm['median_house_value'].isnull() ].drop('median_house_value',axis=1 )<save_to_csv>
application_data.loc[application_data.SK_ID_CURR.isin(bureau.SK_ID_CURR.unique()), 'IS_IN_BUREAU'] = 1
classifier = KNeighborsRegressor(n_neighbors=8, metric='wminkowski', p=2, metric_params={'w': np.divide([18/20]*2+[1-18/20]*9,15)})
 classifier.fit(fit_norm.dropna().drop('median_house_value', axis = 1),fit_norm.dropna().loc[:,'median_house_value'])
 testPred = classifier.predict(test_data)
 arq = open("prediction_knn.csv", "w")
 arq.write("Id,median_house_value
")
 for i, j in zip(test_data.index, testPred):
arq.write(str(i)+ "," + str(int(j)) +"
")
 arq.close()<save_to_csv>
appl_counts = application_data.count().sort_values() /len(application_data)
 appl_counts[(appl_counts < 1)]
regr = RandomForestRegressor(max_depth=20, random_state=0,n_estimators=400)
 regr.fit(fit_norm.dropna().drop('median_house_value', axis = 1),fit_norm.dropna().loc[:,'median_house_value'])
 testPred = regr.predict(test_data)
 arq = open("prediction_rfr.csv", "w")
 arq.write("Id,median_house_value
")
 for i, j in zip(test_data.index, testPred):
arq.write(str(i)+ "," + str(int(j)) +"
")
 arq.close()<save_to_csv>
application_data['HAS_SOCIAL_CIRCLE'] = 0
boost = AdaBoostRegressor(RandomForestRegressor(max_depth=5, random_state=0, n_estimators=300),
n_estimators=100)
 boost.fit(fit_norm.dropna().drop('median_house_value', axis = 1),fit_norm.dropna().loc[:,'median_house_value'])
 testPred = boost.predict(test_data)
 arq = open("prediction_ada_rfr.csv", "w")
 arq.write("Id,median_house_value
")
 for i, j in zip(test_data.index, testPred):
arq.write(str(i)+ "," + str(int(j)) +"
")
 arq.close()<import_modules>
application_data.loc[~application_data.OBS_30_CNT_SOCIAL_CIRCLE.isnull() , 'HAS_SOCIAL_CIRCLE'] = 1
import sklearn
from sklearn.metrics import mean_squared_log_error, make_scorer
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import Lasso
import sklearn.naive_bayes as skNB
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor,AdaBoostRegressor
from sklearn.neighbors import KNeighborsRegressor
from IPython.display import Image
from IPython.core.display import HTML<load_from_csv>
application_data['AMT_CREDIT_FRAC'] = application_data.AMT_CREDIT / application_data.AMT_INCOME_TOTAL
train_raw = pd.read_csv(".. /input/atividade-3-pmr3508/train.csv", sep = ",")
 train = train_raw.copy()
target = train_raw["median_house_value"]
display(train_raw.shape,
train_raw.head() )<load_from_csv>
application_data['AMT_CREDIT_FRAC'] = application_data.AMT_ANNUITY / application_data.AMT_CREDIT
test_raw = pd.read_csv(".. /input/atividade-3-pmr3508/test.csv", sep = ",",header=0)
 test = test_raw.copy()
display(test_raw.shape,test_raw.head())
<feature_engineering>
application_data['AMT_GOODS_FRAC'] = application_data.AMT_GOODS_PRICE / application_data.AMT_CREDIT
def New_features(data):
data.loc[:,'rooms_per_household'] = data.loc[:,'total_rooms']/data.loc[:,'households']
data.loc[:,'rooms_per_person'] = data.loc[:,'total_rooms']/data.loc[:,'population']
data.loc[:,'bedrooms_per_household'] = data.loc[:,'total_bedrooms']/data.loc[:,'households']
data.loc[:,'bedrooms_per_person'] = data.loc[:,'total_bedrooms']/data.loc[:,'households']
data.loc[:,'persons_per_household'] = data.loc[:,'population']/data.loc[:,'households']
data.loc[:, 'median_income_per_person'] = data.loc[:,'median_income']/data.loc[:,'persons_per_household']
New_features(train_raw)
 New_features(test)
 New_features(train)
 train.head()<load_from_csv>
application_data['AMT_ANNUITY_FRAC'] = application_data.AMT_ANNUITY / application_data.AMT_INCOME_TOTAL
arq = open(".. /input/us-citiestxt/US_cities.txt","r")
 new_table=[]
linhas=arq.readlines()
for i in range(87,len(linhas)) :
palavras=linhas[i].split(",")
 if palavras[1] == "5":
new_table.append([palavras[3][1:-1],palavras[5][:-1],palavras[4]])
 arq=open("US_cities.csv","w")
 for i in new_table:
arq.write(i[0]+","+i[1]+","+i[2]+"
" )<load_from_csv>
application_data['AMT_DPD_DEF'] = application_data.DEF_30_CNT_SOCIAL_CIRCLE + application_data.OBS_30_CNT_SOCIAL_CIRCLE
coords={}
arq=open("US_cities.csv")
 for i in arq.readlines() :
lista=i.strip().split(",")
 coords[lista[0]]=(float(lista[1]),float(lista[2]))
coords<concatenate>
bureau_balance = pd.read_csv('.. /input/home-credit-default-risk/bureau_balance.csv',
na_values=['XNA', 'XAP'], na_filter=True )
dSF_LA=[]
a=np.array(coords["San Francisco"])
 b=np.array(coords["Los Angeles"])
 for i,c in train.iterrows() :
c=np.array(( c['longitude'],c['latitude']))
dSF_LA.append(min(np.linalg.norm(a-c),np.linalg.norm(b-c)))
 train = train.join(pd.Series(dSF_LA,name="dSF_LA"))
dSF_LA=[]
a=np.array(coords["San Francisco"])
 b=np.array(coords["Los Angeles"])
 for i,c in train.iterrows() :
c=np.array(( c['longitude'],c['latitude']))
dSF_LA.append(min(np.linalg.norm(a-c),np.linalg.norm(b-c)))
 test = test.join(pd.Series(dSF_LA,name="dSF_LA")) 
<sort_values>
bureau = bureau[bureau.SK_ID_CURR.isin(application_data.SK_ID_CURR.unique())]
train.corr() ["median_house_value"].abs().sort_values(ascending=False )<compute_train_metric>
bureau_balance = pd.get_dummies(bureau_balance )
msle = make_scorer(mean_squared_log_error)
 def score(nome,regressor):
pontos = cross_val_score(regressor,trainF,target,cv=10,scoring=msle)
 print(nome+ ': '+ str(round(np.sqrt(pontos ).mean() ,4)) + "
" )<choose_model_class>
bureau_balance = bureau_balance.sort_values(['SK_ID_BUREAU', 'MONTHS_BALANCE'] )
Lreg = Lasso(positive = True)
 score("Regressor Lasso",Lreg)
<choose_model_class>
temp = bureau_balance.groupby('SK_ID_BUREAU' ).size().to_frame()
temp = temp.rename(columns={0: 'COUNT'})
 temp.reset_index(inplace=True )
RFreg = RandomForestRegressor(max_depth=20, random_state=0,n_estimators=400)
 score("Regressor Random Forest",RFreg )<choose_model_class>
bureau_balance = bureau_balance.groupby('SK_ID_BUREAU' ).agg({'last', 'sum', 'mean'} )
KNNreg = KNeighborsRegressor(n_neighbors=np.array(pontuacao ).argmin() +1 )<compute_test_metric>
bureau_balance.columns = bureau_balance.columns.map('_'.join )
score("Regressor KNN",KNNreg )<choose_model_class>
bureau_balance.reset_index(inplace=True )
Adareg = AdaBoostRegressor(RandomForestRegressor(max_depth=15,n_estimators=10),n_estimators=60 )<train_model>
bureau_balance = bureau_balance.merge(temp, how='left', on='SK_ID_BUREAU' )
Adareg.fit(trainF,target )<predict_on_test>
bureau_balance.columns = bureau_balance.columns.map(lambda x : 'BLN_' + x if x != 'SK_ID_BUREAU' else x )
R = Adareg.predict(testF)
 R<save_to_csv>
bureau = bureau.merge(bureau_balance, how='left', on='SK_ID_BUREAU' )
arq=open("result1.csv","w")
 arq.write("Id,median_house_value
")
 for i,j in zip(list(R),list(Id)) :
arq.write(str(j)+","+str(i)+"
")
 arq.close()<set_options>
bureau.drop(columns='SK_ID_BUREAU', inplace=True )
warnings.filterwarnings('ignore' )<load_from_csv>
del(bureau_balance )
df_train = pd.read_csv('.. /input/dataset_treino.csv', sep = ',', encoding = 'utf-8')
 df_test = pd.read_csv('.. /input/dataset_teste.csv', sep = ',', encoding = 'utf-8' )<filter>
bureau = bureau.sort_values(['SK_ID_CURR', 'DAYS_CREDIT'] )
df_train[df_train.isnull().any(axis=1)]<count_missing_values>
bureau = pd.get_dummies(bureau, dummy_na=True )
df_train.isnull().values.any()<count_missing_values>
temp = bureau.groupby('SK_ID_CURR' ).size().to_frame()
temp = temp.rename(columns={0: 'COUNT'})
 temp.reset_index(inplace=True )
df_train.isnull().any()<count_missing_values>
bureau = bureau.groupby('SK_ID_CURR' ).agg({'sum', 'mean', 'max'} )
df_train.isnull().sum()<count_missing_values>
bureau.columns = bureau.columns.map('_'.join )
df_test.isnull().sum()<count_missing_values>
bureau = bureau.merge(temp, how='left', on='SK_ID_CURR' )
def info_variavel(df):
print(df.head())
 print('')
 print('Quantidade de registros:', df.count())
 print('Possui valores missing?:', df.isnull().any())
 print('Quantidade de valores missing:', df.isnull().sum())
 print('')
 print('Valores únicos:')
 print(df.unique())
 print('')
 print('Contagem dos elementos:')
 print(df.value_counts() )<define_variables>
application_data = application_data.merge(bureau, how='left', on='SK_ID_CURR' )
index = 'Order'<drop_column>
del(bureau )
del df_train[index]
del df_test['OrderId']<define_variables>
prev_application = pd.read_csv('/kaggle/input/home-credit-default-risk/previous_application.csv',
na_values=['XNA', 'XAP'], na_filter=True )
index = 'Property Id'<drop_column>
prev_application = pd.get_dummies(prev_application, dummy_na=True )
del df_train[index] 
<define_variables>
prev_application.drop(columns='SK_ID_PREV', inplace=True )
index = 'Property Name'<drop_column>
prev_application = prev_application.sort_values(['SK_ID_CURR', 'DAYS_DECISION'] )
del df_train[index]
del df_test[index]<define_variables>
temp = prev_application.groupby('SK_ID_CURR' ).size().to_frame()
temp = temp.rename(columns={0: 'COUNT'})
 temp.reset_index(inplace=True )
index = 'Parent Property Id'<drop_column>
prev_application = prev_application.groupby('SK_ID_CURR' ).agg(['max', 'sum', 'mean'] )
del df_train[index]
del df_test[index]<define_variables>
prev_application.columns = prev_application.columns.map('_'.join )
index = 'Parent Property Name'<drop_column>
prev_application = prev_application.merge(temp, how='left', on='SK_ID_CURR' )
del df_train[index]
del df_test[index]<define_variables>
prev_application.columns = prev_application.columns.map(lambda x : 'PREV_' + x if x != 'SK_ID_CURR' else x )
index = 'BBL - 10 digits'<drop_column>
application_data = application_data.merge(prev_application, how='left', on='SK_ID_CURR' )
del df_train[index]
del df_test[index]<define_variables>
del(prev_application )
index = 'NYC Borough, Block and Lot(BBL)self-reported'<drop_column>
pos_cash_balance = pd.read_csv('/kaggle/input/home-credit-default-risk/POS_CASH_balance.csv',
na_values=['XNA', 'XAP'], na_filter=True )
del df_train[index]
del df_test[index]<define_variables>
pos_cash_balance = pos_cash_balance.sort_values(['SK_ID_CURR', 'SK_ID_PREV', 'MONTHS_BALANCE'] )
index = 'NYC Building Identification Number(BIN)'<drop_column>
temp = pos_cash_balance.groupby(['SK_ID_CURR', 'SK_ID_PREV'] ).size().to_frame()
temp = temp.rename(columns={0: 'BLN_COUNT'})
 temp.reset_index(inplace=True )
del df_train[index]
del df_test[index]<define_variables>
pos_cash_balance = pd.get_dummies(pos_cash_balance, dummy_na=True )
index = 'Address 1(self-reported)'<drop_column>
pos_cash_balance = pos_cash_balance.groupby(['SK_ID_PREV', 'SK_ID_CURR'] ).agg(['sum', 'mean', 'max'] )
del df_train[index]
del df_test[index]<define_variables>
pos_cash_balance.columns = pos_cash_balance.columns.map('_'.join )
index = 'Address 2'<drop_column>
pos_cash_balance.reset_index(inplace=True )
del df_train[index]
del df_test[index]<define_variables>
pos_cash_balance = pos_cash_balance.merge(temp, how='left', on=['SK_ID_CURR', 'SK_ID_PREV'] )
index = 'Postal Code'<drop_column>
pos_cash_balance.drop(columns='SK_ID_PREV', inplace=True )
del df_train[index]
del df_test[index]<define_variables>
temp = pos_cash_balance.groupby('SK_ID_CURR' ).size().to_frame()
temp = temp.rename(columns={0: 'COUNT'})
 temp.reset_index(inplace=True )
index = 'Street Number'<drop_column>
pos_cash_balance = pos_cash_balance.groupby(['SK_ID_CURR'] ).agg(['sum', 'mean', 'max'] )
del df_train[index]
del df_test[index]<define_variables>
pos_cash_balance.columns = pos_cash_balance.columns.map('_'.join )
index = 'Street Name'<drop_column>
pos_cash_balance.reset_index(inplace=True )
del df_train[index]
del df_test[index]<define_variables>
pos_cash_balance = pos_cash_balance.merge(temp, how='left', on='SK_ID_CURR' )
index = 'Borough'<data_type_conversions>
pos_cash_balance.columns = pos_cash_balance.columns.map(lambda x : 'CSH_' + x if x != 'SK_ID_CURR' else x )
df_train[index] = df_train[index].astype('category' ).cat.codes
df_test[index] = df_test[index].astype('category' ).cat.codes<filter>
application_data = application_data.merge(pos_cash_balance, how='left', on='SK_ID_CURR' )
df_train = df_train[df_train[index] > -1]<compute_test_metric>
del(pos_cash_balance )
intervalo_valores(df_train[index] )<define_variables>
credit_card_balance = pd.read_csv('/kaggle/input/home-credit-default-risk/credit_card_balance.csv',
na_values=['XNA', 'XAP'], na_filter=True )
index = 'DOF Gross Floor Area'<drop_column>
credit_card_balance = pd.get_dummies(credit_card_balance, dummy_na=True )
del df_train[index]
del df_test[index]<define_variables>
credit_card_balance = credit_card_balance.sort_values(['SK_ID_CURR', 'SK_ID_PREV', 'MONTHS_BALANCE'] )
index = 'Primary Property Type - Self Selected'<data_type_conversions>
temp = credit_card_balance.groupby(['SK_ID_CURR', 'SK_ID_PREV'] ).size().to_frame()
temp = temp.rename(columns={0: 'COUNT'})
 temp.reset_index(inplace=True )
df_train[index] = df_train[index].astype('category' ).cat.codes
df_test[index] = df_test[index].astype('category' ).cat.codes<compute_test_metric>
credit_card_balance = credit_card_balance.groupby(['SK_ID_PREV', 'SK_ID_CURR'] ).agg(['sum', 'mean', 'max'] )
intervalo_valores(df_train[index] )<define_variables>
credit_card_balance.columns = credit_card_balance.columns.map('_'.join )
index = 'List of All Property Use Types at Property'<data_type_conversions>
credit_card_balance.reset_index(inplace=True )
df_train[index] = df_train[index].astype('category' ).cat.codes
df_test[index] = df_test[index].astype('category' ).cat.codes<compute_test_metric>
credit_card_balance = credit_card_balance.merge(temp, how='left', on=['SK_ID_CURR', 'SK_ID_PREV'] )
intervalo_valores(df_train[index] )<define_variables>
credit_card_balance.drop(columns='SK_ID_PREV', inplace=True )
index = 'Largest Property Use Type'<data_type_conversions>
credit_card_balance = credit_card_balance.groupby(['SK_ID_CURR'] ).agg(['sum', 'mean'] )
df_train[index] = df_train[index].astype('category' ).cat.codes
df_test[index] = df_test[index].astype('category' ).cat.codes<compute_test_metric>
credit_card_balance.columns = credit_card_balance.columns.map('_'.join )
intervalo_valores(df_train[index] )<define_variables>
credit_card_balance.reset_index(inplace=True )
index = 'Largest Property Use Type - Gross Floor Area(ft²)'<compute_test_metric>
credit_card_balance.columns = credit_card_balance.columns.map(lambda x : 'CRD_' + x if x != 'SK_ID_CURR' else x )
intervalo_valores(df_train[index] )<define_variables>
application_data = application_data.merge(credit_card_balance, how='left', on='SK_ID_CURR' )
index = '2nd Largest Property Use Type'<data_type_conversions>
del(credit_card_balance )
df_train[index] = df_train[index].astype('category' ).cat.codes
df_test[index] = df_test[index].astype('category' ).cat.codes<compute_test_metric>
installments_payments = pd.read_csv('/kaggle/input/home-credit-default-risk/installments_payments.csv',
na_values=['XNA', 'XAP'], na_filter=True )
intervalo_valores(df_train[index] )<define_variables>
installments_payments = installments_payments.sort_values(['SK_ID_CURR', 'SK_ID_PREV', 'DAYS_ENTRY_PAYMENT'] )
index = '2nd Largest Property Use - Gross Floor Area(ft²)'<feature_engineering>
temp = installments_payments.groupby(['SK_ID_CURR', 'SK_ID_PREV'] ).size().to_frame()
temp = temp.rename(columns={0: 'COUNT'})
 temp.reset_index(inplace=True )
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
installments_payments.fillna(0, inplace=True )
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
installments_payments = installments_payments.groupby(['SK_ID_PREV', 'SK_ID_CURR'] ).agg(['sum', 'mean', 'max', 'min'] )
intervalo_valores(df_train[index] )<define_variables>
installments_payments.columns = installments_payments.columns.map('_'.join )
index = '3rd Largest Property Use Type'<data_type_conversions>
installments_payments.reset_index(inplace=True )
df_train[index] = df_train[index].astype('category' ).cat.codes
df_test[index] = df_test[index].astype('category' ).cat.codes<compute_test_metric>
installments_payments = installments_payments.merge(temp, how='left', on=['SK_ID_CURR', 'SK_ID_PREV'] )
intervalo_valores(df_train[index] )<define_variables>
installments_payments.drop(columns='SK_ID_PREV', inplace=True )
index = '3rd Largest Property Use Type - Gross Floor Area(ft²)'<feature_engineering>
installments_payments = installments_payments.groupby(['SK_ID_CURR'] ).agg(['sum', 'mean', 'max', 'min'] )
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
installments_payments.columns = installments_payments.columns.map('_'.join )
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
installments_payments.reset_index(inplace=True )
intervalo_valores(df_train[index] )<define_variables>
installments_payments.columns = installments_payments.columns.map(lambda x : 'INS_' + x if x != 'SK_ID_CURR' else x )
index = 'Year Built'<compute_test_metric>
application_data = application_data.merge(installments_payments, how='left', on='SK_ID_CURR' )
intervalo_valores(df_train[index] )<define_variables>
del(installments_payments )
index = 'Number of Buildings - Self-reported'<compute_test_metric>
application_data.columns = ["".join(c if c.isalnum() else "_" for c in str(x)) for x in application_data.columns]
intervalo_valores(df_train[index] )<define_variables>
model = lgb.LGBMClassifier()
index = 'Occupancy'<compute_test_metric>
train_data = application_data[application_data.IS_TRAIN == 1.0]
intervalo_valores(df_train[index] )<define_variables>
test_data = application_data[application_data.IS_TRAIN == 0.0]
index = 'Metered Areas(Energy)'<data_type_conversions>
train_data.drop(columns='IS_TRAIN', inplace=True)
 test_data.drop(columns='IS_TRAIN', inplace=True )
df_train[index] = df_train[index].astype('category' ).cat.codes
df_test[index] = df_test[index].astype('category' ).cat.codes<compute_test_metric>
del(application_data )
intervalo_valores(df_train[index] )<define_variables>
params = model.get_params()
index = 'Metered Areas(Water)'<data_type_conversions>
params['objective'] = 'binary'
params['metric'] = 'auc'
df_train[index] = df_train[index].astype('category' ).cat.codes
df_test[index] = df_test[index].astype('category' ).cat.codes<compute_test_metric>
skf = StratifiedKFold(n_splits=3, random_state=42, shuffle=True)
 final_importance = np.zeros(len(train_data.columns))
for n_fold,(train_index, valid_index)in tqdm(enumerate(skf.split(train_data, train_target.TARGET))):
X_train = train_data.iloc[train_index]
y_train = train_target.iloc[train_index].TARGET
X_valid = train_data.iloc[valid_index]
y_valid = train_target.iloc[valid_index].TARGET
lgb_train = lgb.Dataset(data=X_train, label=y_train)
 lgb_eval = lgb.Dataset(data=X_valid, label=y_valid)
 model = lgb.train(params, lgb_train, valid_sets=lgb_eval, early_stopping_rounds=150, verbose_eval=100)
 final_importance += model.feature_importance()
intervalo_valores(df_train[index] )<define_variables>
fi = pd.DataFrame()
fi['FEAT'] = train_data.columns
index = 'Site EUI(kBtu/ft²)'<compute_test_metric>
fi['importance'] = final_importance
intervalo_valores(df_train[index] )<define_variables>
fi = fi.sort_values(by='importance', ascending=False )
index = 'Weather Normalized Site EUI(kBtu/ft²)'<feature_engineering>
fi = fi[fi.importance != 0]
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
cols = list(set(fi.FEAT.values ).union(set(['SK_ID_CURR'])) )
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
train_data = train_data[cols]
intervalo_valores(df_train[index] )<define_variables>
test_data = test_data[cols]
index = 'Weather Normalized Site Electricity Intensity(kWh/ft²)'<feature_engineering>
def get_random_params() :
params = {
'boosting_type': 'gbdt',
'metric': 'auc',
'num_leaves': random.randint(10, 60),
'max_depth': random.randint(10, 30),
'learning_rate': random.choice([0.0001, 0.0005, 0.001, 0.005, 0.01]),
'n_estimators': random.randint(1000, 20000),
'objective': 'binary',
'reg_alpha': random.choice([0.001, 0.005, 0.01, 0.05, 0.1]),
'reg_lambda': random.choice([0.001, 0.005, 0.01, 0.05, 0.1]),
'colsample_bytree': random.choice([0.4, 0.5, 0.6, 0.7, 0.8, 0.9]),
'min_child_samples': random.randint(10, 100),
'subsample_for_bin': random.randint(50000, 300000)
 }
return params
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
best_params = {'boosting_type': 'gbdt',
'metric': 'auc',
'num_leaves': 46,
'max_depth': 18,
'learning_rate': 0.01,
'n_estimators': 6289,
'objective': 'binary',
'reg_alpha': 0.05,
'reg_lambda': 0.05,
'colsample_bytree': 0.4,
'min_child_samples': 79,
'subsample_for_bin': 113092}
best_auc = 0.787228
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
def get_best_params(hyper_rounds, n_folds, best_params=None, best_auc=0):
best_params = best_params
best_auc = best_auc
lgb_train = lgb.Dataset(data=train_data, label=train_target.TARGET)
 for i in tqdm(range(hyper_rounds)) :
curr_params = get_random_params()
start = time.time()
print(curr_params)
 eval_hist = lgb.cv(curr_params, lgb_train, early_stopping_rounds = 200, nfold = n_folds, seed = 42, verbose_eval = 100)
 end = time.time()
print('TIME:', end-start)
 curr_auc = eval_hist['auc-mean'][-1]
if curr_auc > best_auc:
best_params = curr_params
best_auc = curr_auc
return best_params, best_auc
intervalo_valores(df_train[index] )<define_variables>
N_FOLDS = 10
index = 'Weather Normalized Site Natural Gas Intensity(therms/ft²)'<feature_engineering>
skf = StratifiedKFold(n_splits=N_FOLDS, random_state=42, shuffle=True)
 sub_preds = np.zeros(len(test_data))
avg_valid_auc = 0
for n_fold,(train_index, valid_index)in tqdm(enumerate(skf.split(train_data, train_target.TARGET))):
print("FOLD N:", n_fold)
 X_train = train_data.iloc[train_index]
y_train = train_target.iloc[train_index].TARGET
X_valid = train_data.iloc[valid_index]
y_valid = train_target.iloc[valid_index].TARGET
lgb_train = lgb.Dataset(data=X_train, label=y_train)
 lgb_eval = lgb.Dataset(data=X_valid, label=y_valid)
 model = lgb.train(best_params, lgb_train, valid_sets=lgb_eval, early_stopping_rounds=150, verbose_eval=100)
 y_pred = model.predict(X_valid)
 sub_preds += model.predict(test_data)/ skf.n_splits
avg_valid_auc += roc_auc_score(y_valid, y_pred)/ N_FOLDS
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
submit['TARGET'] = sub_preds
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
submit.to_csv('submission.csv', index = False )