Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<normalization>
warnings.filterwarnings("ignore")
 with warnings.catch_warnings() :
warnings.filterwarnings("ignore",category=DeprecationWarning )
class BengaliDatasetTest:
def __init__(self, df, img_height, img_width, mean, std):
self.image_ids = df.image_id.values
self.img_arr = df.iloc[:, 1:].values
self.aug = albumentations.Compose([
albumentations.Resize(img_height, img_width, always_apply=True),
albumentations.Normalize(mean, std, always_apply=True)
 ])
 def __len__(self):
return len(self.image_ids)
 def __getitem__(self, item):
image = self.img_arr[item, :]
img_id = self.image_ids[item]
image = image.reshape(137, 236 ).astype(float)
 image = Image.fromarray(image ).convert("RGB")
 image = self.aug(image=np.array(image)) ["image"]
image = np.transpose(image,(2, 0, 1)).astype(np.float32)
 return {
"image": torch.tensor(image, dtype=torch.float),
"image_id": img_id
}<load_pretrained>
def import_data(data):
df = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/{}.csv' 
.format(data))
return df
train = import_data('train')
 train = train.set_index('Id')
 test = import_data('test')
 ID = test[['Id']]
test = test.set_index('Id' )
model = ResNet34(pretrained=False)
 model.load_state_dict(torch.load(".. /input/resnet34weights/resnet34_fold0.pth"))
model.eval()<create_dataframe>
corrmat = train.corr()
top_corr_features = corrmat.index[abs(corrmat["SalePrice"])>0.6]
for kol in top_corr_features:
z = np.abs(stats.zscore(train[kol]))
train['z'] = z
train = train.drop('z', axis = 1 )
def model_predict() :
g_pred, v_pred, c_pred = [], [], []
img_ids_list = []
for file_idx in range(4):
df = pd.read_parquet(f".. /input/bengaliai-cv19/test_image_data_{file_idx}.parquet")
 dataset = BengaliDatasetTest(df=df,
img_height=IMG_HEIGHT,
img_width=IMG_WIDTH,
mean=MODEL_MEAN,
std=MODEL_STD)
 data_loader = torch.utils.data.DataLoader(
 dataset=dataset,
batch_size= TEST_BATCH_SIZE,
shuffle=False,
num_workers=4 
)
 for bi, d in enumerate(data_loader):
image = d["image"]
img_id = d["image_id"]
image = image.to(DEVICE, dtype=torch.float)
 g, v, c = model(image)
 for ii, imid in enumerate(img_id):
g_pred.append(g[ii].cpu().detach().numpy())
 v_pred.append(v[ii].cpu().detach().numpy())
 c_pred.append(c[ii].cpu().detach().numpy())
 img_ids_list.append(imid)
 return g_pred, v_pred, c_pred, img_ids_list<choose_model_class>
train = train[(train['GrLivArea']<4500)]
model = ResNet34(pretrained=False)
 TEST_BATCH_SIZE = 32
final_g_pred = []
final_v_pred = []
final_c_pred = []
final_img_ids = []
for i in range(5):
model.load_state_dict(torch.load(f".. /input/resnet34weights/resnet34_fold{i}.pth"))
model.to(DEVICE)
 model.eval()
g_pred, v_pred, c_pred, img_ids_list = model_predict()
final_g_pred.append(g_pred)
 final_v_pred.append(v_pred)
 final_c_pred.append(c_pred)
 if i == 0:
final_img_ids.extend(img_ids_list )<prepare_output>
df = pd.concat([train.drop(['SalePrice'], axis=1), test], axis=0 )
final_g = np.argmax(np.mean(np.array(final_g_pred), axis=0), axis=1)
 final_v = np.argmax(np.mean(np.array(final_v_pred), axis=0), axis=1)
 final_c = np.argmax(np.mean(np.array(final_c_pred), axis=0), axis=1 )<define_variables>
