Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<prepare_x_and_y>
warnings.filterwarnings('ignore' )
y=X_set.Revenue<correct_missing_values>
app_train = pd.read_csv('.. /input/application_train.csv')
 print('training data shape: ', app_train.shape)
 app_train.head()
X_set.dropna(axis=0, subset=['Revenue'], inplace=True )<drop_column>
app_test = pd.read_csv('.. /input/application_test.csv')
 print('testing data shape: ', app_test.shape)
 app_test.head()
X_set.drop(['Revenue'], axis=1, inplace=True )<split>
dataset.isnull().sum()
X_train, X_valid, y_train, y_valid = train_test_split(X_set, y,test_size=0.2,random_state=0 )<define_variables>
dataset.dtypes.value_counts()
s =(X_train.dtypes == 'object' )<define_variables>
dataset.select_dtypes('object' ).apply(pd.Series.nunique, axis = 0 )
object_cols = list(s[s].index )<define_variables>
le = LabelEncoder()
count = 0
le_vars = []
for col in dataset:
if dataset[col].dtype == 'object':
if len(list(dataset[col].unique())) == 2:
le.fit(dataset[col])
 dataset[col] = le.transform(dataset[col])
 count += 1
le_vars.append(col)
 print('%d columns were label encoded' % count)
 print(le_vars )
good_label_cols = [col for col in object_cols if
set(X_train[col])== set(X_valid[col])]<categorify>
dataset = pd.get_dummies(dataset)
 print('dataset data shape: ', dataset.shape )
label_encoder = LabelEncoder()<categorify>
dataset['CNT_CHILDREN_outlier'] = dataset['CNT_CHILDREN'] > 6
for i in dataset['CNT_CHILDREN']:
if i > 6:
dataset['CNT_CHILDREN'].replace({i: np.nan}, inplace = True )
for col in set(good_label_cols):
X_train[col] = label_encoder.fit_transform(X_train[col])
 X_valid[col] = label_encoder.transform(X_valid[col])
 X_test[col] = label_encoder.transform(X_test[col] )<choose_model_class>
