Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<prepare_output>
%matplotlib inline
color = sns.color_palette()
sns.set_style("darkgrid")
 def ignore_warn(*args, **kwargs):
pass
warnings.warn = ignore_warn
pd.set_option(
 "display.float_format", lambda x: f"{x:.3f}" 
)
 print(
 check_output(["ls", ".. /input"] ).decode("utf8")
 )
meta_df = pd.DataFrame(data)
 meta_df.head()<groupby>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
meta_df.groupby(by='dataset')[['width', 'height']].aggregate(['min', 'max'] )<merge>
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(f"all_data size is : {all_data.shape}" )
duplicates = meta_df.groupby(by='hash')[['image_id']].count().reset_index()
duplicates = duplicates[duplicates['image_id'] > 1]
duplicates.reset_index(drop=True, inplace=True)
 duplicates = duplicates.merge(meta_df[['image_id', 'hash']], on='hash')
 duplicates.head(20 )<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 
)[:30]
missing_data = pd.DataFrame({"Missing Ratio": all_data_na})
 missing_data.head(20 )
def prepare_train(train_paths, train_labels):
data =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 return data
def prepare_val(val_paths, val_labels):
data =(
 tf.data.Dataset 
.from_tensor_slices(( val_paths, val_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 return data
def prepare_test(test_paths):
data =(
 tf.data.Dataset 
.from_tensor_slices(( test_paths)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
)
 return data<normalization>
all_data["PoolQC"] = all_data["PoolQC"].fillna("None" )
def decode_image(filename, label=None, image_size=(IMG_SIZE_h, IMG_SIZE_w)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_augment(image, label=None, seed=5050):
image = tf.image.random_flip_left_right(image, seed=seed)
 image = tf.image.random_flip_up_down(image, seed=seed)
 image = tf.image.random_crop(image,size=[IMG_SIZE_h,IMG_SIZE_w,3],seed=seed)
 image = tf.image.random_brightness(image,max_delta=0.5, seed=seed)
 if label is None:
return image
else:
return image, label<concatenate>
all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None" )
def outer_product(x):
phi_I = tf.einsum('ijkm,ijkn->imn',x[0],x[1])
 phi_I = tf.reshape(phi_I,[-1,x[0].shape[3]*x[1].shape[3]])
 size1 = int(x[1].shape[1])
 size2 = int(x[1].shape[2])
 phi_I = tf.divide(phi_I, size1*size2)
 y_ssqrt = tf.multiply(tf.sign(phi_I),tf.sqrt(tf.abs(phi_I)+1e-12))
z_l2 = tf.nn.l2_normalize(y_ssqrt, axis=1)
 return z_l2<compute_test_metric>
all_data["Alley"] = all_data["Alley"].fillna("None" )
def f1(y_true, y_pred):
def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))<choose_model_class>
all_data["Fence"] = all_data["Fence"].fillna("None" )
def get_model() :
input_tensor = Input(shape=(IMG_SIZE_h,IMG_SIZE_w,3))
model1 = efn.EfficientNetB3(weights='imagenet', include_top=False, input_tensor=input_tensor,input_shape=(IMG_SIZE_h, IMG_SIZE_w, 3))
model2 = efn.EfficientNetB3(weights='noisy-student', include_top=False, input_tensor=input_tensor,input_shape=(IMG_SIZE_h, IMG_SIZE_w, 3))
for i, layer in enumerate(model1.layers):
layer._name = 'model1_' + layer.name
last_layer1 = model1.get_layer('model1_top_conv')
 last_output1 = last_layer1.output
for i, layer in enumerate(model2.layers):
layer._name = 'model2_' + layer.name
last_layer2 = model2.get_layer('model2_top_conv')
 last_output2 = last_layer2.output
model1_ = Model(inputs=model1.input, outputs=last_output1)
 model2_ = Model(inputs=model2.input, outputs=last_output2)
 model1_.compile(Adam(lr=0.0003, decay=1e-3),loss=categorical_focal_loss_with_label_smoothing(gamma=2.0, alpha=0.75, ls=0.125, classes=4.0))
model2_.compile(Adam(lr=0.0003, decay=1e-3),loss=categorical_focal_loss_with_label_smoothing(gamma=2.0, alpha=0.75, ls=0.125, classes=4.0))
d1=model1_.output
d2=model2_.output
bilinear = Lambda(outer_product, name='outer_product1' )([d1,d2])
 predictions=Dense(4, activation='softmax', name='predictions' )(bilinear)
 model = Model(inputs=model1.input, outputs=predictions)
 opt = Adam(lr=0.0003, decay=1e-3)
 model.compile(optimizer=opt, loss=categorical_focal_loss_with_label_smoothing(gamma=2.0, alpha=0.75, ls=0.125, classes=4.0),metrics=[f1,'categorical_accuracy'])
 return model<choose_model_class>
all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None" )
def Callbacks() :
erl = EarlyStopping(monitor='val_loss', patience=10, verbose=1, mode='min',
restore_best_weights=True)
 rdc = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=2, verbose=1, mode='min')
 return [erl,rdc]<train_model>
all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median())
 )
