Kaggle competition: INGV - Volcanic Eruption Prediction
predict_model(traindf, testdf, predictors_var, outcome_var, svc )<choose_model_class>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from glob import glob
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
knn = KNeighborsClassifier(n_neighbors=3 )<predict_on_test>
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 )
predict_model(traindf, testdf, predictors_var, outcome_var, knn )<choose_model_class>
train_paths = sorted(glob('/kaggle/input/predict-volcanic-eruptions-ingv-oe/train/*'))
gaussian = GaussianNB()<predict_on_test>
sub = pd.read_csv('/kaggle/input/predict-volcanic-eruptions-ingv-oe/sample_submission.csv' )
predict_model(traindf, testdf, predictors_var, outcome_var, gaussian )<choose_model_class>
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
decision_tree = DecisionTreeClassifier()<predict_on_test>
train_samples = np.array(train_samples ).reshape(-1,40)
 del(train_paths)
 gc.collect()
predict_model(traindf, testdf, predictors_var, outcome_var, decision_tree )<choose_model_class>
cat = CatBoostRegressor(loss_function='MAE',iterations = 5000)
 cat.fit(train_samples,y )
random_forest = RandomForestClassifier(n_estimators=100 )<predict_on_test>
print(mean_absolute_error(y,cat.predict(train_samples)) )
predict_model(traindf, testdf, predictors_var, outcome_var, random_forest )<predict_on_test>
test_paths = sorted(glob('/kaggle/input/predict-volcanic-eruptions-ingv-oe/test/*'))
predict_model(traindf, testdf, predictors_var, outcome_var, random_forest)[1]<predict_on_test>
%%time
test_samples = [Parallel(n_jobs=4 )(delayed(feature_gen )(test_path)for test_path in tqdm_notebook(test_paths)) ]
results = predict_model(traindf, testdf, predictors_var, outcome_var, random_forest)[1]<create_dataframe>
test_samples = np.array(test_samples ).reshape(-1,40)
 del(test_paths)
 gc.collect()
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": results
} )<save_to_csv>
preds = cat.predict(test_samples )
submission.to_csv('submission_updated.csv', index=False )<import_modules>
sub['time_to_eruption'] = preds
<load_from_csv><EOS> 
sub.to_csv('submission.csv', header=True, index=False )