Kaggle competition: House Prices - Advanced Regression Techniques
test_df_csv = test_df_csv.join(pd.DataFrame(y_test, columns=[
'any', 'epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural'
]))
test_df_csv = test_df_csv.melt(id_vars=['filename'])
 test_df_csv['ID'] = test_df_csv.filename.apply(lambda x: x.replace('.png', '')) + '_' + test_df_csv.variable
test_df_csv['Label'] = test_df_csv['value']
test_df_csv[['ID', 'Label']].to_csv('submission.csv', index=False )<set_options>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
import os
import numpy as np
import pandas as pd
from tqdm import tqdm
import gc
import seaborn as sns; sns.set()
import pydicom
import matplotlib.pyplot as plt
import cv2
import torch.optim as optim
import torch
import torchvision.models as models
from torch.utils.data import Dataset<define_variables>
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train.SalePrice.values
all_data = pd.concat(( train, test)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))
BASE_PATH = '/kaggle/input/rsna-intracranial-hemorrhage-detection/'
TRAIN_DIR = 'stage_1_train_images/'
TEST_DIR = 'stage_1_test_images/'
TRAIN_CSV = 'stage_1_train.csv'
TEST_CSV = 'stage_1_sample_submission.csv'
MODEL_PATH = '/kaggle/input/baseline-resnext50/resnext50_10.pth'
TRAIN_CSV_PATH = os.path.join(BASE_PATH, TRAIN_CSV)
 TEST_CSV_PATH = os.path.join(BASE_PATH, TEST_CSV )<feature_engineering>
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head(20 )
df_train = pd.read_csv(TRAIN_CSV_PATH)
 df_train[['id', 'img', 'subtype']] = df_train['ID'].str.split('_', n=3, expand=True)
 df_train['img'] = 'ID_' + df_train['img']
df_train.drop_duplicates(inplace=True)
 df_train = df_train.pivot(index='img', columns='subtype', values='Label' ).reset_index()
df_train['path'] = os.path.join(BASE_PATH, TRAIN_DIR)+ df_train['img'] + '.dcm'
legit_images = pd.read_csv('/kaggle/input/true-imagescsv/legit-images.csv')
 df_train = df_train.merge(legit_images, left_on='img', right_on='0' ).drop(['0'], axis=1)
 df_train.head()<feature_engineering>
all_data["PoolQC"] = all_data["PoolQC"].fillna("None" )
df_test = pd.read_csv(TEST_CSV_PATH)
 df_test[['id','img','subtype']] = df_test['ID'].str.split('_', expand=True)
 df_test['img'] = 'ID_' + df_test['img']
df_test = df_test[['img', 'Label']]
df_test['path'] = os.path.join(BASE_PATH, TEST_DIR)+ df_test['img'] + '.dcm'
df_test.drop_duplicates(inplace=True)
 df_test = df_test.reset_index(drop=True )<create_dataframe>
all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None" )
params = {'batch_size': 64,
'shuffle': False,
'num_workers': 4}
train_dataset = RSNADataset(df= df_train, labels=True)
 test_dataset = RSNADataset(df= df_test, labels=False)
 data_train_generator = torch.utils.data.DataLoader(train_dataset, **params)
 data_test_generator = torch.utils.data.DataLoader(test_dataset,**params )<find_best_params>
all_data["Alley"] = all_data["Alley"].fillna("None" )
device = torch.device("cuda:0")
 model0 = models.resnext50_32x4d(pretrained=True)
 model = torch.nn.Sequential(model0, torch.nn.Linear(1000, 6))
model = model.to(device)
 criterion = torch.nn.BCEWithLogitsLoss()<load_pretrained>
all_data["Fence"] = all_data["Fence"].fillna("None" )
n_epochs = 2
optimizer = optim.Adam(model.parameters() , lr=4e-5)
 try:
model.load_state_dict(torch.load(MODEL_PATH))
torch.save(model.state_dict() , 'resnext50_0.pth')
 except Exception as e:
print('The pre-trained model is used' )<train_model>
all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None" )
for epoch in range(1, n_epochs+1):
print('Epoch {}/{}'.format(epoch, n_epochs))
print('-' * 10)
 model.train()
tr_loss = 0
tk0 = tqdm(data_train_generator, desc="Iteration")
 for step, batch in enumerate(tk0):
inputs = batch["image"]
labels = batch["labels"]
inputs = inputs.to(device, dtype=torch.float)
 labels = labels.to(device, dtype=torch.float)
 outputs = model(inputs)
 loss = criterion(outputs, labels)
 loss.backward()
