Kaggle competition: House Prices - Advanced Regression Techniques
def compile_models(models):
for x in models:
models[x].compile(
 optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3),
loss = tf.keras.losses.SparseCategoricalCrossentropy() ,
metrics = ['sparse_categorical_accuracy'], 
)
 models[x].summary()
print('')
 models = {}
with strategy.scope() :
start = time.time()
models['efn7'] = create_efficientnet(7)
 print(f'Created efn7 in {(time.time() - start):.4f}s')
 start = time.time()
models['efn6'] = create_efficientnet(6)
 print(f'Created efn6 in {(time.time() - start):.4f}s')
 start = time.time()
models['efn4-noisy'] = create_efficientnet(4, 'noisy-student')
 print(f'Created efn4 in {(time.time() - start):.4f}s')
 start = time.time()
models['densenet'] = create_densenet()
print(f'Created densenet in {(time.time() - start):.4f}s')
 compile_models(models )<train_model>
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import skew
from sklearn.linear_model import ElasticNet
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from itertools import product
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.metrics import mean_squared_error
train_ds = DS(TRAINING_FILES, augment=True, shuffle=True, batch_size=BATCH_SIZE, repeat=True)
 val_ds = DS(VALIDATION_FILES, batch_size=BATCH_SIZE)
 val_aug_ds = DS(VALIDATION_FILES, augment=True, batch_size=BATCH_SIZE)
 test_ds = DS(TEST_FILES, training=False, batch_size=BATCH_SIZE)
 print(f'Training: {train_ds.count() } images, Validation: {val_ds.count() }, Test: {test_ds.count() } images' )<init_hyperparams>
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )
lr_start = 0.00005
lr_max = 0.0000125 * strategy.num_replicas_in_sync
lr_min = 0.0000001
lr_ramp_ep = 5
lr_sus_ep = 0
lr_decay = 0.8
def lrfn(epoch):
if epoch < lr_ramp_ep:
lr =(lr_max - lr_start)/ lr_ramp_ep * epoch + lr_start
elif epoch < lr_ramp_ep + lr_sus_ep:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_ramp_ep - lr_sus_ep)+ lr_min
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 count = range(100)
 plt.figure(figsize=(12, 6))
