Kaggle competition: LANL Earthquake Prediction
train_features = pd.concat(( train_features, train_features_cluster,train_cluster_pca,train_features_stats), axis=1)
 test_features = pd.concat(( test_features, test_features_cluster,test_cluster_pca,test_features_stats), axis=1 )<merge>
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.preprocessing import StandardScaler
from sklearn.svm import NuSVR
from sklearn.kernel_ridge import KernelRidge
from sklearn.metrics import mean_absolute_error, make_scorer
from sklearn.model_selection import GridSearchCV
train = train_features.merge(train_targets_scored, on='sig_id')
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target = train[train_targets_scored.columns]<drop_column>
train = pd.read_csv('.. /input/train.csv', dtype={'acoustic_data': np.int16, 'time_to_failure': np.float64} )
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1 )<drop_column>
pd.options.display.precision = 15
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist()<filter>
def add_trend_feature(arr, abs_values=False):
idx = np.array(range(len(arr)))
 if abs_values:
arr = np.abs(arr)
 lr = LinearRegression()
lr.fit(idx.reshape(-1, 1), arr)
 return lr.coef_[0]
target=target[target_cols]<categorify>
rows = 150_000
segments = int(np.floor(train.shape[0] / rows))
X_train = pd.DataFrame(index=range(segments), dtype=np.float64,
columns=['ave', 'std', 'max', 'min','q95','q99', 'q05','q01',
'abs_max', 'abs_mean', 'abs_std', 'trend', 'abs_trend', 'iqr',
'q999','q001','ave10'])
 y_train = pd.DataFrame(index=range(segments), dtype=np.float64,
columns=['time_to_failure'])
 for segment in tqdm(range(segments)) :
seg = train.iloc[segment*rows:segment*rows+rows]
x = seg['acoustic_data'].values
y = seg['time_to_failure'].values[-1]
y_train.loc[segment, 'time_to_failure'] = y
X_train.loc[segment, 'ave'] = x.mean()
X_train.loc[segment, 'std'] = x.std()
X_train.loc[segment, 'max'] = x.max()
X_train.loc[segment, 'min'] = x.min()
X_train.loc[segment, 'q95'] = np.quantile(x,0.95)
 X_train.loc[segment, 'q99'] = np.quantile(x,0.99)
 X_train.loc[segment, 'q05'] = np.quantile(x,0.05)
 X_train.loc[segment, 'q01'] = np.quantile(x,0.01)
 X_train.loc[segment, 'abs_max'] = np.abs(x ).max()
X_train.loc[segment, 'abs_mean'] = np.abs(x ).mean()
X_train.loc[segment, 'abs_std'] = np.abs(x ).std()
X_train.loc[segment, 'trend'] = add_trend_feature(x)
 X_train.loc[segment, 'abs_trend'] = add_trend_feature(x, abs_values=True)
 X_train.loc[segment, 'iqr'] = np.subtract(*np.percentile(x, [75, 25]))
X_train.loc[segment, 'q999'] = np.quantile(x,0.999)
 X_train.loc[segment, 'q001'] = np.quantile(x,0.001)
 X_train.loc[segment, 'ave10'] = stats.trim_mean(x, 0.1 )
train = pd.get_dummies(train, columns=['cp_time','cp_dose'])
 test_ = pd.get_dummies(test, columns=['cp_time','cp_dose'] )<define_variables>
scaler = StandardScaler()
scaler.fit(X_train)
 X_train_scaled = scaler.transform(X_train )
feature_cols = [c for c in train.columns if c not in target_cols]
feature_cols = [c for c in feature_cols if c not in ['sig_id']]<drop_column>
scorer = make_scorer(mean_absolute_error, greater_is_better=False)
 parameters = [{ 'gamma': [0.6, 0.7, 0.8],
'C': [2.35, 2.4, 2.45, 2.5],
'nu': [0.85, 0.9, 0.95]}]
reg1 = GridSearchCV(NuSVR(kernel='rbf', tol=0.01), parameters, cv = 3, scoring=scorer)
 reg1.fit(X_train_scaled, y_train.values.flatten())
 y_pred1 = reg1.predict(X_train_scaled)
 print(reg1.best_params_)
 print(reg1.best_score_ )
