Kaggle competition: INGV - Volcanic Eruption Prediction
<SOS> metric: MAE Kaggle data source: ingv-volcanic-eruption-prediction<categorify>
import re
import pandas as pd
import numpy as np
from math import sqrt, ceil
from tqdm.notebook import tqdm
import matplotlib.pyplot as plt
from lightgbm import LGBMRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import mean_absolute_error as mae
from sklearn.feature_selection import VarianceThreshold
from sklearn.preprocessing import MinMaxScaler
def title2int(data):
data["Title"].replace(["Major", "Capt", "Sir", "Dr", "Don", "Mlle", "Mme", "Ms", "Dona", "Lady", "the Countess", "Jonkheer", "Col", "Rev"],
["Mr", "Mr", "Mr", "Mr", "Mr", "Miss", "Miss", "Miss", "Mrs", "Mrs", "Mrs", "Other", "Other", "Other"], inplace = True)
 data["Title"].replace(["Mr", "Miss", "Mrs", "Master", "Other"], [0, 1, 2, 3, 4], inplace = True)
 return data
train = title2int(train )<groupby>
def get_train_dataset(train_features_df, labels_df):
_df = train_features_df.merge(labels_df, on='segment_id', how='left')
 _y = _df['time_to_eruption']
_x = _df.drop(['segment_id', 'time_to_eruption'], axis=1)
 return(_x, _y )
train.groupby("Title")["Age"].mean()<groupby>
def get_dataset_by_range(features_df, labels_df, lower_limit, upper_limit):
_df = features_df.merge(labels_df, on='segment_id', how='left')
 _df = _df[_df['time_to_eruption'].notna() ]
_df = _df[(_df['time_to_eruption'] > lower_limit)&(_df['time_to_eruption'] < upper_limit)]
_y = _df['time_to_eruption']
_x = _df.drop(['segment_id', 'time_to_eruption'], axis=1)
 _x = _df.rename(columns = lambda x:re.sub('[^A-Za-z0-9_]+', '_', x))
return(_x, _y )
train.groupby("Pclass")["Fare"].mean()<feature_engineering>
train_df = pd.read_parquet('/kaggle/input/ingv-parquet/train_features.parquet')
 test_df = pd.read_parquet('/kaggle/input/ingv-parquet/test_features.parquet')
 labels_df = pd.read_parquet('/kaggle/input/ingv-parquet/labels.parquet' )
