Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<define_variables>
warnings.simplefilter(action='ignore', category=FutureWarning )
batch_size = 32
train_dataset = train_dataset.shuffle(1000 ).batch(batch_size, drop_remainder=True )<categorify>
DATA_DIRECTORY = ".. /input/home-credit-default-risk"
y_senti = to_categorical(data_source[data_source.data_type=='validation'].Sentiment)
 v= tokenizer(
 text=data_source[data_source.data_type=='validation'].Phrase.to_list() ,
add_special_tokens=True,
max_length=max_token_length,
truncation=True,
padding=True,
return_tensors='tf',
return_token_type_ids = False,
return_attention_mask = True,
verbose = True)
 validation_dataset = tf.data.Dataset.from_tensor_slices(( v['input_ids'], v['attention_mask'], y_senti))
validation_dataset = validation_dataset.map(map_function)
 validation_dataset = validation_dataset.shuffle(1000 ).batch(batch_size, drop_remainder=True )<choose_model_class>
df_train = pd.read_csv(os.path.join(DATA_DIRECTORY, 'application_train.csv'))
df_test = pd.read_csv(os.path.join(DATA_DIRECTORY, 'application_test.csv'))
df = df_train.append(df_test)
 del df_train, df_test; gc.collect()
input_ids = tf.keras.Input(shape=(max_token_length,), name='input_ids', dtype='int32')
 attention_mask = tf.keras.Input(shape=(max_token_length,), name='attention_mask', dtype='int32')
 inputs = {'input_ids': input_ids, 'attention_mask': attention_mask}
bert = TFAutoModel.from_pretrained('bert-base-cased')
 embeddings = bert.bert(inputs)[1]
xis = tf.keras.layers.Dense(1024,activation='relu' )(embeddings)
 yhat = tf.keras.layers.Dense(sent_values_array.max() +1, activation='softmax', name='outputs' )(xis)
<choose_model_class>
df = df[df['AMT_INCOME_TOTAL'] < 20000000]
df = df[df['CODE_GENDER'] != 'XNA']
df['DAYS_EMPLOYED'].replace(365243, np.nan, inplace=True)
 df['DAYS_LAST_PHONE_CHANGE'].replace(0, np.nan, inplace=True )
optimizer = tf.keras.optimizers.Adam(lr=1e-5, decay=1e-6)
 loss = tf.keras.losses.CategoricalCrossentropy()
accuracy = tf.keras.metrics.CategoricalAccuracy('accuracy' )<choose_model_class>
def get_age_group(days_birth):
age_years = -days_birth / 365
if age_years < 27: return 1
elif age_years < 40: return 2
elif age_years < 50: return 3
elif age_years < 65: return 4
elif age_years < 99: return 5
else: return 0
model.compile(optimizer=optimizer, loss=loss, metrics=[accuracy] )<train_model>
docs = [f for f in df.columns if 'FLAG_DOC' in f]
df['DOCUMENT_COUNT'] = df[docs].sum(axis=1)
 df['NEW_DOC_KURT'] = df[docs].kurtosis(axis=1)
 df['AGE_RANGE'] = df['DAYS_BIRTH'].apply(lambda x: get_age_group(x))
history1 = model.fit(
 train_dataset,
validation_data= validation_dataset,
epochs=8 )<save_model>
df['EXT_SOURCES_PROD'] = df['EXT_SOURCE_1'] * df['EXT_SOURCE_2'] * df['EXT_SOURCE_3']
df['EXT_SOURCES_WEIGHTED'] = df.EXT_SOURCE_1 * 2 + df.EXT_SOURCE_2 * 1 + df.EXT_SOURCE_3 * 3
np.warnings.filterwarnings('ignore', r'All-NaN(slice|axis)encountered')
 for function_name in ['min', 'max', 'mean', 'nanmedian', 'var']:
feature_name = 'EXT_SOURCES_{}'.format(function_name.upper())
 df[feature_name] = eval('np.{}'.format(function_name))(
 df[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']], axis=1 )
model.save('./sentiment-analysis-on-movie-reviews/Movie_sentiment_analysis_model' )<load_from_csv>
df['CREDIT_TO_ANNUITY_RATIO'] = df['AMT_CREDIT'] / df['AMT_ANNUITY']
df['CREDIT_TO_GOODS_RATIO'] = df['AMT_CREDIT'] / df['AMT_GOODS_PRICE']
df['ANNUITY_TO_INCOME_RATIO'] = df['AMT_ANNUITY'] / df['AMT_INCOME_TOTAL']
df['CREDIT_TO_INCOME_RATIO'] = df['AMT_CREDIT'] / df['AMT_INCOME_TOTAL']
df['INCOME_TO_EMPLOYED_RATIO'] = df['AMT_INCOME_TOTAL'] / df['DAYS_EMPLOYED']
df['INCOME_TO_BIRTH_RATIO'] = df['AMT_INCOME_TOTAL'] / df['DAYS_BIRTH']
df['EMPLOYED_TO_BIRTH_RATIO'] = df['DAYS_EMPLOYED'] / df['DAYS_BIRTH']
df['ID_TO_BIRTH_RATIO'] = df['DAYS_ID_PUBLISH'] / df['DAYS_BIRTH']
df['CAR_TO_BIRTH_RATIO'] = df['OWN_CAR_AGE'] / df['DAYS_BIRTH']
df['CAR_TO_EMPLOYED_RATIO'] = df['OWN_CAR_AGE'] / df['DAYS_EMPLOYED']
df['PHONE_TO_BIRTH_RATIO'] = df['DAYS_LAST_PHONE_CHANGE'] / df['DAYS_BIRTH']
test=pd.read_table("/kaggle/working/sentiment-analysis-on-movie-reviews/test.tsv",sep='\t' )<data_type_conversions>
def do_mean(df, group_cols, counted, agg_name):
gp = df[group_cols + [counted]].groupby(group_cols)[counted].mean().reset_index().rename(
 columns={counted: agg_name})
 df = df.merge(gp, on=group_cols, how='left')
 del gp
gc.collect()
return df
x_test = tokenizer(
 text=test.Phrase.to_list() ,
add_special_tokens=True,
max_length=max_token_length,
truncation=True,
padding=True,
return_tensors='tf',
return_token_type_ids = False,
return_attention_mask = True,
verbose = True )<create_dataframe>
def do_median(df, group_cols, counted, agg_name):
gp = df[group_cols + [counted]].groupby(group_cols)[counted].median().reset_index().rename(
 columns={counted: agg_name})
 df = df.merge(gp, on=group_cols, how='left')
 del gp
gc.collect()
return df
test_items=tf.data.Dataset.from_tensor_slices(( x_test['input_ids'],x_test['attention_mask']))<categorify>
def do_std(df, group_cols, counted, agg_name):
gp = df[group_cols + [counted]].groupby(group_cols)[counted].std().reset_index().rename(
 columns={counted: agg_name})
 df = df.merge(gp, on=group_cols, how='left')
 del gp
gc.collect()
return df
def map_func(input_ids, masks):
return {'input_ids': input_ids, 'attention_mask': masks}
test_items = test_items.map(map_func)
 test_items = test_items.batch(32 )<predict_on_test>
def do_sum(df, group_cols, counted, agg_name):
gp = df[group_cols + [counted]].groupby(group_cols)[counted].sum().reset_index().rename(
 columns={counted: agg_name})
 df = df.merge(gp, on=group_cols, how='left')
 del gp
gc.collect()
return df
predictions=model.predict(test_items ).argmax(axis=-1 )<save_to_csv>
group = ['ORGANIZATION_TYPE', 'NAME_EDUCATION_TYPE', 'OCCUPATION_TYPE', 'AGE_RANGE', 'CODE_GENDER']
df = do_median(df, group, 'EXT_SOURCES_MEAN', 'GROUP_EXT_SOURCES_MEDIAN')
 df = do_std(df, group, 'EXT_SOURCES_MEAN', 'GROUP_EXT_SOURCES_STD')
 df = do_mean(df, group, 'AMT_INCOME_TOTAL', 'GROUP_INCOME_MEAN')
 df = do_std(df, group, 'AMT_INCOME_TOTAL', 'GROUP_INCOME_STD')
 df = do_mean(df, group, 'CREDIT_TO_ANNUITY_RATIO', 'GROUP_CREDIT_TO_ANNUITY_MEAN')
 df = do_std(df, group, 'CREDIT_TO_ANNUITY_RATIO', 'GROUP_CREDIT_TO_ANNUITY_STD')
 df = do_mean(df, group, 'AMT_CREDIT', 'GROUP_CREDIT_MEAN')
 df = do_mean(df, group, 'AMT_ANNUITY', 'GROUP_ANNUITY_MEAN')
 df = do_std(df, group, 'AMT_ANNUITY', 'GROUP_ANNUITY_STD' )
submission = pd.DataFrame()
submission['PhraseId'] = test['PhraseId']
submission['Sentiment'] = predictions
submission.to_csv("submission.csv", index=False)
 submission.head()<load_from_csv>
def label_encoder(df, categorical_columns=None):
if not categorical_columns:
categorical_columns = [col for col in df.columns if df[col].dtype == 'object']
for col in categorical_columns:
df[col], uniques = pd.factorize(df[col])
 return df, categorical_columns
train_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/train.tsv.zip', sep = '\t')
 train_data.head()<load_from_csv>
def drop_application_columns(df):
drop_list = [
'CNT_CHILDREN', 'CNT_FAM_MEMBERS', 'HOUR_APPR_PROCESS_START',
'FLAG_EMP_PHONE', 'FLAG_MOBIL', 'FLAG_CONT_MOBILE', 'FLAG_EMAIL', 'FLAG_PHONE',
'FLAG_OWN_REALTY', 'REG_REGION_NOT_LIVE_REGION', 'REG_REGION_NOT_WORK_REGION',
'REG_CITY_NOT_WORK_CITY', 'OBS_30_CNT_SOCIAL_CIRCLE', 'OBS_60_CNT_SOCIAL_CIRCLE',
'AMT_REQ_CREDIT_BUREAU_DAY', 'AMT_REQ_CREDIT_BUREAU_MON', 'AMT_REQ_CREDIT_BUREAU_YEAR',
'COMMONAREA_MODE', 'NONLIVINGAREA_MODE', 'ELEVATORS_MODE', 'NONLIVINGAREA_AVG',
'FLOORSMIN_MEDI', 'LANDAREA_MODE', 'NONLIVINGAREA_MEDI', 'LIVINGAPARTMENTS_MODE',
'FLOORSMIN_AVG', 'LANDAREA_AVG', 'FLOORSMIN_MODE', 'LANDAREA_MEDI',
'COMMONAREA_MEDI', 'YEARS_BUILD_AVG', 'COMMONAREA_AVG', 'BASEMENTAREA_AVG',
'BASEMENTAREA_MODE', 'NONLIVINGAPARTMENTS_MEDI', 'BASEMENTAREA_MEDI',
'LIVINGAPARTMENTS_AVG', 'ELEVATORS_AVG', 'YEARS_BUILD_MEDI', 'ENTRANCES_MODE',
'NONLIVINGAPARTMENTS_MODE', 'LIVINGAREA_MODE', 'LIVINGAPARTMENTS_MEDI',
'YEARS_BUILD_MODE', 'YEARS_BEGINEXPLUATATION_AVG', 'ELEVATORS_MEDI', 'LIVINGAREA_MEDI',
'YEARS_BEGINEXPLUATATION_MODE', 'NONLIVINGAPARTMENTS_AVG', 'HOUSETYPE_MODE',
'FONDKAPREMONT_MODE', 'EMERGENCYSTATE_MODE'
]
for doc_num in [2,4,5,6,7,9,10,11,12,13,14,15,16,17,19,20,21]:
drop_list.append('FLAG_DOCUMENT_{}'.format(doc_num))
df.drop(drop_list, axis=1, inplace=True)
 return df
test_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/test.tsv.zip',sep = '\t')
 test_data.head()<import_modules>
df, le_encoded_cols = label_encoder(df, None)
 df = drop_application_columns(df )
import matplotlib.pyplot as plt
import tensorflow as tf<import_modules>
df = pd.get_dummies(df )
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences<set_options>
bureau = pd.read_csv(os.path.join(DATA_DIRECTORY, 'bureau.csv'))
print("TF version: ", tf.__version__)
 if tf.__version__ < "2.0.0":
tf.enable_eager_execution()
print("Eager execution enabled.")
 else:
print("Eager execution enabled by default.")
 if tf.test.gpu_device_name() :
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF" )<count_unique_values>
bureau['CREDIT_DURATION'] = -bureau['DAYS_CREDIT'] + bureau['DAYS_CREDIT_ENDDATE']
bureau['ENDDATE_DIF'] = bureau['DAYS_CREDIT_ENDDATE'] - bureau['DAYS_ENDDATE_FACT']
bureau['DEBT_PERCENTAGE'] = bureau['AMT_CREDIT_SUM'] / bureau['AMT_CREDIT_SUM_DEBT']
bureau['DEBT_CREDIT_DIFF'] = bureau['AMT_CREDIT_SUM'] - bureau['AMT_CREDIT_SUM_DEBT']
bureau['CREDIT_TO_ANNUITY_RATIO'] = bureau['AMT_CREDIT_SUM'] / bureau['AMT_ANNUITY']
print(train_data['Sentiment'].unique())
 train_data['Sentiment'].nunique()<count_values>
def one_hot_encoder(df, categorical_columns=None, nan_as_category=True):
original_columns = list(df.columns)
 if not categorical_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)
 categorical_columns = [c for c in df.columns if c not in original_columns]
return df, categorical_columns
train_data['Sentiment'].value_counts()<import_modules>
def group(df_to_agg, prefix, aggregations, aggregate_by= 'SK_ID_CURR'):
agg_df = df_to_agg.groupby(aggregate_by ).agg(aggregations)
 agg_df.columns = pd.Index(['{}{}_{}'.format(prefix, e[0], e[1].upper())
 for e in agg_df.columns.tolist() ])
 return agg_df.reset_index()
from tqdm import tqdm
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import re<string_transform>
def group_and_merge(df_to_agg, df_to_merge, prefix, aggregations, aggregate_by= 'SK_ID_CURR'):
agg_df = group(df_to_agg, prefix, aggregations, aggregate_by= aggregate_by)
 return df_to_merge.merge(agg_df, how='left', on= aggregate_by )
def sentence_cleaning(df):
sentence = []
for sent in tqdm(df['Phrase']):
text = re.sub("[^a-zA-Z]"," ",sent)
 word = word_tokenize(text.lower())
 lemmatizer = WordNetLemmatizer()
lemm_word = [lemmatizer.lemmatize(i)for i in word]
sentence.append(lemm_word)
 return(sentence )<prepare_x_and_y>
def get_bureau_balance(path, num_rows= None):
bb = pd.read_csv(os.path.join(path, 'bureau_balance.csv'))
bb, categorical_cols = one_hot_encoder(bb, nan_as_category= False)
 bb_processed = bb.groupby('SK_ID_BUREAU')[categorical_cols].mean().reset_index()
agg = {'MONTHS_BALANCE': ['min', 'max', 'mean', 'size']}
bb_processed = group_and_merge(bb, bb_processed, '', agg, 'SK_ID_BUREAU')
 del bb; gc.collect()
return bb_processed
target_col = train_data.Sentiment.values
y_target = to_categorical(target_col)
 y_target.shape<split>
bureau, categorical_cols = one_hot_encoder(bureau, nan_as_category= False)
 bureau = bureau.merge(get_bureau_balance(DATA_DIRECTORY), how='left', on='SK_ID_BUREAU')
 bureau['STATUS_12345'] = 0
for i in range(1,6):
bureau['STATUS_12345'] += bureau['STATUS_{}'.format(i)]
X_train,X_val,y_train,y_val = train_test_split(train_sent,y_target,test_size = 0.2,stratify = y_target )<count_unique_values>
features = ['AMT_CREDIT_MAX_OVERDUE', 'AMT_CREDIT_SUM_OVERDUE', 'AMT_CREDIT_SUM',
'AMT_CREDIT_SUM_DEBT', 'DEBT_PERCENTAGE', 'DEBT_CREDIT_DIFF', 'STATUS_0', 'STATUS_12345']
agg_length = bureau.groupby('MONTHS_BALANCE_SIZE')[features].mean().reset_index()
agg_length.rename({feat: 'LL_' + feat for feat in features}, axis=1, inplace=True)
 bureau = bureau.merge(agg_length, how='left', on='MONTHS_BALANCE_SIZE')
 del agg_length; gc.collect()
unique_words = set()
max_len = 0
for sent in tqdm(X_train):
unique_words.update(sent)
 if(max_len < len(sent)) :
max_len = len(sent)
 sentence = sent<define_variables>
BUREAU_AGG = {
'SK_ID_BUREAU': ['nunique'],
'DAYS_CREDIT': ['min', 'max', 'mean'],
'DAYS_CREDIT_ENDDATE': ['min', 'max'],
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['max', 'mean', 'sum'],
'AMT_ANNUITY': ['mean'],
'DEBT_CREDIT_DIFF': ['mean', 'sum'],
'MONTHS_BALANCE_MEAN': ['mean', 'var'],
'MONTHS_BALANCE_SIZE': ['mean', 'sum'],
'STATUS_0': ['mean'],
'STATUS_1': ['mean'],
'STATUS_12345': ['mean'],
'STATUS_C': ['mean'],
'STATUS_X': ['mean'],
'CREDIT_ACTIVE_Active': ['mean'],
'CREDIT_ACTIVE_Closed': ['mean'],
'CREDIT_ACTIVE_Sold': ['mean'],
'CREDIT_TYPE_Consumer credit': ['mean'],
'CREDIT_TYPE_Credit card': ['mean'],
'CREDIT_TYPE_Car loan': ['mean'],
'CREDIT_TYPE_Mortgage': ['mean'],
'CREDIT_TYPE_Microloan': ['mean'],
'LL_AMT_CREDIT_SUM_OVERDUE': ['mean'],
'LL_DEBT_CREDIT_DIFF': ['mean'],
'LL_STATUS_12345': ['mean'],
}
BUREAU_ACTIVE_AGG = {
'DAYS_CREDIT': ['max', 'mean'],
'DAYS_CREDIT_ENDDATE': ['min', 'max'],
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM': ['max', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['max', 'mean'],
'DAYS_CREDIT_UPDATE': ['min', 'mean'],
'DEBT_PERCENTAGE': ['mean'],
'DEBT_CREDIT_DIFF': ['mean'],
'CREDIT_TO_ANNUITY_RATIO': ['mean'],
'MONTHS_BALANCE_MEAN': ['mean', 'var'],
'MONTHS_BALANCE_SIZE': ['mean', 'sum'],
}
BUREAU_CLOSED_AGG = {
'DAYS_CREDIT': ['max', 'var'],
'DAYS_CREDIT_ENDDATE': ['max'],
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM_OVERDUE': ['mean'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'sum'],
'DAYS_CREDIT_UPDATE': ['max'],
'ENDDATE_DIF': ['mean'],
'STATUS_12345': ['mean'],
}
BUREAU_LOAN_TYPE_AGG = {
'DAYS_CREDIT': ['mean', 'max'],
'AMT_CREDIT_MAX_OVERDUE': ['mean', 'max'],
'AMT_CREDIT_SUM': ['mean', 'max'],
'AMT_CREDIT_SUM_DEBT': ['mean', 'max'],
'DEBT_PERCENTAGE': ['mean'],
'DEBT_CREDIT_DIFF': ['mean'],
'DAYS_CREDIT_ENDDATE': ['max'],
}
BUREAU_TIME_AGG = {
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM_OVERDUE': ['mean'],
'AMT_CREDIT_SUM': ['max', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['mean', 'sum'],
'DEBT_PERCENTAGE': ['mean'],
'DEBT_CREDIT_DIFF': ['mean'],
'STATUS_0': ['mean'],
'STATUS_12345': ['mean'],
}
vocabulary = len(list(unique_words))
oov = '<OOV>'
embedding_dim = 300
padding = 'post'
trunc = 'post'<string_transform>
agg_bureau = group(bureau, 'BUREAU_', BUREAU_AGG)
 active = bureau[bureau['CREDIT_ACTIVE_Active'] == 1]
agg_bureau = group_and_merge(active,agg_bureau,'BUREAU_ACTIVE_',BUREAU_ACTIVE_AGG)
 closed = bureau[bureau['CREDIT_ACTIVE_Closed'] == 1]
agg_bureau = group_and_merge(closed,agg_bureau,'BUREAU_CLOSED_',BUREAU_CLOSED_AGG)
 del active, closed; gc.collect()
for credit_type in ['Consumer credit', 'Credit card', 'Mortgage', 'Car loan', 'Microloan']:
type_df = bureau[bureau['CREDIT_TYPE_' + credit_type] == 1]
prefix = 'BUREAU_' + credit_type.split(' ')[0].upper() + '_'
agg_bureau = group_and_merge(type_df, agg_bureau, prefix, BUREAU_LOAN_TYPE_AGG)
 del type_df; gc.collect()
for time_frame in [6, 12]:
prefix = "BUREAU_LAST{}M_".format(time_frame)
 time_frame_df = bureau[bureau['DAYS_CREDIT'] >= -30*time_frame]
agg_bureau = group_and_merge(time_frame_df, agg_bureau, prefix, BUREAU_TIME_AGG)
 del time_frame_df; gc.collect()
tokenizer = Tokenizer(num_words = vocabulary,oov_token = oov,char_level = False)
 tokenizer.fit_on_texts(list(X_train))
X_train = tokenizer.texts_to_sequences(X_train)
 X_train = pad_sequences(X_train,maxlen = max_len,padding=padding,truncating = trunc)
 X_val = tokenizer.texts_to_sequences(X_val)
 X_val = pad_sequences(X_val,maxlen = max_len,padding=padding,truncating = trunc)
 X_test = tokenizer.texts_to_sequences(test_sent)
 X_test = pad_sequences(X_test,maxlen = max_len,padding=padding,truncating = trunc )<import_modules>
sort_bureau = bureau.sort_values(by=['DAYS_CREDIT'])
 gr = sort_bureau.groupby('SK_ID_CURR')['AMT_CREDIT_MAX_OVERDUE'].last().reset_index()
gr.rename({'AMT_CREDIT_MAX_OVERDUE': 'BUREAU_LAST_LOAN_MAX_OVERDUE'}, inplace=True)
 agg_bureau = agg_bureau.merge(gr, on='SK_ID_CURR', how='left')
 agg_bureau['BUREAU_DEBT_OVER_CREDIT'] = \
agg_bureau['BUREAU_AMT_CREDIT_SUM_DEBT_SUM']/agg_bureau['BUREAU_AMT_CREDIT_SUM_SUM']
agg_bureau['BUREAU_ACTIVE_DEBT_OVER_CREDIT'] = \
agg_bureau['BUREAU_ACTIVE_AMT_CREDIT_SUM_DEBT_SUM']/agg_bureau['BUREAU_ACTIVE_AMT_CREDIT_SUM_SUM']
from keras.models import Sequential
from keras.layers import Dense,Bidirectional,Activation,Dropout,LSTM,Embedding
from keras.layers.embeddings import Embedding<choose_model_class>
df = pd.merge(df, agg_bureau, on='SK_ID_CURR', how='left')
 del agg_bureau, bureau
gc.collect()
model = tf.keras.Sequential()
model.add(Embedding(vocabulary,embedding_dim,input_length = max_len))
model.add(Bidirectional(LSTM(128, dropout = 0.8, recurrent_dropout=0.8, return_sequences=True)))
 model.add(Bidirectional(LSTM(128,dropout = 0.5,recurrent_dropout=0.5,return_sequences=False)))
 model.add(Dense(64,activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(5,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 model.summary()<choose_model_class>
prev = pd.read_csv(os.path.join(DATA_DIRECTORY, 'previous_application.csv'))
pay = pd.read_csv(os.path.join(DATA_DIRECTORY, 'installments_payments.csv'))
model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'] )<train_model>
PREVIOUS_AGG = {
'SK_ID_PREV': ['nunique'],
'AMT_ANNUITY': ['min', 'max', 'mean'],
'AMT_DOWN_PAYMENT': ['max', 'mean'],
'HOUR_APPR_PROCESS_START': ['min', 'max', 'mean'],
'RATE_DOWN_PAYMENT': ['max', 'mean'],
'DAYS_DECISION': ['min', 'max', 'mean'],
'CNT_PAYMENT': ['max', 'mean'],
'DAYS_TERMINATION': ['max'],
'CREDIT_TO_ANNUITY_RATIO': ['mean', 'max'],
'APPLICATION_CREDIT_DIFF': ['min', 'max', 'mean'],
'APPLICATION_CREDIT_RATIO': ['min', 'max', 'mean', 'var'],
'DOWN_PAYMENT_TO_CREDIT': ['mean'],
}
PREVIOUS_ACTIVE_AGG = {
'SK_ID_PREV': ['nunique'],
'SIMPLE_INTERESTS': ['mean'],
'AMT_ANNUITY': ['max', 'sum'],
'AMT_APPLICATION': ['max', 'mean'],
'AMT_CREDIT': ['sum'],
'AMT_DOWN_PAYMENT': ['max', 'mean'],
'DAYS_DECISION': ['min', 'mean'],
'CNT_PAYMENT': ['mean', 'sum'],
'DAYS_LAST_DUE_1ST_VERSION': ['min', 'max', 'mean'],
'AMT_PAYMENT': ['sum'],
'INSTALMENT_PAYMENT_DIFF': ['mean', 'max'],
'REMAINING_DEBT': ['max', 'mean', 'sum'],
'REPAYMENT_RATIO': ['mean'],
}
PREVIOUS_LATE_PAYMENTS_AGG = {
'DAYS_DECISION': ['min', 'max', 'mean'],
'DAYS_LAST_DUE_1ST_VERSION': ['min', 'max', 'mean'],
'APPLICATION_CREDIT_DIFF': ['min'],
'NAME_CONTRACT_TYPE_Consumer loans': ['mean'],
'NAME_CONTRACT_TYPE_Cash loans': ['mean'],
'NAME_CONTRACT_TYPE_Revolving loans': ['mean'],
}
PREVIOUS_LOAN_TYPE_AGG = {
'AMT_CREDIT': ['sum'],
'AMT_ANNUITY': ['mean', 'max'],
'SIMPLE_INTERESTS': ['min', 'mean', 'max', 'var'],
'APPLICATION_CREDIT_DIFF': ['min', 'var'],
'APPLICATION_CREDIT_RATIO': ['min', 'max', 'mean'],
'DAYS_DECISION': ['max'],
'DAYS_LAST_DUE_1ST_VERSION': ['max', 'mean'],
'CNT_PAYMENT': ['mean'],
}
PREVIOUS_TIME_AGG = {
'AMT_CREDIT': ['sum'],
'AMT_ANNUITY': ['mean', 'max'],
'SIMPLE_INTERESTS': ['mean', 'max'],
'DAYS_DECISION': ['min', 'mean'],
'DAYS_LAST_DUE_1ST_VERSION': ['min', 'max', 'mean'],
'APPLICATION_CREDIT_DIFF': ['min'],
'APPLICATION_CREDIT_RATIO': ['min', 'max', 'mean'],
'NAME_CONTRACT_TYPE_Consumer loans': ['mean'],
'NAME_CONTRACT_TYPE_Cash loans': ['mean'],
'NAME_CONTRACT_TYPE_Revolving loans': ['mean'],
}
PREVIOUS_APPROVED_AGG = {
'SK_ID_PREV': ['nunique'],
'AMT_ANNUITY': ['min', 'max', 'mean'],
'AMT_CREDIT': ['min', 'max', 'mean'],
'AMT_DOWN_PAYMENT': ['max'],
'AMT_GOODS_PRICE': ['max'],
'HOUR_APPR_PROCESS_START': ['min', 'max'],
'DAYS_DECISION': ['min', 'mean'],
'CNT_PAYMENT': ['max', 'mean'],
'DAYS_TERMINATION': ['mean'],
'CREDIT_TO_ANNUITY_RATIO': ['mean', 'max'],
'APPLICATION_CREDIT_DIFF': ['max'],
'APPLICATION_CREDIT_RATIO': ['min', 'max', 'mean'],
'DAYS_FIRST_DRAWING': ['max', 'mean'],
'DAYS_FIRST_DUE': ['min', 'mean'],
'DAYS_LAST_DUE_1ST_VERSION': ['min', 'max', 'mean'],
'DAYS_LAST_DUE': ['max', 'mean'],
'DAYS_LAST_DUE_DIFF': ['min', 'max', 'mean'],
'SIMPLE_INTERESTS': ['min', 'max', 'mean'],
}
PREVIOUS_REFUSED_AGG = {
'AMT_APPLICATION': ['max', 'mean'],
'AMT_CREDIT': ['min', 'max'],
'DAYS_DECISION': ['min', 'max', 'mean'],
'CNT_PAYMENT': ['max', 'mean'],
'APPLICATION_CREDIT_DIFF': ['min', 'max', 'mean', 'var'],
'APPLICATION_CREDIT_RATIO': ['min', 'mean'],
'NAME_CONTRACT_TYPE_Consumer loans': ['mean'],
'NAME_CONTRACT_TYPE_Cash loans': ['mean'],
'NAME_CONTRACT_TYPE_Revolving loans': ['mean'],
}
hist_model = model.fit(X_train,y_train,
validation_data =(X_val, y_val),
epochs = 4,
batch_size = 256,
verbose = 1 )<define_variables>
ohe_columns = [
'NAME_CONTRACT_STATUS', 'NAME_CONTRACT_TYPE', 'CHANNEL_TYPE',
'NAME_TYPE_SUITE', 'NAME_YIELD_GROUP', 'PRODUCT_COMBINATION',
'NAME_PRODUCT_TYPE', 'NAME_CLIENT_TYPE']
prev, categorical_cols = one_hot_encoder(prev, ohe_columns, nan_as_category= False )
test_id = test_data['PhraseId']
test_id<save_to_csv>
prev['APPLICATION_CREDIT_DIFF'] = prev['AMT_APPLICATION'] - prev['AMT_CREDIT']
prev['APPLICATION_CREDIT_RATIO'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']
prev['CREDIT_TO_ANNUITY_RATIO'] = prev['AMT_CREDIT']/prev['AMT_ANNUITY']
prev['DOWN_PAYMENT_TO_CREDIT'] = prev['AMT_DOWN_PAYMENT'] / prev['AMT_CREDIT']
total_payment = prev['AMT_ANNUITY'] * prev['CNT_PAYMENT']
prev['SIMPLE_INTERESTS'] =(total_payment/prev['AMT_CREDIT'] - 1)/prev['CNT_PAYMENT']
y_pred = np.argmax(model.predict(X_test), axis = -1)
 submission_df = pd.DataFrame({'PhraseId': test_id, 'Sentiment': y_pred})
 submission_df.to_csv('submission.csv', index=False)
 submission_df.head()<import_modules>
approved = prev[prev['NAME_CONTRACT_STATUS_Approved'] == 1]
active_df = approved[approved['DAYS_LAST_DUE'] == 365243]
active_pay = pay[pay['SK_ID_PREV'].isin(active_df['SK_ID_PREV'])]
active_pay_agg = active_pay.groupby('SK_ID_PREV')[['AMT_INSTALMENT', 'AMT_PAYMENT']].sum()
active_pay_agg.reset_index(inplace= True)
 active_pay_agg['INSTALMENT_PAYMENT_DIFF'] = active_pay_agg['AMT_INSTALMENT'] - active_pay_agg['AMT_PAYMENT']
active_df = active_df.merge(active_pay_agg, on= 'SK_ID_PREV', how= 'left')
 active_df['REMAINING_DEBT'] = active_df['AMT_CREDIT'] - active_df['AMT_PAYMENT']
active_df['REPAYMENT_RATIO'] = active_df['AMT_PAYMENT'] / active_df['AMT_CREDIT']
active_agg_df = group(active_df, 'PREV_ACTIVE_', PREVIOUS_ACTIVE_AGG)
 active_agg_df['TOTAL_REPAYMENT_RATIO'] = active_agg_df['PREV_ACTIVE_AMT_PAYMENT_SUM']/\
active_agg_df['PREV_ACTIVE_AMT_CREDIT_SUM']
del active_pay, active_pay_agg, active_df; gc.collect()
import matplotlib.pyplot as plt
import tensorflow as tf
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences<load_from_csv>
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 )
train_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/train.tsv.zip', sep = '\t')
 train_data.head()<load_from_csv>
prev['DAYS_LAST_DUE_DIFF'] = prev['DAYS_LAST_DUE_1ST_VERSION'] - prev['DAYS_LAST_DUE']
approved['DAYS_LAST_DUE_DIFF'] = approved['DAYS_LAST_DUE_1ST_VERSION'] - approved['DAYS_LAST_DUE']
test_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/test.tsv.zip',sep = '\t')
 test_data.head()<count_unique_values>
categorical_agg = {key: ['mean'] for key in categorical_cols}
print(train_data['Sentiment'].unique())
 train_data['Sentiment'].nunique()<count_values>
agg_prev = group(prev, 'PREV_', {**PREVIOUS_AGG, **categorical_agg})
 agg_prev = agg_prev.merge(active_agg_df, how='left', on='SK_ID_CURR')
 del active_agg_df; gc.collect()
train_data['Sentiment'].value_counts()<import_modules>
agg_prev = group_and_merge(approved, agg_prev, 'APPROVED_', PREVIOUS_APPROVED_AGG)
 refused = prev[prev['NAME_CONTRACT_STATUS_Refused'] == 1]
agg_prev = group_and_merge(refused, agg_prev, 'REFUSED_', PREVIOUS_REFUSED_AGG)
 del approved, refused; gc.collect()
from tqdm import tqdm
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import re<string_transform>
for loan_type in ['Consumer loans', 'Cash loans']:
type_df = prev[prev['NAME_CONTRACT_TYPE_{}'.format(loan_type)] == 1]
prefix = 'PREV_' + loan_type.split(" ")[0] + '_'
agg_prev = group_and_merge(type_df, agg_prev, prefix, PREVIOUS_LOAN_TYPE_AGG)
 del type_df; gc.collect()
def clean_sentences(df):
reviews = []
for sent in tqdm(df['Phrase']):
text = re.sub("[^a-zA-Z]"," ",sent)
 words = word_tokenize(text.lower())
 new_words = [ ele for ele in words if ele.lower() not in stopwords.words('english')]
lem = WordNetLemmatizer()
lem_words = [lem.lemmatize(i)for i in new_words]
reviews.append(lem_words)
 return(reviews )<string_transform>
pay['LATE_PAYMENT'] = pay['DAYS_ENTRY_PAYMENT'] - pay['DAYS_INSTALMENT']
pay['LATE_PAYMENT'] = pay['LATE_PAYMENT'].apply(lambda x: 1 if x > 0 else 0)
 dpd_id = pay[pay['LATE_PAYMENT'] > 0]['SK_ID_PREV'].unique()
%%time
train_sentences = clean_sentences(train_data)
 test_sentences = clean_sentences(test_data)
 print(len(train_sentences))
print(len(test_sentences))<string_transform>
agg_dpd = group_and_merge(prev[prev['SK_ID_PREV'].isin(dpd_id)], agg_prev,
'PREV_LATE_', PREVIOUS_LATE_PAYMENTS_AGG)
 del agg_dpd, dpd_id; gc.collect()
print(train_data['Phrase'][0])
 print(( " " ).join(train_sentences[0]))<prepare_x_and_y>
df = pd.merge(df, agg_prev, on='SK_ID_CURR', how='left' )
y_target = to_categorical(train_data['Sentiment'].values )<split>
train = df[df['TARGET'].notnull() ]
test = df[df['TARGET'].isnull() ]
del df
gc.collect()
X_train,X_val,y_train,y_val = train_test_split(train_sentences,y_target,test_size = 0.2,stratify = y_target )<count_unique_values>
labels = train['TARGET']
train = train.drop(columns=['TARGET'])
 test = test.drop(columns=['TARGET'] )
unique_words = set()
len_max = -1
for sent in tqdm(X_train):
unique_words.update(sent)
 if(len_max < len(sent)) :
len_max = len(sent)
 print('Words in vocab : ' , len(list(unique_words)))
 print('Max_length : ' , len_max )<define_variables>
feature = list(train.columns)
 train.replace([np.inf, -np.inf], np.nan, inplace=True)
 test.replace([np.inf, -np.inf], np.nan, inplace=True)
 test_df = test.copy()
train_df = train.copy()
train_df['TARGET'] = labels
vocab_size = len(list(unique_words))
embedding_dim = 300
max_length = len_max
trunc_type = 'post'
padding_type = 'post'
oov_tok = '<OOV>'<string_transform>
imputer = SimpleImputer(strategy = 'median')
 imputer.fit(train)
 train = imputer.transform(train)
 test = imputer.transform(test )
%%time
tokenizer = Tokenizer(num_words = vocab_size,
',
oov_token = oov_tok,
char_level = False)
 tokenizer.fit_on_texts(list(X_train))
X_train = tokenizer.texts_to_sequences(X_train)
 X_train = pad_sequences(X_train,
maxlen = max_length,
padding = padding_type,
truncating = trunc_type)
 X_val = tokenizer.texts_to_sequences(X_val)
 X_val = pad_sequences(X_val,
maxlen = max_length,
padding = padding_type,
truncating = trunc_type)
 X_test = tokenizer.texts_to_sequences(test_sentences)
 X_test = pad_sequences(X_test,
maxlen = max_length,
padding = padding_type,
truncating = trunc_type )<import_modules>
scaler = MinMaxScaler(feature_range =(0, 1))
scaler.fit(train)
 train = scaler.transform(train)
 est = scaler.transform(test )
from keras.models import Sequential
from keras.layers import Dense,Bidirectional,LSTM,Activation,Conv1D,MaxPool1D,Dropout
from keras.layers.embeddings import Embedding<choose_model_class>
log_reg = LogisticRegression(C = 0.0001)
 log_reg.fit(train, labels )
model = Sequential()
model.add(Embedding(vocab_size,embedding_dim,input_length = max_length))
model.add(Bidirectional(LSTM(128,dropout = 0.2, recurrent_dropout = 0.2, return_sequences=True)))
 model.add(Bidirectional(LSTM(64, dropout = 0.2, recurrent_dropout = 0.2, return_sequences=False)))
 model.add(Dense(128,activation = 'relu'))
model.add(Dense(y_target.shape[1],activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
 model.summary()<choose_model_class>
log_reg_pred = log_reg.predict_proba(test)[:, 1]
early_stopping = EarlyStopping(min_delta = 0.001,
mode = 'max',
monitor = 'val_acc',
patience = 2)
 callback = [early_stopping]<train_model>
submit = test_df[['SK_ID_CURR']]
submit['TARGET'] = log_reg_pred
%%time
num_epochs = 4
history = model.fit(X_train,y_train,
validation_data =(X_val, y_val),
epochs = num_epochs,
batch_size = 256,
verbose = 1,
callbacks = callback )<define_variables>
submit.to_csv('log_reg.csv', index = False )
test_id = test_data['PhraseId']<predict_on_test>
target = train_df.pop('TARGET')
 len_train = len(train_df)
 merged_df = pd.concat([train_df, test_df])
 meta_df = merged_df.pop('SK_ID_CURR')
 del test_df, train_df
gc.collect()
y_pred = np.argmax(model.predict(X_test), axis = -1 )<save_to_csv>
categorical_feats = merged_df.columns[merged_df.dtypes == 'object']
print('Using %d prediction variables'%(merged_df.shape[1]))
print('Encoding %d non-numeric columns...'%(merged_df.columns[merged_df.dtypes == 'object'].shape))
for feat in categorical_feats:
merged_df[feat].fillna('MISSING', inplace=True)
 encoder = LabelBinarizer()
new_columns = encoder.fit_transform(merged_df[feat])
 i=0
for u in merged_df[feat].unique() :
if i<new_columns.shape[1]:
merged_df[feat+'_'+u]=new_columns[:,i]
i+=1
merged_df.drop(feat, axis=1, inplace=True)
 print('Now using %d prediction variables'%(merged_df.shape[1]))
submission_df = pd.DataFrame({'PhraseId': test_id, 'Sentiment': y_pred})
 submission_df.to_csv('submission_.csv', index=False)
 submission_df.head()<load_from_csv>
null_counts = merged_df.isnull().sum()
null_counts = null_counts[null_counts > 0]
null_ratios = null_counts / len(merged_df)
train_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/train.tsv.zip', sep = '\t')
 train_data.head()<load_from_csv>
null_thresh =.8
null_cols = null_ratios[null_ratios > null_thresh].index
merged_df.drop(null_cols, axis=1, inplace=True)
 if null_cols.shape[0] > 0:
print('Columns dropped for being over %.2f null:'%(null_thresh))
for col in null_cols:
print(col )
test_data = pd.read_csv('.. /input/sentiment-analysis-on-movie-reviews/test.tsv.zip',sep = '\t')
 test_data.head()<import_modules>
merged_df.fillna(0, inplace=True)
import matplotlib.pyplot as plt
import tensorflow as tf<import_modules>
for feat in merged_df.columns:
if(merged_df[feat].max() > 100)|(merged_df[feat].min() < -100):
merged_df[feat]=merged_df[feat].astype(np.float64)
 scaler = StandardScaler()
continuous_feats = merged_df.columns[merged_df.dtypes == 'float64']
print('Scaling %d features...'%(continuous_feats.shape))
s1 = merged_df.shape[0],1
for feat in continuous_feats:
merged_df[feat] = scaler.fit_transform(merged_df[feat].values.reshape(s1))
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences<set_options>
train_df = merged_df[:len_train]
test_df = merged_df[len_train:]
del merged_df
gc.collect()
print("TF version: ", tf.__version__)
 if tf.__version__ < "2.0.0":
tf.enable_eager_execution()
print("Eager execution enabled.")
 else:
print("Eager execution enabled by default.")
 if tf.test.gpu_device_name() :
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF" )<count_unique_values>
L2c = 4e-4
lr0 = 0.02
lr_decay = 0.90
iterations = 11
ROWS = train_df.shape[0]
VARS = train_df.shape[1]
NUMB = 10000
NN = int(ROWS/NUMB )
print(train_data['Sentiment'].unique())
 train_data['Sentiment'].nunique()<count_values>
tf.disable_v2_behavior()
y_ = tf.placeholder(tf.float32, [None, 1])
 x = tf.placeholder(tf.float32, [None, VARS])
 W = tf.Variable(tf.truncated_normal([VARS,1],mean=0.0,stddev=0.001),dtype=np.float32)
 NUML1 = 10
W1 = tf.Variable(tf.truncated_normal([VARS,NUML1],mean=0.0,stddev=0.0001),dtype=np.float32)
 W1f = tf.Variable(tf.truncated_normal([NUML1,1],mean=0.0,stddev=0.0001),dtype=np.float32)
 logit1 = tf.matmul(x, W)+ tf.matmul(tf.nn.relu(tf.matmul(x, W1)) , W1f)
 y = tf.nn.sigmoid(logit1 )
train_data['Sentiment'].value_counts()<import_modules>
loss0 = tf.reduce_mean(( y_-y)*(y_-y))
loss1 = L2c *(tf.nn.l2_loss(W)+ tf.nn.l2_loss(W1)+ tf.nn.l2_loss(W1f))
loss = loss0 + loss1
global_step = tf.Variable(0, trainable=False)
 learning_rate = tf.train.exponential_decay(lr0, global_step, NN, lr_decay)
 train_step = tf.train.AdamOptimizer(learning_rate=learning_rate ).minimize(loss,global_step=global_step)
 sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
from tqdm import tqdm
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import re<string_transform>
y0=target.values.astype(np.float32)
 x0=train_df.values.astype(np.float32)
 del train_df
gc.collect()
y0_1=np.where(y0[0:int(NN*0.8)*NUMB] == 1)[0]
y0_0=np.where(y0[0:int(NN*0.8)*NUMB] == 0)[0]
for i in range(iterations):
for j in range(int(NN*0.8)) :
pos_ratio = 0.5
pos_idx = np.random.choice(y0_1, size=int(np.round(NUMB*pos_ratio)))
 neg_idx = np.random.choice(y0_0, size=int(np.round(NUMB*(1-pos_ratio))))
idx = np.concatenate([pos_idx, neg_idx])
 fd = {y_: y0[idx].reshape(NUMB,1),x: x0[idx,:]}
_= sess.run([train_step], feed_dict=fd)
 if i%10 == 0:
fd = {y_: y0.reshape(y0.shape[0],1),x: x0}
y1 = sess.run(y, feed_dict=fd)
 lim = int(NN*0.8)* NUMB
auc1 = roc_auc_score(y0[0:lim],y1[0:lim,0])
 auc2 = roc_auc_score(y0[lim:y0.shape[0]],y1[lim:y0.shape[0],0])
 print('iteration %d, auc train/validatet %.5f/%.5f'%(i,auc1,auc2))
def sentence_cleaning(df):
sentence = []
for sent in tqdm(df['Phrase']):
text = re.sub("[^a-zA-Z]"," ",sent)
 word = word_tokenize(text.lower())
 lemmatizer = WordNetLemmatizer()
lemm_word = [lemmatizer.lemmatize(i)for i in word]
sentence.append(lemm_word)
 return(sentence )<prepare_x_and_y>
x0 = test_df.values.astype(np.float32)
 fd = {y_: np.zeros([x0.shape[0],1]),x: x0}
y_pred = sess.run(y, feed_dict=fd)
 out_df = pd.DataFrame({'SK_ID_CURR': meta_df[len_train:], 'TARGET': y_pred[:,0]})
 out_df.to_csv('nn_submission.csv', index=False)
target_col = train_data.Sentiment.values
y_target = to_categorical(target_col)
 y_target.shape<split>
nn_result=pd.read_csv('.. /input/pial-data-n/nn_p.csv')
 log_result=pd.read_csv('.. /input/pial-data/lgb_p.csv')
 nn_result.rename(columns={'TARGET':'nn_TARGET'},inplace=True)
 log_result.rename(columns={'TARGET':'log_TARGET'},inplace=True)
 sub=pd.merge(nn_result,log_result,on='SK_ID_CURR')
 sub['TARGET']=0*sub['nn_TARGET']+1*sub['log_TARGET']
sub=sub.drop(columns=['log_TARGET','nn_TARGET'])
 sub.to_csv('subm_nn_log_p.csv', index=False, float_format='%.8f' )