tr_loss += loss.item()
optimizer.step()
optimizer.zero_grad()
torch.save(model.state_dict() , f'resnext50_{epoch}.pth')
 epoch_loss = tr_loss / len(data_train_generator)
 print('Training Loss: {:.4f}'.format(epoch_loss))<predict_on_test>
all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))
for param in model.parameters() :
param.requires_grad = False
model.eval()
test_pred = np.zeros(( len(test_dataset)* 6, 1))
for i, batch_ in enumerate(tqdm(data_test_generator)) :
batch_ = batch_["image"]
batch_ = batch_.to(device, dtype=torch.float)
 with torch.no_grad() :
pred = model(batch_)
 test_pred[(i * 64 * 6):(( i + 1)* 64 * 6)] = torch.sigmoid(
 pred ).detach().cpu().reshape(( len(batch_)* 6, 1))<save_to_csv>
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
all_data[col] = all_data[col].fillna('None' )
submission = pd.read_csv(TEST_CSV_PATH)
 submission = pd.concat([submission.drop(columns=['Label']), pd.DataFrame(test_pred)], axis=1)
 submission.columns = ['ID', 'Label']
submission.to_csv('submission.csv', index=False )<define_variables>
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_data[col] = all_data[col].fillna(0 )
start_time = datetime.datetime.now()
demographic_cols = ['fecha_dato',
'ncodpers','ind_empleado','pais_residencia','sexo','age','fecha_alta','ind_nuevo','antiguedad','indrel',
'indrel_1mes','tiprel_1mes','indresi','indext','conyuemp','canal_entrada','indfall',
'tipodom','cod_prov','ind_actividad_cliente','renta','segmento']
notuse = ["ult_fec_cli_1t","nomprov"]
product_col = [
'ind_ahor_fin_ult1','ind_aval_fin_ult1','ind_cco_fin_ult1','ind_cder_fin_ult1','ind_cno_fin_ult1','ind_ctju_fin_ult1',
'ind_ctma_fin_ult1','ind_ctop_fin_ult1','ind_ctpp_fin_ult1','ind_deco_fin_ult1','ind_deme_fin_ult1',
'ind_dela_fin_ult1','ind_ecue_fin_ult1','ind_fond_fin_ult1','ind_hip_fin_ult1','ind_plan_fin_ult1',
'ind_pres_fin_ult1','ind_reca_fin_ult1','ind_tjcr_fin_ult1','ind_valo_fin_ult1','ind_viv_fin_ult1','ind_nomina_ult1',
'ind_nom_pens_ult1','ind_recibo_ult1']
train_cols = demographic_cols + product_col<load_from_csv>
for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[col] = all_data[col].fillna(0 )
df_train = pd.read_csv('.. /input/makejuneextradata-fff8dd/juneExtra.csv', usecols=train_cols )<load_from_csv>
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[col] = all_data[col].fillna('None' )
df_test = pd.read_csv('.. /input/santander-product-recommendation/test_ver2.csv', usecols = demographic_cols )<filter>
all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0 )
df_train = df_train[df_train['ind_nuevo'] == 0]
df_train = df_train[df_train['antiguedad'] != -999999]
df_train = df_train[df_train['indrel'] == 1]
df_train = df_train[df_train['indresi'] == 'S']
df_train = df_train[df_train['indfall'] == 'N']
df_train = df_train[df_train['tipodom'] == 1]
df_train = df_train[df_train['ind_empleado'] == 'N']
df_train = df_train[df_train['pais_residencia'] == 'ES']
df_train = df_train[df_train['indrel_1mes'] == 1]
df_train = df_train[df_train['tiprel_1mes'] ==('A' or 'I')]
df_train = df_train[df_train['indext'] == 'N']<count_missing_values>
all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0] )
df_train.isnull().sum()<count_values>
all_data = all_data.drop(['Utilities'], axis=1 )
df_train.tiprel_1mes.value_counts()<drop_column>
all_data["Functional"] = all_data["Functional"].fillna("Typ" )
drop_column = ['ind_nuevo','indrel','indresi','indfall','tipodom','ind_empleado','pais_residencia','indrel_1mes','indext','conyuemp','fecha_alta','tiprel_1mes']
df_train.drop(drop_column, axis=1, inplace = True)
 df_test.drop(drop_column, axis=1, inplace = True )<data_type_conversions>