def fareG2int(data):
data["Fare_group"] = "NaN"
data.loc[data["Fare"] < 10, "Fare_group"] = 2
data.loc[(data["Fare"] >= 10)&(data["Fare"] < 65), "Fare_group"] = 2
data.loc[data["Fare"] >= 65, "Fare_group"] = 1
return data
train = fareG2int(train )<feature_engineering>
train_df = train_df.rename(columns = lambda x:re.sub('[^A-Za-z0-9_]+', '_', x))
test_df = test_df.rename(columns = lambda x:re.sub('[^A-Za-z0-9_]+', '_', x))
train["Embarked"] = train["Embarked"].fillna("S" )<categorify>
features = [
'sensor_1__value_count__value_1',
'sensor_1__value_count__value_-1',
'sensor_4__median',
'sensor_6__value_count__value_1',
'sensor_6__value_count__value_-1',
'sensor_10__median',
'sensor_10__value_count__value_0',
'sensor_10__linear_trend__attr_"pvalue"',
'sensor_7__mean_change',
'sensor_7__first_location_of_minimum',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_0.2__ql_0.0',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_0.4__ql_0.0',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_0.4__ql_0.2',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_0.6__ql_0.2',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_0.8__ql_0.2',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_0.6__ql_0.4',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_0.8__ql_0.4',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_0.8__ql_0.6',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_1.0__ql_0.6',
'sensor_7__change_quantiles__f_agg_"mean"__isabs_False__qh_1.0__ql_0.8',
'sensor_7__value_count__value_1',
'sensor_7__value_count__value_-1',
'sensor_2__abs_energy',
'sensor_2__mean_abs_change',
'sensor_2__kurtosis',
'sensor_2__longest_strike_below_mean',
'sensor_2__longest_strike_above_mean',
'sensor_2__cid_ce__normalize_True',
'sensor_2__number_cwt_peaks__n_1',
'sensor_2__number_cwt_peaks__n_5',
'sensor_2__number_peaks__n_1',
'sensor_2__number_peaks__n_3',
'sensor_2__number_peaks__n_5',
'sensor_2__number_peaks__n_10',
'sensor_2__spkt_welch_density__coeff_2',
'sensor_2__spkt_welch_density__coeff_5',
'sensor_2__spkt_welch_density__coeff_8',
'sensor_2__ar_coefficient__coeff_1__k_10',
'sensor_2__ar_coefficient__coeff_2__k_10',
'sensor_2__ar_coefficient__coeff_3__k_10',
'sensor_2__ar_coefficient__coeff_4__k_10',
'sensor_2__ar_coefficient__coeff_7__k_10',
'sensor_2__ar_coefficient__coeff_8__k_10',
'sensor_2__ar_coefficient__coeff_9__k_10',
'sensor_2__ar_coefficient__coeff_10__k_10',
'sensor_2__change_quantiles__f_agg_"var"__isabs_False__qh_0.6__ql_0.2',
'sensor_2__fft_coefficient__attr_"abs"__coeff_7',
'sensor_2__fft_coefficient__attr_"abs"__coeff_8',
'sensor_2__fft_coefficient__attr_"abs"__coeff_9',
'sensor_2__fft_coefficient__attr_"abs"__coeff_10',
'sensor_2__fft_coefficient__attr_"abs"__coeff_11',
'sensor_2__fft_coefficient__attr_"abs"__coeff_12',
'sensor_2__fft_coefficient__attr_"abs"__coeff_13',
'sensor_2__fft_coefficient__attr_"abs"__coeff_14',
'sensor_2__fft_coefficient__attr_"abs"__coeff_15',
'sensor_2__fft_coefficient__attr_"abs"__coeff_16',
'sensor_2__fft_coefficient__attr_"abs"__coeff_17',
'sensor_2__fft_coefficient__attr_"abs"__coeff_18',
'sensor_2__fft_coefficient__attr_"abs"__coeff_19',
'sensor_2__fft_coefficient__attr_"abs"__coeff_20',
'sensor_2__fft_coefficient__attr_"abs"__coeff_21',
'sensor_2__fft_coefficient__attr_"abs"__coeff_22',
'sensor_2__fft_coefficient__attr_"abs"__coeff_24',
'sensor_2__approximate_entropy__m_2__r_0.1',
'sensor_2__approximate_entropy__m_2__r_0.3',
'sensor_2__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"mean"',
'sensor_2__number_crossing_m__m_0',
'sensor_2__permutation_entropy__dimension_3__tau_1',
'sensor_9__abs_energy',
'sensor_9__mean_abs_change',
'sensor_9__standard_deviation',
'sensor_9__kurtosis',
'sensor_9__longest_strike_below_mean',
'sensor_9__longest_strike_above_mean',
'sensor_9__maximum',
'sensor_9__cid_ce__normalize_True',
'sensor_9__quantile__q_0.6',
'sensor_9__agg_autocorrelation__f_agg_"mean"__maxlag_40',
'sensor_9__agg_autocorrelation__f_agg_"median"__maxlag_40',
'sensor_9__number_cwt_peaks__n_1',
'sensor_9__number_cwt_peaks__n_5',
'sensor_9__number_peaks__n_1',
'sensor_9__number_peaks__n_3',
'sensor_9__number_peaks__n_5',
'sensor_9__number_peaks__n_10',
'sensor_9__number_peaks__n_50',
'sensor_9__binned_entropy__max_bins_10',
'sensor_9__spkt_welch_density__coeff_2',
'sensor_9__spkt_welch_density__coeff_5',
'sensor_9__spkt_welch_density__coeff_8',
'sensor_9__ar_coefficient__coeff_1__k_10',
'sensor_9__ar_coefficient__coeff_2__k_10',
'sensor_9__ar_coefficient__coeff_3__k_10',
'sensor_9__ar_coefficient__coeff_4__k_10',
'sensor_9__ar_coefficient__coeff_5__k_10',
'sensor_9__ar_coefficient__coeff_6__k_10',
'sensor_9__ar_coefficient__coeff_7__k_10',
'sensor_9__ar_coefficient__coeff_8__k_10',
'sensor_9__ar_coefficient__coeff_9__k_10',
'sensor_9__change_quantiles__f_agg_"var"__isabs_False__qh_0.4__ql_0.2',
'sensor_9__change_quantiles__f_agg_"var"__isabs_False__qh_0.6__ql_0.4',
'sensor_9__fft_coefficient__attr_"real"__coeff_6',
'sensor_9__fft_coefficient__attr_"real"__coeff_11',
'sensor_9__fft_coefficient__attr_"real"__coeff_12',
'sensor_9__fft_coefficient__attr_"real"__coeff_13',
'sensor_9__fft_coefficient__attr_"real"__coeff_14',
'sensor_9__fft_coefficient__attr_"real"__coeff_31',
'sensor_9__fft_coefficient__attr_"imag"__coeff_6',
'sensor_9__fft_coefficient__attr_"imag"__coeff_11',
'sensor_9__fft_coefficient__attr_"imag"__coeff_12',
'sensor_9__fft_coefficient__attr_"imag"__coeff_30',
'sensor_9__fft_coefficient__attr_"imag"__coeff_32',
'sensor_9__fft_coefficient__attr_"abs"__coeff_1',
'sensor_9__fft_coefficient__attr_"abs"__coeff_2',
'sensor_9__fft_coefficient__attr_"abs"__coeff_3',
'sensor_9__fft_coefficient__attr_"abs"__coeff_4',
'sensor_9__fft_coefficient__attr_"abs"__coeff_5',
'sensor_9__fft_coefficient__attr_"abs"__coeff_6',
'sensor_9__fft_coefficient__attr_"abs"__coeff_7',
'sensor_9__fft_coefficient__attr_"abs"__coeff_8',
'sensor_9__fft_coefficient__attr_"abs"__coeff_9',
'sensor_9__fft_coefficient__attr_"abs"__coeff_10',
'sensor_9__fft_coefficient__attr_"abs"__coeff_11',
'sensor_9__fft_coefficient__attr_"abs"__coeff_12',
'sensor_9__fft_coefficient__attr_"abs"__coeff_13',
'sensor_9__fft_coefficient__attr_"abs"__coeff_14',
'sensor_9__fft_coefficient__attr_"abs"__coeff_15',
'sensor_9__fft_coefficient__attr_"abs"__coeff_16',
'sensor_9__fft_coefficient__attr_"abs"__coeff_17',
'sensor_9__fft_coefficient__attr_"abs"__coeff_18',
'sensor_9__fft_coefficient__attr_"abs"__coeff_19',
'sensor_9__fft_coefficient__attr_"abs"__coeff_20',
'sensor_9__fft_coefficient__attr_"abs"__coeff_21',
'sensor_9__fft_coefficient__attr_"abs"__coeff_22',
'sensor_9__fft_coefficient__attr_"abs"__coeff_23',
'sensor_9__fft_coefficient__attr_"abs"__coeff_24',
'sensor_9__fft_coefficient__attr_"abs"__coeff_25',
'sensor_9__fft_coefficient__attr_"abs"__coeff_26',
'sensor_9__fft_coefficient__attr_"abs"__coeff_27',
'sensor_9__fft_coefficient__attr_"abs"__coeff_28',
'sensor_9__fft_coefficient__attr_"abs"__coeff_29',
'sensor_9__fft_coefficient__attr_"abs"__coeff_30',
'sensor_9__fft_coefficient__attr_"abs"__coeff_31',
'sensor_9__fft_coefficient__attr_"abs"__coeff_32',
'sensor_9__fft_coefficient__attr_"abs"__coeff_33',
'sensor_9__fft_coefficient__attr_"abs"__coeff_34',
'sensor_9__fft_coefficient__attr_"abs"__coeff_35',
'sensor_9__fft_coefficient__attr_"abs"__coeff_36',
'sensor_9__fft_coefficient__attr_"abs"__coeff_37',
'sensor_9__fft_coefficient__attr_"abs"__coeff_38',
'sensor_9__fft_coefficient__attr_"abs"__coeff_39',
'sensor_9__fft_coefficient__attr_"abs"__coeff_40',
'sensor_9__fft_coefficient__attr_"abs"__coeff_41',
'sensor_9__fft_coefficient__attr_"abs"__coeff_42',
'sensor_9__fft_coefficient__attr_"abs"__coeff_43',
'sensor_9__fft_coefficient__attr_"abs"__coeff_44',
'sensor_9__fft_coefficient__attr_"abs"__coeff_45',
'sensor_9__fft_coefficient__attr_"abs"__coeff_47',
'sensor_9__fft_coefficient__attr_"abs"__coeff_50',
'sensor_9__fft_coefficient__attr_"abs"__coeff_52',
'sensor_9__fft_coefficient__attr_"abs"__coeff_54',
'sensor_9__fft_coefficient__attr_"abs"__coeff_55',
'sensor_9__fft_coefficient__attr_"abs"__coeff_56',
'sensor_9__fft_coefficient__attr_"abs"__coeff_58',
'sensor_9__fft_coefficient__attr_"abs"__coeff_59',
'sensor_9__fft_coefficient__attr_"abs"__coeff_60',
'sensor_9__fft_coefficient__attr_"abs"__coeff_61',
'sensor_9__fft_coefficient__attr_"abs"__coeff_62',
'sensor_9__fft_coefficient__attr_"abs"__coeff_63',
'sensor_9__fft_coefficient__attr_"abs"__coeff_64',
'sensor_9__fft_coefficient__attr_"abs"__coeff_69',
'sensor_9__fft_coefficient__attr_"abs"__coeff_71',
'sensor_9__fft_coefficient__attr_"abs"__coeff_79',
'sensor_9__fft_coefficient__attr_"abs"__coeff_80',
'sensor_9__fft_coefficient__attr_"abs"__coeff_81',
'sensor_9__fft_coefficient__attr_"abs"__coeff_82',
'sensor_9__fft_coefficient__attr_"abs"__coeff_83',
'sensor_9__fft_coefficient__attr_"abs"__coeff_87',
'sensor_9__fft_coefficient__attr_"abs"__coeff_90',
'sensor_9__fft_coefficient__attr_"abs"__coeff_92',
'sensor_9__fft_coefficient__attr_"abs"__coeff_93',
'sensor_9__fft_coefficient__attr_"abs"__coeff_94',
'sensor_9__fft_coefficient__attr_"angle"__coeff_23',
'sensor_9__fft_coefficient__attr_"angle"__coeff_32',
'sensor_9__fft_coefficient__attr_"angle"__coeff_44',
'sensor_9__fft_coefficient__attr_"angle"__coeff_75',
'sensor_9__fft_coefficient__attr_"angle"__coeff_77',
'sensor_9__approximate_entropy__m_2__r_0.1',
'sensor_9__approximate_entropy__m_2__r_0.3',
'sensor_9__agg_linear_trend__attr_"stderr"__chunk_len_10__f_agg_"var"',
'sensor_9__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"max"',
'sensor_9__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"mean"',
'sensor_9__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"var"',
'sensor_9__number_crossing_m__m_0',
'sensor_9__ratio_beyond_r_sigma__r_0.5',
'sensor_9__permutation_entropy__dimension_3__tau_1',
'sensor_9__permutation_entropy__dimension_4__tau_1',
'sensor_8__abs_energy',
'sensor_8__mean_abs_change',
'sensor_8__standard_deviation',
'sensor_8__kurtosis',
'sensor_8__longest_strike_below_mean',
'sensor_8__longest_strike_above_mean',
'sensor_8__maximum',
'sensor_8__minimum',
'sensor_8__cid_ce__normalize_True',
'sensor_8__quantile__q_0.2',
'sensor_8__quantile__q_0.3',
'sensor_8__quantile__q_0.4',
'sensor_8__quantile__q_0.6',
'sensor_8__quantile__q_0.7',
'sensor_8__quantile__q_0.8',
'sensor_8__agg_autocorrelation__f_agg_"mean"__maxlag_40',
'sensor_8__number_cwt_peaks__n_1',
'sensor_8__number_cwt_peaks__n_5',
'sensor_8__number_peaks__n_1',
'sensor_8__number_peaks__n_3',
'sensor_8__number_peaks__n_5',
'sensor_8__number_peaks__n_10',
'sensor_8__number_peaks__n_50',
'sensor_8__binned_entropy__max_bins_10',
'sensor_8__spkt_welch_density__coeff_2',
'sensor_8__spkt_welch_density__coeff_5',
'sensor_8__spkt_welch_density__coeff_8',
'sensor_8__ar_coefficient__coeff_1__k_10',
'sensor_8__ar_coefficient__coeff_2__k_10',
'sensor_8__ar_coefficient__coeff_3__k_10',
'sensor_8__ar_coefficient__coeff_4__k_10',
'sensor_8__ar_coefficient__coeff_5__k_10',
'sensor_8__ar_coefficient__coeff_6__k_10',
'sensor_8__ar_coefficient__coeff_7__k_10',
'sensor_8__ar_coefficient__coeff_8__k_10',
'sensor_8__ar_coefficient__coeff_9__k_10',
'sensor_8__ar_coefficient__coeff_10__k_10',
'sensor_8__change_quantiles__f_agg_"mean"__isabs_True__qh_0.2__ql_0.0',
'sensor_8__change_quantiles__f_agg_"var"__isabs_False__qh_0.4__ql_0.2',
'sensor_8__change_quantiles__f_agg_"mean"__isabs_True__qh_0.4__ql_0.2',
'sensor_8__change_quantiles__f_agg_"var"__isabs_True__qh_0.4__ql_0.2',
'sensor_8__change_quantiles__f_agg_"var"__isabs_False__qh_0.6__ql_0.2',
'sensor_8__change_quantiles__f_agg_"mean"__isabs_True__qh_0.6__ql_0.2',
'sensor_8__change_quantiles__f_agg_"var"__isabs_True__qh_0.6__ql_0.2',
'sensor_8__change_quantiles__f_agg_"var"__isabs_False__qh_0.8__ql_0.2',
'sensor_8__change_quantiles__f_agg_"mean"__isabs_True__qh_0.8__ql_0.2',
'sensor_8__change_quantiles__f_agg_"var"__isabs_True__qh_0.8__ql_0.2',
'sensor_8__change_quantiles__f_agg_"var"__isabs_False__qh_0.6__ql_0.4',
'sensor_8__change_quantiles__f_agg_"mean"__isabs_True__qh_0.6__ql_0.4',
'sensor_8__fft_coefficient__attr_"real"__coeff_31',
'sensor_8__fft_coefficient__attr_"real"__coeff_34',
'sensor_8__fft_coefficient__attr_"abs"__coeff_1',
'sensor_8__fft_coefficient__attr_"abs"__coeff_3',
'sensor_8__fft_coefficient__attr_"abs"__coeff_4',
'sensor_8__fft_coefficient__attr_"abs"__coeff_5',
'sensor_8__fft_coefficient__attr_"abs"__coeff_6',
'sensor_8__fft_coefficient__attr_"abs"__coeff_7',
'sensor_8__fft_coefficient__attr_"abs"__coeff_8',
'sensor_8__fft_coefficient__attr_"abs"__coeff_9',
'sensor_8__fft_coefficient__attr_"abs"__coeff_10',
'sensor_8__fft_coefficient__attr_"abs"__coeff_11',
'sensor_8__fft_coefficient__attr_"abs"__coeff_12',
'sensor_8__fft_coefficient__attr_"abs"__coeff_13',
'sensor_8__fft_coefficient__attr_"abs"__coeff_14',
'sensor_8__fft_coefficient__attr_"abs"__coeff_15',
'sensor_8__fft_coefficient__attr_"abs"__coeff_16',
'sensor_8__fft_coefficient__attr_"abs"__coeff_17',
'sensor_8__fft_coefficient__attr_"abs"__coeff_18',
'sensor_8__fft_coefficient__attr_"abs"__coeff_19',
'sensor_8__fft_coefficient__attr_"abs"__coeff_20',
'sensor_8__fft_coefficient__attr_"abs"__coeff_21',
'sensor_8__fft_coefficient__attr_"abs"__coeff_22',
'sensor_8__fft_coefficient__attr_"abs"__coeff_23',
'sensor_8__fft_coefficient__attr_"abs"__coeff_24',
'sensor_8__fft_coefficient__attr_"abs"__coeff_25',
'sensor_8__fft_coefficient__attr_"abs"__coeff_26',
'sensor_8__fft_coefficient__attr_"abs"__coeff_27',
'sensor_8__fft_coefficient__attr_"abs"__coeff_28',
'sensor_8__fft_coefficient__attr_"abs"__coeff_29',
'sensor_8__fft_coefficient__attr_"abs"__coeff_30',
'sensor_8__fft_coefficient__attr_"abs"__coeff_31',
'sensor_8__fft_coefficient__attr_"abs"__coeff_32',
'sensor_8__fft_coefficient__attr_"abs"__coeff_33',
'sensor_8__fft_coefficient__attr_"abs"__coeff_34',
'sensor_8__fft_coefficient__attr_"abs"__coeff_35',
'sensor_8__fft_coefficient__attr_"abs"__coeff_36',
'sensor_8__fft_coefficient__attr_"abs"__coeff_37',
'sensor_8__fft_coefficient__attr_"abs"__coeff_38',
'sensor_8__fft_coefficient__attr_"abs"__coeff_39',
'sensor_8__fft_coefficient__attr_"abs"__coeff_40',
'sensor_8__fft_coefficient__attr_"abs"__coeff_41',
'sensor_8__fft_coefficient__attr_"abs"__coeff_42',
'sensor_8__fft_coefficient__attr_"abs"__coeff_43',
'sensor_8__fft_coefficient__attr_"abs"__coeff_44',
'sensor_8__fft_coefficient__attr_"abs"__coeff_45',
'sensor_8__fft_coefficient__attr_"abs"__coeff_46',
'sensor_8__fft_coefficient__attr_"abs"__coeff_47',
'sensor_8__fft_coefficient__attr_"abs"__coeff_48',
'sensor_8__fft_coefficient__attr_"abs"__coeff_49',
'sensor_8__fft_coefficient__attr_"abs"__coeff_50',
'sensor_8__fft_coefficient__attr_"abs"__coeff_83',
'sensor_8__value_count__value_1',
'sensor_8__value_count__value_-1',
'sensor_8__approximate_entropy__m_2__r_0.1',
'sensor_8__approximate_entropy__m_2__r_0.3',
'sensor_8__approximate_entropy__m_2__r_0.5',
'sensor_8__agg_linear_trend__attr_"intercept"__chunk_len_50__f_agg_"max"',
'sensor_8__agg_linear_trend__attr_"intercept"__chunk_len_50__f_agg_"min"',
'sensor_8__agg_linear_trend__attr_"intercept"__chunk_len_50__f_agg_"var"',
'sensor_8__agg_linear_trend__attr_"stderr"__chunk_len_5__f_agg_"var"',
'sensor_8__agg_linear_trend__attr_"stderr"__chunk_len_10__f_agg_"var"',
'sensor_8__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"max"',
'sensor_8__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"mean"',
'sensor_8__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"var"',
'sensor_8__number_crossing_m__m_0',
'sensor_8__ratio_beyond_r_sigma__r_0.5',
'sensor_8__permutation_entropy__dimension_3__tau_1',
'sensor_3__abs_energy',
'sensor_3__standard_deviation',
'sensor_3__kurtosis',
'sensor_3__longest_strike_below_mean',
'sensor_3__longest_strike_above_mean',
'sensor_3__maximum',
'sensor_3__cid_ce__normalize_True',
'sensor_3__quantile__q_0.2',
'sensor_3__quantile__q_0.4',
'sensor_3__quantile__q_0.6',
'sensor_3__quantile__q_0.7',
'sensor_3__agg_autocorrelation__f_agg_"mean"__maxlag_40',
'sensor_3__agg_autocorrelation__f_agg_"median"__maxlag_40',
'sensor_3__number_cwt_peaks__n_1',
'sensor_3__number_cwt_peaks__n_5',
'sensor_3__number_peaks__n_1',
'sensor_3__number_peaks__n_3',
'sensor_3__number_peaks__n_5',
'sensor_3__number_peaks__n_10',
'sensor_3__number_peaks__n_50',
'sensor_3__binned_entropy__max_bins_10',
'sensor_3__spkt_welch_density__coeff_2',
'sensor_3__spkt_welch_density__coeff_5',
'sensor_3__spkt_welch_density__coeff_8',
'sensor_3__ar_coefficient__coeff_1__k_10',
'sensor_3__ar_coefficient__coeff_2__k_10',
'sensor_3__ar_coefficient__coeff_3__k_10',
'sensor_3__ar_coefficient__coeff_4__k_10',
'sensor_3__ar_coefficient__coeff_5__k_10',
'sensor_3__ar_coefficient__coeff_6__k_10',
'sensor_3__ar_coefficient__coeff_7__k_10',
'sensor_3__ar_coefficient__coeff_8__k_10',
'sensor_3__ar_coefficient__coeff_9__k_10',
'sensor_3__change_quantiles__f_agg_"var"__isabs_False__qh_0.6__ql_0.4',
'sensor_3__fft_coefficient__attr_"real"__coeff_11',
'sensor_3__fft_coefficient__attr_"real"__coeff_12',
'sensor_3__fft_coefficient__attr_"real"__coeff_13',
'sensor_3__fft_coefficient__attr_"real"__coeff_14',
'sensor_3__fft_coefficient__attr_"imag"__coeff_1',
'sensor_3__fft_coefficient__attr_"imag"__coeff_11',
'sensor_3__fft_coefficient__attr_"imag"__coeff_12',
'sensor_3__fft_coefficient__attr_"imag"__coeff_13',
'sensor_3__fft_coefficient__attr_"imag"__coeff_14',
'sensor_3__fft_coefficient__attr_"abs"__coeff_0',
'sensor_3__fft_coefficient__attr_"abs"__coeff_1',
'sensor_3__fft_coefficient__attr_"abs"__coeff_2',
'sensor_3__fft_coefficient__attr_"abs"__coeff_3',
'sensor_3__fft_coefficient__attr_"abs"__coeff_4',
'sensor_3__fft_coefficient__attr_"abs"__coeff_5',
'sensor_3__fft_coefficient__attr_"abs"__coeff_6',
'sensor_3__fft_coefficient__attr_"abs"__coeff_7',
'sensor_3__fft_coefficient__attr_"abs"__coeff_8',
'sensor_3__fft_coefficient__attr_"abs"__coeff_9',
'sensor_3__fft_coefficient__attr_"abs"__coeff_10',
'sensor_3__fft_coefficient__attr_"abs"__coeff_11',
'sensor_3__fft_coefficient__attr_"abs"__coeff_12',
'sensor_3__fft_coefficient__attr_"abs"__coeff_13',
'sensor_3__fft_coefficient__attr_"abs"__coeff_14',
'sensor_3__fft_coefficient__attr_"abs"__coeff_15',
'sensor_3__fft_coefficient__attr_"abs"__coeff_16',
'sensor_3__fft_coefficient__attr_"abs"__coeff_17',
'sensor_3__fft_coefficient__attr_"abs"__coeff_18',
'sensor_3__fft_coefficient__attr_"abs"__coeff_19',
'sensor_3__fft_coefficient__attr_"abs"__coeff_20',
'sensor_3__fft_coefficient__attr_"abs"__coeff_21',
'sensor_3__fft_coefficient__attr_"abs"__coeff_22',
'sensor_3__fft_coefficient__attr_"abs"__coeff_23',
'sensor_3__fft_coefficient__attr_"abs"__coeff_24',
'sensor_3__fft_coefficient__attr_"abs"__coeff_25',
'sensor_3__fft_coefficient__attr_"abs"__coeff_26',
'sensor_3__fft_coefficient__attr_"abs"__coeff_27',
'sensor_3__fft_coefficient__attr_"abs"__coeff_28',
'sensor_3__fft_coefficient__attr_"abs"__coeff_29',
'sensor_3__fft_coefficient__attr_"abs"__coeff_30',
'sensor_3__fft_coefficient__attr_"abs"__coeff_31',
'sensor_3__fft_coefficient__attr_"abs"__coeff_32',
'sensor_3__fft_coefficient__attr_"abs"__coeff_33',
'sensor_3__fft_coefficient__attr_"abs"__coeff_34',
'sensor_3__fft_coefficient__attr_"abs"__coeff_35',
'sensor_3__fft_coefficient__attr_"abs"__coeff_36',
'sensor_3__fft_coefficient__attr_"abs"__coeff_37',
'sensor_3__fft_coefficient__attr_"abs"__coeff_38',
'sensor_3__fft_coefficient__attr_"abs"__coeff_39',
'sensor_3__fft_coefficient__attr_"abs"__coeff_40',
'sensor_3__fft_coefficient__attr_"abs"__coeff_41',
'sensor_3__fft_coefficient__attr_"abs"__coeff_42',
'sensor_3__fft_coefficient__attr_"abs"__coeff_43',
'sensor_3__fft_coefficient__attr_"abs"__coeff_44',
'sensor_3__fft_coefficient__attr_"abs"__coeff_45',
'sensor_3__fft_coefficient__attr_"abs"__coeff_46',
'sensor_3__fft_coefficient__attr_"abs"__coeff_47',
'sensor_3__fft_coefficient__attr_"abs"__coeff_48',
'sensor_3__fft_coefficient__attr_"abs"__coeff_49',
'sensor_3__fft_coefficient__attr_"abs"__coeff_53',
'sensor_3__fft_coefficient__attr_"abs"__coeff_72',
'sensor_3__fft_coefficient__attr_"abs"__coeff_77',
'sensor_3__fft_coefficient__attr_"abs"__coeff_84',
'sensor_3__fft_coefficient__attr_"abs"__coeff_86',
'sensor_3__fft_coefficient__attr_"abs"__coeff_88',
'sensor_3__fft_coefficient__attr_"abs"__coeff_89',
'sensor_3__fft_coefficient__attr_"abs"__coeff_94',
'sensor_3__value_count__value_1',
'sensor_3__value_count__value_-1',
'sensor_3__approximate_entropy__m_2__r_0.1',
'sensor_3__approximate_entropy__m_2__r_0.3',
'sensor_3__approximate_entropy__m_2__r_0.5',
'sensor_3__agg_linear_trend__attr_"slope"__chunk_len_5__f_agg_"mean"',
'sensor_3__agg_linear_trend__attr_"stderr"__chunk_len_5__f_agg_"var"',
'sensor_3__agg_linear_trend__attr_"stderr"__chunk_len_10__f_agg_"var"',
'sensor_3__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"max"',
'sensor_3__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"mean"',
'sensor_3__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"var"',
'sensor_3__number_crossing_m__m_0',
'sensor_3__ratio_beyond_r_sigma__r_0.5',
'sensor_3__permutation_entropy__dimension_3__tau_1',
'sensor_3__permutation_entropy__dimension_4__tau_1',
'sensor_5__mean_abs_change',
'sensor_5__longest_strike_below_mean',
'sensor_5__longest_strike_above_mean',
'sensor_5__cid_ce__normalize_True',
'sensor_5__cid_ce__normalize_False',
'sensor_5__number_cwt_peaks__n_1',
'sensor_5__number_cwt_peaks__n_5',
'sensor_5__number_peaks__n_1',
'sensor_5__number_peaks__n_3',
'sensor_5__number_peaks__n_5',
'sensor_5__number_peaks__n_10',
'sensor_5__spkt_welch_density__coeff_2',
'sensor_5__spkt_welch_density__coeff_5',
'sensor_5__spkt_welch_density__coeff_8',
'sensor_5__ar_coefficient__coeff_1__k_10',
'sensor_5__ar_coefficient__coeff_2__k_10',
'sensor_5__ar_coefficient__coeff_3__k_10',
'sensor_5__ar_coefficient__coeff_4__k_10',
'sensor_5__ar_coefficient__coeff_5__k_10',
'sensor_5__ar_coefficient__coeff_6__k_10',
'sensor_5__ar_coefficient__coeff_7__k_10',
'sensor_5__ar_coefficient__coeff_8__k_10',
'sensor_5__ar_coefficient__coeff_9__k_10',
'sensor_5__ar_coefficient__coeff_10__k_10',
'sensor_5__change_quantiles__f_agg_"mean"__isabs_True__qh_0.6__ql_0.0',
'sensor_5__change_quantiles__f_agg_"var"__isabs_False__qh_1.0__ql_0.0',
'sensor_5__change_quantiles__f_agg_"var"__isabs_False__qh_0.8__ql_0.2',
'sensor_5__change_quantiles__f_agg_"mean"__isabs_True__qh_0.8__ql_0.2',
'sensor_5__fft_coefficient__attr_"real"__coeff_11',
'sensor_5__fft_coefficient__attr_"real"__coeff_12',
'sensor_5__fft_coefficient__attr_"real"__coeff_13',
'sensor_5__fft_coefficient__attr_"imag"__coeff_11',
'sensor_5__fft_coefficient__attr_"imag"__coeff_12',
'sensor_5__fft_coefficient__attr_"imag"__coeff_13',
'sensor_5__fft_coefficient__attr_"abs"__coeff_4',
'sensor_5__fft_coefficient__attr_"abs"__coeff_5',
'sensor_5__fft_coefficient__attr_"abs"__coeff_6',
'sensor_5__fft_coefficient__attr_"abs"__coeff_7',
'sensor_5__fft_coefficient__attr_"abs"__coeff_8',
'sensor_5__fft_coefficient__attr_"abs"__coeff_9',
'sensor_5__fft_coefficient__attr_"abs"__coeff_10',
'sensor_5__fft_coefficient__attr_"abs"__coeff_11',
'sensor_5__fft_coefficient__attr_"abs"__coeff_12',
'sensor_5__fft_coefficient__attr_"abs"__coeff_13',
'sensor_5__fft_coefficient__attr_"abs"__coeff_14',
'sensor_5__fft_coefficient__attr_"abs"__coeff_15',
'sensor_5__fft_coefficient__attr_"abs"__coeff_16',
'sensor_5__fft_coefficient__attr_"abs"__coeff_17',
'sensor_5__fft_coefficient__attr_"abs"__coeff_18',
'sensor_5__fft_coefficient__attr_"abs"__coeff_19',
'sensor_5__fft_coefficient__attr_"abs"__coeff_20',
'sensor_5__fft_coefficient__attr_"abs"__coeff_21',
'sensor_5__fft_coefficient__attr_"abs"__coeff_22',
'sensor_5__fft_coefficient__attr_"abs"__coeff_23',
'sensor_5__fft_coefficient__attr_"abs"__coeff_24',
'sensor_5__fft_coefficient__attr_"abs"__coeff_25',
'sensor_5__fft_coefficient__attr_"abs"__coeff_26',
'sensor_5__fft_coefficient__attr_"abs"__coeff_27',
'sensor_5__fft_coefficient__attr_"abs"__coeff_28',
'sensor_5__fft_coefficient__attr_"abs"__coeff_29',
'sensor_5__fft_coefficient__attr_"abs"__coeff_30',
'sensor_5__fft_coefficient__attr_"abs"__coeff_31',
'sensor_5__fft_coefficient__attr_"abs"__coeff_32',
'sensor_5__value_count__value_1',
'sensor_5__value_count__value_-1',
'sensor_5__approximate_entropy__m_2__r_0.1',
'sensor_5__approximate_entropy__m_2__r_0.3',
'sensor_5__approximate_entropy__m_2__r_0.5',
'sensor_5__agg_linear_trend__attr_"stderr"__chunk_len_50__f_agg_"mean"',
'sensor_5__number_crossing_m__m_0',
'sensor_5__permutation_entropy__dimension_3__tau_1'
]
train["Embarked"].replace(["S", "Q", "C"], [0, 1, 2], inplace = True )<feature_engineering>
features = [re.sub('[^A-Za-z0-9_]+', '_', feature)for feature in features]
def cab2int(data):
data.loc[data["Cabin"] == "Known", 'Cabin'] = 1
data.loc[data["Cabin"] == "Unknown", 'Cabin'] = 0
return data
train = cab2int(train )<categorify>
seg_id_test = test_df['segment_id']
x_test = test_df.drop(['segment_id'], axis=1)[features]
train["Sex"].replace(["male", "female"], [0, 1], inplace = True )<predict_on_test>
train, label = get_train_dataset(train_df, labels_df)
 train = train[features]