def null_view(df):
null_view = df[[col for col in df.columns if df[col].isna().sum() > 0]].isna().sum().sort_values(ascending = False)
 null_view = pd.DataFrame(null_view, columns=['n_nulls'])
 null_view['pct_null'] = null_view.n_nulls.apply(lambda x: str(round(( x/len(df)) *100, 1)) +'%')
 print(null_view.head(10))
null_view(test)
 print('')
 null_view(train )
predictions = []
for ii, imid in enumerate(final_img_ids):
predictions.append(( f"{imid}_grapheme_root", final_g[ii]))
predictions.append(( f"{imid}_vowel_diacritic", final_v[ii]))
predictions.append(( f"{imid}_consonant_diacritic", final_c[ii]))<create_dataframe>
df['MSSubClass'] = df['MSSubClass'].apply(str)
 df['YrSold'] = df['YrSold'].astype(str)
 df['MoSold'] = df['MoSold'].astype(str)
 commonNa = [
'MSZoning', 'Electrical',
'KitchenQual', 'Exterior1st',
'Exterior2nd', 'SaleType',
'LotFrontage', 'Functional'
]
toZero = [
'MasVnrArea', 'GarageYrBlt',
'BsmtHalfBath', 'BsmtFullBath',
'GarageArea', 'GarageCars',
'BsmtFinSF1', 'BsmtFinSF2',
'BsmtUnfSF', 'TotalBsmtSF'
]
toNoData = [
'PoolQC', 'MiscFeature',
'Fence','MasVnrType',
'FireplaceQu', 'GarageType',
'GarageFinish', 'GarageQual',
'GarageCond', 'BsmtQual',
'BsmtCond', 'BsmtExposure',
'BsmtFinType1', 'BsmtFinType2',
'Alley'
]
def fillNa_fe(df):
df['Functional'] = df['Functional'].fillna('Typ')
 for i in commonNa:
df[i] = df[i].fillna(df[i].mode() [0])
 for i in toNoData:
df[i] = df[i].fillna('None')
 for i in toZero:
df[i] = df[i].fillna(0)
 df.drop(['Utilities', 'PoolQC', 'MiscFeature'], axis=1, inplace=True)
 return df
df = fillNa_fe(df )
sub = pd.DataFrame(predictions, columns=["row_id", "target"] )<save_to_csv>
def skew_(df):
colls = [col for col in df.columns if df[col].dtype in ['int64','float']]
skews_df = [col for col in df[colls].columns if abs(df[col].skew())>.7]
def skewfix(data, data2):
for i in data2:
data[i] = np.log1p(data[i])
 return data
return skewfix(df, skews_df)
 df = skew_(df )