all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0] )
df_train["renta"] = pd.to_numeric(df_train["renta"], errors="coerce")
 df_test["renta"] = pd.to_numeric(df_test["renta"], errors="coerce" )<feature_engineering>
all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0] )
for cod in unique_prov:
df_test.loc[df_test['cod_prov']==cod,['renta']] = df_test.loc[df_test['cod_prov']==cod,['renta']].fillna({'renta':grouped[cod]} ).values
df_train.loc[df_train['cod_prov']==cod,['renta']] = df_train.loc[df_train['cod_prov']==cod,['renta']].fillna({'renta':grouped[cod]} ).values<correct_missing_values>
all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0] )
df_test.renta.fillna(df_test["renta"].median() , inplace=True )<count_missing_values>
all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0] )
df_train.isnull().sum()<count_missing_values>
all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None")
df_test.isnull().sum()<categorify>
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head()
replace_mapping = {
"sexo": {"V":0,'H':1},
}
df_train.replace(replace_mapping, inplace= True )<define_variables>
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['OverallCond'] = all_data['OverallCond'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str)
dummy_col = ['sexo','canal_entrada','cod_prov','segmento']<count_values>
cols =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(all_data[c].values))
all_data[c] = lbl.transform(list(all_data[c].values))
print('Shape all_data: {}'.format(all_data.shape))
limit = int(0.05 * len(df_train.index))
for col in dummy_col:
if len(df_train[col].unique())> 6:
trainlist = df_train[col].value_counts()
print(trainlist)
 use_col = []
for i,item in enumerate(trainlist):
if item > limit:
use_col.append(df_train[col].value_counts().index[i])
 print(use_col)
 for item in df_train[col].unique() :
if item not in use_col:
row_index = df_train[col] == item
print(row_index)
 df_train.loc[row_index,col] = np.nan
print(df_train.loc[row_index,col])
 for item in df_test[col].unique() :
if item not in use_col:
row_index = df_test[col] == item
df_test.loc[row_index,col] = np.nan<categorify>
all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']
df_train = pd.get_dummies(df_train, prefix=dummy_col, columns = dummy_col)
 df_test = pd.get_dummies(df_test, prefix=dummy_col, columns = dummy_col )<categorify>
skewness = skewness[abs(skewness)> 0.75]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
all_data[feat] = boxcox1p(all_data[feat], lam)
df_train["age"] = pd.to_numeric(df_train["age"], errors="coerce")
 df_test["age"] = pd.to_numeric(df_test["age"], errors="coerce")
 age_group = [[0,19],[19,26],[26,36],[36,41],[41,47],[47,55],[55,60],[60,70],[70,80],[80,170]]
def create_age_group(df):
df['age_group'] = np.nan
for i,age in enumerate(age_group):
row_index =(df['age'] >= age[0])&(df['age'] < age[1])
 df.loc[row_index,'age_group'] = i
create_age_group(df_train)
 create_age_group(df_test)
 df_train.drop('age', axis=1, inplace = True)
 df_test.drop('age', axis=1, inplace = True )<train_model>
all_data = pd.get_dummies(all_data)
 print(all_data.shape )
models = {}
model_preds = {}
id_preds = defaultdict(list)
 ids = df_test['ncodpers'].values
for c in product_col:
if c != 'ncodpers':
print(c)
 y_train = df_train[c]
x_train = df_train.drop(product_col + ['ncodpers',"fecha_dato"], 1)
 clf = ensemble.RandomForestClassifier(n_estimators=60, n_jobs=-1,max_depth=10, min_samples_split=10, verbose=1)
 clf.fit(x_train, y_train)
 p_train1 = clf.predict_proba(x_train)[:,1]
x_train2 = df_test.drop(['ncodpers',"fecha_dato"], 1)
 p_train = clf.predict_proba(x_train2)[:,1]
p_train2 = clf.predict_proba(x_train)[:,1]
models[c] = clf
model_preds[c] = p_train
for id, p in zip(ids, p_train):
id_preds[id].append(p)
<load_from_csv>
train = all_data[:ntrain]
test = all_data[ntrain:]
df_recent = pd.read_csv('.. /input/santander-product-recommendation/train_ver2.csv',usecols=['ncodpers'] + product_col)
 df_recent = df_recent.drop_duplicates(['ncodpers'], keep='last' )<load_from_csv>
