Kaggle competition: INGV - Volcanic Eruption Prediction
<SOS> metric: MAE Kaggle data source: ingv-volcanic-eruption-prediction<categorify>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
from sklearn.model_selection import train_test_split
from lightgbm import LGBMRegressor
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import mean_absolute_error as mae
dataset = pd.get_dummies(dataset)
 X = dataset[:len(home)]
test = dataset[len(home):]
X.shape<split>
sample_submission = pd.read_csv('/kaggle/input/predict-volcanic-eruptions-ingv-oe/sample_submission.csv')
 sample_submission.head(10 )
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.20, random_state=0 )<define_search_space>
train_set = pd.read_csv('/kaggle/input/volcano-stft-data/output_train_set.csv', index_col=0 ).reset_index(drop=True)
 train_set.head(10 )
kfolds = KFold(n_splits=5, shuffle=True, random_state=42)
 e_alphas = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007]
e_l1ratio = [0.8, 0.85, 0.9, 0.95, 0.99, 1]
alphas_alt = [14.5, 14.6, 14.7, 14.8, 14.9, 15, 15.1, 15.2, 15.3, 15.4, 15.5]
alphas2 = [0.00005, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008]<compute_train_metric>
test_set = pd.read_csv('/kaggle/input/volcano-stft-data/output_test_set.csv', index_col=0 ).reset_index(drop=True)
 test_set.head(10 )
lasso = make_pipeline(RobustScaler() , LassoCV(max_iter=1e7, alphas = alphas2, random_state = 42, cv=kfolds))
lasso_score = cross_val_score(lasso, X, y, cv=kfolds, scoring='neg_mean_squared_error')
 lasso_rmse = np.sqrt(-lasso_score.mean())
 print("LASSO RMSE: ", lasso_rmse)
 print("LASSO STD: ", lasso_score.std() )<train_model>
sum(sample_submission['segment_id']==test_set['segment_id'])/ len(test_set )
lasso.fit(X_train, y_train )<compute_train_metric>
train_set['time_to_eruption_pc'] = train_set['time_to_eruption'] / train_set['time_to_eruption'].max()
NFOLDS=10
skf = StratifiedKFold(n_splits=NFOLDS)
 train_set['label_strat'] = np.round(train_set['time_to_eruption_pc'] * 20, 0)
 train_set['fold']=0
f=0
for trn_idx, val_idx in skf.split(train_set[['segment_id']], train_set['label_strat']):
train_set.loc[val_idx, 'fold']=f
f+=1
train_set['fold'].value_counts()
ridge = make_pipeline(RobustScaler() , RidgeCV(alphas = alphas_alt, cv=kfolds))
ridge_score = cross_val_score(ridge, X, y, cv=kfolds, scoring='neg_mean_squared_error')
 ridge_rmse = np.sqrt(-ridge_score.mean())
 print("RIDGE RMSE: ", ridge_rmse)
 print("RIDGE STD: ", ridge_score.std() )<train_model>
DROP_FTS = ['segment_id', 'time_to_eruption','h:m:s', 'label_strat', 'fold', 'time_to_eruption_pc',]
SEL_FTS = [x for x in train_set.columns if x not in DROP_FTS]
LABEL = 'time_to_eruption'
ridge.fit(X_train, y_train )<compute_train_metric>
optuna.logging.set_verbosity(optuna.logging.WARNING)
 OPTUNA_TRIALS = 30
class Optimizer:
def __init__(self, metric, trials=OPTUNA_TRIALS):
self.metric = metric
self.trials = trials
self.sampler = TPESampler(seed=42)
 def objective(self, trial):
model = create_model(trial)
 model.fit(X_train, y_train)
 preds = model.predict(X_val)
 if self.metric == 'mae':
return -mae(y_val, preds)
 else:
return -np.sqrt(mse(y_val, preds))
def optimize(self):
study = optuna.create_study(direction="maximize", sampler=self.sampler)
 study.optimize(self.objective, n_trials=self.trials)
 return study.best_params
def create_model(trial):
colsample_bytree = trial.suggest_uniform("colsample_bytree", 0.2, 0.95)
 subsample = trial.suggest_uniform("subsample", 0.2, 0.9)
 reg_alpha = trial.suggest_uniform("reg_alpha", 0.001, 100)
 max_depth = trial.suggest_int("max_depth", 1, 15)
 min_child_samples = trial.suggest_int('min_child_samples', 1, 15)
 n_estimators = trial.suggest_int('n_estimators', 100, 250)
 model = LGBMRegressor(random_state = 42,
max_depth = max_depth,
n_estimators = n_estimators,
colsample_bytree=colsample_bytree,
subsample=subsample,
reg_alpha=reg_alpha,
learning_rate = 0.05)
 return model
elasticnet = make_pipeline(RobustScaler() , ElasticNetCV(max_iter=1e7, alphas=e_alphas, cv=kfolds, l1_ratio=e_l1ratio))
elastic_score = cross_val_score(elasticnet, X, y, cv=kfolds, scoring='neg_mean_squared_error')
 elastic_rmse = np.sqrt(-elastic_score.mean())
 print("ELASTICNET RMSE: ", elastic_rmse)
 print("ELASTICNET STD: ", elastic_score.std() )<train_model>
param_outputs = []
FOLD_VALUES = [x for x in train_set['fold'].unique() ]
for fold in train_set['fold'].unique() :
trn_idx = train_set['fold']!=fold
val_idx = train_set['fold']==fold
X_train = train_set.loc[trn_idx,SEL_FTS].values
y_train = train_set.loc[trn_idx, LABEL].values
X_val = train_set.loc[val_idx,SEL_FTS].values
y_val = train_set.loc[val_idx, LABEL].values
optimizer = Optimizer('mse')
 lgb_params = optimizer.optimize()
param_outputs += [lgb_params]
lgb_params['random_state'] = 42
model_output = LGBMRegressor(**lgb_params)
 print(fold, lgb_params)
 model_output.fit(X_train, y_train)
 preds = model_output.predict(X_val)
 print(mae(y_val, preds))
param_headings = ['colsample_bytree','subsample', 'reg_alpha',
'max_depth', 'min_child_samples', 'n_estimators', 'random_state']
params_output_df = pd.DataFrame(columns=param_headings, index=FOLD_VALUES, data=0.0)
 for idx in params_output_df.index:
for h in param_headings:
params_output_df.at[idx, h] = param_outputs[idx][h]
print('saving CSV')
 params_output_df.to_csv('optuna_lgbm_params.csv', index=True)
 param_means = params_output_df.mean(axis=0)
 param_means
elasticnet.fit(X_train, y_train )<choose_model_class>
sample_submission['time_to_eruption'] = test_predictions
sample_submission.to_csv('submission.csv', index=False )