test_df = test_df[features + ['segment_id']]
scaler = MinMaxScaler().fit(pd.concat([train, x_test]))
x_test = pd.DataFrame(scaler.transform(x_test))
train = pd.DataFrame(scaler.transform(train))
x_test.columns = features
train.columns = features
def assign_missing_ages(data_frame, features):
age_data = data_frame[features]
known_ages = age_data[age_data.Age.notnull() ].as_matrix()
unknown_ages = age_data[age_data.Age.isnull() ].as_matrix()
target = known_ages[:, 0]
eigen_val = known_ages[:, 1:]
rfr = RandomForestRegressor(random_state = 0, n_estimators = 2000, n_jobs = -1)
 rfr.fit(eigen_val, target)
 Age_predictions = rfr.predict(unknown_ages[:, 1::])
 data_frame.loc[(data_frame.Age.isnull()), "Age"] = Age_predictions
return data_frame, rfr
age_features = ["Age", "Sex", "SibSp", "Parch", "Pclass"]
train, rfr = assign_missing_ages(train, age_features)
 train["Age"] = train["Age"].astype(int)
 train.head(10 )<groupby>
x_train, x_val, y_train, y_val = train_test_split(train, label, random_state=786, test_size=0.2, shuffle=False )
train.groupby("Title")["Age"].mean()<feature_engineering>
params = {'application':'regression',
'boosting ': 'dart',
'num_iterations':8000,
'learning_rate':0.03,
'num_leaves': 45,
'extra_trees': True,
'feature_fraction':0.8,
'bagging_fraction':0.9,
'lambda_l1':0.1,
'lambda_l2':0.1,
'min_split_gain':0.01,
'early_stopping_round':100,
'max_depth':6,
'min_child_weight':40,
'n_estimators': 400,
'metric':'mse',
'verbosity': -1}
def ageG2int(data):
data["Age_group"] = "NaN"
data.loc[data["Age"] <= 16, "Age_group"] = 0
data.loc[(data["Age"] > 16)&(data["Age"] <= 32), "Age_group"] = 1
data.loc[(data["Age"] > 32)&(data["Age"] <= 48), "Age_group"] = 3
data.loc[(data["Age"] > 48)&(data["Age"] <= 64), "Age_group"] = 4
data.loc[data["Age"] > 64, "Age_group"] = 5
return data
train = ageG2int(train )<feature_engineering>
lgb_first = LGBMRegressor(**params)
 lgb_first.fit(x_train, y_train, eval_set=(x_val, y_val))
