Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<train_model>
import numpy as np
import pandas as pd
import seaborn as sns
import sklearn
from sklearn import metrics
import tensorflow as tf
from tensorflow import keras
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split, StratifiedKFold
import numpy as np
import keras_tuner as kt
from tensorflow import keras
import matplotlib.pyplot as plt
print("Song of Pygnut :")
 ipd.Audio(audio_data_pygnut )<train_model>
train = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
print("Song of Vesspa :")
 ipd.Audio(audio_data_vesspa )<train_model>
def evaluate(model, x_val, y_val):
y_pred = model.predict(x_val ).reshape(y_val.shape)
 r2 = metrics.r2_score(y_val, y_pred)
 mse = metrics.mean_squared_error(y_val, y_pred)
 mae = metrics.mean_absolute_error(y_val, y_pred)
 msle = metrics.mean_squared_log_error(y_val, y_pred)
 mape = np.mean(keras.metrics.mean_absolute_percentage_error(y_val, y_pred ).numpy())
 rmse = np.sqrt(mse)
 rmlse_score = rmlse(y_val, y_pred ).numpy()
print("R2 Score:", r2)
 print("MSE:", mse)
 print("MAE:", mae)
 print("MSLE:", msle)
 print("MAPE", mape)
 print("RMSE:", rmse)
 print("RMLSE", rmlse_score)
 return {"r2": r2, "mse": mse, "mae": mae, "msle": msle, "mape": mape, "rmse": rmse, "rmlse": rmlse_score}
print("Song of Yetvir :")
 ipd.Audio(audio_data_yetvir )<load_pretrained>
def rmlse(y_true, y_pred):
return tf.sqrt(tf.reduce_mean(tf.square(tf.math.log(y_pred + 1)- tf.math.log(y_true + 1))))
librosa.load(audio_data_vesspa, sr=85000 )<load_pretrained>
def submit(model, X, ids, file_path):
SalePrice = model.predict(X)
 submission = pd.DataFrame({"Id": ids, "SalePrice": SalePrice.reshape(-1)})
 submission.to_csv(file_path, index=False )
ipd.Audio(audio_data_vesspa )<normalization>
train.corr() ["SalePrice"].sort_values(key = lambda x: abs(x), ascending=False )
S, phase = librosa.magphase(D_ameavo)
 print(S.dtype, phase.dtype, np.allclose(D_ameavo, S * phase))<normalization>
for data in [train, test]:
null_counts = data.isnull().sum()
null_counts[null_counts > 0]
null_columns = list(pd.DataFrame(null_counts[null_counts > 0] ).index)
 for column in null_columns:
if data[column].dtype == object:
data[column] = data[[column]].replace(np.NAN, "Unknown")
 else:
data[column] = data[column].replace(np.NAN, data[column].mean() )
X_ameavo = librosa.stft(x_ameavo)
 X_mouchi = librosa.stft(x_mouchi)
 X_nutwoo = librosa.stft(x_nutwoo)
 X_pygnut = librosa.stft(x_pygnut)
 X_vesspa = librosa.stft(x_vesspa)
 X_yetvir = librosa.stft(x_yetvir)
 Xdb_ameavo = librosa.amplitude_to_db(abs(X_ameavo))
Xdb_mouchi = librosa.amplitude_to_db(abs(X_mouchi))
Xdb_nutwoo = librosa.amplitude_to_db(abs(X_nutwoo))
Xdb_pygnut = librosa.amplitude_to_db(abs(X_pygnut))
Xdb_vesspa = librosa.amplitude_to_db(abs(X_vesspa))
Xdb_yetvir = librosa.amplitude_to_db(abs(X_yetvir))
fig, ax = plt.subplots(2, 3, figsize=(16, 10))
fig.suptitle('Spectrogram', fontsize=12)
 librosa.display.specshow(Xdb_ameavo, sr = sr, x_axis = 'time', y_axis = 'hz', ax=ax[0, 0])
 ax[0, 0].set_title('Ameavo', fontsize = 12)
 librosa.display.specshow(Xdb_mouchi, sr = sr, x_axis = 'time', y_axis = 'hz', ax=ax[0, 1])
 ax[0, 1].set_title('Mouchi', fontsize = 12)
 librosa.display.specshow(Xdb_nutwoo, sr = sr, x_axis = 'time', y_axis = 'hz', ax=ax[0, 2])
 ax[0, 2].set_title('Nutwoo', fontsize = 12)
 librosa.display.specshow(Xdb_pygnut, sr = sr, x_axis = 'time', y_axis = 'hz', ax=ax[1, 0])
 ax[1, 0].set_title('Pygnut', fontsize = 12)
 librosa.display.specshow(Xdb_vesspa, sr = sr, x_axis = 'time', y_axis = 'hz', ax=ax[1, 1])
 ax[1, 1].set_title('Vesspa', fontsize = 12)
 librosa.display.specshow(Xdb_yetvir, sr = sr, x_axis = 'time', y_axis = 'hz', ax=ax[1, 2])
 ax[1, 2].set_title('Yetvir', fontsize = 12 )<set_options>
