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
from glob import glob
import pandas as pd
import os
import dask.dataframe as dd
from tsfresh.feature_extraction import feature_calculators
import librosa
import pywt
from joblib import Parallel, delayed
from tqdm import tqdm_notebook
import scipy as sp
import itertools
import gc
from sklearn.metrics import mean_absolute_error
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor
from xgboost import XGBRegressor
import lightgbm as lgb
import gc
def mydeap(mungedtrain, epochs):
inputs = mungedtrain.drop('Survived', axis = 1 ).values.tolist()
outputs = mungedtrain['Survived'].values.tolist()
def protectedDiv(left, right):
try:
return left / right
except ZeroDivisionError:
return 1
pset = gp.PrimitiveSet("MAIN", 26)
 pset.addPrimitive(operator.add, 2)
 pset.addPrimitive(operator.sub, 2)
 pset.addPrimitive(operator.mul, 2)
 pset.addPrimitive(protectedDiv, 2)
 pset.addPrimitive(operator.neg, 1)
 pset.addPrimitive(math.cos, 1)
 pset.addPrimitive(math.sin, 1)
 pset.addPrimitive(max, 2)
 pset.addPrimitive(min, 2)
 pset.renameArguments(ARG0='x1')
 pset.renameArguments(ARG1='x2')
 pset.renameArguments(ARG2='x3')
 pset.renameArguments(ARG3='x4')
 pset.renameArguments(ARG4='x5')
 pset.renameArguments(ARG5='x6')
 pset.renameArguments(ARG6='x7')
 pset.renameArguments(ARG7='x8')
 pset.renameArguments(ARG8='x9')
 pset.renameArguments(ARG9='x10')
 pset.renameArguments(ARG10='x11')
 pset.renameArguments(ARG11='x12')
 pset.renameArguments(ARG12='x13')
 pset.renameArguments(ARG13='x14')
 pset.renameArguments(ARG14='x15')
 pset.renameArguments(ARG15='x16')
 pset.renameArguments(ARG16='x17')
 pset.renameArguments(ARG17='x18')
 pset.renameArguments(ARG18='x19')
 pset.renameArguments(ARG19='x20')
 pset.renameArguments(ARG20='x21')
 pset.renameArguments(ARG21='x22')
 pset.renameArguments(ARG22='x23')
 pset.renameArguments(ARG23='x24')
 pset.renameArguments(ARG24='x25')
 pset.renameArguments(ARG25='x26')
 creator.create("FitnessMin", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin)
 toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=3)
 toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
 toolbox.register("population", tools.initRepeat, list, toolbox.individual)
 toolbox.register("compile", gp.compile, pset=pset)
 def evalSymbReg(individual):
func = toolbox.compile(expr=individual)
 return sum(round(1.-(1./(1.+np.exp(-func(*in_)))))== out for in_, out in zip(inputs, outputs)) /len(mungedtrain),
toolbox.register("evaluate", evalSymbReg)
 toolbox.register("select", tools.selTournament, tournsize=3)
 toolbox.register("mate", gp.cxOnePoint)
 toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
 toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
 toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
random.seed(318)
 pop = toolbox.population(n=300)
 hof = tools.HallOfFame(1)
 stats_fit = tools.Statistics(lambda ind: ind.fitness.values)
 stats_size = tools.Statistics(len)
 mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size)
 mstats.register("avg", np.mean)
 mstats.register("std", np.std)
 mstats.register("min", np.min)
 mstats.register("max", np.max)
 pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.2, epochs, stats=mstats,
halloffame=hof, verbose=True)
 print(hof[0])
 func2 =toolbox.compile(expr=hof[0])
 return func2<categorify>
train = pd.read_csv('/kaggle/input/predict-volcanic-eruptions-ingv-oe/train.csv')
 train['segment_id'] = train['segment_id'].astype(str)
 train = train.sort_values('segment_id')
 y = train['time_to_eruption']