val_preds = lgb_first.predict(x_val )
def child2int(data):
data["Child"] = "NaN"
data.loc[data["Age"] <= 18, "Child"] = 0
data.loc[data["Age"] > 18, "Child"] = 1
return data
train = child2int(train )<feature_engineering>
output = pd.DataFrame(list(zip(y_val, val_preds)))
 output.columns = ['val', 'pred']
output = output.groupby('val' ).mean().reset_index()
print('Simple LGB model rmse: ', sqrt(mse(output['val'].to_numpy() , output['pred'].to_numpy())))
 print('Simple LGB model mae: ', mae(output['val'].to_numpy() , output['pred'].to_numpy()))
train["FamSize"] = train["SibSp"] + train["Parch"] + 1<feature_engineering>
steps = [(0, 2500000),(2500000, 15000000),(10000000, 25000000), 
(20000000, 35000000),(30000000, 44000000),(44000000, 50000000)]
_mae_list = []
for l, u in steps:
_output = output[(output['val'] > l)&(output['val'] < u)]
_range = str(l)+'-'+str(u)
 _mae_list.append(( _range, mae(_output['val'].to_numpy() , _output['pred'].to_numpy())))
 mae_df = pd.DataFrame(_mae_list, columns=['range', 'mae'] )
def famG2int(data):
data["Fam_group"] = "NaN"
data.loc[data["FamSize"] == 1, "Fam_group"] = 0
data.loc[data["FamSize"] > 1, "Fam_group"] = 1
return data
train = famG2int(train )<prepare_x_and_y>
test_preds = lgb_first.predict(x_test)
 test_first_preds_df = pd.DataFrame(list(zip(seg_id_test, test_preds)))
 test_first_preds_df.columns = ['segment_id', 'time_to_eruption']