train = train[feature_cols]
test = test_[feature_cols]<prepare_x_and_y>
parameters = [{ 'gamma': [0.06, 0.1, 0.08, 0.09],
'alpha': [0.005, 0.01, 0.05]}]
reg2 = GridSearchCV(KernelRidge(kernel='rbf'), parameters, cv = 3, scoring=scorer)
 reg2.fit(X_train_scaled, y_train.values.flatten())
 y_pred2 = reg2.predict(X_train_scaled)
 print(reg2.best_params_)
 print(reg2.best_score_ )
X_test = test.values<compute_test_metric>
score1 = mean_absolute_error(y_train.values.flatten() , y_pred1)
 print(f'Score1: {score1:0.3f}')
 score2 = mean_absolute_error(y_train.values.flatten() , y_pred2)
 print(f'Score2: {score2:0.3f}')
 score3 = mean_absolute_error(y_train.values.flatten() , y_pred1*0.5+y_pred2*0.5)
 print(f'Score3: {score3:0.3f}' )
class LogitsLogLoss(Metric):
def __init__(self):
self._name = "logits_ll"
self._maximize = False
def __call__(self, y_true, y_pred):
logits = 1 /(1 + np.exp(-y_pred))
aux =(1 - y_true)* np.log(1 - logits + 5e-5)+ y_true * np.log(logits + 5e-5)
 return np.mean(-aux )<choose_model_class>
submission = pd.read_csv('.. /input/sample_submission.csv', index_col='seg_id' )
MAX_EPOCH = 200
tabnet_params = dict(
 n_d = 32,
n_a = 32,
n_steps = 1,
gamma = 1.3,
lambda_sparse = 0,
optimizer_fn = optim.Adam,
optimizer_params = dict(lr = 2e-2, weight_decay = 1e-5),
mask_type = "entmax",
scheduler_params = dict(mode = "min", patience = 5, min_lr = 1e-5, factor = 0.9),
scheduler_fn = ReduceLROnPlateau,
seed = seed,
verbose = 10
)<find_best_model_class>
X_test = pd.DataFrame(columns=X_train.columns, dtype=np.float64, index=submission.index )
%%time
test_cv_preds = []
NB_SPLITS = 10
mskf = MultilabelStratifiedKFold(n_splits = NB_SPLITS, random_state = 0, shuffle = True)
 SEED = [20,21,22]
for s in SEED:
tabnet_params['seed'] = s
for fold_nb,(train_idx, val_idx)in enumerate(mskf.split(train, target)) :
model = TabNetRegressor()
model.load_model(f".. /input/moa-tabnet-train-inference/TabNet_seed_{s}_fold_{fold_nb+1}.zip")
 preds_test = model.predict(X_test)
 test_cv_preds.append(1 /(1 + np.exp(-preds_test)))
 test_preds_all = np.stack(test_cv_preds )<load_from_csv>
for seg_id in X_test.index:
seg = pd.read_csv('.. /input/test/' + seg_id + '.csv')
 x = seg['acoustic_data'].values
X_test.loc[seg_id, 'ave'] = x.mean()
X_test.loc[seg_id, 'std'] = x.std()
X_test.loc[seg_id, 'max'] = x.max()
X_test.loc[seg_id, 'min'] = x.min()
X_test.loc[seg_id, 'q95'] = np.quantile(x,0.95)
 X_test.loc[seg_id, 'q99'] = np.quantile(x,0.99)
 X_test.loc[seg_id, 'q05'] = np.quantile(x,0.05)
 X_test.loc[seg_id, 'q01'] = np.quantile(x,0.01)
 X_test.loc[seg_id, 'abs_max'] = np.abs(x ).max()
X_test.loc[seg_id, 'abs_mean'] = np.abs(x ).mean()
X_test.loc[seg_id, 'abs_std'] = np.abs(x ).std()
X_test.loc[seg_id, 'trend'] = add_trend_feature(x)
 X_test.loc[seg_id, 'abs_trend'] = add_trend_feature(x, abs_values=True)
 X_test.loc[seg_id, 'iqr'] = np.subtract(*np.percentile(x, [75, 25]))
X_test.loc[seg_id, 'q999'] = np.quantile(x,0.999)
 X_test.loc[seg_id, 'q001'] = np.quantile(x,0.001)
 X_test.loc[seg_id, 'ave10'] = stats.trim_mean(x, 0.1 )
<import_modules><EOS> 
X_test_scaled = scaler.transform(X_test)
 submission['time_to_failure'] = reg1.predict(X_test_scaled)*0.5 + reg2.predict(X_test_scaled)*0.5
submission.to_csv('submission.csv' )