dataset['DAYS_EMPLOYED_outlier'] = dataset['DAYS_EMPLOYED'] == 365243
dataset['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True )
myimputer = SimpleImputer()<create_dataframe>
dataset['OWN_CAR_AGE_outlier'] = dataset['OWN_CAR_AGE'] > 60
for i in dataset['OWN_CAR_AGE']:
if i > 60:
dataset['OWN_CAR_AGE'].replace({i: np.nan}, inplace = True )
imX_train = pd.DataFrame(myimputer.fit_transform(X_train))
imX_valid = pd.DataFrame(myimputer.transform(X_valid))<choose_model_class>
print(np.nanpercentile(dataset['CNT_FAM_MEMBERS'], 99))
model = RandomForestClassifier(n_estimators=100,random_state=2)
<prepare_output>
dataset['CNT_FAM_MEMBERS_outlier'] = dataset['CNT_FAM_MEMBERS'] > 5
for i in dataset['CNT_FAM_MEMBERS']:
if i > 5:
dataset['CNT_FAM_MEMBERS'].replace({i: np.nan}, inplace = True )
X_train = imX_train
X_valid = imX_valid<train_model>
for i in dataset['REGION_RATING_CLIENT_W_CITY']:
if i == -1:
dataset['REGION_RATING_CLIENT_W_CITY'].replace({i: 1}, inplace = True )
model.fit(X_train,y_train )<predict_on_test>
dataset['OBS_30_CNT_SOCIAL_CIRCLE_outlier'] = dataset['OBS_30_CNT_SOCIAL_CIRCLE'] > 17
for i in dataset['OBS_30_CNT_SOCIAL_CIRCLE']:
if i > 17:
dataset['OBS_30_CNT_SOCIAL_CIRCLE'].replace({i: np.nan}, inplace = True )
pred = model.predict(X_valid )<compute_test_metric>
dataset['DEF_30_CNT_SOCIAL_CIRCLE_outlier'] = dataset['DEF_30_CNT_SOCIAL_CIRCLE'] > 5
for i in dataset['DEF_30_CNT_SOCIAL_CIRCLE']:
if i > 5:
dataset['DEF_30_CNT_SOCIAL_CIRCLE'].replace({i: np.nan}, inplace = True )
error = accuracy_score(pred,y_valid )<compute_test_metric>
dataset['OBS_60_CNT_SOCIAL_CIRCLE_outlier'] = dataset['OBS_60_CNT_SOCIAL_CIRCLE'] > 16
for i in dataset['OBS_60_CNT_SOCIAL_CIRCLE']:
if i > 16:
dataset['OBS_60_CNT_SOCIAL_CIRCLE'].replace({i: np.nan}, inplace = True )
print(confusion_matrix(pred,y_valid))<count_missing_values>
dataset['DEF_60_CNT_SOCIAL_CIRCLE_outlier'] = dataset['DEF_60_CNT_SOCIAL_CIRCLE'] > 4
for i in dataset['DEF_60_CNT_SOCIAL_CIRCLE']:
if i > 4:
dataset['DEF_60_CNT_SOCIAL_CIRCLE'].replace({i: np.nan}, inplace = True )
column_with_missing = [col for col in X_test.columns
if X_test[col].isnull().any() ]<correct_missing_values>
dataset['AMT_REQ_CREDIT_BUREAU_HOUR_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_HOUR'] > 1
for i in dataset['AMT_REQ_CREDIT_BUREAU_HOUR']:
if i > 1:
dataset['AMT_REQ_CREDIT_BUREAU_HOUR'].replace({i: np.nan}, inplace = True )
X_test.dropna(axis=0, subset=['ID'], inplace=True )<drop_column>
dataset['AMT_REQ_CREDIT_BUREAU_DAY_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_DAY'] > 2
for i in dataset['AMT_REQ_CREDIT_BUREAU_DAY']:
if i > 2:
dataset['AMT_REQ_CREDIT_BUREAU_DAY'].replace({i: np.nan}, inplace = True )
X_test.drop(['ID'], axis=1, inplace=True )<predict_on_test>
dataset['AMT_REQ_CREDIT_BUREAU_WEEK_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_WEEK'] > 2
for i in dataset['AMT_REQ_CREDIT_BUREAU_WEEK']:
if i > 2:
dataset['AMT_REQ_CREDIT_BUREAU_WEEK'].replace({i: np.nan}, inplace = True )
preds_test = model.predict(X_test )<save_to_csv>
dataset['AMT_REQ_CREDIT_BUREAU_MON_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_MON'] > 5
for i in dataset['AMT_REQ_CREDIT_BUREAU_MON']:
if i > 5:
dataset['AMT_REQ_CREDIT_BUREAU_MON'].replace({i: np.nan}, inplace = True )
output = pd.DataFrame({'ID': X_test.index+1,'Revenue': preds_test})
 X_test.to_csv('new_submission.csv', index=None,header=True )<feature_engineering>
dataset['AMT_REQ_CREDIT_BUREAU_QRT_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_QRT'] > 5
for i in dataset['AMT_REQ_CREDIT_BUREAU_QRT']:
if i > 5:
dataset['AMT_REQ_CREDIT_BUREAU_QRT'].replace({i: np.nan}, inplace = True )
X_set['exit']= X_set['ExitRates'].apply(newly)
 X_test['exit']= X_set['ExitRates'].apply(newly)
<save_to_csv>
dataset['AMT_REQ_CREDIT_BUREAU_YEAR_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_YEAR'] > 10
for i in dataset['AMT_REQ_CREDIT_BUREAU_YEAR']:
if i > 10:
dataset['AMT_REQ_CREDIT_BUREAU_YEAR'].replace({i: np.nan}, inplace = True )
train_with_4 = pd.read_csv('.. /input/train.csv')
 train_with_4['target'] = train_with_4['target']/4
train_with_4.to_csv('train.csv', index = False )<load_from_csv>
dataset['EMPLOY_AGE'] = dataset['DAYS_EMPLOYED'] / dataset['DAYS_BIRTH']
dataset['INCOME_AGE'] = dataset['AMT_INCOME_TOTAL'] / dataset['DAYS_BIRTH']
dataset['CREDIT_AGE'] = dataset['AMT_CREDIT'] / dataset['DAYS_BIRTH']
dataset['CREDIT_INCOME'] = dataset['AMT_CREDIT'] / dataset['AMT_INCOME_TOTAL']
dataset['ANNUITY_INCOME'] = dataset['AMT_ANNUITY'] / dataset['AMT_INCOME_TOTAL']
dataset['ANNUITY_CREDIT'] = dataset['AMT_ANNUITY'] / dataset['AMT_CREDIT']
train_with_1 = pd.read_csv('train.csv')
 train_with_1.head()<categorify>
bureau = pd.read_csv('.. /input/bureau.csv')
 bureau.head()
def generate_bigrams(x):
n_grams = set(zip(*[x[i:] for i in range(2)]))
for n_gram in n_grams:
x.append(' '.join(n_gram))
return x<categorify>
dataset = dataset.merge(BUREAU_count, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_count'])
 print('%.4f' % corr )
generate_bigrams(['This', 'film', 'is', 'terrible'] )<prepare_x_and_y>
DAYS_CREDIT_max = bureau.groupby('SK_ID_CURR', as_index=False)['DAYS_CREDIT'].max().rename(columns = {'DAYS_CREDIT': 'bureau_DAYS_CREDIT_max'})
 DAYS_CREDIT_max.head()
SEED = 1234
torch.manual_seed(SEED)
 torch.backends.cudnn.deterministic = True
TEXT = data.Field(preprocessing = generate_bigrams)
 TARGET = data.LabelField(dtype = torch.float )<define_variables>
dataset = dataset.merge(DAYS_CREDIT_max, on = 'SK_ID_CURR', how = 'left' )
fields_train = [(None, None),(None, None),(None, None),('text', TEXT),('target', TARGET)]<load_from_csv>
CREDIT_DAY_OVERDUE_max = bureau.groupby('SK_ID_CURR', as_index=False)['CREDIT_DAY_OVERDUE'].max().rename(columns = {'CREDIT_DAY_OVERDUE': 'bureau_CREDIT_DAY_OVERDUE_max'} )
train_data = data.TabularDataset(path = 'train.csv',
format = 'csv',
fields = fields_train,
skip_header = True
)<split>
CREDIT_DAY_OVERDUE_max[CREDIT_DAY_OVERDUE_max['bureau_CREDIT_DAY_OVERDUE_max'] > 0].count()
train_data, valid_data = train_data.split(random_state = random.seed(SEED))<categorify>
CREDIT_DAY_OVERDUE_max['bureau_CREDIT_DAY_OVERDUE_max_flag'] = CREDIT_DAY_OVERDUE_max['bureau_CREDIT_DAY_OVERDUE_max'].where(CREDIT_DAY_OVERDUE_max['bureau_CREDIT_DAY_OVERDUE_max']==0,other=1 )
MAX_VOCAB_SIZE = 25_000
TEXT.build_vocab(train_data,
max_size = MAX_VOCAB_SIZE,
vectors = "glove.6B.100d",
unk_init = torch.Tensor.normal_)
 TARGET.build_vocab(train_data )<define_variables>
CREDIT_DAY_OVERDUE_max = CREDIT_DAY_OVERDUE_max.drop('bureau_CREDIT_DAY_OVERDUE_max', axis=1)
 dataset = dataset.merge(CREDIT_DAY_OVERDUE_max, on = 'SK_ID_CURR', how = 'left' )
BATCH_SIZE = 64
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 train_iterator = data.Iterator(dataset = train_data, batch_size = BATCH_SIZE,device = device, shuffle = None, train = True, sort_key = lambda x: len(x.text), sort_within_batch = False)
 valid_iterator = data.Iterator(dataset = valid_data, batch_size = BATCH_SIZE,device = device, shuffle = None, train = False, sort_key = lambda x: len(x.text), sort_within_batch = False )<categorify>
DAYS_CREDIT_ENDDATE_max['bureau_DAYS_CREDIT_ENDDATE_max_outlier'] = DAYS_CREDIT_ENDDATE_max['bureau_DAYS_CREDIT_ENDDATE_max'] < -10000
for i in DAYS_CREDIT_ENDDATE_max['bureau_DAYS_CREDIT_ENDDATE_max']:
if i < -10000:
DAYS_CREDIT_ENDDATE_max['bureau_DAYS_CREDIT_ENDDATE_max'].replace({i: np.nan}, inplace = True )
class FastText(nn.Module):
def __init__(self, vocab_size, embedding_dim, output_dim, pad_idx):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx=pad_idx)
 self.fc = nn.Linear(embedding_dim, output_dim)
 def forward(self, text):
embedded = self.embedding(text)
 embedded = embedded.permute(1, 0, 2)
 pooled = F.avg_pool2d(embedded,(embedded.shape[1], 1)).squeeze(1)
 return self.fc(pooled )<categorify>
dataset = dataset.merge(DAYS_CREDIT_ENDDATE_max, on = 'SK_ID_CURR', how = 'left' )
INPUT_DIM = len(TEXT.vocab)
 EMBEDDING_DIM = 100
OUTPUT_DIM = 1
PAD_IDX = TEXT.vocab.stoi[TEXT.pad_token]
model = FastText(INPUT_DIM, EMBEDDING_DIM, OUTPUT_DIM, PAD_IDX )<find_best_params>
DAYS_ENDDATE_FACT_mean['bureau_DAYS_ENDDATE_FACT_mean_outlier'] = DAYS_ENDDATE_FACT_mean['bureau_DAYS_ENDDATE_FACT_mean'] < -4000
for i in DAYS_ENDDATE_FACT_mean['bureau_DAYS_ENDDATE_FACT_mean']:
if i < -4000:
DAYS_ENDDATE_FACT_mean['bureau_DAYS_ENDDATE_FACT_mean'].replace({i: np.nan}, inplace = True )
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
 print(f'The model has {count_parameters(model):,} trainable parameters' )<load_pretrained>
dataset = dataset.merge(DAYS_ENDDATE_FACT_mean, on = 'SK_ID_CURR', how = 'left' )
pretrained_embeddings = TEXT.vocab.vectors
model.embedding.weight.data.copy_(pretrained_embeddings )<feature_engineering>
dataset = dataset.merge(AMT_CREDIT_MAX_OVERDUE_max, on = 'SK_ID_CURR', how = 'left')
 dataset = dataset.merge(AMT_CREDIT_MAX_OVERDUE_mean, on = 'SK_ID_CURR', how = 'left' )
UNK_IDX = TEXT.vocab.stoi[TEXT.unk_token]
model.embedding.weight.data[UNK_IDX] = torch.zeros(EMBEDDING_DIM)
 model.embedding.weight.data[PAD_IDX] = torch.zeros(EMBEDDING_DIM )<choose_model_class>
dataset = dataset.merge(CNT_CREDIT_PROLONG_sum, on = 'SK_ID_CURR', how = 'left' )
optimizer = optim.Adam(model.parameters() )<find_best_params>
dataset = dataset.merge(AMT_CREDIT_SUM_mean, on = 'SK_ID_CURR', how = 'left' )
criterion = nn.BCEWithLogitsLoss()
model = model.to(device)
 criterion = criterion.to(device )<compute_test_metric>
dataset = dataset.merge(AMT_CREDIT_SUM_DEBT_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_AMT_CREDIT_SUM_DEBT_mean'])
 print('%.4f' % corr )
def binary_accuracy(preds, y):

 rounded_preds = torch.round(torch.sigmoid(preds))
correct =(rounded_preds == y ).float()
acc = correct.sum() / len(correct)
 return acc<train_model>
dataset = dataset.merge(AMT_CREDIT_SUM_LIMIT_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_AMT_CREDIT_SUM_LIMIT_mean'])
 print('%.4f' % corr )
def train(model, iterator, optimizer, criterion):
epoch_loss = 0
epoch_acc = 0
model.train()
for batch in iterator:
optimizer.zero_grad()
predictions = model(batch.text ).squeeze(1)
 loss = criterion(predictions, batch.target)
 acc = binary_accuracy(predictions, batch.target)
 loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator )<compute_train_metric>
dataset = dataset.merge(AMT_CREDIT_SUM_OVERDUE_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_AMT_CREDIT_SUM_OVERDUE_mean'])
 print('%.4f' % corr )
def evaluate(model, iterator, criterion):
epoch_loss = 0
epoch_acc = 0
model.eval()
with torch.no_grad() :
for batch in iterator:
predictions = model(batch.text ).squeeze(1)
 loss = criterion(predictions, batch.target)
 acc = binary_accuracy(predictions, batch.target)
 epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator )<train_model>
dataset = dataset.merge(DAYS_CREDIT_UPDATE_max, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_DAYS_CREDIT_UPDATE_max'])
 print('%.4f' % corr )
def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
 elapsed_secs = int(elapsed_time -(elapsed_mins * 60))
return elapsed_mins, elapsed_secs<train_model>
dataset = dataset.merge(AMT_ANNUITY_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_AMT_ANNUITY_mean'])
 print('%.4f' % corr )
N_EPOCHS = 20
best_valid_loss = float('inf')
 for epoch in range(N_EPOCHS):
start_time = time.time()
train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
 valid_loss, valid_acc = evaluate(model, valid_iterator, criterion)
 end_time = time.time()
epoch_mins, epoch_secs = epoch_time(start_time, end_time)
 if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(model.state_dict() , 'tut3-model.pt')
 print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')
 print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
 print(f'\t Val.Loss: {valid_loss:.3f} | Val.Acc: {valid_acc*100:.2f}%' )<predict_on_test>
bureau_cats = pd.get_dummies(bureau.select_dtypes('object'))
bureau_cats['SK_ID_CURR'] = bureau['SK_ID_CURR']
bureau_cats.head()
def predict_sentiment(model, sentence):
model.eval()
tokenized = generate_bigrams(sentence.split())
 indexed = [TEXT.vocab.stoi[t] for t in tokenized]
tensor = torch.LongTensor(indexed ).to(device)
 tensor = tensor.unsqueeze(1)
 prediction = torch.sigmoid(model(tensor))
return prediction.item()<save_to_csv>
bureau_cats_grouped = bureau_cats.groupby('SK_ID_CURR' ).agg('sum')
 bureau_cats_grouped.head()
preds = []
test_data = pd.read_csv('.. /input/test.csv')
 for i in range(len(test_data)) :
preds.append(( int(predict_sentiment(model, test_data['text'][i])>0.5)) *4)
 ids = test_data['Id']
dict = {'Id': ids, 'target': preds}
df = pd.DataFrame(dict)
 df.to_csv('final_output.csv', index=False )<load_from_csv>
dataset = dataset.merge(bureau_cats_grouped, on = 'SK_ID_CURR', right_index = True, how = 'left')
 dataset.head()
test_data = pd.read_csv('final_output.csv')
 test_data.head()<categorify>
bureau_balance = pd.read_csv('.. /input/bureau_balance.csv')
 bureau_balance.head()
class VowelConsonantDataset(Dataset):
def __init__(self, file_path,train=True,transform=None):
self.transform = transform
self.file_path=file_path
self.train=train
self.file_names=[file for _,_,files in os.walk(self.file_path)for file in files]
self.len = len(self.file_names)
 if self.train:
self.classes_mapping=self.get_classes()
def __len__(self):
return len(self.file_names)
 def __getitem__(self, index):
file_name=self.file_names[index]
image_data=self.pil_loader(self.file_path+"/"+file_name)
 if self.transform:
image_data = self.transform(image_data)
 if self.train:
file_name_splitted=file_name.split("_")
 Y1 = self.classes_mapping[file_name_splitted[0]]
Y2 = self.classes_mapping[file_name_splitted[1]]
z1,z2=torch.zeros(10),torch.zeros(10)
 z1[Y1-10],z2[Y2]=1,1
label=torch.stack([z1,z2])
 return image_data, label
else:
return image_data, file_name
def pil_loader(self,path):
with open(path, 'rb')as f:
img = Image.open(f)
 return img.convert('RGB')
 def get_classes(self):
classes=[]
for name in self.file_names:
name_splitted=name.split("_")
 classes.extend([name_splitted[0],name_splitted[1]])
 classes=list(set(classes))
classes_mapping={}
for i,cl in enumerate(sorted(classes)) :
classes_mapping[cl]=i
return classes_mapping 
<set_options>
MONTHS_BAL = MONTHS_BALANCE_mean.merge(MONTHS_BALANCE_count, on = 'SK_ID_BUREAU', right_index = True, how = 'inner')
 MONTHS_BAL.head()
train_on_gpu = torch.cuda.is_available()<categorify>
bureau_bal_cats = pd.get_dummies(bureau_balance.select_dtypes('object'))
bureau_bal_cats['SK_ID_BUREAU'] = bureau_balance['SK_ID_BUREAU']
bureau_bal_cats.head()
transform_train = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
] )<load_pretrained>
bureau_bal_cats_grouped = bureau_bal_cats.groupby('SK_ID_BUREAU' ).agg('sum')
 bureau_bal_cats_grouped.head()
full_data = VowelConsonantDataset(".. /input/train/train",train=True,transform=transform_train)
 train_size = int(0.95 * len(full_data))
test_size = len(full_data)- train_size
train_data, validation_data = random_split(full_data, [train_size, test_size])
 train_loader = torch.utils.data.DataLoader(train_data, batch_size=20, shuffle=True)
 validation_loader = torch.utils.data.DataLoader(validation_data, batch_size=20, shuffle=True )<create_dataframe>
bureau_bal_merged = MONTHS_BAL.merge(bureau_bal_cats_grouped, right_index = True, left_on = 'SK_ID_BUREAU', how = 'outer')
 bureau_bal_merged = bureau_bal_merged.merge(bureau[['SK_ID_BUREAU', 'SK_ID_CURR']], on = 'SK_ID_BUREAU', how = 'left')
 bureau_bal_merged.head()
test_data = VowelConsonantDataset(".. /input/test/test",train=False,transform=transform_train)
 test_loader = torch.utils.data.DataLoader(test_data, batch_size=20,shuffle=False )<set_options>
bureau_bal_MONTHS_BALANCE_mean_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['bureau_bal_MONTHS_BALANCE_mean'].mean().rename(columns = {'bureau_bal_MONTHS_BALANCE_mean': 'bureau_bal_MONTHS_BALANCE_mean_mean'})
 dataset = dataset.merge(bureau_bal_MONTHS_BALANCE_mean_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_MONTHS_BALANCE_mean_mean'])
 print('%.4f' % corr )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device )<choose_model_class>
bureau_bal_MONTHS_BALANCE_count_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['bureau_bal_MONTHS_BALANCE_count'].mean().rename(columns = {'bureau_bal_MONTHS_BALANCE_count': 'bureau_bal_MONTHS_BALANCE_count_mean'})
 dataset = dataset.merge(bureau_bal_MONTHS_BALANCE_count_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_MONTHS_BALANCE_count_mean'])
 print('%.4f' % corr )
class MyModel(nn.Module):
def __init__(self, num_classes1, num_classes2):
super(MyModel, self ).__init__()
self.model_resnet = models.resnet50(pretrained=True)
 num_ftrs = self.model_resnet.fc.in_features
self.model_resnet.fc = nn.Sequential()
self.fc1 = nn.Sequential(
 nn.BatchNorm1d(num_ftrs),
nn.Dropout(0.3),
nn.Linear(in_features=num_ftrs, out_features=1000,bias=True),
nn.ReLU() ,
nn.BatchNorm1d(1000, eps=1e-05, momentum=0.3),
nn.Dropout(0.3),
nn.Linear(in_features=1000, out_features=10,bias=True))
self.fc2 = nn.Sequential(
 nn.BatchNorm1d(num_ftrs),
nn.Dropout(0.3),
nn.Linear(in_features=num_ftrs,out_features=1000,bias=True),
nn.ReLU() ,
nn.BatchNorm1d(1000, eps=1e-05, momentum=0.3),
nn.Dropout(0.3),
nn.Linear(in_features=1000, out_features=10,bias=True))
def forward(self, x):
x = self.model_resnet(x)
 out1 = self.fc1(x)
 out2 = self.fc2(x)
 return out1, out2<choose_model_class>
bureau_bal_STATUS_0_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_0'].mean().rename(columns = {'STATUS_0': 'bureau_bal_STATUS_0_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_0_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_0_mean'])
 print('%.4f' % corr )
my_model = MyModel(10,10 )<train_model>
bureau_bal_STATUS_1_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_1'].mean().rename(columns = {'STATUS_1': 'bureau_bal_STATUS_1_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_1_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_1_mean'])
 print('%.4f' % corr )
my_model = my_model.to(device )<categorify>
bureau_bal_STATUS_2_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_2'].mean().rename(columns = {'STATUS_2': 'bureau_bal_STATUS_2_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_2_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_2_mean'])
 print('%.4f' % corr )
def evaluation(dataloader,model):
total,correct=0,0
for data in dataloader:
inputs,labels=data
inputs,labels=inputs.to(device),labels.to(device)
 out1,out2=my_model(inputs)
 _,pred1=torch.max(out1.data,1)
 _,pred2=torch.max(out2.data,1)
 _,labels1=torch.max(labels[:,0,:].data,1)
 _,labels2=torch.max(labels[:,1,:].data,1)
 total+=labels.size(0)
 fin1=(pred1==labels1)
 fin2=(pred2==labels2)
 correct+=(fin1==fin2 ).sum().item()
return 100*correct/total<choose_model_class>
bureau_bal_STATUS_3_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_3'].mean().rename(columns = {'STATUS_3': 'bureau_bal_STATUS_3_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_3_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_3_mean'])
 print('%.4f' % corr )
loss_fn = nn.CrossEntropyLoss()
opt = optim.SGD(my_model.parameters() ,lr=0.01,momentum=0.9 )<train_model>
bureau_bal_STATUS_4_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_4'].mean().rename(columns = {'STATUS_4': 'bureau_bal_STATUS_4_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_4_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_4_mean'])
 print('%.4f' % corr )
loss_epoch_arr = []
loss_arr = []
max_epochs = 6
min_loss = 1000
batch_size = 32
n_iters = np.ceil(9000/batch_size)
 for epoch in range(max_epochs):
for i, data in enumerate(train_loader, 0):
my_model.train()
images, labels = data
images = images.to(device)
 targetnp=labels[:,0,:].cpu().numpy()
targetnp1 = labels[:,1,:].cpu().numpy()
with torch.no_grad() :
new_targets1 = np.argmax(targetnp,axis=1)
 new_targets2 = np.argmax(targetnp1,axis=1)
 new_targets1=torch.LongTensor(new_targets1)
 new_targets2=torch.LongTensor(new_targets2)
 new_targets1 = new_targets1.to(device)
 new_targets2 = new_targets2.to(device)
 opt.zero_grad()
out = my_model.forward(images)
 loss_fc1 = loss_fn(out[0], new_targets1)
 loss_fc2 = loss_fn(out[1],new_targets2)
 loss = torch.add(loss_fc1,loss_fc2)
 loss.backward()
opt.step()
if min_loss > loss.item() :
min_loss = loss.item()
best_model = copy.deepcopy(my_model.state_dict())
 print('Min loss %0.2f' % min_loss)
 if i % 100 == 0:
print('Iteration: %d/%d, Loss: %0.2f' %(i, n_iters, loss.item()))
del images, labels, out
torch.cuda.empty_cache()
loss_arr.append(loss.item())
 print("Epoch number :",epoch)
 print("Train Accuracy :",evaluation(train_loader,my_model))
print("Test Accuracy :" ,evaluation(validation_loader,my_model))
loss_epoch_arr.append(loss.item())
 plt.plot(loss_arr)
 plt.show()<compute_test_metric>
bureau_bal_STATUS_5_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_5'].mean().rename(columns = {'STATUS_5': 'bureau_bal_STATUS_5_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_5_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_5_mean'])
 print('%.4f' % corr )
print(evaluation(validation_loader,my_model))<find_best_params>
bureau_bal_STATUS_C_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_C'].mean().rename(columns = {'STATUS_C': 'bureau_bal_STATUS_C_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_C_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_C_mean'])
 print('%.4f' % corr )
my_model.eval()
plist=[]
fn_list=[]
for inputs_test, fn in test_loader:
inputs_test=inputs_test.to(device)
 out1,out2=my_model.forward(inputs_test)
 _,pred1=torch.max(out1,1)
 pred1=pred1.tolist()
_,pred2=torch.max(out2,1)
 pred2=pred2.tolist()
for x,y,z in zip(pred1,pred2,fn):
p="V"+str(x)+"_"+"C"+str(y)
 plist.append(p)
 fn_list.append(z )<save_to_csv>
bureau_bal_STATUS_X_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_X'].mean().rename(columns = {'STATUS_X': 'bureau_bal_STATUS_X_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_X_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_X_mean'])
 print('%.4f' % corr )
submission = pd.DataFrame({"ImageId":fn_list, "Class":plist})
 submission.to_csv('submission.csv', index=False )<load_from_csv>
gc.enable()
del bureau, BUREAU_count, DAYS_CREDIT_max, CREDIT_DAY_OVERDUE_max, DAYS_CREDIT_ENDDATE_max, DAYS_ENDDATE_FACT_mean, AMT_CREDIT_MAX_OVERDUE_max,
AMT_CREDIT_MAX_OVERDUE_mean, CNT_CREDIT_PROLONG_sum, AMT_CREDIT_SUM_mean, AMT_CREDIT_SUM_DEBT_mean, AMT_CREDIT_SUM_LIMIT_mean, AMT_CREDIT_SUM_OVERDUE_mean,
DAYS_CREDIT_UPDATE_max, AMT_ANNUITY_mean, bureau_cats, bureau_cats_grouped, bureau_balance, MONTHS_BALANCE_count, MONTHS_BALANCE_mean, MONTHS_BAL, bureau_bal_cats,
bureau_bal_cats_grouped, bureau_bal_merged, bureau_bal_MONTHS_BALANCE_mean_mean, bureau_bal_MONTHS_BALANCE_count_mean, bureau_bal_STATUS_0_mean,
bureau_bal_STATUS_1_mean, bureau_bal_STATUS_2_mean, bureau_bal_STATUS_3_mean, bureau_bal_STATUS_4_mean, bureau_bal_STATUS_5_mean, bureau_bal_STATUS_C_mean,
bureau_bal_STATUS_X_mean
gc.collect()
%matplotlib inline
warnings.filterwarnings("ignore", category=DeprecationWarning)
 df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 df = df_train.append(df_test , ignore_index = True)
 df_train.shape, df_test.shape, df_train.columns.values<count_missing_values>
credit = pd.read_csv('.. /input/credit_card_balance.csv')
 credit.head()
df['Pclass'].isnull().sum(axis=0 )<groupby>
credit_stats_by_prev = credit[['SK_ID_PREV', 'SK_ID_CURR']]
df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean()<count_values>
credit_MONTHS_BALANCE_count = credit.groupby('SK_ID_PREV', as_index=False)['MONTHS_BALANCE'].count().rename(columns = {'MONTHS_BALANCE': 'credit_MONTHS_BALANCE_count'})
 credit_MONTHS_BALANCE_mean = credit.groupby('SK_ID_PREV', as_index=False)['MONTHS_BALANCE'].mean().rename(columns = {'MONTHS_BALANCE': 'credit_MONTHS_BALANCE_mean'} )
df['Title'] = df.Name.map(lambda x: x.split(',')[1].split('.')[0].strip())
 df['Title'].value_counts()<feature_engineering>
credit_stats_by_prev = credit_stats_by_prev.merge(credit_MONTHS_BALANCE_count, on = 'SK_ID_PREV', how = 'left')
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_MONTHS_BALANCE_mean, on = 'SK_ID_PREV', how = 'left')
 credit_stats_by_prev.head()
df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace(['Mme','Lady','Ms'], 'Mrs')
 df.Title.loc[(df.Title != 'Master')&(df.Title != 'Mr')&(df.Title != 'Miss')
 &(df.Title != 'Mrs')] = 'Others'
df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<count_values>
gc.enable()
del credit_MONTHS_BALANCE_count, credit_MONTHS_BALANCE_mean
gc.collect()
df['Title'].value_counts()<categorify>
credit_AMT_BALANCE_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_BALANCE'].mean().rename(columns = {'AMT_BALANCE': 'credit_AMT_BALANCE_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_BALANCE_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_BALANCE_mean
gc.collect()
df = pd.concat([df, pd.get_dummies(df['Title'])], axis=1 ).drop(labels=['Name'], axis=1 )<count_missing_values>
credit_AMT_CREDIT_LIMIT_ACTUAL_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_CREDIT_LIMIT_ACTUAL'].mean().rename(columns = {'AMT_CREDIT_LIMIT_ACTUAL': 'credit_AMT_CREDIT_LIMIT_ACTUAL_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_CREDIT_LIMIT_ACTUAL_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_CREDIT_LIMIT_ACTUAL_mean
gc.collect()
df.Sex.isnull().sum(axis=0 )<groupby>
credit_AMT_DRAWINGS_ATM_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_DRAWINGS_ATM_CURRENT'].mean().rename(columns = {'AMT_DRAWINGS_ATM_CURRENT': 'credit_AMT_DRAWINGS_ATM_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_DRAWINGS_ATM_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_ATM_CURRENT_mean
gc.collect()
df[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean()<categorify>
credit_AMT_DRAWINGS_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_DRAWINGS_CURRENT'].mean().rename(columns = {'AMT_DRAWINGS_CURRENT': 'credit_AMT_DRAWINGS_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_DRAWINGS_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_CURRENT_mean
gc.collect()
df.Sex = df.Sex.map({'male':0, 'female':1} )<count_missing_values>
credit_AMT_DRAWINGS_OTHER_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_DRAWINGS_OTHER_CURRENT'].mean().rename(columns = {'AMT_DRAWINGS_OTHER_CURRENT': 'credit_AMT_DRAWINGS_OTHER_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_DRAWINGS_OTHER_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_OTHER_CURRENT_mean
gc.collect()
df.Age.isnull().sum(axis=0 )<count_missing_values>
credit_AMT_DRAWINGS_POS_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_DRAWINGS_POS_CURRENT'].mean().rename(columns = {'AMT_DRAWINGS_POS_CURRENT': 'credit_AMT_DRAWINGS_POS_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_DRAWINGS_POS_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_POS_CURRENT_mean
gc.collect()
df.SibSp.isnull().sum(axis=0), df.Parch.isnull().sum(axis=0 )<feature_engineering>
credit_AMT_INST_MIN_REGULARITY_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_INST_MIN_REGULARITY'].mean().rename(columns = {'AMT_INST_MIN_REGULARITY': 'credit_AMT_INST_MIN_REGULARITY_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_INST_MIN_REGULARITY_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_INST_MIN_REGULARITY_mean
gc.collect()
df['Family'] = df['SibSp'] + df['Parch'] + 1
df[['Family', 'Survived']].groupby(['Family'], as_index=False ).mean()<count_values>
credit_AMT_PAYMENT_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_PAYMENT_CURRENT'].mean().rename(columns = {'AMT_PAYMENT_CURRENT': 'credit_AMT_PAYMENT_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_PAYMENT_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_PAYMENT_CURRENT_mean
gc.collect()
df['Family'].value_counts()<groupby>
credit_AMT_PAYMENT_TOTAL_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_PAYMENT_TOTAL_CURRENT'].mean().rename(columns = {'AMT_PAYMENT_TOTAL_CURRENT': 'credit_AMT_PAYMENT_TOTAL_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_PAYMENT_TOTAL_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_PAYMENT_TOTAL_CURRENT_mean
gc.collect()
df.Family = df.Family.map(lambda x: 0 if x > 4 else x)
 df[['Family', 'Survived']].groupby(['Family'], as_index=False ).mean()<count_values>
credit_AMT_RECEIVABLE_PRINCIPAL_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_RECEIVABLE_PRINCIPAL'].mean().rename(columns = {'AMT_RECEIVABLE_PRINCIPAL': 'credit_AMT_RECEIVABLE_PRINCIPAL_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_RECEIVABLE_PRINCIPAL_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_RECEIVABLE_PRINCIPAL_mean
gc.collect()
df['Family'].value_counts()<count_missing_values>
credit_AMT_RECIVABLE_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_RECIVABLE'].mean().rename(columns = {'AMT_RECIVABLE': 'credit_AMT_RECIVABLE_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_RECIVABLE_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_RECIVABLE_mean
gc.collect()
df.Ticket.isnull().sum(axis=0 )<groupby>
credit_AMT_TOTAL_RECEIVABLE_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_TOTAL_RECEIVABLE'].mean().rename(columns = {'AMT_TOTAL_RECEIVABLE': 'credit_AMT_TOTAL_RECEIVABLE_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_TOTAL_RECEIVABLE_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_TOTAL_RECEIVABLE_mean
gc.collect()
df.Ticket = df.Ticket.map(lambda x: x[0])
 df[['Ticket', 'Survived']].groupby(['Ticket'], as_index=False ).mean()<count_values>
credit_CNT_DRAWINGS_ATM_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['CNT_DRAWINGS_ATM_CURRENT'].mean().rename(columns = {'CNT_DRAWINGS_ATM_CURRENT': 'credit_CNT_DRAWINGS_ATM_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_CNT_DRAWINGS_ATM_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_CNT_DRAWINGS_ATM_CURRENT_mean
gc.collect()
df['Ticket'].value_counts()<groupby>
credit_CNT_DRAWINGS_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['CNT_DRAWINGS_CURRENT'].mean().rename(columns = {'CNT_DRAWINGS_CURRENT': 'credit_CNT_DRAWINGS_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_CNT_DRAWINGS_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_CNT_DRAWINGS_CURRENT_mean
gc.collect()
df[['Ticket', 'Fare']].groupby(['Ticket'], as_index=False ).mean()<groupby>
credit_CNT_DRAWINGS_OTHER_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['CNT_DRAWINGS_OTHER_CURRENT'].mean().rename(columns = {'CNT_DRAWINGS_OTHER_CURRENT': 'credit_CNT_DRAWINGS_OTHER_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_CNT_DRAWINGS_OTHER_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_CNT_DRAWINGS_OTHER_CURRENT_mean
gc.collect()
df[['Ticket', 'Pclass']].groupby(['Ticket'], as_index=False ).mean()<count_missing_values>
credit_CNT_DRAWINGS_POS_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['CNT_DRAWINGS_POS_CURRENT'].mean().rename(columns = {'CNT_DRAWINGS_POS_CURRENT': 'credit_CNT_DRAWINGS_POS_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_CNT_DRAWINGS_POS_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_CNT_DRAWINGS_POS_CURRENT_mean
gc.collect()
df.Fare.isnull().sum(axis=0)
<filter>
credit_CNT_INSTALMENT_MATURE_CUM_mean = credit.groupby('SK_ID_PREV', as_index=False)['CNT_INSTALMENT_MATURE_CUM'].mean().rename(columns = {'CNT_INSTALMENT_MATURE_CUM': 'credit_CNT_INSTALMENT_MATURE_CUM_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_CNT_INSTALMENT_MATURE_CUM_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_CNT_INSTALMENT_MATURE_CUM_mean
gc.collect()
df.Ticket[df.Fare.isnull() ]<filter>
credit_SK_DPD_mean = credit.groupby('SK_ID_PREV', as_index=False)['SK_DPD'].mean().rename(columns = {'SK_DPD': 'credit_SK_DPD_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_SK_DPD_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_SK_DPD_mean
gc.collect()
df.Pclass[df.Fare.isnull() ]<filter>
credit_SK_DPD_DEF_mean = credit.groupby('SK_ID_PREV', as_index=False)['SK_DPD_DEF'].mean().rename(columns = {'SK_DPD_DEF': 'credit_SK_DPD_DEF_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_SK_DPD_DEF_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_SK_DPD_DEF_mean
gc.collect()
df.Cabin[df.Fare.isnull() ]<filter>
credit_cats = pd.get_dummies(credit.select_dtypes('object'))
credit_cats['SK_ID_PREV'] = credit['SK_ID_PREV']
credit_cats.head()
df.Embarked[df.Fare.isnull() ]<groupby>
credit_cats_grouped = credit_cats.groupby('SK_ID_PREV' ).agg('sum')
 credit_cats_grouped.head()
df[['Pclass', 'Fare']].groupby(['Pclass'] ).mean()<groupby>
credit_stats_by_prev = credit_stats_by_prev.merge(credit_cats_grouped, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_cats_grouped, credit_cats
gc.collect()
df[['Ticket', 'Fare']].groupby(['Ticket'] ).mean()<groupby>
credit_MONTHS_BALANCE_count_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_MONTHS_BALANCE_count'].mean().rename(columns = {'credit_MONTHS_BALANCE_count': 'credit_MONTHS_BALANCE_count_mean'})
 dataset = dataset.merge(credit_MONTHS_BALANCE_count_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_MONTHS_BALANCE_count_mean
gc.collect()
df[['Embarked', 'Fare']].groupby(['Embarked'] ).mean()<groupby>
credit_MONTHS_BALANCE_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_MONTHS_BALANCE_mean'].mean().rename(columns = {'credit_MONTHS_BALANCE_mean': 'credit_MONTHS_BALANCE_mean_mean'})
 dataset = dataset.merge(credit_MONTHS_BALANCE_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_MONTHS_BALANCE_mean_mean
gc.collect()
guess_Fare = df.Fare.loc[(df.Ticket == '3')&(df.Pclass == 3)&(df.Embarked == 'S')].median()
df.Fare.fillna(guess_Fare , inplace=True)
 df[['Fare', 'Survived']].groupby(['Survived'],as_index=False ).mean()<data_type_conversions>
credit_AMT_BALANCE_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_BALANCE_mean'].mean().rename(columns = {'credit_AMT_BALANCE_mean': 'credit_AMT_BALANCE_mean_mean'})
 dataset = dataset.merge(credit_AMT_BALANCE_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_BALANCE_mean_mean
gc.collect()
df['Fare-bin'] = pd.qcut(df.Fare,5,labels=[1,2,3,4,5] ).astype(int)
 df[['Fare-bin', 'Survived']].groupby(['Fare-bin'], as_index=False ).mean()<count_missing_values>
credit_AMT_CREDIT_LIMIT_ACTUAL_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_CREDIT_LIMIT_ACTUAL_mean'].mean().rename(columns = {'credit_AMT_CREDIT_LIMIT_ACTUAL_mean': 'credit_AMT_CREDIT_LIMIT_ACTUAL_mean_mean'})
 dataset = dataset.merge(credit_AMT_CREDIT_LIMIT_ACTUAL_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_CREDIT_LIMIT_ACTUAL_mean_mean
gc.collect()
df.Cabin.isnull().sum(axis=0 )<drop_column>
credit_AMT_DRAWINGS_ATM_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_DRAWINGS_ATM_CURRENT_mean'].mean().rename(columns = {'credit_AMT_DRAWINGS_ATM_CURRENT_mean': 'credit_AMT_DRAWINGS_ATM_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_AMT_DRAWINGS_ATM_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_ATM_CURRENT_mean_mean
gc.collect()
df = df.drop(labels=['Cabin'], axis=1 )<count_missing_values>
credit_AMT_DRAWINGS_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_DRAWINGS_CURRENT_mean'].mean().rename(columns = {'credit_AMT_DRAWINGS_CURRENT_mean': 'credit_AMT_DRAWINGS_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_AMT_DRAWINGS_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_CURRENT_mean_mean
gc.collect()
df.Embarked.isnull().sum(axis=0 )<data_type_conversions>
credit_AMT_DRAWINGS_OTHER_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_DRAWINGS_OTHER_CURRENT_mean'].mean().rename(columns = {'credit_AMT_DRAWINGS_OTHER_CURRENT_mean': 'credit_AMT_DRAWINGS_OTHER_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_AMT_DRAWINGS_OTHER_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_OTHER_CURRENT_mean_mean
gc.collect()
df.Embarked.fillna('S' , inplace=True )<groupby>
credit_AMT_DRAWINGS_POS_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_DRAWINGS_POS_CURRENT_mean'].mean().rename(columns = {'credit_AMT_DRAWINGS_POS_CURRENT_mean': 'credit_AMT_DRAWINGS_POS_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_AMT_DRAWINGS_POS_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_POS_CURRENT_mean_mean
gc.collect()
df[['Embarked', 'Survived','Pclass','Fare', 'Age', 'Sex']].groupby(['Embarked'], as_index=False ).mean()<drop_column>
credit_AMT_INST_MIN_REGULARITY_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_INST_MIN_REGULARITY_mean'].mean().rename(columns = {'credit_AMT_INST_MIN_REGULARITY_mean': 'credit_AMT_INST_MIN_REGULARITY_mean_mean'})
 dataset = dataset.merge(credit_AMT_INST_MIN_REGULARITY_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_INST_MIN_REGULARITY_mean_mean
gc.collect()
df = df.drop(labels='Embarked', axis=1 )<groupby>
credit_AMT_PAYMENT_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_PAYMENT_CURRENT_mean'].mean().rename(columns = {'credit_AMT_PAYMENT_CURRENT_mean': 'credit_AMT_PAYMENT_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_AMT_PAYMENT_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_PAYMENT_CURRENT_mean_mean
gc.collect()
df[['Title', 'Age']].groupby(['Title'] ).mean()<groupby>
credit_AMT_PAYMENT_TOTAL_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_PAYMENT_TOTAL_CURRENT_mean'].mean().rename(columns = {'credit_AMT_PAYMENT_TOTAL_CURRENT_mean': 'credit_AMT_PAYMENT_TOTAL_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_AMT_PAYMENT_TOTAL_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_PAYMENT_TOTAL_CURRENT_mean_mean
gc.collect()
df[['Fare-bin', 'Age']].groupby(['Fare-bin'] ).mean()<groupby>
credit_AMT_RECEIVABLE_PRINCIPAL_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_RECEIVABLE_PRINCIPAL_mean'].mean().rename(columns = {'credit_AMT_RECEIVABLE_PRINCIPAL_mean': 'credit_AMT_RECEIVABLE_PRINCIPAL_mean_mean'})
 dataset = dataset.merge(credit_AMT_RECEIVABLE_PRINCIPAL_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_RECEIVABLE_PRINCIPAL_mean_mean
gc.collect()
df[['SibSp', 'Age']].groupby(['SibSp'] ).mean()<groupby>
credit_AMT_RECIVABLE_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_RECIVABLE_mean'].mean().rename(columns = {'credit_AMT_RECIVABLE_mean': 'credit_AMT_RECIVABLE_mean_mean'})
 dataset = dataset.merge(credit_AMT_RECIVABLE_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_RECIVABLE_mean_mean
gc.collect()
df[['Parch', 'Age']].groupby(['Parch'] ).mean()<prepare_x_and_y>
credit_AMT_TOTAL_RECEIVABLE_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_AMT_TOTAL_RECEIVABLE_mean'].mean().rename(columns = {'credit_AMT_TOTAL_RECEIVABLE_mean': 'credit_AMT_TOTAL_RECEIVABLE_mean_mean'})
 dataset = dataset.merge(credit_AMT_TOTAL_RECEIVABLE_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_AMT_TOTAL_RECEIVABLE_mean_mean
gc.collect()
df_sub = df[['Age','Master','Miss','Mr','Mrs','Others','Fare-bin','SibSp']]
X_train = df_sub.dropna().drop('Age', axis=1)
 y_train = df['Age'].dropna()
X_test = df_sub.loc[np.isnan(df.Age)].drop('Age', axis=1)
 pca = PCA(n_components=2)
 X_train = pca.fit_transform(X_train)
 X_test = pca.transform(X_test)
 regressor = RandomForestRegressor(n_estimators = 300)
 regressor.fit(X_train, y_train)
 y_pred = np.round(regressor.predict(X_test),1)
 df.Age.loc[df.Age.isnull() ] = y_pred
df.Age.isnull().sum(axis=0 )<categorify>
credit_CNT_DRAWINGS_ATM_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_CNT_DRAWINGS_ATM_CURRENT_mean'].mean().rename(columns = {'credit_CNT_DRAWINGS_ATM_CURRENT_mean': 'credit_CNT_DRAWINGS_ATM_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_CNT_DRAWINGS_ATM_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_CNT_DRAWINGS_ATM_CURRENT_mean_mean
gc.collect()
bins = [ 0, 4, 12, 18, 30, 50, 65, 100]
age_index =(1,2,3,4,5,6,7)
 df['Age-bin'] = pd.cut(df.Age, bins, labels=age_index ).astype(int)
 df[['Age-bin', 'Survived']].groupby(['Age-bin'],as_index=False ).mean()<groupby>
credit_CNT_DRAWINGS_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_CNT_DRAWINGS_CURRENT_mean'].mean().rename(columns = {'credit_CNT_DRAWINGS_CURRENT_mean': 'credit_CNT_DRAWINGS_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_CNT_DRAWINGS_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_CNT_DRAWINGS_CURRENT_mean_mean
gc.collect()
df[['Ticket', 'Survived']].groupby(['Ticket'], as_index=False ).mean()<count_values>
credit_CNT_DRAWINGS_OTHER_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_CNT_DRAWINGS_OTHER_CURRENT_mean'].mean().rename(columns = {'credit_CNT_DRAWINGS_OTHER_CURRENT_mean': 'credit_CNT_DRAWINGS_OTHER_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_CNT_DRAWINGS_OTHER_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_CNT_DRAWINGS_OTHER_CURRENT_mean_mean
gc.collect()
df['Ticket'].value_counts()<groupby>
credit_CNT_DRAWINGS_POS_CURRENT_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_CNT_DRAWINGS_POS_CURRENT_mean'].mean().rename(columns = {'credit_CNT_DRAWINGS_POS_CURRENT_mean': 'credit_CNT_DRAWINGS_POS_CURRENT_mean_mean'})
 dataset = dataset.merge(credit_CNT_DRAWINGS_POS_CURRENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_CNT_DRAWINGS_POS_CURRENT_mean_mean
gc.collect()
df['Ticket'] = df['Ticket'].replace(['A','W','F','L','5','6','7','8','9'], '4')
 df[['Ticket', 'Survived']].groupby(['Ticket'], as_index=False ).mean()<categorify>
credit_CNT_INSTALMENT_MATURE_CUM_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_CNT_INSTALMENT_MATURE_CUM_mean'].mean().rename(columns = {'credit_CNT_INSTALMENT_MATURE_CUM_mean': 'credit_CNT_INSTALMENT_MATURE_CUM_mean_mean'})
 dataset = dataset.merge(credit_CNT_INSTALMENT_MATURE_CUM_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_CNT_INSTALMENT_MATURE_CUM_mean_mean
gc.collect()
df = pd.get_dummies(df,columns=['Ticket'] )<prepare_x_and_y>
credit_SK_DPD_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_SK_DPD_mean'].mean().rename(columns = {'credit_SK_DPD_mean': 'credit_SK_DPD_mean_mean'})
 dataset = dataset.merge(credit_SK_DPD_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_SK_DPD_mean_mean
gc.collect()
df = df.drop(labels=['SibSp','Parch','Age','Fare','Title'], axis=1)
 y_train = df[0:891]['Survived'].values
X_train = df[0:891].drop(['Survived','PassengerId'], axis=1 ).values
X_test = df[891:].drop(['Survived','PassengerId'], axis=1 ).values<train_model>
credit_SK_DPD_DEF_mean_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['credit_SK_DPD_DEF_mean'].mean().rename(columns = {'credit_SK_DPD_DEF_mean': 'credit_SK_DPD_DEF_mean_mean'})
 dataset = dataset.merge(credit_SK_DPD_DEF_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_SK_DPD_DEF_mean_mean
gc.collect()
model = Sequential()
model.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu', input_dim = 17))
model.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dense(units = 5, kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
 model.fit(X_train, y_train, batch_size = 32, epochs = 200 )<save_to_csv>
credit_NAME_CONTRACT_STATUS_Active_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Active'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Active': 'credit_NAME_CONTRACT_STATUS_Active_mean'})
 dataset = dataset.merge(credit_NAME_CONTRACT_STATUS_Active_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_NAME_CONTRACT_STATUS_Active_mean
gc.collect()
y_pred = model.predict(X_test)
 y_final =(y_pred > 0.5 ).astype(int ).reshape(X_test.shape[0])
 output = pd.DataFrame({'PassengerId': df_test['PassengerId'], 'Survived': y_final})
 output.to_csv('prediction-ann.csv', index=False )<set_options>
credit_NAME_CONTRACT_STATUS_Approved_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Approved'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Approved': 'credit_NAME_CONTRACT_STATUS_Approved_mean'})
 dataset = dataset.merge(credit_NAME_CONTRACT_STATUS_Approved_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_NAME_CONTRACT_STATUS_Approved_mean
gc.collect()
plt.style.use('seaborn')
 sns.set(font_scale=2.5)
 warnings.filterwarnings('ignore')
 %matplotlib inline 
<load_from_csv>
credit_NAME_CONTRACT_STATUS_Completed_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Completed'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Completed': 'credit_NAME_CONTRACT_STATUS_Completed_mean'})
 dataset = dataset.merge(credit_NAME_CONTRACT_STATUS_Completed_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_NAME_CONTRACT_STATUS_Completed_mean
gc.collect()
train_data = pd.read_csv('.. /input/train.csv')
 train_data.head()<load_from_csv>
credit_NAME_CONTRACT_STATUS_Demand_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Demand'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Demand': 'credit_NAME_CONTRACT_STATUS_Demand_mean'})
 dataset = dataset.merge(credit_NAME_CONTRACT_STATUS_Demand_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_NAME_CONTRACT_STATUS_Demand_mean
gc.collect()
test_data = pd.read_csv('.. /input/test.csv')
 test_data.head()<count_missing_values>
credit_NAME_CONTRACT_STATUS_Refused_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Refused'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Refused': 'credit_NAME_CONTRACT_STATUS_Refused_mean'})
 dataset = dataset.merge(credit_NAME_CONTRACT_STATUS_Refused_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_NAME_CONTRACT_STATUS_Refused_mean
gc.collect()
train_data.isnull().sum()<count_missing_values>
credit_NAME_CONTRACT_STATUS_Sent_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Sent proposal'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Sent proposal': 'credit_NAME_CONTRACT_STATUS_Sent_mean'})
 dataset = dataset.merge(credit_NAME_CONTRACT_STATUS_Sent_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_NAME_CONTRACT_STATUS_Sent_mean
gc.collect()
for col in train_data.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(train_data[col].isnull().sum() / train_data[col].shape[0]))
print(msg )<count_missing_values>
credit_NAME_CONTRACT_STATUS_Signed_mean = credit_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Signed'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Signed': 'credit_NAME_CONTRACT_STATUS_Signed_mean'})
 dataset = dataset.merge(credit_NAME_CONTRACT_STATUS_Signed_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del credit_NAME_CONTRACT_STATUS_Signed_mean
gc.collect()
for col in test_data.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(test_data[col].isnull().sum() / test_data[col].shape[0]))
print(msg )<groupby>
gc.enable()
del credit, credit_stats_by_prev
gc.collect()
train_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=True ).count() 
<groupby>
install = pd.read_csv('.. /input/installments_payments.csv')
 install.head()
train_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=True ).sum() 
<sort_values>
install['DAYS_DIFF'] = install['DAYS_INSTALMENT'] - install['DAYS_ENTRY_PAYMENT']
install['AMT_DIFF'] = install['AMT_INSTALMENT'] - install['AMT_PAYMENT']
install.head()
train_data[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False)
<feature_engineering>
install_stats_by_prev = install[['SK_ID_PREV', 'SK_ID_CURR']]
train_data['FamilySize'] = train_data['SibSp'] + train_data['Parch'] + 1
test_data['FamilySize'] = test_data['SibSp'] + test_data['Parch'] + 1<feature_engineering>
install_NUM_INSTALMENT_VERSION_count = install.groupby('SK_ID_PREV', as_index=False)['NUM_INSTALMENT_VERSION'].count().rename(columns = {'NUM_INSTALMENT_VERSION': 'install_NUM_INSTALMENT_VERSION_count'})
 install_NUM_INSTALMENT_VERSION_max = install.groupby('SK_ID_PREV', as_index=False)['NUM_INSTALMENT_VERSION'].max().rename(columns = {'NUM_INSTALMENT_VERSION': 'install_NUM_INSTALMENT_VERSION_max'})
 install_stats_by_prev = install_stats_by_prev.merge(install_NUM_INSTALMENT_VERSION_count, on = 'SK_ID_PREV', how = 'left')
 install_stats_by_prev = install_stats_by_prev.merge(install_NUM_INSTALMENT_VERSION_max, on = 'SK_ID_PREV', how = 'left' )
test_data.loc[test_data.Fare.isnull() , 'Fare'] = test_data['Fare'].mean()
train_data['Fare'] = train_data['Fare'].map(lambda i: np.log(i)if i > 0 else 0)
 test_data['Fare'] = test_data['Fare'].map(lambda i: np.log(i)if i > 0 else 0 )<categorify>
gc.enable()
del install_NUM_INSTALMENT_VERSION_count, install_NUM_INSTALMENT_VERSION_max
gc.collect()
def get_one_hot(array):
return np.array(( array['Pclass'] == 1, array['Pclass'] == 2,
array['Pclass'] == 3, array['Sex'] == 'male',
array['Sex'] == 'female', array['SibSp'],
array['Parch'], array['Fare'],
array['Embarked'] == 'C', array['Embarked'] == 'Q',
array['Embarked'] == 'S')).swapaxes(0, 1 ).astype('float32' )<drop_column>
install_DAYS_INSTALMENT_mean = install.groupby('SK_ID_PREV', as_index=False)['DAYS_INSTALMENT'].mean().rename(columns = {'DAYS_INSTALMENT': 'install_DAYS_INSTALMENT_mean'})
 install_stats_by_prev = install_stats_by_prev.merge(install_DAYS_INSTALMENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del install_DAYS_INSTALMENT_mean
gc.collect()
x_train = train_data[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]
x_train.head()<categorify>
install_DAYS_ENTRY_PAYMENT_mean = install.groupby('SK_ID_PREV', as_index=False)['DAYS_ENTRY_PAYMENT'].mean().rename(columns = {'DAYS_ENTRY_PAYMENT': 'install_DAYS_ENTRY_PAYMENT_mean'})
 install_stats_by_prev = install_stats_by_prev.merge(install_DAYS_ENTRY_PAYMENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del install_DAYS_ENTRY_PAYMENT_mean
gc.collect()
x_train = get_one_hot(x_train)
 x_train[:10]<prepare_x_and_y>
install_AMT_INSTALMENT_mean = install.groupby('SK_ID_PREV', as_index=False)['AMT_INSTALMENT'].mean().rename(columns = {'AMT_INSTALMENT': 'install_AMT_INSTALMENT_mean'})
 install_stats_by_prev = install_stats_by_prev.merge(install_AMT_INSTALMENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del install_AMT_INSTALMENT_mean
gc.collect()
y_train = np.array(train_data['Survived'])
 y_train[:10]<categorify>
install_AMT_PAYMENT_mean = install.groupby('SK_ID_PREV', as_index=False)['AMT_PAYMENT'].mean().rename(columns = {'AMT_PAYMENT': 'install_AMT_PAYMENT_mean'})
 install_stats_by_prev = install_stats_by_prev.merge(install_AMT_PAYMENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del install_AMT_PAYMENT_mean
gc.collect()
x_test = get_one_hot(x_test)
 x_test[:10]<drop_column>
install_DAYS_DIFF_mean = install.groupby('SK_ID_PREV', as_index=False)['DAYS_DIFF'].mean().rename(columns = {'DAYS_DIFF': 'install_DAYS_DIFF_mean'})
 install_DAYS_DIFF_max = install.groupby('SK_ID_PREV', as_index=False)['DAYS_DIFF'].max().rename(columns = {'DAYS_DIFF': 'install_DAYS_DIFF_max'})
 install_DAYS_DIFF_min = install.groupby('SK_ID_PREV', as_index=False)['DAYS_DIFF'].min().rename(columns = {'DAYS_DIFF': 'install_DAYS_DIFF_min'})
 install_stats_by_prev = install_stats_by_prev.merge(install_DAYS_DIFF_mean, on = 'SK_ID_PREV', how = 'left')
 install_stats_by_prev = install_stats_by_prev.merge(install_DAYS_DIFF_max, on = 'SK_ID_PREV', how = 'left')
 install_stats_by_prev = install_stats_by_prev.merge(install_DAYS_DIFF_min, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del install_DAYS_DIFF_mean, install_DAYS_DIFF_max, install_DAYS_DIFF_min
gc.collect()
train_data.drop(['PassengerId','Name','SibSp','Parch','Ticket','Cabin'], axis=1, inplace=True)
 test_data.drop(['PassengerId','Name','SibSp','Parch','Ticket','Cabin'], axis=1, inplace=True )<import_modules>
install_AMT_DIFF_mean = install.groupby('SK_ID_PREV', as_index=False)['AMT_DIFF'].mean().rename(columns = {'AMT_DIFF': 'install_AMT_DIFF_mean'})
 install_AMT_DIFF_max = install.groupby('SK_ID_PREV', as_index=False)['AMT_DIFF'].max().rename(columns = {'AMT_DIFF': 'install_AMT_DIFF_max'})
 install_AMT_DIFF_min = install.groupby('SK_ID_PREV', as_index=False)['AMT_DIFF'].min().rename(columns = {'AMT_DIFF': 'install_AMT_DIFF_min'})
 install_stats_by_prev = install_stats_by_prev.merge(install_AMT_DIFF_mean, on = 'SK_ID_PREV', how = 'left')
 install_stats_by_prev = install_stats_by_prev.merge(install_AMT_DIFF_max, on = 'SK_ID_PREV', how = 'left')
 install_stats_by_prev = install_stats_by_prev.merge(install_AMT_DIFF_min, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del install_AMT_DIFF_mean, install_AMT_DIFF_max, install_AMT_DIFF_min
gc.collect()
from mlxtend.classifier import StackingCVClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.impute import SimpleImputer<normalization>
install_NUM_INSTALMENT_VERSION_count_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_NUM_INSTALMENT_VERSION_count'].mean().rename(columns = {'install_NUM_INSTALMENT_VERSION_count': 'install_NUM_INSTALMENT_VERSION_count_mean'})
 dataset = dataset.merge(install_NUM_INSTALMENT_VERSION_count_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_NUM_INSTALMENT_VERSION_count_mean
gc.collect()
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
 imp = imp.fit(x_train)
 x_train_imp = imp.transform(x_train )<choose_model_class>
install_NUM_INSTALMENT_VERSION_max_max = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_NUM_INSTALMENT_VERSION_max'].max().rename(columns = {'install_NUM_INSTALMENT_VERSION_max': 'install_NUM_INSTALMENT_VERSION_max_max'})
 dataset = dataset.merge(install_NUM_INSTALMENT_VERSION_max_max, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_NUM_INSTALMENT_VERSION_max_max
gc.collect()
clf1 = RandomForestClassifier()
clf2 = GradientBoostingClassifier()
lr = LogisticRegression()
sclf = StackingCVClassifier(classifiers=[clf1, clf2], meta_classifier=lr )<define_search_space>
install_DAYS_INSTALMENT_mean_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_DAYS_INSTALMENT_mean'].mean().rename(columns = {'install_DAYS_INSTALMENT_mean': 'install_DAYS_INSTALMENT_mean_mean'})
 dataset = dataset.merge(install_DAYS_INSTALMENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_DAYS_INSTALMENT_mean_mean
gc.collect()
param_test = {'randomforestclassifier__n_estimators': [10, 120],
'randomforestclassifier__max_depth': [2, 15],
'gradientboostingclassifier__n_estimators': [10, 120],
'gradientboostingclassifier__max_depth': [2, 15],
'gradientboostingclassifier__learning_rate' : [0.01, 0.1],
'meta_classifier__C': [0.1, 10.0]}<train_model>
install_DAYS_ENTRY_PAYMENT_mean_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_DAYS_ENTRY_PAYMENT_mean'].mean().rename(columns = {'install_DAYS_ENTRY_PAYMENT_mean': 'install_DAYS_ENTRY_PAYMENT_mean_mean'})
 dataset = dataset.merge(install_DAYS_ENTRY_PAYMENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_DAYS_ENTRY_PAYMENT_mean_mean
gc.collect()
sclf.fit(x_train_imp, y_train )<normalization>
install_AMT_INSTALMENT_mean_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_AMT_INSTALMENT_mean'].mean().rename(columns = {'install_AMT_INSTALMENT_mean': 'install_AMT_INSTALMENT_mean_mean'})
 dataset = dataset.merge(install_AMT_INSTALMENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_AMT_INSTALMENT_mean_mean
gc.collect()
X_test_imp = imp.transform(x_test )<load_from_csv>
install_AMT_PAYMENT_mean_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_AMT_PAYMENT_mean'].mean().rename(columns = {'install_AMT_PAYMENT_mean': 'install_AMT_PAYMENT_mean_mean'})
 dataset = dataset.merge(install_AMT_PAYMENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_AMT_PAYMENT_mean_mean
gc.collect()
submission = pd.read_csv('.. /input/sample_submission.csv' )<prepare_output>
install_DAYS_DIFF_mean_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_DAYS_DIFF_mean'].mean().rename(columns = {'install_DAYS_DIFF_mean': 'install_DAYS_DIFF_mean_mean'})
 dataset = dataset.merge(install_DAYS_DIFF_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_DAYS_DIFF_mean_mean
gc.collect()
submission = pd.DataFrame(data, columns=['PassengerId', 'Survived'])
 submission.set_index('PassengerId', inplace=True)
 submission.head()<predict_on_test>
install_DAYS_DIFF_max_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_DAYS_DIFF_max'].mean().rename(columns = {'install_DAYS_DIFF_max': 'install_DAYS_DIFF_max_mean'})
 dataset = dataset.merge(install_DAYS_DIFF_max_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_DAYS_DIFF_max_mean
gc.collect()
prediction = sclf.predict(x_test)
 submission['Survived'] = prediction
prediction<save_to_csv>
install_DAYS_DIFF_min_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_DAYS_DIFF_min'].mean().rename(columns = {'install_DAYS_DIFF_min': 'install_DAYS_DIFF_min_mean'})
 dataset = dataset.merge(install_DAYS_DIFF_min_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_DAYS_DIFF_min_mean
gc.collect()
submission.to_csv('./submission.csv',index= False )<import_modules>
install_AMT_DIFF_mean_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_AMT_DIFF_mean'].mean().rename(columns = {'install_AMT_DIFF_mean': 'install_AMT_DIFF_mean_mean'})
 dataset = dataset.merge(install_AMT_DIFF_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_AMT_DIFF_mean_mean
gc.collect()
import graphviz
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import tree
from sklearn.model_selection import cross_val_score, GridSearchCV<feature_engineering>
install_AMT_DIFF_max_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_AMT_DIFF_max'].mean().rename(columns = {'install_AMT_DIFF_max': 'install_AMT_DIFF_max_mean'})
 dataset = dataset.merge(install_AMT_DIFF_max_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_AMT_DIFF_max_mean
gc.collect()
train = pd.read_csv('.. /input/train.csv' ).set_index('PassengerId')
 test = pd.read_csv('.. /input/test.csv' ).set_index('PassengerId')
 df = pd.concat([train, test], axis=0, sort=False)
 df['Title'] = df.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
df['IsWomanOrChild'] =(( df.Title == 'Master')|(df.Sex == 'female'))
df['LastName'] = df.Name.str.split(',' ).str[0]
family = df.groupby(df.LastName ).Survived
df['FamilyTotalCount'] = family.transform(lambda s: s[df.IsWomanOrChild].fillna(0 ).count())
 df['FamilyTotalCount'] = df.mask(df.IsWomanOrChild, df.FamilyTotalCount - 1, axis=0)
 df['FamilySurvivedCount'] = family.transform(lambda s: s[df.IsWomanOrChild].fillna(0 ).sum())
 df['FamilySurvivedCount'] = df.mask(df.IsWomanOrChild, df.FamilySurvivedCount - df.Survived.fillna(0), axis=0)
 df['FamilySurvivalRate'] =(df.FamilySurvivedCount / df.FamilyTotalCount.replace(0, np.nan))
df['IsSingleTraveler'] = df.FamilyTotalCount == 0<groupby>
install_AMT_DIFF_min_mean = install_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['install_AMT_DIFF_min'].mean().rename(columns = {'install_AMT_DIFF_min': 'install_AMT_DIFF_min_mean'})
 dataset = dataset.merge(install_AMT_DIFF_min_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del install_AMT_DIFF_min_mean
gc.collect()
name = df.groupby(df.LastName ).Ticket
name.value_counts()<feature_engineering>
gc.enable()
del install, install_stats_by_prev
gc.collect()
df['SingleTraveler'] = 3
ticket_list_single = []
ticket_list_family = []
ticket_list_null = []
for ticket_id in list(df['Ticket'].unique()):
count = df[df['Ticket']==ticket_id].count() [0]
if count > 1 :
ticket_list_family.append(ticket_id)
 else:
ticket_list_single.append(ticket_id)
 def tune_SingleTraveler(df):
for ticket in ticket_list_single:
df.ix[df.Ticket == ticket, "SingleTraveler"] = True
for ticket in ticket_list_family:
df.ix[df.Ticket == ticket, "SingleTraveler"] = False
tune_SingleTraveler(df)
<count_values>
cash = pd.read_csv('.. /input/POS_CASH_balance.csv')
 cash.head()
names = []
for name in list(df['LastName'].unique()):
count = df[df['LastName']==name].count() [0]
if count > 1 :
names.append(name)
 for name in names:
df.ix[df.LastName == name, "SingleTraveler"] = False
print(df['SingleTraveler'].sum() )<groupby>
cash_stats_by_prev = cash[['SK_ID_PREV', 'SK_ID_CURR']]
df['Family_freq'] = df.groupby('LastName')['LastName'].transform('count' )<count_missing_values>
cash_MONTHS_BALANCE_count = cash.groupby('SK_ID_PREV', as_index=False)['MONTHS_BALANCE'].count().rename(columns = {'MONTHS_BALANCE': 'cash_MONTHS_BALANCE_count'})
 cash_MONTHS_BALANCE_mean = cash.groupby('SK_ID_PREV', as_index=False)['MONTHS_BALANCE'].mean().rename(columns = {'MONTHS_BALANCE': 'cash_MONTHS_BALANCE_mean'})
 cash_stats_by_prev = cash_stats_by_prev.merge(cash_MONTHS_BALANCE_count, on = 'SK_ID_PREV', how = 'left')
 cash_stats_by_prev = cash_stats_by_prev.merge(cash_MONTHS_BALANCE_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del cash_MONTHS_BALANCE_count, cash_MONTHS_BALANCE_mean
gc.collect()
df['Sex'].isnull().sum()<feature_engineering>
cash_CNT_INSTALMENT_mean = cash.groupby('SK_ID_PREV', as_index=False)['CNT_INSTALMENT'].mean().rename(columns = {'CNT_INSTALMENT': 'cash_CNT_INSTALMENT_mean'})
 cash_stats_by_prev = cash_stats_by_prev.merge(cash_CNT_INSTALMENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del cash_CNT_INSTALMENT_mean
gc.collect()
df['All'] =(( df.Sex == 'male')|(df.Sex == 'female'))
df['LastName'] = df.Name.str.split(',' ).str[0]
family = df.groupby(df.LastName ).Survived
df['FamilyTotalCountM'] = family.transform(lambda s: s[df.All].fillna(0 ).count())
 df['FamilyTotalCountM'] = df.mask(df.All, df.FamilyTotalCountM - 1, axis=0)
 df['FamilySurvivedCountM'] = family.transform(lambda s: s[df.All].fillna(0 ).sum())
 df['FamilySurvivedCountM'] = df.mask(df.All, df.FamilySurvivedCountM - df.Survived.fillna(0), axis=0)
 df['FamilySurvivalRateM'] =(df.FamilySurvivedCountM / df.FamilyTotalCountM.replace(0, np.nan))<count_missing_values>
cash_CNT_INSTALMENT_FUTURE_mean = cash.groupby('SK_ID_PREV', as_index=False)['CNT_INSTALMENT_FUTURE'].mean().rename(columns = {'CNT_INSTALMENT_FUTURE': 'cash_CNT_INSTALMENT_FUTURE_mean'})
 cash_stats_by_prev = cash_stats_by_prev.merge(cash_CNT_INSTALMENT_FUTURE_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del cash_CNT_INSTALMENT_FUTURE_mean
gc.collect()
df['All'].isnull().sum()<feature_engineering>
cash_SK_DPD_mean = cash.groupby('SK_ID_PREV', as_index=False)['SK_DPD'].mean().rename(columns = {'SK_DPD': 'cash_SK_DPD_mean'})
 cash_stats_by_prev = cash_stats_by_prev.merge(cash_SK_DPD_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del cash_SK_DPD_mean
gc.collect()
_ = df.rename({'Cabin':'Deck'},axis=1,inplace=True)
 df['Deck'] = df['Deck'].fillna('N')
 len(df['Deck'] )<feature_engineering>
cash_SK_DPD_DEF_mean = cash.groupby('SK_ID_PREV', as_index=False)['SK_DPD_DEF'].mean().rename(columns = {'SK_DPD_DEF': 'cash_SK_DPD_DEF_mean'})
 cash_stats_by_prev = cash_stats_by_prev.merge(cash_SK_DPD_DEF_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del cash_SK_DPD_DEF_mean
gc.collect()
def cabin_to_deck(row):
return row['Deck'][0]
df['Deck'] = df.apply(cabin_to_deck,axis=1 )<count_unique_values>
cash_cats = pd.get_dummies(cash.select_dtypes('object'))
cash_cats['SK_ID_PREV'] = cash['SK_ID_PREV']
cash_cats.head()
ticket_list = []
for ticket_id in list(df['Ticket'].unique()):
count = df[df['Ticket']==ticket_id].count() [0]
decks = df[df['Ticket']==ticket_id]['Deck']
empty_decks =(decks=='N' ).sum()
if(count > 1)and(empty_decks > 0)and(empty_decks < len(decks)) :
ticket_list.append(ticket_id)
 print(ticket_list )<filter>
cash_cats_grouped = cash_cats.groupby('SK_ID_PREV' ).agg('sum')
 cash_cats_grouped.head()
for ticket in ticket_list:
display(df[df['Ticket']==ticket] )<feature_engineering>
cash_stats_by_prev = cash_stats_by_prev.merge(cash_cats_grouped, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del cash_cats_grouped, cash_cats
gc.collect()
_ = df.set_value(533,'Deck',value=df.loc[128]['Deck'])
 _ = df.set_value(1308,'Deck',value=df.loc[128]['Deck'])
 _ = df.set_value(258,'Deck',df.loc[679]['Deck'])
 _ = df.set_value(373,'Deck',value='C')
 _ = df.set_value(290,'Deck',value=df.loc[741]['Deck'])
 _ = df.set_value(708,'Deck',value=df.loc[297]['Deck'])
 _ = df.set_value(1032,'Deck',value=df.loc[297]['Deck'])
 _ = df.set_value(306,'Deck',value='C')
 _ = df.set_value(1266,'Deck',value=df.loc[1033]['Deck'])
 _ = df.set_value(856,'Deck',value=df.loc[318]['Deck'])
 _ = df.set_value(1108,'Deck',value=df.loc[318]['Deck'])
 _ = df.set_value(380,'Deck',value='C')
 _ = df.set_value(557,'Deck',value='C')
 _ = df.set_value(537,'Deck',value='C')
 _ = df.set_value(1215,'Deck',value='E')
 _ = df.set_value(841,'Deck',value=df.loc[772]['Deck'])
<drop_column>
cash_MONTHS_BALANCE_count_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['cash_MONTHS_BALANCE_count'].mean().rename(columns = {'cash_MONTHS_BALANCE_count': 'cash_MONTHS_BALANCE_count_mean'})
 dataset = dataset.merge(cash_MONTHS_BALANCE_count_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_MONTHS_BALANCE_count_mean
gc.collect()
for i in range(3):
if 'N' in decks_by_class[i]:
decks_by_class[i].remove('N')
 if 'T' in decks_by_class[i]:
decks_by_class[i].remove('T' )<define_variables>
cash_MONTHS_BALANCE_mean_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['cash_MONTHS_BALANCE_mean'].mean().rename(columns = {'cash_MONTHS_BALANCE_mean': 'cash_MONTHS_BALANCE_mean_mean'})
 dataset = dataset.merge(cash_MONTHS_BALANCE_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_MONTHS_BALANCE_mean_mean
gc.collect()
weights_by_class = [[],[],[]]
for i,deck_list in enumerate(decks_by_class):
for deck in deck_list:
if i == 0:
class_total = df[(df['Deck']!='N')&(df['Pclass']==i+1)].count() [0]-1
else:
class_total = df[(df['Deck']!='N')&(df['Pclass']==i+1)].count() [0]
deck_total = df[(df['Deck']==deck)&(df['Pclass']==i+1)].count() [0]
weights_by_class[i].append(deck_total/class_total)
 print(f'Pclass = {i+1} weights:',np.round(weights_by_class[i],3))<define_variables>
cash_CNT_INSTALMENT_mean_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['cash_CNT_INSTALMENT_mean'].mean().rename(columns = {'cash_CNT_INSTALMENT_mean': 'cash_CNT_INSTALMENT_mean_mean'})
 dataset = dataset.merge(cash_CNT_INSTALMENT_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_CNT_INSTALMENT_mean_mean
gc.collect()
ticket_dict = {}<categorify>
cash_CNT_INSTALMENT_FUTURE_mean_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['cash_CNT_INSTALMENT_FUTURE_mean'].mean().rename(columns = {'cash_CNT_INSTALMENT_FUTURE_mean': 'cash_CNT_INSTALMENT_FUTURE_mean_mean'})
 dataset = dataset.merge(cash_CNT_INSTALMENT_FUTURE_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_CNT_INSTALMENT_FUTURE_mean_mean
gc.collect()
def impute_deck(row):
ticket = row['Ticket']
deck = row['Deck']
pclass = row['Pclass']
if(deck == 'N')and(ticket not in ticket_dict):
if pclass == 1:
deck = list(np.random.choice(decks_by_class[0],size=1,
p=weights_by_class[0])) [0]
elif pclass ==2:
deck = list(np.random.choice(decks_by_class[1],size=1,
p=weights_by_class[1])) [0]
elif pclass ==3:
deck = list(np.random.choice(decks_by_class[2],size=1,
p=weights_by_class[2])) [0]
ticket_dict[ticket] = deck
elif(deck == 'N')and(ticket in ticket_dict):
deck = ticket_dict[ticket]
return deck<feature_engineering>
cash_SK_DPD_mean_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['cash_SK_DPD_mean'].mean().rename(columns = {'cash_SK_DPD_mean': 'cash_SK_DPD_mean_mean'})
 dataset = dataset.merge(cash_SK_DPD_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_SK_DPD_mean_mean
gc.collect()
df['Deck'] = df.apply(impute_deck,axis=1 )<categorify>
cash_SK_DPD_DEF_mean_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['cash_SK_DPD_DEF_mean'].mean().rename(columns = {'cash_SK_DPD_DEF_mean': 'cash_SK_DPD_DEF_mean_mean'})
 dataset = dataset.merge(cash_SK_DPD_DEF_mean_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_SK_DPD_DEF_mean_mean
gc.collect()
df['Deck'] = df['Deck'].map({'F':0,'C':1,'E':2,'G':3,'D':4,'A':5,
'B':6,'T':7} ).astype(int )<prepare_x_and_y>
cash_NAME_CONTRACT_STATUS_Active_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Active'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Active': 'cash_NAME_CONTRACT_STATUS_Active_mean'})
 dataset = dataset.merge(cash_NAME_CONTRACT_STATUS_Active_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_NAME_CONTRACT_STATUS_Active_mean
gc.collect()
<feature_engineering>
cash_NAME_CONTRACT_STATUS_Amortized_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Amortized debt'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Amortized debt': 'cash_NAME_CONTRACT_STATUS_Amortized_mean'})
 dataset = dataset.merge(cash_NAME_CONTRACT_STATUS_Amortized_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_NAME_CONTRACT_STATUS_Amortized_mean
gc.collect()
df.ix[df.Title == 'Master', "Sex"] = 'boy' 
<prepare_x_and_y>
cash_NAME_CONTRACT_STATUS_Approved_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Approved'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Approved': 'cash_NAME_CONTRACT_STATUS_Approved_mean'})
 dataset = dataset.merge(cash_NAME_CONTRACT_STATUS_Approved_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_NAME_CONTRACT_STATUS_Approved_mean
gc.collect()
x = pd.concat([
df.FamilySurvivalRate.fillna(0),
df.SingleTraveler,
df.Sex.replace({'male': 0, 'female': 1, 'boy': 2}),
df.Deck,
], axis=1)
 train_x, test_x = x.loc[train.index], x.loc[test.index]
train_y = df.Survived.loc[train.index]<train_on_grid>
cash_NAME_CONTRACT_STATUS_Canceled_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Canceled'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Canceled': 'cash_NAME_CONTRACT_STATUS_Canceled_mean'})
 dataset = dataset.merge(cash_NAME_CONTRACT_STATUS_Canceled_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_NAME_CONTRACT_STATUS_Canceled_mean
gc.collect()
clf_dt = tree.DecisionTreeClassifier()
grid = GridSearchCV(clf_dt, cv=5, param_grid={
'criterion': ['gini', 'entropy'],
'max_depth': [2, 3, 4, 5]})
 grid.fit(train_x, train_y)
 grid.best_params_<find_best_params>
cash_NAME_CONTRACT_STATUS_Completed_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Completed'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Completed': 'cash_NAME_CONTRACT_STATUS_Completed_mean'})
 dataset = dataset.merge(cash_NAME_CONTRACT_STATUS_Completed_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_NAME_CONTRACT_STATUS_Completed_mean
gc.collect()
model_dt = grid.best_estimator_<compute_test_metric>
cash_NAME_CONTRACT_STATUS_Demand_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Demand'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Demand': 'cash_NAME_CONTRACT_STATUS_Demand_mean'})
 dataset = dataset.merge(cash_NAME_CONTRACT_STATUS_Demand_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_NAME_CONTRACT_STATUS_Demand_mean
gc.collect()
model_dt.score(train_x, train_y )<save_to_csv>
cash_NAME_CONTRACT_STATUS_Returned_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Returned to the store'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Returned to the store': 'cash_NAME_CONTRACT_STATUS_Returned_mean'})
 dataset = dataset.merge(cash_NAME_CONTRACT_STATUS_Returned_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_NAME_CONTRACT_STATUS_Returned_mean
gc.collect()
test_y = model_dt.predict(test_x ).astype(int)
 pd.DataFrame({'Survived': test_y}, index=test.index)\ 
.reset_index() \ 
.to_csv(f'submission_dt.csv', index=False )<predict_on_test>
cash_NAME_CONTRACT_STATUS_Signed_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_Signed'].mean().rename(columns = {'NAME_CONTRACT_STATUS_Signed': 'cash_NAME_CONTRACT_STATUS_Signed_mean'})
 dataset = dataset.merge(cash_NAME_CONTRACT_STATUS_Signed_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_NAME_CONTRACT_STATUS_Signed_mean
gc.collect()
preds = pd.DataFrame()
preds = x.loc[test.index]
preds['Pclass'] = test['Pclass']
preds['pred'] = model_dt.predict_proba(test_x)[:, 1]
preds = preds.drop(['FamilySurvivalRate','SingleTraveler','Sex', 'Deck'], axis=1)
 preds.head() 
<count_values>
cash_NAME_CONTRACT_STATUS_XNA_mean = cash_stats_by_prev.groupby('SK_ID_CURR', as_index=False)['NAME_CONTRACT_STATUS_XNA'].mean().rename(columns = {'NAME_CONTRACT_STATUS_XNA': 'cash_NAME_CONTRACT_STATUS_XNA_mean'})
 dataset = dataset.merge(cash_NAME_CONTRACT_STATUS_XNA_mean, on = 'SK_ID_CURR', how = 'left')
 gc.enable()
del cash_NAME_CONTRACT_STATUS_XNA_mean
gc.collect()
preds.groupby(['Pclass'] ).pred.value_counts()<data_type_conversions>
gc.enable()
del cash, cash_stats_by_prev
gc.collect()
preds.loc[preds['Pclass'] == 3, 'pred'] = preds.loc[preds['Pclass'] == 3, 'pred'] -0.3
sub = []
sub = preds['pred'].values
sub = np.around(sub ).astype(int )<save_to_csv>
dataset.dtypes.value_counts()
test_y = sub.astype(int)
 pd.DataFrame({'Survived': test_y}, index=test.index)\ 
.reset_index() \ 
.to_csv(f'submission_dt_m0.3.csv', index=False )<save_to_csv>
dataset['bureau_DAYS_CREDIT_ENDDATE_max_outlier'] = dataset['bureau_DAYS_CREDIT_ENDDATE_max_outlier'].map({False:0, True:1})
 dataset['bureau_DAYS_ENDDATE_FACT_mean_outlier'] = dataset['bureau_DAYS_ENDDATE_FACT_mean_outlier'].map({False:0, True:1} )
model_xgb = xgboost.XGBClassifier()
model_xgb.fit(train_x, train_y)
 test_y = model_xgb.predict(test_x ).astype(int)
 pd.DataFrame({'Survived': test_y}, index=test.index)\ 
.reset_index() \ 
.to_csv(f'submission_xgb.csv', index=False )<prepare_x_and_y>
y_temp = dataset[['TARGET']]
X_temp = dataset.drop(['TARGET'], axis=1)
 X_big, X_small, y_big, y_small = train_test_split(X_temp, y_temp, test_size=0.2, random_state=1 )
X_train = train_x
Y_train = train_y<train_model>
upper_corr = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
upper_corr.head()
def crossValidation(data,target, model, cv = 5):
ksplits = KFold(n_splits=cv)
 errors = []
for j,(indexTrain, indexTest)in enumerate(ksplits.split(data, target)) :
print("This is fold ", j + 1, "of the cross validation")
 x_train, y_train = data.iloc[indexTrain, :], target.iloc[indexTrain]
x_test, y_test = data.iloc[indexTest, :], target.iloc[indexTest]
print("Fitting the model")
 model.fit(x_train, y_train)
 predictions = model.predict(x_test)
 errorFold = accuracy_score(y_test, predictions)
 errors.append(errorFold)
 print("The mean accuracy score over the folds is: ", np.mean(errors))
return<compute_train_metric>
drop_cols = [column for column in upper_corr.columns if any(upper_corr[column] > 0.9)]
print('Columns to remove: ', len(drop_cols))
modelXGboost_tuned = xgboost.XGBClassifier(base_score = 0.5,
colsample_bytree= 0.65,
gamma= 0,
learning_rate= 0.5,
max_depth= 3,
min_child_weight= 1,
n_estimators= 120,
scale_pos_weight= 1)
 crossValidation(X_train, Y_train, modelXGboost_tuned )<compute_train_metric>
dataset_missing =(dataset.isnull().sum() / len(dataset)).sort_values(ascending = False)
 dataset_missing.head(10 )
modelK_tuned = KNeighborsClassifier(algorithm='ball_tree', n_neighbors=5, p=1, weights='distance')
 crossValidation(X_train, Y_train, modelK_tuned )<compute_train_metric>
dataset_missing = dataset_missing.index[dataset_missing > 0.75]
print('Columns with more than 75% missing values: ', len(dataset_missing))
modelForest_tuned = RandomForestClassifier(max_depth =5, n_estimators=25)
 crossValidation(X_train, Y_train, modelForest_tuned )<compute_train_metric>
train = dataset[:train_len]
x_test = dataset[train_len:]
train_ids = train['SK_ID_CURR']
test_ids = x_test['SK_ID_CURR']
train.drop(columns=['SK_ID_CURR'], axis = 1, inplace=True)
 x_test.drop(columns=['TARGET', 'SK_ID_CURR'], axis = 1, inplace=True )
modelLogistic = LogisticRegression(solver="liblinear")
 crossValidation(X_train, Y_train, modelLogistic )<compute_train_metric>
train['TARGET'] = train['TARGET'].astype(int)
 y_train = train['TARGET']
x_train = train.drop(columns=['TARGET'], axis = 1 )
modelXGboost = xgboost.XGBClassifier()
crossValidation(X_train, Y_train, modelXGboost )<compute_train_metric>
feature_imp = np.zeros(x_train.shape[1] )
modelDiscriminant = LinearDiscriminantAnalysis()
crossValidation(X_train, Y_train, modelDiscriminant )<compute_train_metric>
model = lgb.LGBMClassifier(objective='binary', boosting_type='goss', n_estimators=10000, class_weight='balanced' )
modelForest = RandomForestClassifier()
crossValidation(X_train, Y_train, modelForest )<compute_train_metric>
for i in range(2):
train_x1, train_x2, train_y1, train_y2 = train_test_split(x_train, y_train, test_size = 0.25, random_state = i)
 model.fit(train_x1, train_y1, early_stopping_rounds=100, eval_set = [(train_x2, train_y2)], eval_metric = 'auc', verbose = 200)
 feature_imp += model.feature_importances_
modelK = KNeighborsClassifier()
crossValidation(X_train, Y_train, modelK )<compute_test_metric>
zero_imp = list(feature_imp[feature_imp['importance'] == 0.0]['feature'])
 print('count of features with 0 importance: ', len(zero_imp))
feature_imp.tail(10 )
crossValidation(X_train, Y_train, model_dt )<compute_train_metric>
x_train = x_train.drop(columns = zero_imp)
 x_test = x_test.drop(columns = zero_imp )
def crossValMixed(X, y, model,cv = 5):
n_folds = KFold(n_splits = cv,shuffle=True)
 counter = 0
errorMetr = []
for indexTrain, indexTest in n_folds.split(X,y):
print("This is fold: ", counter, "of the cross validation")
 X_train, y_train = X.iloc[indexTrain, :], y.iloc[indexTrain]
X_test, y_test = X.iloc[indexTest, :], y.iloc[indexTest]
print("Fitting the model")
 predictions = model.stackingActive(X_train, y_train, X_test)
 print("The metric in the fold ", counter, "is: ", accuracy_score(y_test,predictions))
counter += 1
errorMetr.append(accuracy_score(y_test,predictions))
print("The mean absolute error over the ", cv, "folds is: ", np.mean(errorMetr))
return predictions 
<save_to_csv>
test_predictions = np.zeros(x_test.shape[0])
 out_of_fold = np.zeros(x_train.shape[0])
 valid_scores = []
train_scores = []
pd.DataFrame({'Survived': predictions.astype(int)}, index=test.index)\ 
.reset_index() \ 
.to_csv(f'submission_stack_rkd.csv', index=False )<save_to_csv>
k_fold = KFold(n_splits = 5, shuffle = False, random_state = 50 )
pd.DataFrame({'Survived': predictions.astype(int)}, index=test.index)\ 
.reset_index() \ 
.to_csv(f'submission_stack_xkx_tuned.csv', index=False )<prepare_output>
x_train = np.array(x_train)
 x_test = np.array(x_test )
preds = pd.DataFrame()
preds = x.loc[test.index]
preds['Pclass'] = test['Pclass']
preds['pred'] = predictions_proba[:, 1]
preds.head()<data_type_conversions>
for train_indices, valid_indices in k_fold.split(x_train):
train_features, train_labels = x_train[train_indices], y_train[train_indices]
valid_features, valid_labels = x_train[valid_indices], y_train[valid_indices]
model = lgb.LGBMClassifier(n_estimators=10000, objective = 'binary', boosting_type='goss',class_weight = 'balanced',
learning_rate = 0.05, reg_alpha = 0.1, reg_lambda = 0.1, n_jobs = -1, random_state = 50)
 model.fit(train_features, train_labels, eval_metric = 'auc',
eval_set = [(valid_features, valid_labels),(train_features, train_labels)],
eval_names = ['valid', 'train'], early_stopping_rounds = 100, verbose = 200)
 best_iteration = model.best_iteration_
test_predictions += model.predict_proba(x_test, num_iteration = best_iteration)[:, 1] / k_fold.n_splits
out_of_fold[valid_indices] = model.predict_proba(valid_features, num_iteration = best_iteration)[:, 1]
valid_score = model.best_score_['valid']['auc']
train_score = model.best_score_['train']['auc']
valid_scores.append(valid_score)
 train_scores.append(train_score)
 gc.enable()
del model, train_features, valid_features
gc.collect()
preds.loc[preds['Pclass'] == 3, 'pred'] = preds.loc[preds['Pclass'] == 3, 'pred'] -0.2
sub = []
sub = preds['pred'].values
sub = np.around(sub ).astype(int )<save_to_csv>
valid_auc = roc_auc_score(y_train, out_of_fold)
 valid_scores.append(valid_auc)
 train_scores.append(np.mean(train_scores))
fold_names = list(range(5))
fold_names.append('overall')
 metrics = pd.DataFrame({'fold': fold_names, 'train': train_scores, 'valid': valid_scores} )
pd.DataFrame({'Survived': sub}, index=test.index)\ 
.reset_index() \ 
.to_csv(f'submission_stack_xkx_m.csv', index=False )<set_options>
submission = pd.DataFrame({'SK_ID_CURR': test_ids, 'TARGET': test_predictions})
 submission.to_csv('submission.csv', index = False )