test_first_preds_df = test_first_preds_df.groupby('segment_id' ).mean().reset_index()
train_one = train[:]<categorify>
steps = [(0, 2500000),(0, 15000000),(10000000, 25000000), 
(20000000, 35000000),(30000000, 50000000),(44000000, 50000000)]
output_list, model_list = [], []
y_val_list, val_preds_list = [], []
test["Embarked"].replace(["S", "Q", "C"], [0, 1, 2], inplace = True)
 test["Fare"] = test["Fare"].fillna(test["Fare"].median())
 test, test_titles = get_title(test)
 test = title2int(test)
 test["Sex"].replace(["male", "female"], [0, 1], inplace = True)
 test = Cabin_type(test)
 test = cab2int(test)
 test = fareG2int(test)
 temp_test = test[age_features]
test_unknown_ages = temp_test[test["Age"].isnull() ].as_matrix()
test_Age_predictions = rfr.predict(test_unknown_ages[:, 1:])
 test.loc[(test["Age"].isnull()), "Age"] = test_Age_predictions
test["Age"] = test["Age"].astype(int)
 test = ageG2int(test)
 test = child2int(test)
 test["FamSize"] = test["SibSp"] + test["Parch"] + 1
test = famG2int(test)
 test_one = test[:]
test_columns_titles = ["PassengerId", "Pclass", "Title", "Sex", "Child", "Fam_group", "Fare", "Cabin", "Embarked"]
test_one = test_one[test_columns_titles]
test_one.info()
test_one.head()<train_model>
params = {'application':'regression',
'boosting ': 'dart',
'num_iterations':8000,
'learning_rate':0.05,
'num_leaves': 95,
'extra_trees': True,
'feature_fraction':0.8,
'bagging_fraction':0.9,
'lambda_l1':0.1,
'lambda_l2':0.1,
'min_split_gain':0.01,
'early_stopping_round':100,
'max_depth': 7,
'min_child_weight':40,
'n_estimators': 400,
'metric':'mae',
'verbosity': -1}
def my_models(model, X_train, Y_train, X_test, Y_test):
my_model = model.fit(X_train, Y_train)
 print(my_model.feature_importances_)
 print(my_model.score(X_train, Y_train))
