Kaggle competition: INGV - Volcanic Eruption Prediction
<SOS> metric: MAE Kaggle data source: ingv-volcanic-eruption-prediction<define_variables>
import numpy as np
import pandas as pd
import scipy
from scipy import signal
import random
import pickle
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
import xgboost
percentages = []
first = 136 / 216
second = 87/ 184
third = 119/491
percentages.append(first)
 percentages.append(second)
 percentages.append(third )<create_dataframe>
percents = pd.DataFrame(percentages)
 percents.index+=1<feature_engineering>
train['Family'] = train.apply(lambda x: x['SibSp'] + x['Parch'], axis = 1)
 test['Family'] = test.apply(lambda x: x['SibSp'] + x['Parch'], axis = 1 )<drop_column>
train.drop(['SibSp', 'Parch', 'Name', 'Ticket'], axis = 1, inplace = True)
 test.drop(['SibSp', 'Parch', 'Name', 'Ticket'], axis = 1, inplace = True )<count_missing_values>
test.isna().sum()<correct_missing_values>
train_df_stft = pd.read_pickle('.. /input/volcano-features/train_df_stft.pkl')
 time = train_df_stft['time_to_eruption']
train_df_stft = train_df_stft.drop(['segment_id', 'time_to_eruption'], axis=1)
 train_df_sd = pd.read_pickle('.. /input/volcano-features/train_df_sd.pkl')
 test_df_stft = pd.read_pickle('.. /input/volcano-features/test_df_stft.pkl')
 test_df_stft = test_df_stft.drop(['segment_id'],axis = 1)
 test_df_sd = pd.read_pickle('.. /input/volcano-features/test_df_sd.pkl' )
test['Fare'].fillna(test['Fare'].median() , inplace = True )<categorify>
pca = PCA(n_components=0.999,svd_solver="full")
 train_pca = pca.fit_transform(train_df_sd)
 test_pca = pca.transform(test_df_sd)
 train_pca = pd.DataFrame(train_pca)
 test_pca = pd.DataFrame(test_pca )
train_df = pd.get_dummies(train)
 test_df = pd.get_dummies(test )<drop_column>
train_df = pd.concat([train_df_stft,train_pca] ,axis=1)
 test_df = pd.concat([test_df_stft,test_pca],axis = 1 )
train_df.drop('PassengerId', axis = 1, inplace = True )<prepare_x_and_y>
input_df = pd.read_csv('.. /input/predict-volcanic-eruptions-ingv-oe/train.csv')
 input_df = input_df.sort_values("time_to_eruption")
 fold_list = [1,2,3,4,5]
folds = []
for i in range(int(( input_df.shape[0]-1)/5)) :
random.shuffle(fold_list)
 folds.extend(fold_list)
 folds = folds + [1]
input_df['fold'] = folds
y = train_df['Survived']
train_df.drop('Survived', axis = 1, inplace = True)
 train_df.drop('Cabin_T', axis = 1, inplace = True)
 test_df.drop('PassengerId', axis = 1, inplace = True )<prepare_x_and_y>
predictions = np.zeros(len(test_df))
for fold in range(1,6):
train_index_list = input_df[input_df['fold'] != fold].index
test_index_list = input_df[input_df['fold'] == fold].index
X_train = train_df.iloc[train_index_list]
y_train = time[train_index_list]
X_val = train_df.iloc[test_index_list]
y_val = time[test_index_list]
model = xgboost.XGBRegressor(n_estimators=100000,tree_method='gpu_hist',max_depth=8,learning_rate=0.05,alpha=0.1,SUBSAMPLE=0.6)
 eval_set = [(X_val, y_val)]
model.fit(X_train, y_train,early_stopping_rounds=5,eval_metric='mae', eval_set=eval_set, verbose=False)
 print(model.evals_result() ['validation_0']['mae'][-5:])
 predictions += model.predict(test_df)
 predictions = predictions/5
sample_submission_df1=pd.read_csv('.. /input/predict-volcanic-eruptions-ingv-oe/sample_submission.csv')
 sample_submission_df1['time_to_eruption']=predictions
sample_submission_df1.to_csv('xgb_5fldst_ft_sdpca_stft.csv',index=False )