plt.subplot(1, 3, 1)
 plt.plot(count, [lrfn(x)for x in count])
 cosine_decay_learning_rate = tf.keras.experimental.CosineDecayRestarts(
 0.001, 0.8, t_mul=2.0, m_mul=1.0, alpha=0.0,
name=None 
)
<train_model>
features = ['GrLivArea','OverallQual','2ndFlrSF','TotalBsmtSF','1stFlrSF','GarageCars','YearBuilt', 'GarageArea','ExterQual',
'YearRemodAdd','FireplaceQu','GarageYrBlt','FullBath','MSSubClass','LotArea','Fireplaces','KitchenQual','MSZoning',
'GarageType','BsmtFinSF1','Neighborhood','BsmtQual','TotRmsAbvGrd','HalfBath','BldgType','GarageFinish',
'Foundation','BedroomAbvGr','HouseStyle','CentralAir','OpenPorchSF','HeatingQC','BsmtFinType1','BsmtUnfSF',
'GarageCond','GarageQual','KitchenAbvGr','OverallCond','BsmtCond','MasVnrArea','BsmtFullBath','Exterior1st',
'Exterior2nd','PavedDrive','WoodDeckSF','LandContour','MasVnrType','BsmtFinType2','Functional','Fence']
class Visibility(tf.keras.callbacks.Callback):
def __init__(self):
super(Visibility, self ).__init__()
self.metrics = ['sparse_categorical_accuracy', 'loss']
def on_train_begin(self, logs=None):
self.train_begin = time.time()
def on_train_end(self, logs=None):
print('
Training took ', time.time() - self.train_begin, 'seconds')
 def on_epoch_end(self, epoch, logs=None):
output = ""
for metric in self.metrics:
diff =(( logs['val_'+metric] / logs[metric])- 1)*100
output += f"val/{metric}: {diff:.2f}% "
print("
" + output)
 def on_train_batch_begin(self, batch, logs=None):
print("▪", end="")
 def on_test_batch_end(self, batch, logs=None):
print("▫", end="")
<train_model>
train = train[features+['SalePrice']]
test = test[['Id']+features]
all_data = pd.concat(( train.loc[:,'GrLivArea':'Fence'],
test.loc[:,'GrLivArea':'Fence']))
EPOCHS = 50
history = {}
def fit_models(models, history):
for x in models:
print(f'Starting training for {x}')
 hist = models[x].fit(
 train_ds.data() ,
validation_data = val_ds.data() ,
epochs = EPOCHS,
steps_per_epoch = train_ds.count() // BATCH_SIZE,
verbose=2,
callbacks=[
tf.keras.callbacks.ModelCheckpoint(
 monitor='val_sparse_categorical_accuracy', verbose=2, save_best_only=True, save_weights_only=True, mode='max', filepath=f'{x}.h5'),
lr_callback,
tf.keras.callbacks.EarlyStopping(
 monitor='val_sparse_categorical_accuracy', min_delta=0.001, patience=3, verbose=2, mode='max', baseline=None, restore_best_weights=True 
),
Visibility() ,
], 
)
 history[x] = hist
fit_models(models, history )<predict_on_test>
train['SalePrice'] = np.log1p(train['SalePrice'])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = train[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
all_data[skewed_feats] = np.log1p(all_data[skewed_feats] )
highest_f1 = 0
highest_f1_model = ''
preds = list()
def get_y_true() :
def get_val_classes() :
all_classes = np.array([])
 for examples in val_ds.data() :
all_classes = np.concatenate([all_classes, examples[1].numpy() ])
 return all_classes
y_true = get_val_classes()
print('True Values:: Length:', len(y_true), ', List:', y_true)
 return y_true
def predict_models(models, y_true):
global highest_f1
global highest_f1_model
global preds
val_tpu_ds = val_aug_ds.data().map(lambda image, ids: image)
 for x in models:
val_aug_count = 2
y_val_pred = np.zeros(( 3712,104), dtype=np.float64)
 for i in range(val_aug_count):
y_val_loop = models[x].predict(val_tpu_ds, verbose=1)
 y_val_loop = tf.cast(y_val_loop, tf.float32)
 y_val_pred += y_val_loop.numpy()
y_val_pred /= val_aug_count
y_val = np.argmax(y_val_pred, axis=-1 ).astype(np.float32)
 print(f'
Scores for {x}
')
 print(f'{x}: Pred Values:: Length:{len(y_val)} List: {y_val}')
 model_f1_score = f1_score(y_true, y_val, average='macro', zero_division=0)*100
if model_f1_score > highest_f1:
highest_f1 = model_f1_score
highest_f1_model = x
scores = {
"metrics": [
"F1",
"Precision",
"Recall",
],
"scores": [
model_f1_score,
precision_score(y_true, y_val, average='macro', zero_division=0)*100,
recall_score(y_true, y_val, average='macro', zero_division=0)*100,
]
}
print(pd.DataFrame.from_dict(scores))
preds.append((
x,
y_val_pred,
model_f1_score, 
))
y_true = get_y_true()
predict_models(models, y_true )<compute_test_metric>
for feature in all_data.select_dtypes(exclude=['object'] ).columns:
all_data[feature].fillna(all_data[feature].median() , inplace = True)
 for feature in all_data.select_dtypes(include=['object'] ).columns:
all_data[feature].fillna(all_data[feature].value_counts().idxmax() , inplace = True )
def get_best_alpha(pred1, pred2):
best_alpha = 0
best_f1 = 0
for x in np.linspace(0, 1, 101):
mixed =(pred1 * x)+(pred2 *(1-x))
y_mixed = np.argmax(mixed, axis=-1 ).astype(np.float32)
 mixed_f1 = f1_score(y_true, y_mixed, average='macro', zero_division=0)*100
if mixed_f1 > best_f1:
best_f1 = mixed_f1
best_alpha = x
return best_alpha, best_f1
def get_alpha(preds, y_true):
output = list()
preds = np.array(preds)
 preds = preds[preds[:,2].argsort() [::-1]]
model_name = preds[0][0]
predictions = preds[0][1]
model_f1 = preds[0][2]
output.append(( model_name, 1))
best_predictions = predictions
best_f1 = model_f1
print(best_f1, output)
 for i in range(1, len(preds)) :
i_name = preds[i][0]
i_preds = preds[i][1]
i_f1 = preds[i][1]
alpha, f1 = get_best_alpha(best_predictions, i_preds)
 if f1 > best_f1:
best_predictions =(alpha * i_preds)+(( 1-alpha)* best_predictions)
 best_f1 = f1
output.append(( i_name, alpha))
print(best_f1, output)
 return output
alpha_chain = get_alpha(preds, y_true )<compute_test_metric>
all_data['TotalSF'] =(all_data['TotalBsmtSF']
+ all_data['1stFlrSF']
+ all_data['2ndFlrSF'])
 all_data['YrBltAndRemod'] = all_data['YearBuilt'] + all_data['YearRemodAdd']
all_data['Total_sqr_footage'] =(all_data['BsmtFinSF1']
+ all_data['1stFlrSF']
+ all_data['2ndFlrSF'] 
)
 all_data['Total_Bathrooms'] =(all_data['FullBath']
+(0.5 * all_data['HalfBath'])
 + all_data['BsmtFullBath'] 
)
 all_data['Total_porch_sf'] =(all_data['OpenPorchSF']
+ all_data['WoodDeckSF']
)
print(f'{highest_f1_model} has the highest f1 score - {highest_f1:0.5f}' )<predict_on_test>
all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']
ds_submission = DS(TEST_FILES, training=False, augment=True, batch_size=BATCH_SIZE ).data()
ds_submission = ds_submission.map(lambda image, ids: image)
 def get_test_ids() :
all_ids = np.array([])
 for examples in test_ds.data() :
all_ids = np.concatenate([all_ids, examples[1].numpy().astype('U')])
 return all_ids
def get_prediction_with_tta(model, ds_submission):
tta = 5
y_pred = np.zeros(( 7382,104), dtype=np.float64)
 for x in range(tta):
y_pred_loop = model.predict(ds_submission, verbose=2)
 y_pred_loop = tf.cast(y_pred_loop, tf.float32)
 y_pred += y_pred_loop.numpy()
y_pred /= tta
return y_pred
def create_submission(alpha_chain, models, ds_submission):
y_pred = np.zeros(( 7382,104), dtype=np.float64)
 for x in alpha_chain:
model = x[0]
alpha = x[1]
print('Predicting with', model, 'with alpha', alpha)
 y_pred_chain = get_prediction_with_tta(models[model], ds_submission)
 y_pred =(alpha * y_pred_chain)+(( 1-alpha)* y_pred)
 y_pred = np.argmax(y_pred, axis=-1)
 filename = f'submission.csv'
np.savetxt(
 filename,
np.column_stack(( get_test_ids() , y_pred)) ,
fmt=('%s', '%s'),
delimiter=',',
header='id,label',
comments='' 
)
 create_submission(alpha_chain, models, ds_submission )<install_modules>
print(all_data.shape )
!pip install -q tensorflow==2.3.0
!pip install -q git+https://github.com/keras-team/keras-tuner@master
!pip install -q cloud-tpu-client<import_modules>
X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
y = train.SalePrice
print('Tensorflow version ' + tf.__version__)
<set_options>
categoricals = train.select_dtypes(include=['object'] ).columns
for c in categoricals:
for cell in X_train[c].unique().tolist() :
num = X_train[c].value_counts() [cell]
X_train[c] = X_train[c].replace({cell: num} )
try:
c = Client()
c.configure_tpu_version(tf.__version__, restart_type='ifNeeded')
 tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<define_variables>
X_train[categoricals]
IMAGE_SIZE = [331, 331]
EPOCHS_SEARCH = 10
EPOCHS_FINAL = 20
SEED = 123
BATCH_SIZE = 32 * strategy.num_replicas_in_sync<define_variables>
X_train[X_train.select_dtypes(exclude=['object'] ).columns]
GCS_DS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')
 GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec' )<define_variables>
CLASSES = ['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'wild geranium', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood', 'globe thistle',
'snapdragon', "colt's foot", 'king protea', 'spear thistle', 'yellow iris', 'globe-flower', 'purple coneflower', 'peruvian lily', 'balloon flower', 'giant white arum lily',
'fire lily', 'pincushion flower', 'fritillary', 'red ginger', 'grape hyacinth', 'corn poppy', 'prince of wales feathers', 'stemless gentian', 'artichoke', 'sweet william',
'carnation', 'garden phlox', 'love in the mist', 'cosmos', 'alpine sea holly', 'ruby-lipped cattleya', 'cape flower', 'great masterwort', 'siam tulip', 'lenten rose',
'barberton daisy', 'daffodil', 'sword lily', 'poinsettia', 'bolero deep blue', 'wallflower', 'marigold', 'buttercup', 'daisy', 'common dandelion',
'petunia', 'wild pansy', 'primula', 'sunflower', 'lilac hibiscus', 'bishop of llandaff', 'gaura', 'geranium', 'orange dahlia', 'pink-yellow dahlia',
'cautleya spicata', 'japanese anemone', 'black-eyed susan', 'silverbush', 'californian poppy', 'osteospermum', 'spring crocus', 'iris', 'windflower', 'tree poppy',
'gazania', 'azalea', 'water lily', 'rose', 'thorn apple', 'morning glory', 'passion flower', 'lotus', 'toad lily', 'anthurium',
'frangipani', 'clematis', 'hibiscus', 'columbine', 'desert-rose', 'tree mallow', 'magnolia', 'cyclamen ', 'watercress', 'canna lily',
'hippeastrum ', 'bee balm', 'pink quill', 'foxglove', 'bougainvillea', 'camellia', 'mallow', 'mexican petunia', 'bromelia', 'blanket flower',
'trumpet creeper', 'blackberry lily', 'common tulip', 'wild rose']<categorify>
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X_train, y, scoring="neg_mean_squared_error", cv = 5))
return(rmse )
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)
 image = tf.ensure_shape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
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(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord,
num_parallel_calls = AUTOTUNE)
 return dataset
def one_hot_encoding(image, label, num_classes=len(CLASSES)) :
return image, tf.one_hot(label, num_classes)
 def get_training_dataset(dataset,do_aug=True):
dataset = dataset.map(one_hot_encoding, num_parallel_calls=AUTOTUNE)
 if do_aug: dataset = dataset.map(transform, num_parallel_calls=AUTOTUNE)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE, drop_remainder=True)
 dataset = dataset.prefetch(AUTOTUNE)
 return dataset
