Kaggle competition: Petals to the Metal - Flower Classification on TPU
<SOS> metric: macrofscore Kaggle data source: petals-to-the-metal-flower-classification-on-tpu<save_to_csv>
!pip install -U -t /kaggle/working/ git+https://github.com/Kaggle/learntools.git
binder.bind(globals())
 step_1.check()
test=torch.tensor(test, dtype=torch.float)
 sub=net(test)
 sub=sub.argmax(dim=1)
 sub=sub.numpy()
sub.reshape(-1,1)
 submission=pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 submission["Label"]=sub
submission.to_csv('submission.csv', index=False )<count_duplicates>
from petal_helper import *
duplicate_count = train["Id"].nunique() - train.shape[0];
if(duplicate_count == 0):
print("There is no duplicate data in the train data.")
 else:
print("There are %d duplicate data in the train data." %duplicate_count)
 train.drop("Id", axis = 1, inplace = True )<define_variables>
try:
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.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )
med = train['SalePrice'].median()
medAbsDev = abs(train['SalePrice'] - med ).median()
ind =(abs(train['SalePrice'] - med)*.6745 / medAbsDev)> 3.5
print("There are suspected %d outliers in the train data." % sum(ind))
train = train[-ind]
train = train[train.GrLivArea < 4000]<data_type_conversions>
ds_train = get_training_dataset()
ds_valid = get_validation_dataset()
ds_test = get_test_dataset()
print("Training:", ds_train)
 print("Validation:", ds_valid)
 print("Test:", ds_test )
train["Alley"].fillna("None", inplace=True)
 train["BsmtQual"].fillna("None", inplace=True)
 train["BsmtCond"].fillna("None", inplace=True)
 train["BsmtExposure"].fillna("None", inplace=True)
 train["BsmtFinType1"].fillna("None", inplace=True)
 train["BsmtFinSF1"].fillna(0, inplace=True)
 train["BsmtFinType2"].fillna("None", inplace=True)
 train["BsmtFinSF2"].fillna(0, inplace=True)
 train["BsmtFullBath"].fillna(0, inplace=True)
 train["BsmtHalfBath"].fillna(0, inplace=True)
 train["BsmtUnfSF"].fillna(0, inplace=True)
 train["TotalBsmtSF"].fillna(0, inplace=True)
 train["KitchenQual"].fillna(0, inplace=True)
 train["Functional"].fillna(0, inplace=True)
 train["FireplaceQu"].fillna("None", inplace=True)
 train["GarageType"].fillna("None", inplace=True)
 train["GarageQual"].fillna(0, inplace=True)
 train["GarageCond"].fillna(0, inplace=True)
 train["GarageYrBlt"].fillna("None", inplace=True)
 train["GarageFinish"].fillna("None", inplace=True)
 train["GarageCars"].fillna(0, inplace=True)
 train["GarageArea"].fillna(0, inplace=True)
 train["PoolQC"].fillna("None", inplace=True)
 train["Fence"].fillna("None", inplace=True)
 train["MiscFeature"].fillna("None", inplace=True)
 train["LotFrontage"].fillna(0, inplace=True)
 train["MasVnrType"].fillna("None", inplace=True)
 train["MasVnrArea"].fillna(0, inplace=True)
 train["Electrical"].fillna("SBrkr", inplace=True)
 train["Utilities"].fillna("None", inplace=True)
<categorify>
print("Number of classes: {}".format(len(CLASSES)))
 print("First five classes, sorted alphabetically:")
 for name in sorted(CLASSES)[:5]:
print(name)
 print("Number of training images: {}".format(NUM_TRAINING_IMAGES))