from sklearn.linear_model import ElasticNet, Lasso, BayesianRidge, LassoLarsIC
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
import lightgbm as lgb
df_recent = pd.read_csv('.. /input/santander-product-recommendation/train_ver2.csv',usecols=['ncodpers'] + product_col )<load_from_csv>
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(train.values)
 rmse= np.sqrt(-cross_val_score(model, train.values, y_train, scoring="neg_mean_squared_error", cv = kf))
return(rmse )
sample = pd.read_csv('.. /input/santander-product-recommendation/sample_submission.csv' )<define_variables>
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0005, random_state=1))
already_active = {}
for row in df_recent.values:
row = list(row)
 id = row.pop(0)
 active = [c[0] for c in zip(tuple(product_col), row)if c[1] > 0]
already_active[id] = active
train_preds = {}
for id, p in id_preds.items() :
preds = [i[0] for i in sorted([i for i in zip(tuple(product_col), p)if i[0] not in already_active[id]],
key=lambda i:i [1],
reverse=True)[:7]]
train_preds[id] = preds
test_preds = []
for row in sample.values:
id = row[0]
p = train_preds[id]
test_preds.append(' '.join(p)) 
<prepare_output>
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
sample['added_products'] = test_preds 
<save_to_csv>
KRR = KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5 )
sample.to_csv('RForest_demographic_sub.csv', index=False)
 print(datetime.datetime.now() -start_time)
<set_options>
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5 )
%matplotlib inline
K.set_image_dim_ordering('th')
<load_from_csv>
model_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1)
INPUT_PATH='.. /input/'
print(os.getcwd())
 dims=[128,128]
img_rows=dims[0]
img_cols=dims[1]
train=sorted(glob.glob(INPUT_PATH+'train/*.jpg'))
masks=sorted(glob.glob(INPUT_PATH+'train_masks/*.gif'))
test=sorted(glob.glob(INPUT_PATH+'test/*.jpg'))
print('Number of training images: ', len(train), 'Number of corresponding masks: ', len(masks), 'Number of test images: ', len(test))
meta=pd.read_csv(INPUT_PATH+'metadata.csv')
 mask_df=pd.read_csv(INPUT_PATH+'train_masks.csv')
 ids_train=mask_df['img'].map(lambda s: s.split('_')[0] ).unique()
print('Length of ids_train ', len(ids_train))<load_from_csv>
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11 )
image = cv2.imread(INPUT_PATH+"train/00087a6bd4dc_01.jpg")
 plt.imshow(image)
 plt.show()<compute_test_metric>