model_prediction = my_model.predict(X_test)
 acc = metrics.accuracy_score(model_prediction, Y_test)
 return acc, my_model<prepare_x_and_y>
for l, u in steps:
_x, _y = get_dataset_by_range(train_df, labels_df, l, u)
 _x = _x[features]
_x = pd.DataFrame(scaler.transform(_x))
_x.columns = features
_x_train, _x_val, _y_train, _y_val = train_test_split(_x, _y, random_state=786, test_size=0.2, shuffle=False)
 _lgb = LGBMRegressor(**params)
 _lgb.fit(_x_train, _y_train, eval_set=(_x_val, _y_val))
_val_preds = _lgb.predict(_x_val)
 _output = pd.DataFrame(list(zip(_y_val, _val_preds)))
 _output.columns = ['val', 'pred']
_output = _output.groupby('val' ).median().reset_index()
_output['diff'] = _output['val'] - _output['pred']
model_list.append(_lgb)
 output_list.append(_output )
final_features = ["Pclass", "Title", "Sex", "Child", "Fam_group", "Fare", "Cabin", "Embarked"]
final_data = train_one[["Survived"] + final_features]
training, testing = train_test_split(final_data, test_size = 0.3, random_state = 0, stratify = final_data["Survived"])
 X_train = training[training.columns[1:]]
