Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<set_options>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import xgboost as xgb
from hyperopt import STATUS_OK, Trials, fmin, hp, tpe
fmin, fmax = 25, 11000
f1 = MelSpectrogram(sample_rate=args['std_sr'], n_fft=1024, hop_length=args['hop_length'], f_min=fmin, f_max=fmax, n_mels=args['n_mels'] ).cuda()
f2 = MelSpectrogram(sample_rate=args['std_sr'], n_fft=2048, hop_length=args['hop_length'], f_min=fmin, f_max=fmax, n_mels=args['n_mels'] ).cuda()
f3 = MelSpectrogram(sample_rate=args['std_sr'], n_fft=4096, hop_length=args['hop_length'], f_min=fmin, f_max=fmax, n_mels=args['n_mels'] ).cuda()<categorify>
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv' )
class TestDataset(data.Dataset):
def __init__(self, df, spectrogram):
self.df = df
self.spectrogram = spectrogram
def __len__(self):
return len(self.df)
 def normalize(self, spect):
spect = spect.contiguous()
miu = spect.view(spect.size(0), -1 ).mean(-1 ).unsqueeze(-1 ).unsqueeze(-1)
 std = spect.view(spect.size(0), -1 ).std(-1 ).unsqueeze(-1 ).unsqueeze(-1)+ 1e-8
return(spect - miu)/ std / 2
def __getitem__(self, idx):
sample = self.df.loc[idx, :]
site = sample.site
row_id = sample.row_id
if site == "site_3":
y = self.spectrogram
len_y = y.shape[-1]
start = 0
end = args['std_sr'] * 5 // args['hop_length']
y_all = []
while len_y > start:
y_batch = self.normalize(y[:, :, start:end])
 if end >= len_y:
clip_length = args['std_sr'] * 5 // args['hop_length']
y_batch = self.normalize(y[:, :, -clip_length:])
 y_all.append(y_batch)
 break
start = end
end = end + args['std_sr'] * 5 // args['hop_length']
y_all.append(y_batch)
 y_all = torch.stack(y_all)
 return y_all, row_id, site
else:
end_seconds = int(sample.seconds)
 start_seconds = int(end_seconds - 5)
 start_index = args['std_sr'] * start_seconds // args['hop_length']
end_index = args['std_sr'] * end_seconds // args['hop_length']
y = self.spectrogram[:, :, start_index:end_index]
y = self.normalize(y)
 return y, row_id, site
def prediction_for_clip(dataset, models, threshold=0.5):
loader = data.DataLoader(dataset, batch_size=1, shuffle=False)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 prediction_dict = {}
for image, row_id, site in loader:
site = site[0]
row_id = row_id[0]
image = image.to(device ).float()
preds = []
if site in {"site_1", "site_2"}:
image = image.to(device ).float()
with torch.no_grad() :
for model in models:
model.eval()
prediction = model(image)
 proba = prediction.view(-1 ).sigmoid().cpu().numpy()
preds.append(proba)
 else:
image = image.squeeze(0)
 batch_size = 16
whole_size = image.size(0)
 if whole_size % batch_size == 0:
n_iter = whole_size // batch_size
else:
n_iter = whole_size // batch_size + 1
probas = [[] for _ in range(len(models)) ]
all_events = set()
for batch_i in range(n_iter):
batch = image[batch_i * batch_size:(batch_i + 1)* batch_size]
if batch.ndim == 3:
batch = batch.unsqueeze(0)
 batch = batch.to(device)
 with torch.no_grad() :
for idx, model in enumerate(models):
model.eval()
prediction = model(batch)
 proba = prediction.view(batch.size(0), -1 ).sigmoid().cpu().numpy()
probas[idx].append(proba)
 for sub_probas in probas:
sub_probas = np.vstack(sub_probas)
 proba = sub_probas.max(0)
 preds.append(proba)
 prediction_dict[row_id] = preds
return prediction_dict<define_variables>
Y_train = train.SalePrice
X_train = train.drop(columns= ['SalePrice'] , inplace = False )
model_paths = [
'.. /input/birdweights/c2__17.pt',
'.. /input/birdweights/c3_10_42.pt',
'.. /input/birdweights/c3_3_29.pt',
'.. /input/birdweights/c3_4_17.pt'
]<find_best_params>
test.drop(columns = ['Id'] , inplace = True)
 X_train.drop(columns = ['Id'] , inplace = True )
models = []
for path in model_paths:
model = torch.jit.load(path, map_location='cpu' ).cuda()
models.append(model )<define_variables>
null_list = X_train.isnull().sum()
h = X_train.shape[0]
columns_name = X_train.columns
for i in range(len(null_list)) :
if null_list[i]>h/3 :
X_train.drop(columns = [columns_name[i]] , inplace = True)
 test.drop(columns = [columns_name[i]] , inplace = True )
prediction_dict = {}
for key, preds in submission.items() :
pred3 = preds[0]
pred4 = preds[1]
pred5 = preds[2]
pred6 = preds[3]
maxbird=3
threshold=0.35
if 'site_3' in key:
maxbird=8
threshold=0.45
en_pred = 0.25*pred3 +0.25*pred4 +0.25*pred5 +0.25*pred6
events =(en_pred>=threshold*1)
 labels = np.argsort(-en_pred)[:events.sum() ].tolist()
