Kaggle competition: Home Credit Default Risk
print(mean_squared_error(valid['nota_mat'], preds)**(1/2))
print(accuracy_score(valid['nota_mat'], preds))<save_to_csv>
print('CONVERTING ALL INPUT FILES.... ')
 print('-' * 80)
 print('train')
 app_train = import_data('.. /input/application_train.csv')
 print('-' * 80)
 print('test')
 app_test = import_data('.. /input/application_test.csv')
 print('-' * 80)
 print('bureau_balance')
 bur_bal = import_data('.. /input/bureau_balance.csv')
 print('-' * 80)
 print('bureau')
 bur = import_data('.. /input/bureau.csv')
 print('-' * 80)
 print('credit_card_balance')
 credit = import_data('.. /input/credit_card_balance.csv')
 print('-' * 80)
 print('installments_payments')
 install = import_data('.. /input/installments_payments.csv')
 print('-' * 80)
 print('pos_cash_balance')
 pos = import_data('.. /input/POS_CASH_balance.csv')
 print('-' * 80)
 print('previous_application')
 prev = import_data('.. /input/previous_application.csv')
 print('CONVERSION FOR ALL FILES COMPLETE.... ')
preds = lr.predict(test[feats])
 test['nota_mat'] = preds
test[['codigo_mun', 'nota_mat']].to_csv('saida_lr.csv', index=False )<choose_model_class>
print('SAVING A COPY OF TEST FILE FOR LATER USE.... ')
 app_test1 = app_test
print('SAVING COMPLETE.... ' )
def moda(melhores, treino, teste, feats, alvo):
preds = []
models = {
'RandomForest': RandomForestClassifier(random_state=42, **melhores['RandomForest'][1]),
'ExtraTrees': ExtraTreesClassifier(random_state=42, **melhores['ExtraTrees'][1]),
'AdaBoost': AdaBoostClassifier(random_state=42, **melhores['AdaBoost'][1]),
'GradientBoosting': GradientBoostingClassifier(random_state=42, **melhores['GradientBoosting'][1]),
'DecisionTree': DecisionTreeClassifier(random_state=42, **melhores['DecisionTree'][1]),
'LogisticRegression': LogisticRegression(penalty='l2', random_state=42, **melhores['LogisticRegression'][1]),
'KNN 11': KNeighborsClassifier(**melhores['KNN 11'][1]),
'MLPClassifier': MLPClassifier(random_state=42, **melhores['MLPClassifier'][1]),
'SVC': SVC(**melhores['SVC'][1]),
'GaussianNB': GaussianNB(**melhores['GaussianNB'][1]),
'BernoulliNB': BernoulliNB(**melhores['BernoulliNB'][1]),
}
for nome, modelo in models.items() :
m = modelo
m.fit(treino[feats], treino[alvo])
 preds.append(m.predict(teste[feats]))
df = pd.DataFrame(preds)
 print(df)
 return df.T.mode(axis=1)
<split>
print('WRITING A PROGRAM TO LABEL CODE CATEGORY COL.... ')
 def encode(df, nan_as_category = True):
original_columns = list(df.columns)
 categorical_columns = [col for col in df.columns if df[col].dtype == 'object']
df = pd.get_dummies(df, columns= categorical_columns, dummy_na= nan_as_category)
 new_columns = [c for c in df.columns if c not in original_columns]
return new_columns,df
print('PROGRAM COMPLETE.... ' )
treino = df[-df['nota_mat'].isnull() ] 
<save_to_csv>
print('IMPUTING OCCUPATION TYPE BASED ON SALARY SLAB IN TRAIN & TEST DATA.... ')
 miss = app_train['OCCUPATION_TYPE'].isnull()
miss1 = app_test['OCCUPATION_TYPE'].isnull()
test = []
test1 = []
df = app_train.loc[miss,'AMT_INCOME_TOTAL']
df_test = app_test.loc[miss1, 'AMT_INCOME_TOTAL']
for x in df:
if x < 130791:
test.append('Cleaning staff')
 elif(x>130791)&(x< 133228):