%%time
skf = StratifiedKFold(n_splits=FOLDS, shuffle=True, random_state=SEED)
 test_pred = []
val_roc_auc = []
for i,(train_idx, val_idx)in enumerate(skf.split(train_paths, train_labels.argmax(1))):
print() ; print('
print('
print('
X_train, X_val = train_paths[train_idx], train_paths[val_idx]
y_train, y_val = train_labels[train_idx], train_labels[val_idx]
strategy = TPU()
with strategy.scope() :
model = get_model()
history = model.fit(
 prepare_train(X_train,y_train),
steps_per_epoch=y_train.shape[0] // BATCH_SIZE,
validation_data=prepare_val(X_val, y_val),
validation_steps=y_val.shape[0] // BATCH_SIZE,
callbacks=Callbacks() ,
epochs=EPOCHS,
verbose=1 
)
 test_pred.append(model.predict(prepare_test(test_paths), verbose=1))
val_roc_auc.append(roc_auc_score(y_val,model.predict(prepare_val(X_val, y_val), verbose=1)) )<load_pretrained>
for col in("GarageType", "GarageFinish", "GarageQual", "GarageCond"):
all_data[col] = all_data[col].fillna("None" )
model_json = model.to_json()
with open("Model.json", "w")as json_file:
json_file.write(model_json)
 model.save_weights("Model.h5" )<load_pretrained>
for col in("GarageYrBlt", "GarageArea", "GarageCars"):
all_data[col] = all_data[col].fillna(0 )
json_file = open('Model.json', 'r')
 loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
 loaded_model.load_weights("Model.h5")
<define_variables>
for col in(
 "BsmtFinSF1",
"BsmtFinSF2",
"BsmtUnfSF",
"TotalBsmtSF",
"BsmtFullBath",
"BsmtHalfBath", 
):
all_data[col] = all_data[col].fillna(0 )
all_test = 0
for i in range(FOLDS):
all_test += test_pred[i]<predict_on_test>
for col in("BsmtQual", "BsmtCond", "BsmtExposure", "BsmtFinType1", "BsmtFinType2"):
all_data[col] = all_data[col].fillna("None" )
<define_variables>
all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0 )
all_models = all_test/FOLDS
all_models<load_from_csv>
all_data["MSZoning"] = all_data["MSZoning"].fillna(all_data["MSZoning"].mode() [0] )
sumb = pd.read_csv('.. /input/plant-pathology-2020-fgvc7/sample_submission.csv')
 sumb.iloc[:,1:] = all_models
sumb.head()<save_to_csv>
all_data = all_data.drop(["Utilities"], axis=1 )
sumb.to_csv('submission.csv', index=False)
 pd.Series(np.argmax(sumb[cols].values,axis=1)).value_counts()<define_variables>
all_data["Functional"] = all_data["Functional"].fillna("Typ" )
def preprocess(df,test=False):
paths = df.image_id.apply(lambda x: GCS_DS_PATH + '/images/' + x + '.jpg' ).values
labels = df.loc[:,'healthy':].values
if test==False:
return paths,labels
else:
return paths<save_to_csv>
all_data["Electrical"] = all_data["Electrical"].fillna(all_data["Electrical"].mode() [0] )
TTA = 4
test_pred_tta = np.zeros(( len(test_df),4))
for i in range(TTA):
test_dataset_tta =(tf.data.Dataset 
.from_tensor_slices(preprocess(test_df,test=True)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.batch(BATCH_SIZE))
test_pred_tta += model.predict(test_dataset_tta, verbose=1)
 sumb = pd.read_csv(DIR_INPUT + '/sample_submission.csv')
 sumb[['healthy', 'multiple_diseases', 'rust', 'scab']] = test_pred_tta/TTA
sumb.to_csv('submission_TTA.csv', index=False)
 pd.Series(np.argmax(sumb[cols].values,axis=1)).value_counts()<load_from_csv>
all_data["KitchenQual"] = all_data["KitchenQual"].fillna(all_data["KitchenQual"].mode() [0] )
sub1 = pd.read_csv('.. /input/average-efficientnet/submission.csv')
 sub1.head()<load_from_csv>
all_data["Exterior1st"] = all_data["Exterior1st"].fillna(all_data["Exterior1st"].mode() [0])
 all_data["Exterior2nd"] = all_data["Exterior2nd"].fillna(all_data["Exterior2nd"].mode() [0] )
sub2 = pd.read_csv('.. /input/classification-densenet201-efficientnetb7/submission.csv')
 sub2.head()<load_from_csv>
all_data["SaleType"] = all_data["SaleType"].fillna(all_data["SaleType"].mode() [0] )
sub3 = pd.read_csv('.. /input/tf-zoo-models-on-tpu/submission.csv')
 sub3.head()<load_from_csv>
all_data["MSSubClass"] = all_data["MSSubClass"].fillna("None" )
sub4 = pd.read_csv('.. /input/fork-of-plant-2020-tpu-915e9c/submission.csv')
 sub4.head()<load_from_csv>
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()
sub = pd.read_csv('.. /input/plant-pathology-2020-fgvc7/sample_submission.csv')
 sub.head()<feature_engineering>
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 )
sub.healthy =(sub1.healthy + sub2.healthy + sub3.healthy + sub4.healthy)/4<define_variables>
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(f"Shape all_data: {all_data.shape}" )
sub.multiple_diseases =(sub1.multiple_diseases + sub2.multiple_diseases + sub3.multiple_diseases + sub4.multiple_diseases)/4<feature_engineering>
all_data["TotalSF"] = all_data["TotalBsmtSF"] + all_data["1stFlrSF"] + all_data["2ndFlrSF"]
sub.rust =(sub1.rust + sub2.rust + sub3.rust + sub4.rust)/4<save_to_csv>
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)
sub.to_csv('submission.csv', index=False )<save_to_csv>
all_data = pd.get_dummies(all_data)
 print(all_data.shape )
sub.to_csv('submission.csv', index=False )<define_variables>
train = all_data[:ntrain]
test = all_data[ntrain:]
OUTPUT_DIR = './'
MODEL_DIR = '.. /input/resnextv2/'
TRAIN_PATH = '.. /input/cassava-leaf-disease-classification/train_images'
TEST_PATH = '.. /input/cassava-leaf-disease-classification/test_images'<define_variables>
from sklearn.linear_model import ElasticNet, Lasso
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
class CFG:
debug=False
num_workers=4
model_name='resnext50_32x4d'
size=512
batch_size=32
seed=42
target_size=5
target_col='label'
n_fold=5
trn_fold=[0, 1, 2, 3, 4]
train=False
inference=True<normalization>
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
class TestDataset(Dataset):
def __init__(self, df, transform=None):
self.df = df
self.file_names = df['image_id'].values
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
file_name = self.file_names[idx]
file_path = f'{TEST_PATH}/{file_name}'
image = cv2.imread(file_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image 
<choose_model_class>
lasso = make_pipeline(RobustScaler() , Lasso(alpha=0.0005, random_state=1))
class CustomResNext(nn.Module):
def __init__(self, model_name='resnext50_32x4d', pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=pretrained)
 n_features = self.model.fc.in_features
self.model.fc = nn.Linear(n_features, CFG.target_size)
 def forward(self, x):
x = self.model(x)
 return x<predict_on_test>
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=0.9, random_state=3))
def inference(model, states, test_loader, device):
model.to(device)
 tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for state in states:
