Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<categorify>
warnings.simplefilter(action='ignore', category=FutureWarning)
 py.init_notebook_mode(connected=True)
 %matplotlib inline
cf.go_offline()
for i in range(10):
print(maps[i], y[i] )<save_to_csv>
def one_hot_encoder(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 df, new_columns
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv(index=False)
 b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}"target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html )<load_pretrained>
num_rows = None
nan_as_category = True
create_download_link(submission )<import_modules>
print("Start Train Test................." )
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np 
<load_from_csv>
df = pd.read_csv('.. /input/application_train.csv', nrows= num_rows)
 test_df = pd.read_csv('.. /input/application_test.csv', nrows= num_rows)
 print("Train samples: {}, test samples: {}".format(len(df), len(test_df)))
 df = df.append(test_df ).reset_index()
del test_df
gc.collect()
train_df = pd.read_csv('.. /input/train.csv')
 train_df.head()<load_from_csv>
for bin_feature in ['CODE_GENDER', 'FLAG_OWN_CAR', 'FLAG_OWN_REALTY']:
df[bin_feature], uniques = pd.factorize(df[bin_feature])
 df, cat_cols = one_hot_encoder(df, nan_as_category)
 df['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True )
test_df = pd.read_csv('.. /input/test.csv')
 test_df.head()<prepare_x_and_y>
df['DAYS_EMPLOYED_PERC'] = df['DAYS_EMPLOYED'] / df['DAYS_BIRTH']
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']
columns_in = [f"sensor
columns_out = "oil_extraction"
X = train_df[columns_in].values
train_y = train_df[columns_out].values
test_x = test_df[columns_in].values
def transform(x):
diffs = 25
diff = [np.zeros(x.shape)for i in range(diffs)]
n = len(x)
 diff[0][1:,:] = x[:n-1,:]
for i in range(1, diffs):
diff[i][1:,:] = diff[i-1][:n-1,:]
return np.hstack([x, *diff])
 train_x = np.hstack([transform(X), transform(X**2)] )<save_to_csv>
a = df['DAYS_EMPLOYED_PERC'].tolist()
a = [x for x in a if str(x)!= 'nan']
b = df['INCOME_CREDIT_PERC'].tolist()
b = [x for x in b if str(x)!= 'nan']
c = df['INCOME_PER_PERSON'].tolist()
c = [x for x in c if str(x)!= 'nan']
d = df['ANNUITY_INCOME_PERC'].tolist()
d = [x for x in d if str(x)!= 'nan']
def predictions_to_submission_file(predictions):
submission_df = pd.DataFrame(columns=['Expected', 'Id'])
 submission_df['Expected'] = predictions
submission_df['Id'] = range(len(predictions))
submission_df.to_csv('submission.csv', index=False)
 test_predictions = lin_reg.predict(np.hstack([transform(test_x), transform(test_x**2)]))
predictions_to_submission_file(test_predictions )<load_from_csv>
print("End Train Test.................. " )
train = pd.read_csv('/kaggle/input/deepnlp-hse-course/train.csv')
 test = pd.read_csv('/kaggle/input/deepnlp-hse-course/test.csv' )<import_modules>
print("Start Bureau................ " )
import torch
from torch.utils.data import Dataset, DataLoader
from nltk.tokenize import word_tokenize, wordpunct_tokenize
from tqdm import tqdm
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score<string_transform>
bureau = pd.read_csv('.. /input/bureau.csv', nrows = num_rows )
def process_text(text):
words = wordpunct_tokenize(text.lower())
 return words<feature_engineering>
bb = pd.read_csv('.. /input/bureau_balance.csv', nrows = num_rows)
 bb, bb_cat = one_hot_encoder(bb, nan_as_category)
 bureau, bureau_cat = one_hot_encoder(bureau, nan_as_category )
word2freq = {}
for question in tqdm(train.question):
words = process_text(question)
 for word in words:
if word in word2freq:
word2freq[word] += 1
else:
word2freq[word] = 1<randomize_order>
bb_aggregations = {'MONTHS_BALANCE': ['min', 'max', 'size']}
for col in bb_cat:
bb_aggregations[col] = ['mean']
bb_agg = bb.groupby('SK_ID_BUREAU' ).agg(bb_aggregations)
 bb_agg.columns = pd.Index([e[0] + "_" + e[1].upper() for e in bb_agg.columns.tolist() ])
 bureau = bureau.join(bb_agg, how='left', on='SK_ID_BUREAU')
 bureau.drop(columns= 'SK_ID_BUREAU', inplace= True)
 del bb, bb_agg
gc.collect()
word2index = {'PAD': 0}
vectors = []
word2vec_file = open('/kaggle/input/fasttest-common-crawl-russian/cc.ru.300.vec')
 n_words, embedding_dim = word2vec_file.readline().split()
n_words, embedding_dim = int(n_words), int(embedding_dim)
 vectors.append(np.zeros(( 1, embedding_dim)))
 progress_bar = tqdm(desc='Read word2vec', total=n_words)
 while True:
line = word2vec_file.readline().strip()
if not line:
break
current_parts = line.split()
current_word = ' '.join(current_parts[:-embedding_dim])
 if current_word in word2freq:
word2index[current_word] = len(word2index)
 current_vectors = current_parts[-embedding_dim:]
current_vectors = np.array(list(map(float, current_vectors)))
 current_vectors = np.expand_dims(current_vectors, 0)
 vectors.append(current_vectors)
 progress_bar.update(1)
 progress_bar.close()
word2vec_file.close()
vectors = np.concatenate(vectors )<sort_values>
num_aggregations = {
'DAYS_CREDIT': ['min', 'max', 'mean', 'var'],
'CREDIT_DAY_OVERDUE': ['max', 'mean'],
'DAYS_CREDIT_ENDDATE': ['min', 'max', 'mean'],
'AMT_CREDIT_MAX_OVERDUE': ['mean'],
'CNT_CREDIT_PROLONG': ['sum'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['mean'],
'AMT_CREDIT_SUM_LIMIT': ['mean', 'sum'],
'DAYS_CREDIT_UPDATE': ['min', 'max', 'mean'],
'AMT_ANNUITY': ['max', 'mean'],
'MONTHS_BALANCE_MIN': ['min'],
'MONTHS_BALANCE_MAX': ['max'],
'MONTHS_BALANCE_SIZE': ['mean', 'sum']
}
unk_words = [word for word in word2freq if word not in word2index]
unk_counts = [word2freq[word] for word in unk_words]
n_unk = sum(unk_counts)* 100 / sum(list(word2freq.values()))
sub_sample_unk_words = {word: word2freq[word] for word in unk_words}
sorted_unk_words = list(sorted(sub_sample_unk_words, key=lambda x: sub_sample_unk_words[x], reverse=True))
print('Мы не знаем {:.2f} % слов в датасете'.format(n_unk))
print('Количество неизвестных слов {} из {}, то есть {:.2f} % уникальных слов в словаре'.format(
 len(unk_words), len(word2freq), len(unk_words)* 100 / len(word2freq)))
 print('В среднем каждое встречается {:.2f} раз'.format(np.mean(unk_counts)))
 print()
print('Топ 5 невошедших слов:')
 for i in range(5):
print(sorted_unk_words[i], 'с количеством вхождениий -', word2freq[sorted_unk_words[i]] )<load_pretrained>
cat_aggregations = {}
for cat in bureau_cat: cat_aggregations[cat] = ['mean']
for cat in bb_cat: cat_aggregations[cat + "_MEAN"] = ['mean']
bureau_agg = bureau.groupby('SK_ID_CURR' ).agg({**num_aggregations, **cat_aggregations})
 bureau_agg.columns = pd.Index(['BURO_' + e[0] + "_" + e[1].upper() for e in bureau_agg.columns.tolist() ] )
class WordData(Dataset):
def __init__(self, x_data, y_data, word2index, sequence_length=32, pad_token='PAD', verbose=True):
super().__init__()
self.x_data = []
self.y_data = y_data
self.word2index = word2index
self.sequence_length = sequence_length
self.pad_token = pad_token
self.pad_index = self.word2index[self.pad_token]
self.load(x_data, verbose=verbose)
 @staticmethod
def process_text(text):
words = wordpunct_tokenize(text.lower())
 return words
def load(self, data, verbose=True):
data_iterator = tqdm(data, desc='Loading data', disable=not verbose)
 for text in data_iterator:
words = self.process_text(text)
 indexed_words = self.indexing(words)
 self.x_data.append(indexed_words)
 def indexing(self, tokenized_text):
return [self.word2index[token] for token in tokenized_text if token in self.word2index ]
def padding(self, sequence):
if len(sequence)> self.sequence_length:
sequence = sequence[:self.sequence_length]
elif len(sequence)< self.sequence_length:
sequence = sequence + [self.pad_index] *(self.sequence_length - len(sequence))
return sequence
def __len__(self):
return len(self.x_data)
 def __getitem__(self, idx):
x = self.x_data[idx]
x = self.padding(x)
 x = torch.Tensor(x ).long()
y = self.y_data[idx]
return x, y<split>
active = bureau[bureau['CREDIT_ACTIVE_Active'] == 1]
active_agg = active.groupby('SK_ID_CURR' ).agg(num_aggregations)
 active_agg.columns = pd.Index(['ACT_' + e[0] + "_" + e[1].upper() for e in active_agg.columns.tolist() ])
 bureau_agg = bureau_agg.join(active_agg, how='left')
 del active, active_agg
gc.collect()
closed = bureau[bureau['CREDIT_ACTIVE_Closed'] == 1]
closed_agg = closed.groupby('SK_ID_CURR' ).agg(num_aggregations)
 closed_agg.columns = pd.Index(['CLS_' + e[0] + "_" + e[1].upper() for e in closed_agg.columns.tolist() ])
 bureau_agg = bureau_agg.join(closed_agg, how='left')
 del closed, closed_agg, bureau
gc.collect()
x_train, x_validation, y_train, y_validation = train_test_split(train.question, train.main_category, test_size=0.1)
 train_dataset = WordData(list(x_train), list(y_train), word2index)
 train_loader = DataLoader(train_dataset, batch_size=64)
 validation_dataset = WordData(list(x_validation), list(y_validation), word2index)
 validation_loader = DataLoader(validation_dataset, batch_size=64)
 test_dataset = WordData(list(test.question), np.zeros(( test.shape[0])) , word2index)
 test_loader = DataLoader(test_dataset, batch_size=64 )<prepare_x_and_y>
print("End Bureau................ " )
for x, y in test_loader:
break<choose_model_class>
print("Start previous_application................ " )
class DeepAverageNetwork(torch.nn.Module):
def __init__(self, embedding_matrix, n_classes):
super().__init__()
self.embedding_layer = torch.nn.Embedding.from_pretrained(torch.Tensor(embedding_matrix))
self.layers = torch.nn.Sequential(torch.nn.Linear(300, 256),
torch.nn.ReLU() ,
torch.nn.Linear(256, 128),
torch.nn.ReLU() ,
torch.nn.Linear(128, n_classes))
def forward(self, x):
sequence_lengths =(x > 0 ).sum(dim=1)
 sequence_lengths[sequence_lengths == 0.] = 1
x = self.embedding_layer(x)
 x = x.mean(dim=-2)
 lengths_scaling = sequence_lengths.float() / x.size(1)
 lengths_scaling = lengths_scaling.unsqueeze(1 ).repeat(( 1, x.size(-1)))
 x /= lengths_scaling.to(x.device)
 x = self.layers(x)
 return x<choose_model_class>
prev = pd.read_csv('.. /input/previous_application.csv', nrows = num_rows )
model = DeepAverageNetwork(embedding_matrix=vectors, n_classes=n_classes )<prepare_output>
prev, cat_cols = one_hot_encoder(prev, nan_as_category= True)
 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 )
with torch.no_grad() :
pred = model(x )<set_options>
prev['APP_CREDIT_PERC'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']
device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu' )<choose_model_class>
num_aggregations = {
'AMT_ANNUITY': ['min', 'max', 'mean'],
'AMT_APPLICATION': ['min', 'max', 'mean'],
'AMT_CREDIT': ['min', 'max', 'mean'],
'APP_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'],
}
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(params=model.parameters())
 model = model.to(device)
 criterion = criterion.to(device )<train_model>
cat_aggregations = {}
for cat in cat_cols:
cat_aggregations[cat] = ['mean']
prev_agg = prev.groupby('SK_ID_CURR' ).agg({**num_aggregations, **cat_aggregations})
 prev_agg.columns = pd.Index(['PREV_' + e[0] + "_" + e[1].upper() for e in prev_agg.columns.tolist() ] )
epochs = 10
losses = []
best_test_loss = 10.
 test_f1 = []
for n_epoch in range(epochs):
train_losses = []
test_losses = []
test_targets = []
test_pred_class = []
progress_bar = tqdm(total=len(train_loader.dataset), desc='Epoch {}'.format(n_epoch + 1))
model.train()
for x, y in train_loader:
x = x.to(device)
 y = y.to(device)
 optimizer.zero_grad()
pred = model(x)
 loss = criterion(pred, y)
 loss.backward()
optimizer.step()
train_losses.append(loss.item())
 losses.append(loss.item())
 progress_bar.set_postfix(train_loss = np.mean(losses[-500:]))
progress_bar.update(x.shape[0])
 progress_bar.close()
model.eval()
for x, y in validation_loader:
x = x.to(device)
 y = y.to(device)
 with torch.no_grad() :
pred = model(x)
 pred = pred.cpu()
y = y.cpu()
test_targets.append(y.numpy())
 test_pred_class.append(np.argmax(pred, axis=1))
loss = criterion(pred, y)
 test_losses.append(loss.item())
 mean_test_loss = np.mean(test_losses)
 test_targets = np.concatenate(test_targets ).squeeze()
test_pred_class = np.concatenate(test_pred_class ).squeeze()
f1 = f1_score(test_targets, test_pred_class, average='micro')
 test_f1.append(f1)
 print()
print('Losses: train - {:.3f}, test - {:.3f}'.format(np.mean(train_losses), mean_test_loss))
print('F1 test - {:.3f}'.format(f1))
if mean_test_loss < best_test_loss:
best_test_loss = mean_test_loss
else:
print('Early stopping')
 break<predict_on_test>
approved = prev[prev['NAME_CONTRACT_STATUS_Approved'] == 1]
approved_agg = approved.groupby('SK_ID_CURR' ).agg(num_aggregations)
 approved_agg.columns = pd.Index(['APR_' + e[0] + "_" + e[1].upper() for e in approved_agg.columns.tolist() ])
 prev_agg = prev_agg.join(approved_agg, how='left')
 refused = prev[prev['NAME_CONTRACT_STATUS_Refused'] == 1]
refused_agg = refused.groupby('SK_ID_CURR' ).agg(num_aggregations)
 refused_agg.columns = pd.Index(['REF_' + e[0] + "_" + e[1].upper() for e in refused_agg.columns.tolist() ])
 prev_agg = prev_agg.join(refused_agg, how='left')
 del refused, refused_agg, approved, approved_agg, prev
gc.collect()
model.eval()
predictions = []
for x, _ in test_loader:
x = x.to(device)
 with torch.no_grad() :
pred = model(x)
 pred = pred.cpu()
predictions.append(np.argmax(pred, axis=1))
predictions = np.concatenate(predictions ).squeeze()<feature_engineering>
print("End previous_application................ " )
test['main_category'] = predictions<drop_column>
print("Start POS_CASH_balance................ " )
test = test[['index', 'main_category']]<save_to_csv>
pos = pd.read_csv('.. /input/POS_CASH_balance.csv', nrows = num_rows )
test.to_csv('submission.csv', index=False )<set_options>
pos, cat_cols = one_hot_encoder(pos, nan_as_category= True)
 aggregations = {
'MONTHS_BALANCE': ['max', 'mean', 'size'],
'SK_DPD': ['max', 'mean'],
'SK_DPD_DEF': ['max', 'mean']
}
warnings.filterwarnings('ignore')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
for cat in cat_cols:
aggregations[cat] = ['mean']
pos_agg = pos.groupby('SK_ID_CURR' ).agg(aggregations)
 pos_agg.columns = pd.Index(['POS_' + e[0] + "_" + e[1].upper() for e in pos_agg.columns.tolist() ])
 pos_agg['POS_COUNT'] = pos.groupby('SK_ID_CURR' ).size()
del pos
gc.collect()
data = pd.read_csv('/kaggle/input/bu-cs506-spring-2020-midterm/train.csv' )<count_values>
print("Start POS_CASH_balance................ " )
print('There are', str(data.shape[0]), 'records in total.' )<count_values>
ins = pd.read_csv('.. /input/installments_payments.csv', nrows = num_rows)
 ins, cat_cols = one_hot_encoder(ins, nan_as_category= True )
HelpfulnessNumerator0 = data[data['HelpfulnessNumerator'] == 0]['HelpfulnessNumerator'].value_counts()
HelpfulnessNumerator1 = data[data['HelpfulnessNumerator'] == 1]['HelpfulnessNumerator'].value_counts()
HelpfulnessNumerator2 = data[data['HelpfulnessNumerator'] == 2]['HelpfulnessNumerator'].value_counts()
HelpfulnessNumerator3 = data[data['HelpfulnessNumerator'] == 3]['HelpfulnessNumerator'].value_counts()
HelpfulnessNumeratorMoreThan3 = data[data['HelpfulnessNumerator'] > 3]['HelpfulnessNumerator'].value_counts()
labels = '0', '1', '2', '3', 'more than 3'
sizes = [HelpfulnessNumerator0.values.item() , HelpfulnessNumerator1.values.item() , HelpfulnessNumerator2.values.item() ,
HelpfulnessNumerator3.values.item() , HelpfulnessNumeratorMoreThan3.values.sum() ]
explode =(0, 0, 0, 0, 0)
 fig, ax = plt.subplots()
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
 ax.axis('equal')
 plt.title(' Portions of Amount of Helpfulness Labels')
 plt.show()<count_values>
ins['PAYMENT_PERC'] = ins['AMT_PAYMENT'] / ins['AMT_INSTALMENT']
ins['PAYMENT_DIFF'] = ins['AMT_INSTALMENT'] - ins['AMT_PAYMENT']
ins['DPD'] = ins['DAYS_ENTRY_PAYMENT'] - ins['DAYS_INSTALMENT']
ins['DBD'] = ins['DAYS_INSTALMENT'] - ins['DAYS_ENTRY_PAYMENT']
ins['DPD'] = ins['DPD'].apply(lambda x: x if x > 0 else 0)
 ins['DBD'] = ins['DBD'].apply(lambda x: x if x > 0 else 0 )
HelpfulnessDenominator0 = data[data['HelpfulnessDenominator'] == 0]['HelpfulnessDenominator'].value_counts()
HelpfulnessDenominator1 = data[data['HelpfulnessDenominator'] == 1]['HelpfulnessDenominator'].value_counts()
HelpfulnessDenominator2 = data[data['HelpfulnessDenominator'] == 2]['HelpfulnessDenominator'].value_counts()
HelpfulnessDenominator3 = data[data['HelpfulnessDenominator'] == 3]['HelpfulnessDenominator'].value_counts()
HelpfulnessDenominatorMoreThan3 = data[data['HelpfulnessDenominator'] > 3]['HelpfulnessDenominator'].value_counts()
labels = '0', '1', '2', '3', 'more than 3'
sizes = [HelpfulnessDenominator0.values.item() , HelpfulnessDenominator1.values.item() , HelpfulnessDenominator2.values.item() ,
HelpfulnessDenominator3.values.item() , HelpfulnessDenominatorMoreThan3.values.sum() ]
explode =(0, 0, 0, 0, 0)
 fig, ax = plt.subplots()
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
 ax.axis('equal')
 plt.title(' Portions of Amount of Comments Watched')
 plt.show()<categorify>
aggregations = {
'NUM_INSTALMENT_VERSION': ['nunique'],
'DPD': ['max', 'mean', 'sum'],
'DBD': ['max', 'mean', 'sum'],
'PAYMENT_PERC': ['max', 'mean', 'sum', 'var'],
'PAYMENT_DIFF': ['max', 'mean', 'sum', 'var'],
'AMT_INSTALMENT': ['max', 'mean', 'sum'],
'AMT_PAYMENT': ['min', 'max', 'mean', 'sum'],
'DAYS_ENTRY_PAYMENT': ['max', 'mean', 'sum']
}
for cat in cat_cols:
aggregations[cat] = ['mean']
ins_agg = ins.groupby('SK_ID_CURR' ).agg(aggregations)
 ins_agg.columns = pd.Index(['INS_' + e[0] + "_" + e[1].upper() for e in ins_agg.columns.tolist() ])
 ins_agg['INS_COUNT'] = ins.groupby('SK_ID_CURR' ).size()
del ins
gc.collect()
OHE = OneHotEncoder(sparse=True)
 ID_fitter = OHE.fit(data[['ProductId', 'UserId']])
 IDs = ID_fitter.transform(data[['ProductId', 'UserId']] )<count_missing_values>
print("End POS_CASH_balance................ " )
data['Text'].loc[data['Text'].isna() ] = ''
data['Summary'].loc[data['Summary'].isna() ] = ''<normalization>
print("Start credit_card_balance................ " )
data['Helpful'] = data['HelpfulnessNumerator']
data['Unhelpful'] = data['HelpfulnessDenominator'] - data['HelpfulnessNumerator']
scaler = StandardScaler()
scalerFitter = scaler.fit(data[['Helpful', 'Unhelpful', 'Time']])
 data[['Helpful', 'Unhelpful', 'Time']] = scalerFitter.transform(data[['Helpful', 'Unhelpful', 'Time']])
 data = data.drop(['HelpfulnessDenominator','HelpfulnessNumerator'], axis=1 )<feature_engineering>
cc = pd.read_csv('.. /input/credit_card_balance.csv', nrows = num_rows )
text_vectorizer = TfidfVectorizer(input='content', analyzer='word', stop_words='english')
 summary_vectorizer = TfidfVectorizer(input='content', analyzer='word', stop_words='english')
 text_fitter = text_vectorizer.fit(data['Text'])
 text_matrix = text_fitter.transform(data['Text'])
 summary_fitter = summary_vectorizer.fit(data['Summary'])
 summary_matrix = summary_fitter.transform(data['Summary'] )<compute_test_metric>
cc, cat_cols = one_hot_encoder(cc, nan_as_category= True)
 cc.drop(columns = ['SK_ID_PREV'], inplace = True)
 cc_agg = cc.groupby('SK_ID_CURR' ).agg(['min', 'max', 'mean', 'sum', 'var'])
 cc_agg.columns = pd.Index(['CC_' + e[0] + "_" + e[1].upper() for e in cc_agg.columns.tolist() ])
 cc_agg['CC_COUNT'] = cc.groupby('SK_ID_CURR' ).size()
del cc
gc.collect()
text_matrix, summary_matrix<concatenate>
print("End credit_card_balance................ " )
numerical = scipy.sparse.csr_matrix(data[['Helpful', 'Unhelpful', 'Time']].values )<concatenate>
with timer("Process bureau and bureau_balance"):
print("Bureau df shape:", bureau_agg.shape)
 df = df.join(bureau_agg, how='left',on='SK_ID_CURR')
 gc.collect()
with timer("Process previous_applications"):
print("Previous applications df shape:", prev_agg.shape)
 df = df.join(prev_agg, how='left', on='SK_ID_CURR')
 gc.collect()
with timer("Process POS-CASH balance"):
print("Pos-cash balance df shape:", pos_agg.shape)
 df = df.join(pos_agg, how='left', on='SK_ID_CURR')
 gc.collect()
with timer("Process installments payments"):
print("Installments payments df shape:", ins_agg.shape)
 df = df.join(ins_agg, how='left', on='SK_ID_CURR')
 gc.collect()
with timer("Process credit card balance"):
print("Credit card balance df shape:", cc_agg.shape)
 df = df.join(cc_agg, how='left', on='SK_ID_CURR')
 gc.collect()
del bureau_agg,prev_agg,pos_agg,ins_agg,cc_agg
gc.collect()
X = hstack([text_matrix, summary_matrix, numerical, IDs] )<normalization>
print("Done.;.............. ")
mask = data["Score"].isnull()
ind_test = mask.to_numpy().nonzero() [0]
ind_train =(~ mask ).to_numpy().nonzero() [0]
train_X = scipy.sparse.csr_matrix(X)[ind_train]
test_X = scipy.sparse.csr_matrix(X)[ind_test]<prepare_x_and_y>
train_df = df[df['TARGET'].notnull() ]
test_df = df[df['TARGET'].isnull() ]
train_Y = data['Score'].loc[data['Score'].isna() == False]
test_Y = data['Score'].loc[data['Score'].isna() ]
train_Y = train_Y.reset_index() ['Score']
test_Y = test_Y.reset_index() ['Score']<train_model>
train_df = train_df.drop(['index'],axis=1)
 test_df = test_df.drop(['index','TARGET'],axis=1)
 train_df = train_df.fillna(0)
 test_df = test_df.fillna(0 )
ros = RandomOverSampler(random_state=42)
 train_X, train_Y = ros.fit_resample(train_X, train_Y )<split>
label = u'TARGET'
a = list(train_df.columns)
 a.remove(label)
 labels = train_df[label]
data_only = train_df[list(a)]
col_name = data_only.columns
X_train, X_test, y_train, y_test = train_test_split(data_only, labels, test_size=0.1,random_state = 42 )
X_train, X_valid, y_train, y_valid = train_test_split(train_X, train_Y, test_size=0.2, random_state=42 )<find_best_model_class>
clf_lgbm = LGBMClassifier(
 nthread=4,
n_estimators=10000,
learning_rate=0.02,
num_leaves=34,
colsample_bytree=0.9497036,
subsample=0.8715623,
max_depth=8,
reg_alpha=0.041545473,
reg_lambda=0.0735294,
min_split_gain=0.0222415,
min_child_weight=39.3259775,
silent=-1,
verbose=-1,)
 clf_lgbm.fit(X_train,y_train )
def CVKFold(k, X, y, model):
np.random.seed(1)
 highest_accuracy = float('inf')
 best_model = None
kf = KFold(n_splits = k,shuffle =True)
 for train_index,test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
model.fit(X_train,y_train)
 y_test_pred = model.predict(X_test)
 test_accuracy = mean_squared_error(y_test_pred, y_test)
 print("The accuracy is " + str(test_accuracy))
if test_accuracy < highest_accuracy:
best_model = model
highest_accuracy = test_accuracy
print("The highest accuracy is " + str(highest_accuracy))
return best_model, highest_accuracy<choose_model_class>
pred = clf_lgbm.predict_proba(test_df)
 test_df['TARGET'] = pred[:, 0]
<save_to_csv><EOS> 
test_df[['SK_ID_CURR', 'TARGET']].to_csv('submission_clf_lgbm.csv', index= False )