test.append('Low-skill Laborers')
 elif(x>133228)&(x< 138397):
test.append('Cooking staff')
 elif(x>138397)&(x <144273):
test.append('Waiters/barmen staff')
 elif(x>144273)&(x < 149663):
test.append('Security staff')
 elif(x>149663)&(x < 149710):
test.append('Medicine staff')
 elif(x>149710)&(x <152303):
test.append('Sales staff')
 elif(x>152303)&(x <160542):
test.append('Secretaries')
 elif(x>160542)&(x <166357):
test.append('Laborers')
 elif(x>166357)&(x <172657):
test.append('Core staff')
 elif(x>172657)&(x <182335):
test.append('Private service staff')
 elif(x>182335)&(x <182842):
test.append('High skill tech staff')
 elif(x>182842)&(x <187012):
test.append('Drivers')
 elif(x>187012)&(x <188916):
test.append('HR staff')
 elif(x>188916)&(x <194578):
test.append('Accountants')
 elif(x>194578)&(x <195004):
test.append('Realty agents')
 elif(x>195004)&(x <213446):
test.append('IT staff')
 elif(x>213446):
test.append('Managers')
m = moda(melhores, train, test, feats, 'nota_mat')
 test['nota_mat'] = m
test[['codigo_mun', 'nota_mat']].to_csv('saida_moda.csv', index=False)
 print(test[['codigo_mun', 'nota_mat']] )<save_to_csv>
print('BINARIZING Y/N COLUMNS FOR TRAIN AND TEST DATA.... ')
 for df in(app_train,app_test):
for bin_feature in ['CODE_GENDER', 'FLAG_OWN_CAR', 'FLAG_OWN_REALTY']:
df[bin_feature], uniques = pd.factorize(df[bin_feature])
 print('BINARUZING COMPLETE.... ')
 print('FEATURE ENGINEERING FOR TRAIN & TEST.... ')
 for df in(app_train,app_test):