del(train )
if __name__ == "__main__":
train = train_dummies
test = test_dummies.columns
mungedtrain = train_dummies.astype(float)
 GeneticFunction = mydeap(mungedtrain, epochs = 100)
 mytrain = mungedtrain.drop('Survived', axis = 1 ).values.tolist()
trainPredictions = Outputs(np.array([GeneticFunction(*x)for x in mytrain]))
print(accuracy_score(mungedtrain.Survived.astype(int),trainPredictions.astype(int)))
 mungedtest = test_dummies.astype(float)
 mytest = mungedtest.values.tolist()
testPredictions = Outputs(np.array([GeneticFunction(*x)for x in mytest]))
pdtest = pd.DataFrame({'PassengerId': passengerId,
'Survived': testPredictions.astype(int)})
 pdtest.to_csv('gptest.csv', index=False )<import_modules>
train_paths = sorted(glob('/kaggle/input/predict-volcanic-eruptions-ingv-oe/train/*'))
import numpy as np
import pandas as pd<load_from_csv>
sub = pd.read_csv('/kaggle/input/predict-volcanic-eruptions-ingv-oe/sample_submission.csv' )
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 all = [train_data,test_data]<drop_column>
np.random.seed(1337)
 noise = np.random.normal(-10, 200, 60_001)
 def denoise_signal_simple(x, wavelet='db4', level=1):
coeff = pywt.wavedec(x, wavelet, mode="per")
 uthresh = 10
coeff[1:] =(pywt.threshold(i, value=uthresh, mode='hard')for i in coeff[1:])
 return pywt.waverec(coeff, wavelet, mode='per')
 def feature_gen(path):
X =(pd.read_csv(path ).fillna(200 ).values.T + noise ).T
z = X - np.median(X,axis=0)
 features = []
for i in range(X.shape[1]):
sig = z[:,i]
den_sample_simple = denoise_signal_simple(sig)
 mfcc = librosa.feature.mfcc(sig)
 mfcc_mean = mfcc.mean(axis=1)
 percentile_roll50_std_20 = np.percentile(pd.Series(sig ).rolling(50 ).std().dropna().values, 20)
 features.extend([feature_calculators.number_peaks(den_sample_simple, 2),percentile_roll50_std_20,mfcc_mean[18],mfcc_mean[4]])
 return features
for data in all:
data.drop(['PassengerId','Name','Ticket'],axis = 1,inplace=True )<data_type_conversions>
train_samples = np.array(train_samples ).reshape(-1,40)
 del(train_paths)
 gc.collect()