sub.to_csv("submission.csv",index=False )<import_modules>
def feat_eng(df):
df['totalSF'] = df['TotalBsmtSF'] \
+ df['1stFlrSF'] \
+ df['2ndFlrSF'] \
+ df['GrLivArea']
df['YrBltAndRemod']= df['YearBuilt'] + df['YearRemodAdd']
df['Porch_SF'] =(df['OpenPorchSF'] \
+ df['3SsnPorch'] \
+ df['EnclosedPorch'] \
+ df['ScreenPorch'] \
+ df['WoodDeckSF'])
 df['Total_Bathrooms'] =(df['FullBath'] \
+(0.5 * df['HalfBath'])\
+ df['BsmtFullBath'] \
+(0.5 * df['BsmtHalfBath']))
df['hpool'] = df['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 df['h2ndfloor'] = df['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 df['hgarage'] = df['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 df['hbsmt'] = df['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 df['hfireplace'] = df['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 return df
df = feat_eng(df )
import numpy as np
import pandas as pd
from tqdm.auto import tqdm
from glob import glob
import time, gc
import cv2
import matplotlib.image as mpimg
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from keras.callbacks import ReduceLROnPlateau
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import PIL.Image as Image, PIL.ImageDraw as ImageDraw, PIL.ImageFont as ImageFont
from matplotlib import pyplot as plt
import tensorflow as tf
import seaborn as sns
from keras.layers import Input
from keras.layers import Conv2D, MaxPool2D, Dense, BatchNormalization, Activation, add, GlobalAvgPool2D, GlobalAveragePooling2D, Dropout
from keras.models import Model
from keras import regularizers
from keras.utils import plot_model
from keras import backend as K
from keras import models
import os 
<load_from_csv>
encoder = LabelEncoder()
sc = StandardScaler()
def encode(df):
cat_df = [col for col in df.columns if df[col].dtype not in ['int','float']]
for col in cat_df:
df[col] = encoder.fit_transform(df[col])
 df_ = sc.fit_transform(df)
 df = pd.DataFrame(data=df_, columns = df.columns)
 return df

 df = pd.get_dummies(df)
train_df_ = pd.read_csv('/kaggle/input/bengaliai-cv19/train.csv')
 train_df_ = train_df_.drop(['grapheme'], axis=1, inplace=False)
 train_df_[['grapheme_root', 'vowel_diacritic', 'consonant_diacritic']] = train_df_[['grapheme_root', 'vowel_diacritic', 'consonant_diacritic']].astype('uint8')
 IMG_SIZE=64
N_CHANNELS=1<choose_model_class>
train = df.iloc[:train.shape[0],:]
test = df.iloc[train.shape[0]:,:]
def add_regularizers(model,
kernel_regularization_factor=1e-4,
bias_regularization_factor=1e-4,
gamma_regularization_factor=1e-4,
beta_regularization_factor=1e-4):
update_model_required = False
for layer in model.layers:
if not np.isclose(kernel_regularization_factor, 0.0)and hasattr(
 layer, "kernel_regularizer"):
update_model_required = True
layer.kernel_regularizer = regularizers.l2(kernel_regularization_factor)
 if not np.isclose(bias_regularization_factor, 0.0)and hasattr(
 layer, "bias_regularizer"):
if layer.use_bias:
update_model_required = True
layer.bias_regularizer = regularizers.l2(bias_regularization_factor)
 if not np.isclose(gamma_regularization_factor, 0.0)and hasattr(
 layer, "gamma_regularizer"):
if layer.scale:
update_model_required = True
layer.gamma_regularizer = regularizers.l2(gamma_regularization_factor)
 if not np.isclose(beta_regularization_factor, 0.0)and hasattr(
 layer, "beta_regularizer"):
if layer.center:
update_model_required = True
layer.beta_regularizer = regularizers.l2(beta_regularization_factor)
 if update_model_required:
print("Adding regularizers...")
 vanilla_weights = model.get_weights()
model = models.model_from_json(model.to_json())
 model.set_weights(vanilla_weights)
 return model<choose_model_class>
X = train.copy()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42 )
<define_variables>
import datetime
from sklearn.model_selection import RandomizedSearchCV, GridSearchCV
batch_size = 256
epochs = 2<define_variables>
print('
{}\tLightBGM' 
.format(datetime.datetime.now().strftime('%H:%M')))
 parameters = {
'objective':['regression'],
'num_leaves':[5],
'learning_rate':[0.05],
'n_estimators':[720],
'max_bin':[55],
'max_depth':[2, 3],
'bagging_fraction':[.5,.8],
'bagging_freq':[5],
'bagging_seed':[9],
'feature_fraction':[0.2319],
'feature_fraction_seed':[9]
}
light = LGBMRegressor()
clf = GridSearchCV(light, parameters, verbose=0, iid=False)
 clf.fit(X_train, y_train)
 lightgbm = LGBMRegressor(**clf.best_params_)
 print('
Regressor:
', lightgbm, '
')
 print('{}\tDone! 
.format(datetime.datetime.now().strftime('%H:%M')) )
class MultiOutputDataGenerator(ImageDataGenerator):
def flow(self,
x,
y=None,
batch_size=32,
shuffle=True,
sample_weight=None,
seed=None,
save_to_dir=None,
save_prefix='',
save_format='png',
subset=None):
targets = None
target_lengths = {}
ordered_outputs = []
for output, target in y.items() :
if targets is None:
targets = target
else:
targets = np.concatenate(( targets, target), axis=1)
 target_lengths[output] = target.shape[1]
ordered_outputs.append(output)
 for flowx, flowy in super().flow(x, targets, batch_size=batch_size,
shuffle=shuffle):
target_dict = {}
i = 0
for output in ordered_outputs:
target_length = target_lengths[output]
target_dict[output] = flowy[:, i: i + target_length]
i += target_length
yield flowx, target_dict<define_variables>
print('
{}\tXGBoost' 
.format(datetime.datetime.now().strftime('%H:%M')))
 params = {
'colsample_bytree': [0.4],
'gamma': [0.0],
'learning_rate': [0.01],
'max_depth': [3],
'min_child_weight': [2],
'n_estimators': [3460],
'seed': [36],
'subsample': [0.2],
'objective':['reg:squarederror'],
'reg_alpha':[0.00006],
'cale_pos_weight':[1],
'verbosity':[0]
}
gbm = XGBRegressor()
clf = GridSearchCV(gbm, params, verbose=0, iid=False)
 clf.fit(X_train,y_train)
 XGBoost = XGBRegressor(**clf.best_params_)
 print('
Regressor:
', XGBoost, '
')
 print('{}\tDone! 
.format(datetime.datetime.now().strftime('%H:%M')) )
HEIGHT = 137
WIDTH = 236<define_search_space>
print('
{}\tGradient Boost' 
.format(datetime.datetime.now().strftime('%H:%M')))
 parameters = {
'n_estimators':[8000,],
'learning_rate':[0.01],
'max_depth':[2],
'max_features':['sqrt'],
'min_samples_leaf':[10],
'min_samples_split':[5],
'loss':['huber'],
'random_state' :[42],
}
gbr = GradientBoostingRegressor()
clf = GridSearchCV(gbr, parameters, verbose=0, iid=False)
 clf.fit(X_train, y_train)
 gbr = GradientBoostingRegressor(**clf.best_params_)
 print('
Regressor:
', gbr, '
')
 print('{}\tDone! 
.format(datetime.datetime.now().strftime('%H:%M')) )
factors = [0.75,0.5,0.45,0.4,0.35,0.3,0.25,0.2,0.1]
learning_rate_reduction_root = ReduceLROnPlateau(monitor='dense_3_accuracy',
factor=factors[0],
patience=1,
cooldown=1,
verbose=1,
min_lr=0.000001)
<categorify>
print('
{}\tK-nearest neighbor' 
.format(datetime.datetime.now().strftime('%H:%M')))
 parameters = {
'algorithm':['auto'],
'leaf_size':[2],
'weights':['uniform', 'distance'],
'metric':['euclidean', 'manhattan'],
'n_neighbors':[10],
}
knn = KNeighborsRegressor()
clf = GridSearchCV(knn, parameters, verbose=0, iid=False)
 clf.fit(X_train, y_train)
 knn = KNeighborsRegressor(**clf.best_params_)
 print('
Regressor:
', knn, '
')
 print('{}\tDone! 
.format(datetime.datetime.now().strftime('%H:%M')) )
<choose_model_class>
print('
{}\tRandom forest' 
.format(datetime.datetime.now().strftime('%H:%M')))
 parameters = {
'bootstrap': [True],
'max_features': ['auto'],
'min_samples_leaf': [2],
'min_samples_split': [4],
'n_estimators': [1500],
'n_jobs':[-1],
'oob_score':[True]
}
r_forest = RandomForestRegressor()
clf = GridSearchCV(r_forest, parameters, verbose=0, iid=False)
 clf.fit(X_train, y_train)
 knn = RandomForestRegressor(**clf.best_params_)
 print('
Regressor:
', knn, '
')
 print('{}\tDone! 
.format(datetime.datetime.now().strftime('%H:%M')) )
model = models.load_model('/kaggle/input/10thmodel/my_model10_12.h5')
 adam = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.000002)
 model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'] )<train_model>
kfolds = KFold(n_splits=10, shuffle=True, random_state=42)
<categorify>
alphas_ridge = [
14.5, 14.6, 14.7, 14.8, 14.9, 15,
15.1, 15.2, 15.3, 15.4, 15.5, 15.6
]
ridge = make_pipeline(
 RobustScaler() ,
RidgeCV(
 alphas=alphas_ridge,
cv=kfolds 
)
)
 print('
Ridge:
',
ridge,
'
' )
<define_variables>
alphas_lasso = [
5e-05, 0.0001, 0.0002, 0.0003,
0.0004, 0.0005, 0.0006, 0.0007
]
lasso = make_pipeline(
 RobustScaler() ,
LassoCV(
 alphas=alphas_lasso,
max_iter=1e7,
random_state=42,
cv=kfolds 
)
)
 print('
Lasso:
',
lasso,
'
' )
preds_dict = {
'grapheme_root': [],
'vowel_diacritic': [],
'consonant_diacritic': []
}<predict_on_test>
alphas_ela = [
0.0001, 0.0002, 0.0003,
0.0004, 0.0005, 0.0006, 0.0007
]
e_l1ratio = [0.8, 0.85, 0.9, 0.95, 0.99, 1]
elasticnet = make_pipeline(
 RobustScaler() ,
ElasticNetCV(
 max_iter=1e7,
alphas=alphas_ela,
cv=kfolds,
l1_ratio=e_l1ratio 
)
)
 print('
Elastic:
',
elasticnet,
'
' )
components = ['consonant_diacritic', 'grapheme_root', 'vowel_diacritic']
target=[]
row_id=[]
for i in range(4):
df_test_img = pd.read_parquet('/kaggle/input/bengaliai-cv19/test_image_data_{}.parquet'.format(i))
df_test_img.set_index('image_id', inplace=True)
 X_test = resize(df_test_img, need_progress_bar=False)/255
X_test = X_test.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS)
 preds = model.predict(X_test)
 for i, p in enumerate(preds_dict):
preds_dict[p] = np.argmax(preds[i], axis=1)
 for k,id in enumerate(df_test_img.index.values):
for i,comp in enumerate(components):
id_sample=id+'_'+comp
row_id.append(id_sample)
 target.append(preds_dict[comp][k])
 del df_test_img
del X_test
gc.collect()
df_sample = pd.DataFrame(
 {
'row_id': row_id,
'target':target
},
columns = ['row_id','target'] 
)
 df_sample.to_csv('submission.csv',index=False)
 df_sample.head()<install_modules>
%%time
parameters = {
'C': [120],
'gamma': [0.0003]
}
svr_ = SVR()
clf = GridSearchCV(svr_, parameters, verbose=0, iid=False)
 clf.fit(X_train, y_train)
 svr = make_pipeline(
 RobustScaler() ,
SVR(**clf.best_params_)
)
 print('
SVR:
',
svr,
'
' )
!pip install '.. /input/kerasefficientnetb3/efficientnet-1.0.0-py3-none-any.whl' 
<define_variables>
krr = KernelRidge()
HEIGHT = 137
WIDTH = 236
FACTOR = 0.60
HEIGHT_NEW = int(HEIGHT * FACTOR)
 WIDTH_NEW = int(WIDTH * FACTOR)
 CHANNELS = 3
BATCH_SIZE = 16
DIR = '.. /input/bengaliai-cv19'<train_model>
print('Stacking: \t stack_gen')
 stack_gen = StackingCVRegressor(
 regressors=(ridge, lasso, elasticnet, lightgbm, XGBoost, gbr),
meta_regressor=XGBoost,
use_features_in_secondary=True 
)
 print('Done!
')
 print('Stacking: \t stack_gen2')
 stack_gen2 = StackingCVRegressor(
 regressors=(ridge, lasso, elasticnet, lightgbm, XGBoost, gbr),
meta_regressor=lightgbm,
use_features_in_secondary=True 
)
 print('Done!
')
 print('Stacking: \t stack_gen3')
 stack_gen3 = StackingCVRegressor(
 regressors=(ridge, lasso, elasticnet, lightgbm, XGBoost, gbr),
meta_regressor=ridge,
use_features_in_secondary=True 
)
 print('Done!' )
print(HEIGHT_NEW)
 print(WIDTH_NEW)
 def resize_image(img, WIDTH_NEW, HEIGHT_NEW):
img = 255 - img
img =(img *(255.0 / img.max())).astype(np.uint8)
 img = img.reshape(HEIGHT, WIDTH)
 image_resized = cv2.resize(img,(WIDTH_NEW, HEIGHT_NEW), interpolation = cv2.INTER_AREA)
 return image_resized.reshape(-1 )<compute_train_metric>
models = {
'svr':svr,
'KernelRidge':krr,
'Ridge':ridge,
'Lightgbm':lightgbm,
'XGBoost':XGBoost,
'GradientBoost':gbr,
'Lasso':lasso,
'Elasticnet':elasticnet,
'Random forest':r_forest,
'KNN':knn
}
def calc(model):
return int(np.sqrt(metrics.mean_squared_error(
 np.expm1(y_test), np.expm1(model.predict(X_test)))))
 liste = []
for one, two in models.items() :
print('Fitting:\t{}'.format(one))
two.fit(X_train, y_train)
 print('Done! Error:\t{}
'.format(calc(two)))
 df = pd.DataFrame(
 data=[calc(two)],
index=[one],
columns=['error'] 
)
 liste.append(df )
gm_exp = tf.Variable(3.0, dtype = tf.float32)
 def generalized_mean_pool_2d(X):
pool =(tf.reduce_mean(tf.abs(X**(gm_exp)) , axis = [1, 2], keepdims = False)+ 1.e-7)**(1./gm_exp)
 return pool<choose_model_class>
resList = pd.concat(liste, axis=0 )
def create_model(input_shape):
input = Input(shape = input_shape)
 x_model = efn.EfficientNetB3(weights = None, include_top = False, input_tensor = input, pooling = None, classes = None)
 for layer in x_model.layers:
layer.trainable = True
lambda_layer = Lambda(generalized_mean_pool_2d)
 lambda_layer.trainable_weights.extend([gm_exp])
 x = lambda_layer(x_model.output)
 grapheme_root = Dense(168, activation = 'softmax', name = 'root' )(x)
 vowel_diacritic = Dense(11, activation = 'softmax', name = 'vowel' )(x)
 consonant_diacritic = Dense(7, activation = 'softmax', name = 'consonant' )(x)
 model = Model(inputs = x_model.input, outputs = [grapheme_root, vowel_diacritic, consonant_diacritic])
 return model<choose_model_class>
print('stack_gen: \t fitting...')
 stack_gen_model = stack_gen.fit(np.array(X_test), np.array(y_test))
sg_error = int(np.sqrt(metrics.mean_squared_error(np.expm1(y_test), np.expm1(stack_gen_model.predict(np.array(X_test))))))
print('stack_gen: \t done!')
 print('error:\t\t', sg_error, '
')
 print('stack_gen2: \t fitting...')
 stack_gen_model2 = stack_gen2.fit(np.array(X_test), np.array(y_test))
sg_error2 = int(np.sqrt(metrics.mean_squared_error(np.expm1(y_test), np.expm1(stack_gen_model2.predict(np.array(X_test))))))
print('stack_gen2: \t done!')
 print('error:\t\t', sg_error2, '
')
 print('stack_gen3: \t fitting...')
 stack_gen_model3 = stack_gen3.fit(np.array(X_test), np.array(y_test))
sg_error3 = int(np.sqrt(metrics.mean_squared_error(np.expm1(y_test), np.expm1(stack_gen_model3.predict(np.array(X_test))))))
print('stack_gen3: \t done!')
 print('error:\t\t', sg_error3)
 sg = pd.DataFrame(index=['stack_gen'], data=sg_error, columns=['error'])
 sg2 = pd.DataFrame(index=['stack_gen2'], data=sg_error2, columns=['error'])
 sg3 = pd.DataFrame(index=['stack_gen3'], data=sg_error3, columns=['error'] )
model1 = create_model(input_shape =(HEIGHT_NEW, WIDTH_NEW, CHANNELS))
model2 = create_model(input_shape =(HEIGHT_NEW, WIDTH_NEW, CHANNELS))
model3 = create_model(input_shape =(HEIGHT_NEW, WIDTH_NEW, CHANNELS))
model4 = create_model(input_shape =(HEIGHT_NEW, WIDTH_NEW, CHANNELS))
model5 = create_model(input_shape =(HEIGHT_NEW, WIDTH_NEW, CHANNELS))<load_pretrained>
resList = resList.append(sg)
 resList = resList.append(sg2)
 resList = resList.append(sg3 )
model1.load_weights('.. /input/kerasefficientnetb3/Train1_model_59.h5')
 model2.load_weights('.. /input/kerasefficientnetb3/Train1_model_64.h5')
 model2.load_weights('.. /input/kerasefficientnetb3/Train1_model_66.h5')
 model3.load_weights('.. /input/kerasefficientnetb3/Train1_model_68.h5')
 model5.load_weights('.. /input/kerasefficientnetb3/Train1_model_70.h5' )<define_variables>
def blend(data):
return(
(0.08 * ridge.predict(data)) + 
(0.11 * lightgbm.predict(data)) + 
(0.13 * XGBoost.predict(data)) + 
(0.10 * gbr.predict(data)) + 
(0.15 * stack_gen_model.predict(np.array(data)))+ 
(0.16 * stack_gen_model2.predict(np.array(data)))+ 
(0.16 * stack_gen_model3.predict(np.array(data)))+ 
(0.06 * lasso.predict(data)) + 
(0.05 * elasticnet.predict(data)) 
)
 blended_predictions = blend(test )
class TestDataGenerator(keras.utils.Sequence):
def __init__(self, X, batch_size = 16, img_size =(512, 512, 3), *args, **kwargs):
self.X = X
self.indices = np.arange(len(self.X))
self.batch_size = batch_size
self.img_size = img_size
def __len__(self):
return int(ceil(len(self.X)/ self.batch_size))
def __getitem__(self, index):
indices = self.indices[index*self.batch_size:(index+1)*self.batch_size]
X = self.__data_generation(indices)
 return X
def __data_generation(self, indices):
X = np.empty(( self.batch_size, *self.img_size))
for i, index in enumerate(indices):
image = self.X[index]
image = np.stack(( image,)*CHANNELS, axis=-1)
 image = image.reshape(-1, HEIGHT_NEW, WIDTH_NEW, CHANNELS)
 X[i,] = image
return X<define_variables>
x = blend(X_test)
 bl = pd.DataFrame(
 index=['blend'],
data=int(np.sqrt(metrics.mean_squared_error(np.expm1(y_test), np.expm1(x)))) ,
columns=['error'] 
)
 resList = resList.append(bl )
<save_to_csv><EOS> 
res = pd.DataFrame(data=np.expm1(blend(test)) , index=ID.Id, columns=['SalePrice'])
 q1 = res['SalePrice'].quantile(0.005)
 q2 = res['SalePrice'].quantile(0.995)
 res['SalePrice'] = res['SalePrice'].apply(lambda x: x if x > q1 else x*0.78)
 res['SalePrice'] = res['SalePrice'].apply(lambda x: x if x < q2 else x*1.10)
 res.to_csv('1_submission.csv' )