train = train.replace({"MSSubClass" : {20 : "SC20", 30 : "SC30", 40 : "SC40", 45 : "SC45", 50 : "SC50", 60 : "SC60",
70 : "SC70", 75 : "SC75", 80 : "SC80", 85 : "SC85", 90 : "SC90", 120 : "SC120", 150 : "SC150", 160 : "SC160",
180 : "SC180", 190 : "SC190"},
"MoSold" : {1 : "Jan", 2 : "Feb", 3 : "Mar", 4 : "Apr", 5 : "May", 6 : "Jun", 7 : "Jul", 8 : "Aug", 9 : "Sep",
10 : "Oct", 11 : "Nov", 12 : "Dec"} })
 train = train.replace({"Alley" : {"Grvl" : 1, "Pave" : 2},
"BsmtCond" : {"No" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"BsmtExposure" : {"No" : 0, "Mn" : 1, "Av": 2, "Gd" : 3},
"BsmtFinType1" : {"No" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4, "ALQ" : 5, "GLQ" : 6},
"BsmtFinType2" : {"No" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4, "ALQ" : 5, "GLQ" : 6},
"BsmtQual" : {"No" : 0, "Po" : 1, "Fa" : 2, "TA": 3, "Gd" : 4, "Ex" : 5},
"ExterCond" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"ExterQual" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"FireplaceQu" : {"No" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"Functional" : {"Sal" : 1, "Sev" : 2, "Maj2" : 3, "Maj1" : 4, "Mod": 5, "Min2" : 6, "Min1" : 7, "Typ" : 8},
"GarageCond" : {"None" : 0, "No" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"GarageQual" : {"None" : 0, "No" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"HeatingQC" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"KitchenQual" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"LandSlope" : {"Sev" : 1, "Mod" : 2, "Gtl" : 3},
"LotShape" : {"IR3" : 1, "IR2" : 2, "IR1" : 3, "Reg" : 4},
"PavedDrive" : {"N" : 0, "P" : 1, "Y" : 2},
"PoolQC" : {"No" : 0, "Fa" : 1, "TA" : 2, "Gd" : 3, "Ex" : 4},
"Street" : {"Grvl" : 1, "Pave" : 2},
"Utilities" : {"None" : 0, "ELO" : 1, "NoSeWa" : 2, "NoSewr" : 3, "AllPub" : 4}} )<feature_engineering>
print("Training data shapes:")
 for image, label in ds_train.take(3):
print(image.numpy().shape, label.numpy().shape)
 print("Training data label examples:", label.numpy() )
train["OverallGrade"] = train["OverallQual"] * train["OverallCond"]
train["GarageGrade"] = train["GarageQual"] * train["GarageCond"]
train["ExterGrade"] = train["ExterQual"] * train["ExterCond"]
train["KitchenScore"] = train["KitchenAbvGr"] * train["KitchenQual"]
train["FireplaceScore"] = train["Fireplaces"] * train["FireplaceQu"]
train["GarageScore"] = train["GarageArea"] * train["GarageQual"]
train["PoolScore"] = train["PoolArea"] * train["PoolQC"]
train["TotalBath"] = train["BsmtFullBath"] + train["BsmtHalfBath"]/2 + train["FullBath"] + train["HalfBath"]/2
train["AllSF"] = train["GrLivArea"] + train["TotalBsmtSF"]
train["AllFlrsSF"] = train["1stFlrSF"] + train["2ndFlrSF"]
train["AllPorchSF"] = train["OpenPorchSF"] + train["EnclosedPorch"] + train["3SsnPorch"] + train["ScreenPorch"]<sort_values>
print("Test data shapes:")
 for image, idnum in ds_test.take(3):
print(image.numpy().shape, idnum.numpy().shape)
 print("Test data IDs:", idnum.numpy().astype('U'))
corr = train.corr()
corr.sort_values(["SalePrice"], ascending = False, inplace = True)
 corr.SalePrice<feature_engineering>
one_batch = next(iter(ds_train.unbatch().batch(20)))
 display_batch_of_images(one_batch )
train["AllSF-s2"] = train["AllSF"] ** 2
train["AllSF-Sq"] = np.sqrt(train["AllSF"])
 train["OverallQual-s2"] = train["OverallQual"] ** 2
train["OverallQual-Sq"] = np.sqrt(train["OverallQual"])
 train["AllFlrsSF-s2"] = train["AllFlrsSF"] ** 2
train["AllFlrsSF-Sq"] = np.sqrt(train["AllFlrsSF"])
 train["GrLivArea-s2"] = train["GrLivArea"] ** 2
train["GrLivArea-Sq"] = np.sqrt(train["GrLivArea"])
 train["ExterQual-s2"] = train["ExterQual"] ** 2
train["ExterQual-Sq"] = np.sqrt(train["ExterQual"] )<categorify>
pip install -q efficientnet
categorical_features = train.select_dtypes(include = ["object"] ).columns
numerical_features = train.select_dtypes(exclude = ["object"] ).columns
numerical_features = numerical_features.drop("SalePrice")
 train_num = train[numerical_features]
train_cat = train[categorical_features]
skewness = train_num.apply(lambda x: skew(x))
skewness = skewness[abs(skewness)> 0.5]
print("%d out of %d numerical features are skewed.Apply log transform to these features." %(skewness.shape[0],
train_num.shape[1]))
train_num.loc[:,skewness.index] = np.log1p(np.asarray(train_num[skewness.index] , dtype=float))
train_cat = pd.get_dummies(train_cat )<train_model>
import efficientnet.tfkeras as efficientnet
y = np.log1p(train["SalePrice"])
 X_train, X_test, y_train, y_test = train_test_split(pd.concat([train_num, train_cat], axis = 1),
y, test_size = 0.3, random_state = 514)
 stdSc = StandardScaler()
X_train.loc[:, numerical_features] = stdSc.fit_transform(X_train.loc[:, numerical_features])
 X_test.loc[:, numerical_features] = stdSc.transform(X_test.loc[:, numerical_features] )<compute_train_metric>
with strategy.scope() :
pretrained_model = efficientnet.EfficientNetB7(weights='noisy-student',include_top=False,input_shape=[*IMAGE_SIZE, 3])
 pretrained_model.trainable=True
model = Sequential()
model.add(pretrained_model)
 model.add(GlobalAveragePooling2D())
 model.add(Dense(104, activation='softmax'))
model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=["sparse_categorical_accuracy"] )
scorer = make_scorer(mean_squared_error, greater_is_better = True)
 def rmse_cv_train(model):
rmse = np.sqrt(cross_val_score(model, X_train, y_train, scoring = scorer, cv = 10))
return(rmse)
 def rmse_cv_test(model):
rmse = np.sqrt(cross_val_score(model, X_test, y_test, scoring = scorer, cv = 10))
return(rmse )<train_model>
es = EarlyStopping(monitor='val_loss', mode='min', patience=6,
restore_best_weights=True, verbose=1)
 lr = ReduceLROnPlateau(monitor='val_loss',factor=0.2,patience=4,verbose=1)
 callbacks = [es, lr]
elasticNet = ElasticNetCV(l1_ratio = [0.1, 0.3, 0.5, 0.6, 0.7, 0.8, 0.85, 0.9, 0.95, 1],
alphas = [0.0001, 0.001, 0.01, 0.1, 1],
max_iter = 50000, cv = 10)
 elasticNet.fit(X_train, y_train)
 alpha = elasticNet.alpha_
ratio = elasticNet.l1_ratio_
print("Best l1_ratio :", ratio)
 print("Best alpha :", alpha )<train_on_grid>
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
EPOCHS = 30
STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
history = model.fit(
 ds_train,
validation_data=ds_valid,
epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,
callbacks=callbacks
)
print("Try again for more precision with l1_ratio centered around " + str(ratio))
elasticNet = ElasticNetCV(l1_ratio = [ratio *.9, ratio *.95, ratio, ratio * 1.05, ratio * 1.1],
alphas = [0.0001, 0.001, 0.01, 0.1, 1],
max_iter = 50000, cv = 10)
 elasticNet.fit(X_train, y_train)
 alpha = elasticNet.alpha_
ratio = elasticNet.l1_ratio_
print("Best l1_ratio :", ratio)
 print("Best alpha :", alpha )<train_on_grid>
cmdataset = get_validation_dataset(ordered=True)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
cm_probabilities = model.predict(images_ds)
 cm_predictions = np.argmax(cm_probabilities, axis=-1)
 labels = range(len(CLASSES))
cmat = confusion_matrix(
 cm_correct_labels,
cm_predictions,
labels=labels, 
)
 cmat =(cmat.T / cmat.sum(axis=1)).T
print("Now try again for more precision on alpha, with l1_ratio fixed at " + str(ratio)+
" and alpha centered around " + str(alpha))
elasticNet = ElasticNetCV(l1_ratio = ratio,
alphas = [alpha *.6, alpha *.7, alpha *.8, alpha *.9,
alpha, alpha * 1.1, alpha * 1.2, alpha * 1.3, alpha * 1.4],
max_iter = 50000, cv = 5)
 elasticNet.fit(X_train, y_train)
 print("Best l1_ratio :", elasticNet.l1_ratio_)
 print("Best alpha :", elasticNet.alpha_)
 print("ElasticNet RMSE on Training set :", rmse_cv_train(elasticNet ).mean())
 print("ElasticNet RMSE on Test set :", rmse_cv_test(elasticNet ).mean())
 y_train_ela = elasticNet.predict(X_train)
 y_test_ela = elasticNet.predict(X_test )<load_from_csv>
score = f1_score(
 cm_correct_labels,
cm_predictions,
labels=labels,
average='macro', 
)
 precision = precision_score(
 cm_correct_labels,
cm_predictions,
labels=labels,
average='macro', 
)
 recall = recall_score(
 cm_correct_labels,
cm_predictions,
labels=labels,
average='macro', 
)
 display_confusion_matrix(cmat, score, precision, recall )
test = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
 test["Alley"].fillna("None", inplace=True)
 test["BsmtQual"].fillna("None", inplace=True)
 test["BsmtCond"].fillna("None", inplace=True)
 test["BsmtExposure"].fillna("None", inplace=True)
 test["BsmtFinType1"].fillna("None", inplace=True)
 test["BsmtFinSF1"].fillna(0, inplace=True)
 test["BsmtFinType2"].fillna("None", inplace=True)
 test["BsmtFinSF2"].fillna(0, inplace=True)
 test["BsmtFullBath"].fillna(0, inplace=True)
 test["BsmtHalfBath"].fillna(0, inplace=True)
 test["BsmtUnfSF"].fillna(0, inplace=True)
 test["TotalBsmtSF"].fillna(0, inplace=True)
 test["KitchenQual"].fillna(0, inplace=True)
 test["Functional"].fillna(0, inplace=True)
 test["FireplaceQu"].fillna("None", inplace=True)
 test["GarageType"].fillna("None", inplace=True)
 test["GarageQual"].fillna(0, inplace=True)
 test["GarageCond"].fillna(0, inplace=True)
 test["GarageYrBlt"].fillna("None", inplace=True)
 test["GarageFinish"].fillna("None", inplace=True)
 test["GarageCars"].fillna(0, inplace=True)
 test["GarageArea"].fillna(0, inplace=True)
 test["PoolQC"].fillna("None", inplace=True)
 test["Fence"].fillna("None", inplace=True)
 test["MiscFeature"].fillna("None", inplace=True)
 test["LotFrontage"].fillna(0, inplace=True)
 test["MasVnrType"].fillna("None", inplace=True)
 test["MasVnrArea"].fillna(0, inplace=True)
 test["Electrical"].fillna("SBrkr", inplace=True)
 test["Utilities"].fillna("None", inplace=True)
 test = test.replace({"MSSubClass" : {20 : "SC20", 30 : "SC30", 40 : "SC40", 45 : "SC45", 50 : "SC50", 60 : "SC60",
70 : "SC70", 75 : "SC75", 80 : "SC80", 85 : "SC85", 90 : "SC90", 120 : "SC120", 150 : "SC150", 160 : "SC160",
180 : "SC180", 190 : "SC190"},
"MoSold" : {1 : "Jan", 2 : "Feb", 3 : "Mar", 4 : "Apr", 5 : "May", 6 : "Jun", 7 : "Jul", 8 : "Aug", 9 : "Sep",
10 : "Oct", 11 : "Nov", 12 : "Dec"} })
 test = test.replace({"Alley" : {"Grvl" : 1, "Pave" : 2},
"BsmtCond" : {"No" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"BsmtExposure" : {"No" : 0, "Mn" : 1, "Av": 2, "Gd" : 3},
"BsmtFinType1" : {"No" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4, "ALQ" : 5, "GLQ" : 6},
"BsmtFinType2" : {"No" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4, "ALQ" : 5, "GLQ" : 6},
"BsmtQual" : {"No" : 0, "Po" : 1, "Fa" : 2, "TA": 3, "Gd" : 4, "Ex" : 5},
"ExterCond" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"ExterQual" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"FireplaceQu" : {"No" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"Functional" : {"Sal" : 1, "Sev" : 2, "Maj2" : 3, "Maj1" : 4, "Mod": 5, "Min2" : 6, "Min1" : 7, "Typ" : 8},
"GarageCond" : {"None" : 0, "No" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"GarageQual" : {"None" : 0, "No" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"HeatingQC" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"KitchenQual" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"LandSlope" : {"Sev" : 1, "Mod" : 2, "Gtl" : 3},
"LotShape" : {"IR3" : 1, "IR2" : 2, "IR1" : 3, "Reg" : 4},
"PavedDrive" : {"N" : 0, "P" : 1, "Y" : 2},
"PoolQC" : {"No" : 0, "Fa" : 1, "TA" : 2, "Gd" : 3, "Ex" : 4},
"Street" : {"Grvl" : 1, "Pave" : 2},
"Utilities" : {"None" : 0, "ELO" : 1, "NoSeWa" : 2, "NoSewr" : 3, "AllPub" : 4}})
 test["OverallGrade"] = test["OverallQual"] * test["OverallCond"]
test["GarageGrade"] = test["GarageQual"] * test["GarageCond"]
test["ExterGrade"] = test["ExterQual"] * test["ExterCond"]
test["KitchenScore"] = test["KitchenAbvGr"] * test["KitchenQual"]
test["FireplaceScore"] = test["Fireplaces"] * test["FireplaceQu"]
test["GarageScore"] = test["GarageArea"] * test["GarageQual"]
test["PoolScore"] = test["PoolArea"] * test["PoolQC"]
test["TotalBath"] = test["BsmtFullBath"] + test["BsmtHalfBath"]/2 + test["FullBath"] + test["HalfBath"]/2
test["AllSF"] = test["GrLivArea"] + test["TotalBsmtSF"]
test["AllFlrsSF"] = test["1stFlrSF"] + test["2ndFlrSF"]
test["AllPorchSF"] = test["OpenPorchSF"] + test["EnclosedPorch"] + test["3SsnPorch"] + test["ScreenPorch"]
test["AllSF-s2"] = test["AllSF"] ** 2
test["AllSF-Sq"] = np.sqrt(test["AllSF"])
 test["OverallQual-s2"] = test["OverallQual"] ** 2
test["OverallQual-Sq"] = np.sqrt(test["OverallQual"])
 test["AllFlrsSF-s2"] = test["AllFlrsSF"] ** 2
test["AllFlrsSF-Sq"] = np.sqrt(test["AllFlrsSF"])
 test["GrLivArea-s2"] = test["GrLivArea"] ** 2
test["GrLivArea-Sq"] = np.sqrt(test["GrLivArea"])
 test["ExterQual-s2"] = test["ExterQual"] ** 2
test["ExterQual-Sq"] = np.sqrt(test["ExterQual"])
 test_num = test[numerical_features]
test_cat = test[categorical_features]
test_num.loc[:,skewness.index] = np.log1p(np.asarray(test_num[skewness.index] , dtype=float))
test_cat = pd.get_dummies(test_cat)
 test_processed = pd.concat([test_num, test_cat], axis = 1)
 test_processed.loc[:, numerical_features] = stdSc.transform(test_processed.loc[:, numerical_features] )<save_to_csv>
dataset = get_validation_dataset()
dataset = dataset.unbatch().batch(20)
 batch = iter(dataset )
final_train, final_test = X_train.align(test_processed, join='left', axis=1, fill_value=0)
 y_test = elasticNet.predict(final_test)
 sub = pd.DataFrame()
sub['Id'] = test["Id"]
sub['SalePrice'] = np.expm1(y_test)
 sub.to_csv('submission.csv',index=False )<import_modules>
images, labels = next(batch)
 probabilities = model.predict(images)
 predictions = np.argmax(probabilities, axis=-1)
 display_batch_of_images(( images, labels), predictions )
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dense, Input, BatchNormalization, Dropout, Concatenate
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.callbacks import ModelCheckpoint<load_from_csv>
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities = model.predict(test_images_ds)
 predictions = np.argmax(probabilities, axis=-1)
 print(predictions )
<define_variables><EOS> 
print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt(
 'submission.csv',
np.rec.fromarrays([test_ids, predictions]),
fmt=['%s', '%d'],
delimiter=',',
header='id,label',
comments='', 
)
 !head submission.csv