model.load_state_dict(state['model'])
 model.eval()
with torch.no_grad() :
y_preds = model(images)
 avg_preds.append(y_preds.softmax(1 ).to('cpu' ).numpy())
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs<load_from_csv>
KRR = KernelRidge(alpha=0.6, kernel="polynomial", degree=2, coef0=2.5 )
test = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 test.head()<save_to_csv>
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,
)
model = CustomResNext(CFG.model_name, pretrained=False)
 states = [torch.load(MODEL_DIR+f'{CFG.model_name}_fold{fold}_best.pth')for fold in CFG.trn_fold]
test_dataset = TestDataset(test, transform=get_transforms(data='valid'))
test_loader = DataLoader(test_dataset, batch_size=CFG.batch_size, shuffle=False,
num_workers=CFG.num_workers, pin_memory=True)
 predictions = inference(model, states, test_loader, device)
 test['label'] = predictions.argmax(1)
 test[['image_id', 'label']].to_csv(OUTPUT_DIR+'submission.csv', index=False)
 test.head()<import_modules>
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,
)
print("Tensorflow version " + tf.__version__ )<define_variables>
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,
)
AUTOTUNE = tf.data.experimental.AUTOTUNE
GCS_PATH = ".. /input/cassava-leaf-disease-classification"
GCS_PATH_STRATIFICATED =".. /input/cassava-recreate-stratificated-tfrecords"
REPLICAS = strategy.num_replicas_in_sync
BATCH_SIZE = 32
AUG_BATCH = BATCH_SIZE
IMAGE_SIZE = [512, 512]
DIM = IMAGE_SIZE[0]
CLASSES = ['0', '1', '2', '3', '4']
EPOCHS = 20
FOLDS = 5
debug = True
TTA = 1
WGTS = [1/FOLDS]*FOLDS
calc_oof = False<load_from_csv>
score = rmsle_cv(lasso)
 print(f"
Lasso score: {score.mean() :.4f}({score.std() :.4f})
 " )
test_df = pd.read_csv(GCS_PATH + '/sample_submission.csv')
 train_df = pd.read_csv(GCS_PATH + '/train.csv' )<sort_values>
score = rmsle_cv(ENet)
 print(f"ElasticNet score: {score.mean() :.4f}({score.std() :.4f})
 " )
files_test = np.sort(np.array(tf.io.gfile.glob(GCS_PATH + '/test_tfrecords/*.tfrec')) )<define_variables>
score = rmsle_cv(KRR)
 print(f"Kernel Ridge score: {score.mean() :.4f}({score.std() :.4f})
 " )
ROT_ = 180.0
SHR_ = 2.0
HZOOM_ = 8.0
WZOOM_ = 8.0
HSHIFT_ = 8.0
WSHIFT_ = 8.0<normalization>
score = rmsle_cv(GBoost)
 print(f"Gradient Boosting score: {score.mean() :.4f}({score.std() :.4f})
 " )
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 def get_3x3_mat(lst):
return tf.reshape(tf.concat([lst],axis=0), [3,3])
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = get_3x3_mat([c1, s1, zero,
-s1, c1, zero,
zero, zero, one])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = get_3x3_mat([one, s2, zero,
zero, c2, zero,
zero, zero, one])
 zoom_matrix = get_3x3_mat([one/height_zoom, zero, zero,
zero, one/width_zoom, zero,
zero, zero, one])
 shift_matrix = get_3x3_mat([one, zero, height_shift,
zero, one, width_shift,
zero, zero, one])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform_mat(image, DIM=IMAGE_SIZE[0]):
XDIM = DIM%2
rot = ROT_ * tf.random.normal([1], dtype='float32')
 shr = SHR_ * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ HZOOM_
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ WZOOM_
h_shift = HSHIFT_ * tf.random.normal([1], dtype='float32')
 w_shift = WSHIFT_ * tf.random.normal([1], dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])
 z = tf.ones([DIM*DIM], dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d, [DIM, DIM,3] )<data_type_conversions>
score = rmsle_cv(model_xgb)
 print(f"Xgboost score: {score.mean() :.4f}({score.std() :.4f})
 " )
def to_float32(image, label):
return tf.cast(image, tf.float32), label<categorify>
score = rmsle_cv(model_lgb)
 print(f"LGBM score: {score.mean() :.4f}({score.std() :.4f})
 " )
def decode_image(image):
image = tf.image.decode_jpeg(image, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image<split>
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 )
def read_labeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['target']<categorify>
averaged_models = AveragingModels(models=(ENet, GBoost, KRR, lasso))
score = rmsle_cv(averaged_models)
 print(f" Averaged base models score: {score.mean() :.4f}({score.std() :.4f})
 " )
def read_tfrecord(example, labeled):
tfrecord_format = {
"image": tf.io.FixedLenFeature([], tf.string),
"target": tf.io.FixedLenFeature([], tf.int64)
 } if labeled else {
"image": tf.io.FixedLenFeature([], tf.string),
"image_name": tf.io.FixedLenFeature([], tf.string)
 }
example = tf.io.parse_single_example(example, tfrecord_format)
 image = decode_image(example['image'])
 if labeled:
label = tf.cast(example['target'], tf.int32)
 return image, label
idnum = example['image_name']
return image, idnum<split>
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 )
def read_unlabeled_tfrecord(example, return_image_name):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['image_name'] if return_image_name else '0'<create_dataframe>
stacked_averaged_models = StackingAveragedModels(
 base_models=(ENet, GBoost, KRR), meta_model=lasso 
)
 score = rmsle_cv(stacked_averaged_models)
 print(f"Stacking Averaged models score: {score.mean() :.4f}({score.std() :.4f})" )
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTOTUNE)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(partial(read_tfrecord, labeled=labeled), num_parallel_calls=AUTOTUNE)
 return dataset<define_variables>
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
TRAINING_FILENAMES = tf.io.gfile.glob('.. /input/cassava-leaf-disease-classification/train_tfrecords/' + '*train*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob('.. /input/cassava-leaf-disease-classification/test_tfrecords/' + 'ld_test*.tfrec' )<define_variables>
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))
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_TRAINING_IMAGES = int(count_data_items(TRAINING_FILENAMES)*(FOLDS-1.) /FOLDS)
 NUM_VALIDATION_IMAGES = int(count_data_items(TRAINING_FILENAMES)*(1./FOLDS))
NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
print('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<categorify>
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))
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 return image, label<create_dataframe>
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))
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTOTUNE)
 return dataset<groupby>

 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))
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n )<categorify>
ensemble = stacked_pred * 0.70 + xgb_pred * 0.15 + lgb_pred * 0.15
<categorify><EOS> 
sub = pd.DataFrame()
sub["Id"] = test_ID
sub["SalePrice"] = ensemble
sub.to_csv("submission.csv", index=False )