train_test = pd.get_dummies(pd.concat([train, test]))
sr = 22000
T = 5.0
t = np.linspace(0, T, int(T*sr), endpoint=False)
 x = 0.5*np.sin(2*np.pi*220*t)
 ipd.Audio(x, rate=sr)
 librosa.output.write_wav('tone_220.wav', x, sr )<define_variables>
train_features = train_test.iloc[0: len(train)]
test_features = train_test.iloc[len(train):]
_ = train_features.pop("Id")
 _ = test_features.pop("SalePrice")
 test_ids = test_features.pop("Id" )
zero_crossings_ameavo = librosa.zero_crossings(x_ameavo[n0:n1], pad=False)
 zero_crossings_mouchi = librosa.zero_crossings(x_mouchi[n0:n1], pad=False)
 zero_crossings_nutwoo = librosa.zero_crossings(x_nutwoo[n0:n1], pad=False)
 zero_crossings_pygnut = librosa.zero_crossings(x_pygnut[n0:n1], pad=False)
 zero_crossings_vesspa = librosa.zero_crossings(x_vesspa[n0:n1], pad=False)
 zero_crossings_yetvir = librosa.zero_crossings(x_yetvir[n0:n1], pad=False)
 print("No.of Zero Crossings :")
 print('For Ameavo :', sum(zero_crossings_ameavo))
print('For Mouchi :', sum(zero_crossings_mouchi))
print('For Nutwoo :', sum(zero_crossings_nutwoo))
print('For Pygnut :', sum(zero_crossings_pygnut))
print('For Vesspa :', sum(zero_crossings_vesspa))
print('For Yetvir :', sum(zero_crossings_yetvir))<compute_test_metric>
train_features, val_features = train_test_split(train_features, test_size=0.2, random_state=np.random.randint(1000))
fs=15
mfccs_ameavo = librosa.feature.mfcc(x_ameavo, sr=fs)
 mfccs_mouchi = librosa.feature.mfcc(x_mouchi, sr=fs)
 mfccs_nutwoo = librosa.feature.mfcc(x_nutwoo, sr=fs)
 mfccs_pygnut = librosa.feature.mfcc(x_pygnut, sr=fs)
 mfccs_vesspa = librosa.feature.mfcc(x_vesspa, sr=fs)
 mfccs_yetvir = librosa.feature.mfcc(x_yetvir, sr=fs)
 print("MFCC shapes :")
 print("Ameavo :", mfccs_ameavo.shape)
 print("Mouchi :", mfccs_mouchi.shape)
 print("Nutwoo :", mfccs_nutwoo.shape)
 print("Pygnut :", mfccs_pygnut.shape)
 print("Vesspa :", mfccs_vesspa.shape)
 print("Yetvir :", mfccs_yetvir.shape)
 fig, ax = plt.subplots(6, figsize =(16, 14))
fig.suptitle('MFCCs Display', fontsize=14)
 librosa.display.specshow(mfccs_ameavo, sr=sr, x_axis='time', cmap='spring', ax=ax[0])
 ax[0].set_ylabel('Ameavo')
 librosa.display.specshow(mfccs_mouchi, sr=sr, x_axis='time', cmap='spring', ax=ax[1])
 ax[1].set_ylabel('Mouchi')
 librosa.display.specshow(mfccs_nutwoo, sr=sr, x_axis='time', cmap='spring', ax=ax[2])
 ax[2].set_ylabel('Nutwoo')
 librosa.display.specshow(mfccs_pygnut, sr=sr, x_axis='time', cmap='spring', ax=ax[3])
 ax[3].set_ylabel('Pygnut')
 librosa.display.specshow(mfccs_vesspa, sr=sr, x_axis='time', cmap='spring', ax=ax[4])
 ax[4].set_ylabel('Vesspa')
 librosa.display.specshow(mfccs_yetvir, sr=sr, x_axis='time', cmap='spring', ax=ax[5])
 ax[5].set_ylabel('Yetvir' )<import_modules>
thresold = 0.05
correlated_scores = train_features.corr() ["SalePrice"]
correlated_scores = correlated_scores[correlated_scores.abs() >= thresold]
correlated_columns = list(correlated_scores.index)
 correlated_columns.remove("SalePrice")
 print(correlated_columns )