df['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True)
 df['DAYS_EMPLOYED_PERC'] = df['DAYS_EMPLOYED'] / df['DAYS_BIRTH']
df['DAYS_REG_PERC'] = df['DAYS_REGISTRATION'] / df['DAYS_BIRTH']
df['DAYS_ID_PERC'] = df['DAYS_ID_PUBLISH'] / df['DAYS_REGISTRATION']
df['INCOME_CREDIT_PERC'] = df['AMT_INCOME_TOTAL'] / df['AMT_CREDIT']
df['INCOME_PER_PERSON'] = df['AMT_INCOME_TOTAL'] / df['CNT_FAM_MEMBERS']
df['ANNUITY_INCOME_PERC'] = df['AMT_ANNUITY'] / df['AMT_INCOME_TOTAL']
df['LOAN_INCOME_RATIO'] = df['AMT_CREDIT'] / df['AMT_INCOME_TOTAL']
df['CONSUMER_GOODS_RATIO'] = df['AMT_CREDIT'] / df['AMT_GOODS_PRICE']
df['ANNUITY LENGTH'] = df['AMT_CREDIT'] / df['AMT_ANNUITY']
df['ANN_LENGTH_EMPLOYED_RATIO'] = df['ANNUITY LENGTH'] / df['DAYS_EMPLOYED']
df['WORKING_LIFE_RATIO'] = df['DAYS_EMPLOYED'] / df['DAYS_BIRTH']
df['TOTAL_DOCS_SUBMITTED'] = df.loc[:, df.columns.str.contains('FLAG_DOCUMENT')].sum(axis=1)
 print('FEATURE ENGINEERING COMPLETE.... ')
param = {'max_depth': 4, 'max_leaf_nodes': 2, 'min_samples_split': 2, 'n_estimators': 200}
gb = GradientBoostingClassifier(**param)
 gb.fit(treino[feats], treino['nota_mat'])
 preds = gb.predict(test[feats])
 test['nota_mat'] = preds
test[['codigo_mun', 'nota_mat']].to_csv('saida_gb.csv', index=False )<train_model>
print('REPLACE ODD VALUES WITH NAN.... ')
 prev['DAYS_FIRST_DRAWING'].replace(365243, np.nan, inplace= True)
 prev['DAYS_FIRST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE_1ST_VERSION'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_TERMINATION'].replace(365243, np.nan, inplace= True)
 print('FEATURE ENGINEERING FOR PREVIOUS APP.... ')
 prev['ASK_AMT_RATIO'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']
prev['APPROVED'] =(prev['NAME_CONTRACT_STATUS'] == 'Approved' ).astype(int)
 prev['REFUSED'] =(prev['NAME_CONTRACT_STATUS'] == 'Refused' ).astype(int)
 prev['APP_CREDIT_PERC'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']
agg = {
'AMT_ANNUITY': ['mean','max','sum','var','median'],
'AMT_APPLICATION': ['mean','max','sum','var','median'],
'AMT_CREDIT': ['mean','max','sum','var','median'],
'APP_CREDIT_PERC': ['mean','max','sum','var','median'],
'AMT_DOWN_PAYMENT': ['mean','max','sum','var','median'],
'AMT_GOODS_PRICE': ['mean','max','sum','var','median'],
'HOUR_APPR_PROCESS_START': ['mean','max','sum','var','median'],
'RATE_DOWN_PAYMENT': ['mean','max','sum','var','median'],
'DAYS_DECISION': ['mean','max','sum','var','median'],
'CNT_PAYMENT': ['mean', 'sum','max','var','median'],
}
prev=prev.groupby('SK_ID_CURR' ).agg(agg)
 prev.reset_index(inplace=True)
 prev.columns = pd.Index([e[0] + "_" + e[1].upper() for e in prev.columns.tolist() ])
 prev.rename(columns={'SK_ID_CURR_':'SK_ID_CURR'}, inplace=True)
 print('FEATURE ENGINEERING FOR PREV COMPLETE' )
pca = PCA(n_components=4)
 pca.fit(treino[feats])
 print(pca.explained_variance_ratio_)
 print(pca.singular_values_)
 novo = pca.fit_transform(treino[feats])
 print(novo, novo.shape)
 novo_valid = pca.transform(valid[feats])
 novo_teste = pca.fit_transform(test[feats])
 params = {'C': 0.1, 'dual': True, 'max_iter': 130}
lg = LogisticRegression(penalty='l2', **params)
 lg.fit(novo, treino['nota_mat'])
 pred = lg.predict(novo_valid)
 print(valid.shape)
 print(accuracy_score(valid['nota_mat'], pred))
print(lg.score(novo_valid, valid['nota_mat']))
test['nota_mat'] = preds
test[['codigo_mun', 'nota_mat']].to_csv('saida_lg2.csv', index=False )<save_to_csv>
print('FEATURE ENGINEERING FOR CREDIT.....')
 credit['AMT_DRAW'] = credit['AMT_DRAWINGS_ATM_CURRENT'] + credit['AMT_DRAWINGS_CURRENT'] +credit['AMT_DRAWINGS_OTHER_CURRENT'] + credit['AMT_DRAWINGS_POS_CURRENT']
credit['CNT_DRAW'] = credit['CNT_DRAWINGS_ATM_CURRENT'] + credit['CNT_DRAWINGS_CURRENT'] + credit['CNT_DRAWINGS_OTHER_CURRENT'] + credit['CNT_DRAWINGS_POS_CURRENT']
credit['cr_util'] =(credit['AMT_BALANCE']*100)/ credit['AMT_CREDIT_LIMIT_ACTUAL']
credit['PAID%'] =(credit['AMT_PAYMENT_TOTAL_CURRENT']*100)/ credit['AMT_TOTAL_RECEIVABLE']
credit=credit.replace(np.inf, np.nan)
 credit=credit.replace(-np.inf, np.nan)
 credit=credit.groupby('SK_ID_CURR' ).agg(['mean','sum','max','min','var','median'])
 credit.reset_index(inplace=True)
 credit.columns = pd.Index([e[0] + "_" + e[1].upper() for e in credit.columns.tolist() ])
 del credit['SK_ID_PREV_MEAN']
del credit['SK_ID_PREV_MAX']
del credit['SK_ID_PREV_MIN']
del credit['SK_ID_PREV_SUM']
del credit['SK_ID_PREV_VAR']
del credit['SK_ID_PREV_MEDIAN']
print('FEATURE ENGINEERING FOR CREDIT COMPLETE' )
gnb = GaussianNB(var_smoothing=1e-9)
 gnb.fit(train[feats], train['nota_mat'])
 print(gnb.score(valid[feats], valid['nota_mat']))
pred = gnb.predict(test[feats])
 test[['codigo_mun', 'nota_mat']].to_csv('saida_gnb.csv', index=False )<save_to_csv>
credit = credit.rename(columns={'SK_ID_CURR_':'SK_ID_CURR'} )
mnb = MultinomialNB()
mnb.fit(train[feats], train['nota_mat'])
 print(mnb.score(valid[feats], valid['nota_mat']))
pred = mnb.predict(test[feats])
 test['nota_mat'] = pred
test[['codigo_mun', 'nota_mat']].to_csv('saida_mnb.csv', index=False )<save_to_csv>
print('FEATURE ENGINEERING FOR INSTALLMENT.....')
 install['PAYMENT_PERC'] = install['AMT_PAYMENT'] / install['AMT_INSTALMENT']
install['PAYMENT_DIFF'] = install['AMT_INSTALMENT'] - install['AMT_PAYMENT']
install['DAYS_INSTALLMENT_LATE'] = install['DAYS_INSTALMENT'] - install['DAYS_ENTRY_PAYMENT']
install['PAYMENT_DISCREPANCY'] = install['AMT_INSTALMENT'] - install['AMT_PAYMENT']
install['DPD'] = install['DAYS_ENTRY_PAYMENT'] - install['DAYS_INSTALMENT']
install['DBD'] = install['DAYS_INSTALMENT'] - install['DAYS_ENTRY_PAYMENT']
install['DPD'] = install['DPD'].apply(lambda x: x if x > 0 else 0)
 install['DBD'] = install['DBD'].apply(lambda x: x if x > 0 else 0)
 install = install.groupby('SK_ID_CURR' ).agg(['mean','sum','max','min','var','median'])
 install.columns = pd.Index([e[0] + "_" + e[1].upper() for e in install.columns.tolist() ])
 install.reset_index(inplace=True)
 del install['SK_ID_PREV_MEAN']
del install['SK_ID_PREV_SUM']
del install['SK_ID_PREV_MAX']
del install['SK_ID_PREV_MIN']
del install['SK_ID_PREV_VAR']
del install['SK_ID_PREV_MEDIAN']
print('FEATURE ENGINEERING FOR INSTALL COMPLETED.... ' )
bnb = BernoulliNB(alpha=0)
 bnb.fit(train[feats], train['nota_mat'])
 print(bnb.score(valid[feats], valid['nota_mat']))
pred = bnb.predict(test[feats])
 test['nota_mat'] = pred
test[['codigo_mun', 'nota_mat']].to_csv('saida_bnb.csv', index=False )<choose_model_class>
print('FEATURE ENGINEERING FOR POS.....')
 pos=pos.groupby('SK_ID_CURR' ).agg(['mean','sum','max','var','min','median'])
 pos.columns = pd.Index([e[0] + "_" + e[1].upper() for e in pos.columns.tolist() ])
 pos.reset_index(inplace=True)
 del pos['SK_ID_PREV_MEAN']
del pos['SK_ID_PREV_MAX']
del pos['SK_ID_PREV_VAR']
del pos['SK_ID_PREV_SUM']
del pos['SK_ID_PREV_MIN']
del pos['SK_ID_PREV_MEDIAN']
print('FEATURE ENGINEERING FOR POS COMPLETED.... ' )
melhores = {'max_depth': 2, 'min_samples_split': 2, 'n_estimators': 100, 'oob_score': False}
lr = LogisticRegression(solver='lbfgs', multi_class='multinomial', random_state=42)
 rf = RandomForestClassifier(random_state=42, **melhores)
 bnb = BernoulliNB(alpha=0)
 gnb = GaussianNB(var_smoothing=1e-9)
 vc = VotingClassifier(estimators=[('lr', lr),('rf', rf),('gnb', gnb),('bnb', bnb)], voting='hard')
 vc.fit(train[feats], train['nota_mat'])
 vc.score(valid[feats], valid['nota_mat'])
 pred = vc.predict(test[feats])
 test['nota_mat'] = pred
test[['codigo_mun', 'nota_mat']].to_csv('voting.csv', index=False)
<import_modules>
print('FEATURE ENGINEERING FOR BUR AND BAL.....')
 bal_agg = {'MONTHS_BALANCE': ['min', 'max','size','mean','var','median']}
bur_bal = bur_bal.groupby('SK_ID_BUREAU' ).agg(bal_agg)
 bur_bal.columns = pd.Index([e[0] + "_" + e[1].upper() for e in bur_bal.columns.tolist() ])
 bur_bal = bur.join(bur_bal, how='left', on='SK_ID_BUREAU')
 bur_bal.drop(columns= 'SK_ID_BUREAU', inplace= True)
 a = {
'DAYS_CREDIT': ['mean','max','sum','min','var','median'],
'CREDIT_DAY_OVERDUE': ['max','min','mean','sum','var','median'],
'DAYS_CREDIT_ENDDATE': ['mean','max','min','sum','var','median'],
'AMT_CREDIT_MAX_OVERDUE': ['mean','max','min','sum','var','median'],
'CNT_CREDIT_PROLONG': ['sum','max','min','mean','var','median'],
'AMT_CREDIT_SUM': ['mean', 'sum','max','min','var','median'],
'AMT_CREDIT_SUM_DEBT': ['mean', 'sum','max','min','var','median'],
'AMT_CREDIT_SUM_OVERDUE': ['mean','max','min','var','sum','median'],
'AMT_CREDIT_SUM_LIMIT': ['mean', 'sum','max','min','var','median'],
'DAYS_CREDIT_UPDATE': ['mean','max','min','var','sum','median'],
'AMT_ANNUITY': ['mean','max','min','sum','var','median'],
'MONTHS_BALANCE_MIN': ['min'],
'MONTHS_BALANCE_MAX': ['max'],
'MONTHS_BALANCE_SIZE': ['mean']
}
bur_bal = bur_bal.groupby('SK_ID_CURR' ).agg(a)
 bur_bal.columns = pd.Index(['BUR_' + e[0] + "_" + e[1].upper() for e in bur_bal.columns.tolist() ])
 bur_bal.reset_index(inplace=True)
 bur_bal.head()
print('FEATURE ENGINEERING FOR BUR AND BAL COMPLETED.....')
import datetime
import numpy as np
import os
import pandas as pd
from lightgbm import LGBMRegressor
from sklearn.model_selection import cross_val_score, KFold, GridSearchCV<load_from_csv>
print('JOIN ALL DATAS ON SK_ID_CURR.... ')
 app_train = app_train.merge(right=bur_bal, how='left', on='SK_ID_CURR')
 app_test = app_test.merge(right=bur_bal, how='left', on='SK_ID_CURR')
 app_train = app_train.merge(right=prev, how='left', on='SK_ID_CURR')
 app_test = app_test.merge(right=prev, how='left', on='SK_ID_CURR')
 app_train = app_train.merge(right=pos, how='left', on='SK_ID_CURR')
 app_test = app_test.merge(right=pos, how='left', on='SK_ID_CURR')
 app_train = app_train.merge(right=credit, how='left', on='SK_ID_CURR')
 app_test = app_test.merge(right=credit, how='left', on='SK_ID_CURR')
 app_train = app_train.merge(right=install, how='left', on='SK_ID_CURR')
 app_test = app_test.merge(right=install, how='left', on='SK_ID_CURR')
 del app_train['SK_ID_CURR']
del app_test['SK_ID_CURR']
print('JOIN ALL DATAS ON SK_ID_CURR COMPLETE.... ')
def ImportData() :
train = pd.read_csv(os.path.join('.. /input/data-science-master/train_booking.csv', 'train_booking.csv'), sep=",", encoding="utf-8")
 val = train = pd.read_csv(os.path.join('.. /input/data-science-master/test_booking.csv', 'test_booking.csv'), sep=",", encoding="utf-8")
 return train, val<data_type_conversions>
train = app_train.drop(['TARGET'],axis=1)
 test = app_test
target = app_train['TARGET']
del app_train['TARGET']
def Cible(train, val, cible):
train = train.drop(columns = ['id'])
 id_val = val['id']
val = val.drop(columns = ['id'])
 y_train = train[cible]
train = train.drop(columns = [cible])
 train = train[~(y_train.isnull())].reset_index(drop = True)
 y_train = y_train[~(y_train.isnull())]
return train, val, y_train, id_val
def TextToDate(train, val, list_col):
for col in list_col:
train[col] = pd.to_datetime(train[col].str[:10])
 val[col] = pd.to_datetime(val[col].str[:10])
 return(train, val )<feature_engineering>
print('LABEL ENCODING ALL CATEGORY COL...... ')
 var_mod = train.select_dtypes(include = 'object' ).columns.tolist()
num = train.columns.tolist()
le = LabelEncoder()
for i in var_mod:
train[i] = le.fit_transform(train[i].astype('str'))
test[i] = le.fit_transform(test[i].astype('str'))
print('LABEL ENCODING ALL CATEGORY COL COMPLETE...... ')
def TreatVariables(train, val, dates, split):
train = train.drop(columns = ['host_id'])
 val = val.drop(columns = ['host_id'])
 for col in dates:
train[col + '_an'] = train[col].dt.year
val[col + '_an'] = val[col].dt.year
train[col + '_mois'] = train[col].dt.month
val[col + '_mois'] = val[col].dt.month
train[col + '_jour'] = train[col].dt.day
val[col + '_jour'] = val[col].dt.day
train[col + '_heure'] = train[col].dt.hour
val[col + '_heure'] = val[col].dt.hour
train[col + '_ecart'] =(datetime.datetime.now() - train[col] ).dt.days
val[col + '_ecart'] =(datetime.datetime.now() - val[col] ).dt.days
train = train.drop(columns = [col])
 val = val.drop(columns = [col])
 for var in split:
train[var + '_nb'] = train[var].map(lambda row: len(str(row ).split(',')))
 val[var + '_nb'] = val[var].map(lambda row: len(str(row ).split(',')))
 modalites = list(train[var].astype(str ).to_numpy().flat)
 modalites = list(set(','.join(modalites ).split(',')))
 for modalite in modalites:
col = var + '_' + ''.join(e for e in modalite if e.isalnum())
 train[col] = train[var].str.contains(modalite ).astype(float)
 val[col] = val[var].str.contains(modalite ).astype(float)
 train = train.drop(columns = [var])
 val = val.drop(columns = [var])
 return train, val<drop_column>
folds = KFold(n_splits=5, shuffle=True, random_state=546789)
 oof_preds = np.zeros(train.shape[0])
 sub_preds = np.zeros(test.shape[0])
 feature_importance_df = pd.DataFrame()
for n_fold,(train_idx, valid_idx)in enumerate(folds.split(train, target)) :
train_x, train_y = train.iloc[train_idx], target.iloc[train_idx]
valid_x, valid_y = train.iloc[valid_idx], target.iloc[valid_idx]
print('PREPARING THE LGBM CLASSIFIER.....')
 clf = LGBMClassifier(
 n_estimators=10000,
learning_rate=0.03,
num_leaves=45,
colsample_bytree=0.9,
subsample=0.8,
max_depth=8,
reg_alpha=.8,
reg_lambda=.7,
min_split_gain=.01,
min_child_weight=300,
silent=-1,
verbose=-1,
class_weight='balanced',
boosting_type = 'dart',
bagging_fraction = 0.6,
bagging_freq = 100 
)
 print('CLASSIFIER IS TUNNED.... ')
 clf.fit(train_x, train_y,
eval_set= [(train_x, train_y),(valid_x, valid_y)],
eval_metric='auc', verbose=100, early_stopping_rounds=100, feature_name = num, categorical_feature=var_mod 
)
 oof_preds[valid_idx] = clf.predict_proba(valid_x, num_iteration=clf.best_iteration_)[:, 1]
sub_preds += clf.predict_proba(test, num_iteration=clf.best_iteration_)[:, 1] / folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = train_x.columns.tolist()
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(valid_y, oof_preds[valid_idx])))
 del train_x, train_y, valid_x, valid_y
gc.collect()
def Remove(train, val):
texte = ['name','description','neighborhood_overview','notes','transit']
texte += ['access','interaction','house_rules']
train = train.drop(columns = texte)
 val = val.drop(columns = texte)
 geo = ['latitude_booking', 'longitude_booking', 'geolocation', 'geopoint_announce']
train = train.drop(columns = geo)
 val = val.drop(columns = geo)
 return train, val<categorify>
print('FIT MODEL WITH TOP 100 FEATURES.....')
 cols = best_features.feature.tolist()
train = train[cols]
test = test[cols]
clf.fit(train,target )
def OneHot(train, val):
categorielles = train.dtypes[train.dtypes == 'object'].index.tolist()
for var in categorielles:
for modalite in train[var].unique() :
if modalite in val[var].unique() :
if modalite == modalite:
col = var + '_' + ''.join(e for e in modalite if e.isalnum() and e not in ['É', 'é', 'ô'])
 train[col] =(train[var] == modalite ).astype(float)
 val[col] =(val[var].astype(str)== modalite ).astype(float)
 else:
col = var + '_' + str(mod)
 train[col] = train[var].isnull().astype(float)
 val[col] = val[var].isnull().astype(float)
 train = train.drop(columns = categorielles)
 val = val.drop(columns = categorielles)
 return train, val<load_from_csv>
print('PREDICTING TEST DATA...... ')
 pred = clf.predict_proba(test,num_iteration=clf.best_iteration_)[:,1]
dir_path = os.path.join('.. /input', os.listdir('.. /input')[0])
 print("Files are :", os.listdir(dir_path))
def load_data(dir_path, filename):
df = pd.read_csv(os.path.join(dir_path, filename), sep=",", encoding="utf-8")
 return df
train_data = load_data(dir_path, os.path.join('train_booking.csv', 'train_booking.csv'))
test_data = load_data(dir_path, os.path.join('test_booking.csv', 'test_booking.csv'))<prepare_x_and_y>
sub= app_test1
sub['TARGET'] = p5
t2 = sub[['SK_ID_CURR','TARGET']]
t2.head()
%%time
print('--- 1.Import Data ---
')
 train_raw, val_raw = ImportData()
train_raw = train_data
val_raw = test_data
print('--- 2.Pretraitement ---')
 print('--- Cible & id ---')
 train, val, y_train, id_val = Cible(train_raw, val_raw, 'price')
 print('--- Texte vers Date ---')
 train, val = TextToDate(train, val, ['host_since', 'first_review', 'last_review'])
 print('--- Feat engineering ---')
 train, val = TreatVariables(train, val, ['host_since', 'first_review', 'last_review'],['host_verifications', 'amenities'])
 print('--- Col inutiles ---')
 train, val = Remove(train, val)
 print('--- One Hot Encoding ---')
 train, val = OneHot(train, val)
 print('
---------------------------')
 print('Done.' )<split>
print(t2.isnull().sum())
 t2.head()
<train_model><EOS> 
t2.to_csv('submission.csv',index=False )