if len(labels)== 0:
prediction_dict[key] = "nocall"
else:
labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))
label_string = " ".join(sorted(labels_str_list[:maxbird]))
prediction_dict[key] = label_string 
<save_to_csv>
def fill_nulls(X):
for col in X.columns:
if X[col].isnull().sum() >0:
if X[col].dtype == 'float64' :
X[col].fillna(X[col].mean() ,inplace=True)
 else :
X[col].fillna(X[col].mode() [0],inplace=True )
prediction_dfs = []
row_id = list(prediction_dict.keys())
 birds = list(prediction_dict.values())
 prediction_df = pd.DataFrame({
"row_id": row_id,
"birds": birds
})
 prediction_dfs.append(prediction_df)
 prediction_df = pd.concat(prediction_dfs, axis=0, sort=False ).reset_index(drop=True)
 prediction_df.to_csv("submission.csv", index=False )<count_values>
fill_nulls(X_train)
 fill_nulls(test )
prediction_df['birds'].value_counts()<install_modules>
def category(columns_X , X):
for col in columns_X:
df = pd.get_dummies(X[col] , drop_first = True)
 X.drop(columns =col , inplace = True)
 X = pd.concat([X , df] , axis =1)
 return X
!pip install librosa<set_options>
list_categorical = []
for i in X_train.columns :
if X_train[i].dtype == object:
list_categorical +=[i]
X_train_test = pd.concat([X_train , test] , axis = 0)
 X_train_test = category(list_categorical , X_train_test )
warnings.filterwarnings("ignore" )<load_pretrained>
X_train_test = X_train_test.loc[: , ~X_train_test.columns.duplicated() ]
X_train = X_train_test.iloc[:1460 ,:]
test = X_train_test.iloc[1460: , :]
audio_data_ameavo = '.. /input/birdsong-recognition/train_audio/ameavo/XC133080.mp3'
audio_data_mouchi = '.. /input/birdsong-recognition/train_audio/mouchi/XC109939.mp3'
audio_data_nutwoo = '.. /input/birdsong-recognition/train_audio/nutwoo/XC109667.mp3'
audio_data_pygnut = '.. /input/birdsong-recognition/train_audio/pygnut/XC11739.mp3'
audio_data_vesspa = '.. /input/birdsong-recognition/train_audio/vesspa/XC102960.mp3'
audio_data_yetvir = '.. /input/birdsong-recognition/train_audio/yetvir/XC120867.mp3'
x_ameavo , sr = librosa.load(audio_data_ameavo)
 x_mouchi , sr = librosa.load(audio_data_mouchi)
 x_nutwoo , sr = librosa.load(audio_data_nutwoo)
 x_pygnut , sr = librosa.load(audio_data_pygnut)
 x_vesspa , sr = librosa.load(audio_data_vesspa)
 x_yetvir , sr = librosa.load(audio_data_yetvir)
 print(type(x_ameavo), type(sr))
print("Ameavo :", x_ameavo.shape, sr)
 print("Mouchi :", x_mouchi.shape, sr)
 print("Nutwoo :", x_nutwoo.shape, sr)
 print("Pygnut :", x_pygnut.shape, sr)
 print("Vesspa :", x_vesspa.shape, sr)
 print("Yetvir :", x_yetvir.shape, sr )<load_pretrained>
def objective(trial):
train_x, test_x, train_y, test_y = train_test_split(X_train, Y_train , test_size=0.2,random_state=42)
 param = {
'tree_method':'gpu_hist',
'lambda': trial.suggest_loguniform('lambda', 1e-2, 100.0),
'alpha': trial.suggest_loguniform('alpha', 1e-2, 100.0),
'colsample_bytree': trial.suggest_categorical('colsample_bytree', [0.1,0.2,0.3,0.4,0.5,0.6,0.7, 0.8]),
'subsample': trial.suggest_categorical('subsample', [0.4,0.5,0.6,0.7,0.8,1.0]),
'learning_rate': trial.suggest_float('learning_rate', 0.025,0.1),
'n_estimators': 1500,
'max_depth': trial.suggest_int('max_depth', 2,9),
'random_state': trial.suggest_categorical('random_state', [24, 48,2020]),
'min_child_weight': trial.suggest_int('min_child_weight', 1, 20),
}
model = xgb.XGBRegressor(**param)
 model.fit(train_x,train_y,eval_set=[(test_x,test_y)],early_stopping_rounds=100,verbose=False)
 preds = model.predict(test_x)
 rmse = mean_squared_error(test_y, preds,squared=False)
 return rmse
librosa.load(audio_data_ameavo)
 librosa.load(audio_data_mouchi)
 librosa.load(audio_data_nutwoo)
 librosa.load(audio_data_pygnut)
 librosa.load(audio_data_vesspa)
 librosa.load(audio_data_yetvir )<train_model>
best_model = optuna.create_study(direction= 'minimize')
 best_model.optimize(objective, n_trials = 50)
print("Song of Ameavo :")
 ipd.Audio(audio_data_ameavo )<train_model>
model = xgb.XGBRegressor(**best_model.best_trial.params)
 x_train , x_val , y_train , y_val = train_test_split(X_train , Y_train , random_state = 0 , test_size = 0.1)
 model.fit(x_train , y_train)
 mean_squared_error(y_val, model.predict(x_val), squared=False )
<train_model><EOS> 
y_test = model.predict(test)
 submission['SalePrice'] = y_test
submission.to_csv('./HousePrice_submission.csv', index=False )