Kaggle competition: INGV - Volcanic Eruption Prediction
<SOS> metric: MAE Kaggle data source: ingv-volcanic-eruption-prediction<data_type_conversions>
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
df.loc[df['GarageType'].isnull() , ['GarageYrBlt']] = 0
df['GarageType'].fillna('None', inplace=True)
 df['GarageFinish'].fillna('None',inplace=True)
 df['GarageCars'].fillna(0,inplace=True)
 df['GarageArea'].fillna(0,inplace=True)
 df['GarageQual'].fillna('None',inplace=True)
 df['GarageCond'].fillna('None',inplace=True )<data_type_conversions>
sample_submission = pd.read_csv('/kaggle/input/predict-volcanic-eruptions-ingv-oe/sample_submission.csv')
 sample_submission.head(10 )
df['FireplaceQu'].fillna('None', inplace=True )<feature_engineering>
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 )
for feature in ['Exterior1st', 'Exterior2nd', 'Electrical', 'KitchenQual', 'MSZoning', 'Functional', 'SaleType', 'Utilities']:
df[feature] = df.groupby(['Neighborhood', 'MSSubClass'])[feature].apply(lambda x: x.fillna(x.mode() [0]))
df['LotFrontage'] = df.groupby(['Neighborhood'])['LotFrontage'].apply(lambda x : x.fillna(x.median()))<train_model>
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 )
print('Training Set SalePrice Skew: {}'.format(train['SalePrice'].skew()))
print('Training Set SalePrice Kurtosis: {}'.format(train['SalePrice'].kurt()))
print('Training Set SalePrice Mean: {}'.format(train['SalePrice'].mean()))
print('Training Set SalePrice Median: {}'.format(train['SalePrice'].median()))
print('Training Set SalePrice Max: {}'.format(train['SalePrice'].max())) 
<filter>
sum(sample_submission['segment_id']==test_set['segment_id'])/ len(test_set )
train_corr[train_corr['Feature_1'] == 'SalePrice']<feature_engineering>
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()
df['TotalSF'] = df['TotalBsmtSF'] + df['1stFlrSF'] + df['2ndFlrSF']
df['TotalBath'] = df['FullBath']+df['BsmtFullBath']+(df['BsmtHalfBath']+df['HalfBath'])*0.5
df['TotalPorchSF'] = df['OpenPorchSF'] + df['3SsnPorch'] + df['EnclosedPorch'] + df['ScreenPorch']<compute_test_metric>
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'
df['YearBuiltRemod'] = df['YearBuilt'] + df['YearRemodAdd']
print(f'Correaltion with Target variable: {df.SalePrice.corr(df.YearBuiltRemod ).round(3)}' )<feature_engineering>
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
df['HasPool'] = df['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 df['HasFireplace'] = df['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 df['isNewer'] = 0
df.loc[df['YrSold'] == df['YearBuilt'], 'isNewer'] = 1<data_type_conversions>
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
<define_variables><EOS> 
sample_submission['time_to_eruption'] = test_predictions
sample_submission.to_csv('submission.csv', index=False )