Y_train = training[training.columns[:1]]
X_test = testing[testing.columns[1:]]
Y_test = testing[testing.columns[:1]]<choose_model_class>
_mae_list = []
for idx, _output in enumerate(output_list):
l, h = steps[idx]
_mae = mae(_output['val'].to_numpy() , _output['pred'].to_numpy())
 _range = '%s-%s' %(l, h)
 _mae_list.append(( _range, _mae))
new_mae_df = pd.DataFrame(_mae_list, columns=['range', 'mae'] )
tree_model = tree.DecisionTreeClassifier(max_depth = 8, max_leaf_nodes = 7, min_samples_leaf = 10, random_state = 0)
 forest_model = RandomForestClassifier(max_depth = 8, max_leaf_nodes = 9, n_estimators = 300, random_state = 0)
 gradboost_model = GradientBoostingClassifier(learning_rate = 0.01, max_depth = 7,
max_features = 1.0, n_estimators = 200, subsample = 1.0, random_state = 0 )<train_model>
test_pred_df = pd.merge(test_df, test_first_preds_df, on='segment_id' )
tree_acc, my_tree = my_models(tree_model, X_train, Y_train, X_test, Y_test)
 print("The accuracy of Decision Tree is", tree_acc)
 forest_acc, my_forest = my_models(forest_model, X_train, Y_train, X_test, Y_test)
 print("The accuracy of Random Forest is", forest_acc)
 gradboost_acc, my_gradboost = my_models(gradboost_model, X_train, Y_train, X_test, Y_test)
 print("The accuracy of Gradient Boosting is", gradboost_acc )<save_to_csv>