def get_validation_dataset(dataset):
dataset = dataset.map(one_hot_encoding, num_parallel_calls=AUTOTUNE)
 dataset = dataset.batch(BATCH_SIZE, drop_remainder=True)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTOTUNE)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE, drop_remainder=False)
 dataset = dataset.prefetch(AUTOTUNE)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n )<normalization>
alphas = [0.0005, 0.001, 0.01, 0.03, 0.05, 0.1]
l1_ratios = [1.5, 1.1, 1, 0.9, 0.8, 0.7, 0.5]
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))<normalization>
cv_elastic = [rmse_cv(ElasticNet(alpha = alpha, l1_ratio=l1_ratio)).mean()
for(alpha, l1_ratio)in product(alphas, l1_ratios)]
def transform(image,label):
image = tf.image.random_flip_left_right(image)
 DIM = IMAGE_SIZE[0]
XDIM = DIM%2
rot = 15.* tf.random.normal([1],dtype='float32')
 shr = 5.* tf.random.normal([1],dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 w_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 h_shift = 16.* tf.random.normal([1],dtype='float32')
 w_shift = 16.* 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]),label<choose_model_class>
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=0.5, random_state=3))
hm = HyperEfficientNet(input_shape=[IMAGE_SIZE[0], IMAGE_SIZE[1], 3] , classes=len(CLASSES))
hp = HyperParameters()
hp.Choice('version', ['B0', 'B1', 'B2', 'B3', 'B4'])
 tuner = kt.tuners.randomsearch.RandomSearch(
 hypermodel=hm,
objective='val_accuracy',
max_trials=5,
distribution_strategy=strategy,
directory='flower_classification',
project_name='randomsearch_efficientnet',
hyperparameters=hp, 
)
 tuner.search_space_summary()<create_dataframe>
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, X_train.values, y, scoring="neg_mean_squared_error", cv = kf))
return(rmse )
num_val_samples = count_data_items(VALIDATION_FILENAMES)
 num_train_samples = count_data_items(TRAINING_FILENAMES)
 train_ds = get_training_dataset(load_dataset(TRAINING_FILENAMES))