for data in all:
data['Sex'] = data['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 train_data.head()<drop_column>
test_paths = sorted(glob('/kaggle/input/predict-volcanic-eruptions-ingv-oe/test/*'))
for data in all:
data.drop(['Cabin'],axis = 1,inplace= True )<feature_engineering>
%%time
test_samples = [Parallel(n_jobs=4 )(delayed(feature_gen )(test_path)for test_path in tqdm_notebook(test_paths)) ]
for data in all:
for i in range(0,2):
for j in range(1,4):
age_mean = data[(data['Sex'] == i)&(data['Pclass'] == j)]['Age'].dropna().mean()
data.loc[(data.Age.isnull())&(data.Sex == i)&(data.Pclass == j),'Age'] = age_mean<data_type_conversions>
test_samples = np.array(test_samples ).reshape(-1,40)
 del(test_paths)
 gc.collect()
train_data['Embarked'] = train_data['Embarked'].fillna(train_data.Embarked.mode(dropna=True)[0] )<filter>
train_features = pd.DataFrame(train_samples)
 train_targets = pd.DataFrame({'target': y})
 test_features = pd.DataFrame(test_samples)
 train_features.head()
test_data.loc[(data.Fare.isnull())]<data_type_conversions>
train_data = pd.concat([train_features, train_targets], axis = 1)
 test_features['target'] = None
train_data.head()
test_data['Fare'].fillna(test_data[(test_data['Pclass'] == 3)]['Pclass'].dropna().mean() ,inplace = True)
<drop_column>
all_data = pd.concat([train_data, test_features])
 all_data.head()
for data in all:
data['Family'] = data['Parch'] + data['SibSp'] + 1
data.drop(['Parch','SibSp'],axis = 1,inplace = True)
<categorify>
all_data['id'] = all_data.index
all_data.head()
for data in all:
data['Embarked'] = data['Embarked'].map({'S': 1, 'C': 2,'Q' : 3})
 train_data.head()<prepare_x_and_y>
all_data['target'] = all_data['target'].astype('float64' )
X_train = train_data.drop(['Survived'],axis = 1)
 y_train = train_data.Survived<normalization>
scaler = StandardScaler()
for col in all_data.drop(['target', 'id'],axis=1 ).columns:
all_data[col] = scaler.fit_transform(np.array(all_data[col] ).reshape(-1, 1))
all_data.head()
scalerModel = StandardScaler()
X_train = scalerModel.fit_transform(X_train)
 test_data = scalerModel.fit_transform(test_data )<split>
train_data = all_data[:train_features.shape[0]]
test_data = all_data[train_features.shape[0]:]
X_train,X_test,y_train,y_test = train_test_split(X_train,y_train,test_size = 0.21,shuffle = True,random_state=33 )<define_variables>
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.preprocessing import MinMaxScaler, StandardScaler, scale
train_scores = []
test_scores = []<train_model>
train_features = train_data.drop(['target'],axis=1)
 train_targets = pd.DataFrame(train_data['target'])
 test_features = test_data.drop(['target'],axis=1)
 train_targets.head()
SVCModel = SVC(kernel= 'rbf',
max_iter=3000,C=.10,gamma='auto')
 SVCModel.fit(X_train, y_train)
 print('train data score',SVCModel.score(X_train,y_train))
print('test data score',SVCModel.score(X_test,y_test))
train_scores.append(SVCModel.score(X_train,y_train))
test_scores.append(SVCModel.score(X_test,y_test))<choose_model_class>
hyper_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': ['mae'],
'learning_rate': 0.001,
'feature_fraction': 0.9,
'subsample': 0.85,
'subsample_freq': 2,
'verbose': 0,
"max_depth": 40,
"num_leaves": 250,
"max_bin": 128,
"num_iterations": 10000,
'device': 'gpu',
'gpu_platform_id': 0,
'gpu_device_id': 0
}
<train_model><EOS> 
score = []
skf = KFold(n_splits = 10, shuffle=True, random_state=123)
 skf.get_n_splits(train_features, train_targets)
 oof_lgbm_df = pd.DataFrame()
predictions = pd.DataFrame(test_features['id'])
 x_test = test_features.drop(['id'], axis = 1)
 for fold,(trn_idx, val_idx)in enumerate(skf.split(train_features, train_targets)) :
x_train, y_train = train_features.iloc[trn_idx], train_targets.iloc[trn_idx]['target']
x_valid, y_valid = train_features.iloc[val_idx], train_targets.iloc[val_idx]['target']
index = x_valid['id']
x_train = x_train.drop(['id'], axis = 1)
 x_valid = x_valid.drop(['id'], axis = 1)
 p_valid = 0
yp = 0
gbm = lgb.LGBMRegressor(**hyper_params)
 gbm.fit(x_train, y_train,
eval_set=[(x_valid, y_valid)],
eval_metric='mae',
verbose = 500,
early_stopping_rounds=100)
 score.append(mean_absolute_error(gbm.predict(x_valid), y_valid))
yp += gbm.predict(x_test)
 fold_pred = pd.DataFrame({'ID': index,
'label':gbm.predict(x_valid)})
 oof_rfr_df = pd.concat([oof_lgbm_df, fold_pred], axis=0)
 predictions['fold{}'.format(fold+1)] = yp