segment_id, time_to_eruption = [], []
final_test = test_one[final_features]
tree_prediction = my_tree.predict(final_test)
 forest_prediction = my_forest.predict(final_test)
 gradboost_prediction = my_gradboost.predict(final_test)
 test_cp1 = test_one[:]
test_cp2 = test_one[:]
test_cp3 = test_one[:]
headers = ["PassengerId", "Survived"]
test_cp1["Survived"] = tree_prediction
tree_prediction = pd.DataFrame(test_cp1, columns = headers)
 tree_prediction.to_csv("tree_prediction.csv", index = False)
 test_cp2["Survived"] = forest_prediction
forest_prediction = pd.DataFrame(test_cp2, columns = headers)
 forest_prediction.to_csv("forest_prediction.csv", index = False)
 test_cp3["Survived"] = gradboost_prediction
gradboost_prediction = pd.DataFrame(test_cp3, columns = headers)
 gradboost_prediction.to_csv("gradboost_prediction.csv", index = False )<load_from_csv>
for idx, value in enumerate(steps):
l, h = value
if l == 0:
l = -50000000
if h == 50000000:
h = 100000000
_test_df = test_pred_df[(test_pred_df['time_to_eruption'] >= l)& 
(test_pred_df['time_to_eruption'] <= h)]
_test_id = _test_df['segment_id']
_x_test = _test_df.drop(['segment_id', 'time_to_eruption'], axis=1)
 _x_test = pd.DataFrame(scaler.transform(_x_test))
_x_test.columns = features
_preds = model_list[idx].predict(_x_test)
 segment_id += list(_test_id)
 time_to_eruption += list(_preds )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )<drop_column>
kaggle_submit = pd.DataFrame(list(zip(segment_id, time_to_eruption)))
 kaggle_submit.columns = ['segment_id', 'time_to_eruption']
kaggle_submit = kaggle_submit.groupby('segment_id' ).median().reset_index()
passengerId = test_df.PassengerId
train_df = train_df.drop('PassengerId', axis = 1)
 test_df = test_df.drop('PassengerId', axis = 1 )<count_values>
assert len(kaggle_submit)== 4520
assert kaggle_submit['segment_id'].dtypes == 'int64'
assert kaggle_submit['time_to_eruption'].dtypes == 'float64'
<define_variables><EOS> 
kaggle_submit.to_csv('./submission.csv', index=False )