validation_ds = get_validation_dataset(load_dataset(VALIDATION_FILENAMES))
num_train_batches = num_train_samples // BATCH_SIZE
num_val_batches = num_val_samples // BATCH_SIZE<train_on_grid>
score = rmsle_cv(ENet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
tuner.search(train_ds,
epochs=EPOCHS_SEARCH,
validation_data=validation_ds,
steps_per_epoch=num_train_batches,
verbose=2 )<train_model>
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
ds_all = get_training_dataset(load_dataset(TRAINING_FILENAMES + VALIDATION_FILENAMES))
model.fit(ds_all,
epochs=EPOCHS_FINAL,
steps_per_epoch=num_train_batches + num_val_batches,
callbacks=[tf.keras.callbacks.ReduceLROnPlateau() ],
verbose=2 )<define_variables>
X_train2, X_test2, y_train2, y_test2 = train_test_split(X_train, y, test_size=0.3, random_state=0)
 ENet.fit(X_train2, y_train2)
 ENet.score(X_test2,y_test2)*100
ds_test = get_test_dataset(ordered=True)
 print('Computing predictions...')
 predictions = []
for i,(test_img, test_id)in enumerate(ds_test):
print('Processing batch ', i)
 probabilities = model(test_img)
 prediction = np.argmax(probabilities, axis=-1)
 predictions.append(prediction)
 predictions = np.concatenate(predictions)
 print('Number of test examples predicted: ', predictions.shape )<save_to_csv>
ENet.fit(X_train.values, y)
 ENet_train_pred = ENet.predict(X_train.values)
 print('RMSLE score on train data:')
 print(rmsle(y, ENet_train_pred))
ds_test_ids = ds_test.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(ds_test_ids.batch(np.iinfo(np.int64 ).max)) ).numpy().astype('U')
 np.savetxt(
 'submission.csv',
np.rec.fromarrays([test_ids, predictions]),
fmt=['%s', '%d'],
delimiter=',',
header='id,label',
comments='', 
)
 !head submission.csv<install_modules>
for c in categoricals:
for cell in X_test[c].unique().tolist() :
num = X_test[c].value_counts() [cell]
X_test[c] = X_test[c].replace({cell: num} )
!pip install -q efficientnet<import_modules>
ENet_pred = np.expm1(ENet.predict(X_test.values))
<set_options><EOS> 
sub = pd.DataFrame()
sub['Id'] = test['Id']
sub['SalePrice'] = ENet_pred
sub.to_csv('submission.csv',index=False )