score = rmsle_cv(lasso)
 print("
Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
masked_img=cv2.bitwise_and(image,img2mask)
 plt.imshow(masked_img )<compute_test_metric>
score = rmsle_cv(ENet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
smooth = 1.
 def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
 y_pred_f = K.flatten(y_pred)
 intersection=K.sum(y_true_f * y_pred_f)
 return(2.* intersection + smooth)/(K.sum(y_true_f)+ K.sum(y_pred_f)+ smooth)
 def dice_coef_mp(y_true, y_pred):
y_true_f=y_true.flatten()
y_pred_f=y_pred.flatten()
intersection = np.sum(y_true_f*y_pred_f)
 return(2.* intersection + smooth)/(np.sum(y_pred_f)+ mp.sum(y_pred_f)+ smooth)
 def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred )<choose_model_class>
score = rmsle_cv(KRR)
 print("Kernel Ridge score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
def get_layer(inputs, pixel, pool=True):
conv = Conv2D(pixel,(3, 3), padding="same", activation='relu' )(inputs)
 conv = Conv2D(pixel,(3 ,3), padding="same", activation='relu' )(conv)
 if pool:
pool = MaxPooling2D(pool_size=(2, 2))(conv)
 else:
pool = None
return conv, pool<choose_model_class>
score = rmsle_cv(GBoost)
 print("Gradient Boosting score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
def get_unet() :
inputs = Input(( 3,img_rows, img_cols))
conv1 = Conv2D(32,(3, 3), padding="same", activation='relu' )(inputs)
 conv1 = Conv2D(32,(3, 3), padding="same", activation='relu' )(conv1)
 pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
 conv2 = Conv2D(64,(3, 3), padding="same", activation='relu' )(pool1)
 conv2 = Conv2D(64,(3, 3), padding="same", activation='relu' )(conv2)
 pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
 conv3 = Conv2D(128,(3, 3), padding="same", activation='relu' )(pool2)
 conv3 = Conv2D(128,(3, 3), padding="same", activation='relu' )(conv3)
 pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
 conv4 = Conv2D(256,(3, 3), padding="same", activation='relu' )(pool3)
 conv4 = Conv2D(256,(3, 3), padding="same", activation='relu' )(conv4)
 pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
 conv5 = Conv2D(512,(3, 3), padding="same", activation='relu' )(pool4)
 conv5 = Conv2D(512,(3, 3), padding="same", activation='relu' )(conv5)
 up6 = Concatenate(axis=1 )([UpSampling2D(size=(2, 2))(conv5), conv4])
 conv6 = Conv2D(256,(3, 3), padding="same", activation='relu' )(up6)
 conv6 = Conv2D(256,(3, 3), padding="same", activation='relu' )(conv6)
 up7 = Concatenate(axis=1 )([UpSampling2D(size=(2, 2))(conv6), conv3])
 conv7 = Conv2D(128,(3, 3), padding="same", activation='relu' )(up7)
 conv7 = Conv2D(128,(3, 3), padding="same", activation='relu' )(conv7)
 up8 = Concatenate(axis=1 )([UpSampling2D(size=(2, 2))(conv7), conv2])
 conv8 = Conv2D(64,(3, 3), padding="same", activation='relu' )(up8)
 conv8 = Conv2D(64,(3, 3), padding="same", activation='relu' )(conv8)
 up9 = Concatenate(axis=1 )([UpSampling2D(size=(2, 2))(conv8), conv1])
 conv9 = Conv2D(32,(3, 3), padding="same", activation='relu' )(up9)
 conv9 = Conv2D(32,(3, 3), padding="same", activation='relu' )(conv9)
 conv10 = Conv2D(1,(1, 1), activation='sigmoid' )(conv9)
 model = Model(inputs=inputs, outputs=conv10)
 model.compile(optimizer=Adam(5e-4), loss='binary_crossentropy', metrics=[dice_coef])
 return model<split>
score = rmsle_cv(model_xgb)
 print("Xgboost score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
train_images, validation_images = train_test_split(train, train_size=0.8, test_size=0.2)
 print('Split into training set with ', len(train_images), ' images anf validation set with ', len(validation_images), ' images' )<categorify>
score = rmsle_cv(model_lgb)
 print("LGBM score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
def grey2rgb(img):
new_img = []
for i in range(img.shape[0]):
for j in range(img.shape[1]):
new_img.append(list(img[i][j])*3)
 new_img = np.array(new_img ).reshape(img.shape[0], img.shape[1], 3)
 return new_img
def data_gen_small(data_dir, masks, images, batch_size, dims):

 while True:
if batch_size==1:
ix=np.array([0])
 else:
ix=np.random.choice(np.arange(len(images)) , batch_size)
 imgs = []
labels = []
for i in ix:
if batch_size==1:
original_img = cv2.imread(images)
 else:
original_img = cv2.imread(images[i])
 resized_img = imresize(original_img, dims + [3])
 array_img = resized_img/255
array_img = array_img.swapaxes(0, 2)
 imgs.append(array_img)
 try:
mask_filename = basename(images[i])
 no_extension = os.path.splitext(mask_filename)[0]
correct_mask = INPUT_PATH + 'train_masks/' + no_extension + '_mask.gif'
original_mask = Image.open(correct_mask ).convert('L')
 data = np.asarray(original_mask, dtype="int32")
 resized_mask = imresize(original_mask, dims+[3])
 array_mask = resized_mask / 255
labels.append(array_mask)
 except Exception as e:
labels=None
imgs = np.array(imgs)
 labels = np.array(labels)
 try:
relabel = labels.reshape(-1, dims[0], dims[1], 1)
 relabel = relabel.swapaxes(1, 3)
 except Exception as e:
relabel=labels
yield imgs, relabel<train_model>
class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, models):
self.models = models
def fit(self, X, y):
self.models_ = [clone(x)for x in self.models]
for model in self.models_:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([
model.predict(X)for model in self.models_
])
 return np.mean(predictions, axis=1 )
train_gen = data_gen_small(INPUT_PATH + 'train/', masks, train_images, 2, dims)
 img, msk = next(train_gen)
 print('Size of batch: ', len(img))
print('shape of img ', img.shape, 'number dimensions: ', img[0].ndim)
 print('shape of msk ', msk.shape, 'number dimensions: ', msk[0].ndim)
 newshape = img[0].swapaxes(0,2)
 plt.imshow(newshape)
 plt.show()
resized_img = imresize(img[0], [1280, 1918]+[3])
 print('resized up: ', resized_img.shape)
 newshape = resized_img.swapaxes(0,1)
 print('resized swapaxes: ', newshape.shape)
 print('resized swapaxes shape[-1]: ', newshape.shape[-1])
 plt.imshow(newshape)
 plt.show()
newshape = msk.swapaxes(1,3)
 print(newshape.shape)
 plt.imshow(grey2rgb(newshape[0]), alpha=0.5)
 plt.show()<categorify>
averaged_models = AveragingModels(models =(ENet, GBoost, KRR, lasso))
score = rmsle_cv(averaged_models)
 print(" Averaged base models score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
validation_gen = data_gen_small(INPUT_PATH + 'train/', masks, validation_images, 4, dims )<train_model>
class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, base_models, meta_model, n_folds=5):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = n_folds
def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
 kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)
 out_of_fold_predictions = np.zeros(( X.shape[0], len(self.base_models)))
 for i, model in enumerate(self.base_models):
for train_index, holdout_index in kfold.split(X, y):
instance = clone(model)
 self.base_models_[i].append(instance)
 instance.fit(X[train_index], y[train_index])
 y_pred = instance.predict(X[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred
self.meta_model_.fit(out_of_fold_predictions, y)
 return self
def predict(self, X):
meta_features = np.column_stack([
np.column_stack([model.predict(X)for model in base_models] ).mean(axis=1)
 for base_models in self.base_models_ ])
 return self.meta_model_.predict(meta_features )
model.fit_generator(train_gen, steps_per_epoch=50, epochs=35, validation_data=validation_gen, validation_steps=50 )<predict_on_test>
stacked_averaged_models = StackingAveragedModels(base_models =(ENet, GBoost, KRR),
meta_model = lasso)
 score = rmsle_cv(stacked_averaged_models)
 print("Stacking Averaged models score: {:.4f}({:.4f})".format(score.mean() , score.std()))
img, msk = next(validation_gen)
 print(img.shape)
 predicted_mask = model.predict(img)
 predicted_mask.shape<predict_on_test>
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
validation_test = data_gen_small(INPUT_PATH + 'test/', masks, test, 4, dims)
 img_tst, msk_tst = next(validation_test)
 print(img_tst.shape)
 predicted_mask_tst = model.predict(img_tst)
 predicted_mask_tst.shape<train_model>
stacked_averaged_models.fit(train.values, y_train)
 stacked_train_pred = stacked_averaged_models.predict(train.values)
 stacked_pred = np.expm1(stacked_averaged_models.predict(test.values))
print(rmsle(y_train, stacked_train_pred))
newshape_tst = predicted_mask_tst.swapaxes(1,3)
 print('newshape shape ', newshape_tst.shape)
 grey_tst = grey2rgb(newshape_tst[3])
 print('grey shape ', grey_tst.shape)
 plt.imshow(grey_tst, alpha = 0.5)
 plt.show()<train_model>
model_xgb.fit(train, y_train)
 xgb_train_pred = model_xgb.predict(train)
 xgb_pred = np.expm1(model_xgb.predict(test))
print(rmsle(y_train, xgb_train_pred))
newshape_tst = img_tst[3].swapaxes(0,2)
 print(newshape_tst.shape)
 plt.imshow(newshape_tst)
 plt.show()<train_model>
model_lgb.fit(train, y_train)
 lgb_train_pred = model_lgb.predict(train)
 lgb_pred = np.expm1(model_lgb.predict(test.values))
print(rmsle(y_train, lgb_train_pred))
newshape_tst = predicted_mask_tst.swapaxes(1,3)
 print('newshape shape ', newshape_tst.shape)
 grey_tst = grey2rgb(newshape_tst[3])
 print('grey shape ', grey_tst.shape)
 plt.imshow(grey_tst, alpha = 0.5)
 plt.show()<import_modules>

 print('RMSLE score on train data:')
 print(rmsle(y_train,stacked_train_pred*0.70 +
xgb_train_pred*0.15 + lgb_train_pred*0.15))
from tqdm import tqdm_notebook as tqdm<create_dataframe>
ensemble = stacked_pred*0.70 + xgb_pred*0.15 + lgb_pred*0.15
df=pd.DataFrame(columns=['img','rle_mask'] )<save_to_csv>
sub = pd.DataFrame()
sub['Id'] = test_ID
sub['SalePrice'] = ensemble
sub.to_csv('submission.csv',index=False )