import cv2
import audioread
import logging
import os
import random
import time
import warnings
import librosa
import numpy as np
import pandas as pd
import soundfile as sf
import torch
import torch.nn as nn
import torch.cuda
import torch.nn.functional as F
import torch.utils.data as data
from contextlib import contextmanager
from pathlib import Path
from typing import Optional
from fastprogress import progress_bar
from sklearn.metrics import f1_score
from torchvision import models<set_options>
train_targets = train_features.pop("SalePrice")
 val_targets = val_features.pop("SalePrice" )
def set_seed(seed: int = 12345):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def get_logger(out_file=None):
logger = logging.getLogger()
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
 logger.handlers = []
logger.setLevel(logging.INFO)
 handler = logging.StreamHandler()
handler.setFormatter(formatter)
 handler.setLevel(logging.INFO)
 logger.addHandler(handler)
 if out_file is not None:
fh = logging.FileHandler(out_file)
 fh.setFormatter(formatter)
 fh.setLevel(logging.INFO)
 logger.addHandler(fh)
 logger.info("logger set up")
 return logger
@contextmanager
def timer(name: str, logger: Optional[logging.Logger] = None):
t0 = time.time()
msg = f"[{name}] start"
if logger is None:
print(msg)
 else:
logger.info(msg)
 yield
msg = f"[{name}] done in {time.time() - t0:.2f} s"
if logger is None:
print(msg)
 else:
logger.info(msg )<set_options>
categorical_columns = set(train.dtypes[train.dtypes==object].index )
logger = get_logger("main.log")
 set_seed(12345 )<define_variables>
scale_strategies = ["none", "standard_scale", "standard_scale_exclude_categorcial_features"]
scale_strategy = scale_strategies[2]
if scale_strategy == scale_strategies[1]:
train_features =(train_features - mean_value)/ std_value
val_features =(val_features - mean_value)/ std_value
test_features =(test_features - mean_value)/ std_value
if scale_strategy == scale_strategies[2]:
for column in train_features.columns:
is_categorical_feature = False
components = column.split("_")
 if len(components)== 2 and components[0] in categorical_columns:
is_categorical_feature = True
if is_categorical_feature == False:
for features in [train_features, val_features, test_features]:
features.loc[:, column] =(features.loc[:, column] - mean_value[column])/ std_value[column]
TARGET_SR = 36000
TEST = Path(".. /input/birdsong-recognition/test_audio" ).exists()<load_from_csv>
use_correlated_columns = True
if use_correlated_columns:
train_features = train_features[correlated_columns]
val_features = val_features[correlated_columns]
test_features = test_features[correlated_columns]
if TEST:
DATA_DIR = Path(".. /input/birdsong-recognition/")
 else:
DATA_DIR = Path(".. /input/birdcall-check/")
 test = pd.read_csv(DATA_DIR / "test.csv")
 test_audio = DATA_DIR / "test_audio"
test.head()<save_to_csv>
def build_model(hp):
model = keras.Sequential()
model.add(keras.Input(( train_features.shape[1])))
 for depth in range(hp.Choice('depth', [3, 4, 5, 6, 7])) :
model.add(keras.layers.Dense(hp.Choice('width', [4, 8, 16, 32, 64]), activation=hp.Choice('activation', ["relu", "elu", "tanh", "selu", "sigmoid"]), kernel_regularizer=keras.regularizers.l2()))
model.add(keras.layers.Dense(1))
adam = keras.optimizers.Adam(learning_rate=hp.Choice("learing_rate", [1e-3, 5e-3, 1e-4, 5e-4, 1e-5]))
model.compile(loss='mse', optimizer=adam, metrics=["mae", rmlse])
 return model
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 sub.to_csv("submission.csv", index=False )<choose_model_class>
early_stop = keras.callbacks.EarlyStopping(patience=10)
 reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, min_lr=1e-7)
 model_checkpoint_path = "model.h5"
model_checkpoint = keras.callbacks.ModelCheckpoint(model_checkpoint_path, save_best_model=True)
 history = best_model.fit(train_features, train_targets, epochs=300, validation_data=(val_features, val_targets), callbacks=[early_stop, model_checkpoint, reduce_lr])
 for metric in ["loss", "mae", "rmlse"]:
pd.DataFrame(history.history, columns=[metric, "val_" + metric] ).plot()
plt.show()
<define_variables><EOS> 
SalePrice = best_model.predict(test_features ).reshape(-1)
 submission = pd.DataFrame({"Id": test_ids, "SalePrice": SalePrice})
 submission.to_csv("submission.csv", index=False )