Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<normalization>
%matplotlib inline
pd.options.display.precision = 15
warnings.filterwarnings("ignore")
 workaround = artgor_utils.prepare_altair()
HTML("".join((
"<script>",
workaround,
"</script>", 
)) )
class DenseCrossEntropy(nn.Module):
def __init__(self):
super(DenseCrossEntropy, self ).__init__()
def forward(self, logits, labels):
logits = logits.float()
labels = labels.float()
logprobs = F.log_softmax(logits, dim=-1)
 loss = -labels * logprobs
loss = loss.sum(-1)
 return loss.mean() 
<train_model>
train_features = pd.read_csv('.. /input/lanl-features/train_features.csv')
 test_features = pd.read_csv('.. /input/lanl-features/test_features.csv')
 train_features_denoised = pd.read_csv('.. /input/lanl-features/train_features_denoised.csv')
 test_features_denoised = pd.read_csv('.. /input/lanl-features/test_features_denoised.csv')
 train_features_denoised.columns = [f'{i}_denoised' for i in train_features_denoised.columns]
test_features_denoised.columns = [f'{i}_denoised' for i in test_features_denoised.columns]
y = pd.read_csv('.. /input/lanl-features/y.csv' )
def train_one_fold(i_fold, model, criterion, optimizer, dataloader_train, dataloader_valid):
train_fold_results = []
for epoch in range(N_EPOCHS):
os.system(f'echo " Epoch {epoch}"')
 model.train()
tr_loss = 0
for step, batch in enumerate(dataloader_train):
images = batch[0]
labels = batch[1]
images = images.to(device, dtype=torch.float)
 labels = labels.to(device, dtype=torch.float)
 outputs = model(images)
 loss = criterion(outputs, labels.squeeze(-1))
loss.backward()
tr_loss += loss.item()
optimizer.step()
optimizer.zero_grad()
model.eval()
val_loss = 0
val_preds = None
val_labels = None
for step, batch in enumerate(dataloader_valid):
images = batch[0]
labels = batch[1]
if val_labels is None:
val_labels = labels.clone().squeeze(-1)
 else:
val_labels = torch.cat(( val_labels, labels.squeeze(-1)) , dim=0)
 images = images.to(device, dtype=torch.float)
 labels = labels.to(device, dtype=torch.float)
 with torch.no_grad() :
outputs = model(images)
 loss = criterion(outputs, labels.squeeze(-1))
val_loss += loss.item()
preds = torch.softmax(outputs, dim=1 ).data.cpu()
if val_preds is None:
val_preds = preds
else:
val_preds = torch.cat(( val_preds, preds), dim=0)
 train_fold_results.append({
'fold': i_fold,
'epoch': epoch,
'train_loss': tr_loss / len(dataloader_train),
'valid_loss': val_loss / len(dataloader_valid),
'valid_score': roc_auc_score(val_labels, val_preds, average='macro'),
})
 return val_preds, train_fold_results<split>
X = pd.concat([train_features, train_features_denoised], axis=1 ).drop(['seg_id_denoised', 'target_denoised'], axis=1)
 X_test = pd.concat([test_features, test_features_denoised], axis=1 ).drop(['seg_id_denoised', 'target_denoised'], axis=1)
 X = X[:-1]
y = y[:-1]
submissions = None
train_results = []
for i_fold,(train_idx, valid_idx)in enumerate(folds.split(train_df, train_y)) :
print("Fold {}/{}".format(i_fold + 1, N_FOLDS))
valid = train_df.iloc[valid_idx]
valid.reset_index(drop=True, inplace=True)
 train = train_df.iloc[train_idx]
train.reset_index(drop=True, inplace=True)
 dataset_train = PlantDataset(df=train, transforms=transforms_train)
 dataset_valid = PlantDataset(df=valid, transforms=transforms_valid)
 dataloader_train = DataLoader(dataset_train, batch_size=BATCH_SIZE, num_workers=4, shuffle=True)
 dataloader_valid = DataLoader(dataset_valid, batch_size=BATCH_SIZE, num_workers=4, shuffle=False)
 device = torch.device("cuda:0")
 model = PlantModel(num_classes=4)
 model.to(device)
 criterion = DenseCrossEntropy()
plist = [{'params': model.parameters() , 'lr': 5e-5}]
optimizer = optim.Adam(plist, lr=5e-5)
 val_preds, train_fold_results = train_one_fold(i_fold, model, criterion, optimizer, dataloader_train, dataloader_valid)
 oof_preds[valid_idx, :] = val_preds.numpy()
train_results = train_results + train_fold_results
model.eval()
test_preds = None
for step, batch in enumerate(dataloader_test):
images = batch[0]
images = images.to(device, dtype=torch.float)
 with torch.no_grad() :
outputs = model(images)
 if test_preds is None:
test_preds = outputs.data.cpu()
else:
test_preds = torch.cat(( test_preds, outputs.data.cpu()), dim=0)
 submission_df[['healthy', 'multiple_diseases', 'rust', 'scab']] = torch.softmax(test_preds, dim=1)
 submission_df.to_csv('submission_fold_{}.csv'.format(i_fold), index=False)
 if submissions is None:
submissions = test_preds / N_FOLDS
else:
submissions += test_preds / N_FOLDS
print("5-Folds CV score: {:.4f}".format(roc_auc_score(train_labels, oof_preds, average='macro')) )<prepare_output>
n_fold = 10
folds = KFold(n_splits=n_fold, shuffle=True, random_state=11 )
train_results = pd.DataFrame(train_results)
 train_results.head(10 )<save_to_csv>
params = {'num_leaves': 128,
'min_child_samples': 79,
'objective': 'gamma',
'max_depth': -1,
'learning_rate': 0.01,
"boosting_type": "gbdt",
"subsample_freq": 5,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501,
'colsample_bytree': 0.1
}
result_dict_lgb = artgor_utils.train_model_regression(X=X, X_test=X_test, y=y, params=params, folds=folds, model_type='lgb',
eval_metric='mae', plot_feature_importance=True )
submission_df[['healthy', 'multiple_diseases', 'rust', 'scab']] = torch.softmax(submissions, dim=1)
 submission_df.to_csv('submission.csv', index=False )<install_modules>
submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv', index_col='seg_id')
 submission['time_to_failure'] = result_dict_lgb['prediction']
print(submission.head())
 submission.to_csv('submission.csv' )
!pip install efficientnet<import_modules>
sub1 = pd.read_csv('.. /input/lanl-features/submission_1.csv')
 sub1.to_csv('submission_1.csv', index=False )
import math, re, os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from kaggle_datasets import KaggleDatasets
import tensorflow as tf
import tensorflow.keras.layers as L
import efficientnet.tfkeras as efn
from keras.applications.densenet import DenseNet201
from sklearn import metrics
from sklearn.model_selection import train_test_split
from keras.callbacks import ModelCheckpoint<set_options>
scaler = StandardScaler()
scaler.fit(X)
 X_train_scaled = pd.DataFrame(scaler.transform(X), columns=X.columns)
 X_test_scaled = pd.DataFrame(scaler.transform(X_test), columns=X_test.columns )
AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<define_variables>
%%time
n = 10
neigh = NearestNeighbors(n, n_jobs=-1)
 neigh.fit(X_train_scaled)
 dists, _ = neigh.kneighbors(X_train_scaled, n_neighbors=n)
 mean_dist = dists.mean(axis=1)
 max_dist = dists.max(axis=1)
 min_dist = dists.min(axis=1)
 X_train_scaled['mean_dist'] = mean_dist
X_train_scaled['max_dist'] = max_dist
X_train_scaled['min_dist'] = min_dist
test_dists, _ = neigh.kneighbors(X_test_scaled, n_neighbors=n)
 test_mean_dist = test_dists.mean(axis=1)
 test_max_dist = test_dists.max(axis=1)
 test_min_dist = test_dists.min(axis=1)
 X_test_scaled['mean_dist'] = test_mean_dist
X_test_scaled['max_dist'] = test_max_dist
X_test_scaled['min_dist'] = test_min_dist
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
EPOCHS = 40
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
IM_Z = 768<define_variables>
params = {'num_leaves': 32,
'min_data_in_leaf': 79,
'objective': 'gamma',
'max_depth': -1,
'learning_rate': 0.01,
"boosting": "gbdt",
"bagging_freq": 5,
"bagging_fraction": 0.8126672064208567,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501,
'feature_fraction': 0.1
}
result_dict_lgb = artgor_utils.train_model_regression(X=X, X_test=X_test, y=y, params=params, folds=folds, model_type='lgb',
eval_metric='mae', plot_feature_importance=True )
def format_path(st):
return GCS_DS_PATH + '/images/' + st + '.jpg'<load_from_csv>
submission['time_to_failure'] = result_dict_lgb['prediction']
submission.to_csv('submission_nn.csv' )
train = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/train.csv')
 test = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/test.csv')
 sub = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/sample_submission.csv')
 train_paths = train.image_id.apply(format_path ).values
test_paths = test.image_id.apply(format_path ).values
train_labels = train.loc[:, 'healthy':].values
train_paths, valid_paths, train_labels, valid_labels = train_test_split(
 train_paths, train_labels, test_size=0.1, random_state=2020 )<normalization>
top_columns = ['iqr1_denoised', 'percentile_5_denoised', 'abs_percentile_90_denoised', 'percentile_95_denoised', 'ave_roll_std_10', 'num_peaks_10', 'percentile_roll_std_20',
'ratio_unique_values_denoised', 'fftr_percentile_roll_std_75_denoised', 'num_crossing_0_denoised', 'percentile_95', 'ffti_percentile_roll_std_75_denoised',
'min_roll_std_10000', 'percentile_roll_std_1', 'percentile_roll_std_10', 'fftr_percentile_roll_std_70_denoised', 'ave_roll_std_50', 'ffti_percentile_roll_std_70_denoised',
'exp_Moving_std_300_mean_denoised', 'ffti_percentile_roll_std_30_denoised', 'mean_change_rate', 'percentile_roll_std_5', 'range_-1000_0', 'mad',
'fftr_range_1000_2000_denoised', 'percentile_10_denoised', 'ffti_percentile_roll_std_80', 'percentile_roll_std_25', 'fftr_percentile_10_denoised',
'ffti_range_-2000_-1000_denoised', 'autocorrelation_5', 'min_roll_std_100', 'fftr_percentile_roll_std_80', 'min_roll_std_500', 'min_roll_std_50', 'min_roll_std_1000',
'ffti_percentile_20_denoised', 'iqr1', 'classic_sta_lta5_mean_denoised', 'classic_sta_lta6_mean_denoised', 'percentile_roll_std_10_denoised',
'fftr_percentile_70_denoised', 'ffti_c3_50_denoised', 'ffti_percentile_roll_std_75', 'abs_percentile_90', 'range_0_1000', 'spkt_welch_density_50_denoised',
'ffti_percentile_roll_std_40_denoised', 'ffti_range_-4000_-3000', 'mean_change_rate_last_50000']
X_train, X_valid, y_train, y_valid = train_test_split(X[top_columns], y, test_size=0.1)
 model = lgb.LGBMRegressor(**params, n_estimators = 50000, n_jobs = -1, verbose=-1)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='mae',
verbose=10000, early_stopping_rounds=200)
 perm = eli5.sklearn.PermutationImportance(model, random_state=1 ).fit(X_train, y_train )
def decode_image(filename, label=None, image_size=(IM_Z, IM_Z)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_augment(image, label=None):
image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 if label is None:
return image
else:
return image, label<categorify>
eli5.show_weights(perm, top=50, feature_names=top_columns )
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.cache() 
.map(data_augment, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( valid_paths, valid_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )<choose_model_class>
submission['time_to_failure'] = result_dict_lgb['prediction']
submission.to_csv('submission_eli5.csv' )
def build_lrfn(lr_start=0.00001, lr_max=0.000075,
lr_min=0.000001, lr_rampup_epochs=20,
lr_sustain_epochs=0, lr_exp_decay=.8):
lr_max = lr_max * strategy.num_replicas_in_sync
def lrfn(epoch):
if epoch < lr_rampup_epochs:
lr =(lr_max - lr_start)/ lr_rampup_epochs * epoch + lr_start
elif epoch < lr_rampup_epochs + lr_sustain_epochs:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_exp_decay**(epoch - lr_rampup_epochs - lr_sustain_epochs)+ lr_min
return lr
return lrfn
ch_p = ModelCheckpoint(filepath="model_ef.h5", monitor='val_loss', save_weights_only=True,
verbose=1 )<choose_model_class>
params = {'num_leaves': 32,
'min_child_samples': 79,
'objective': 'gamma',
'max_depth': -1,
'learning_rate': 0.01,
"boosting_type": "gbdt",
"subsample_freq": 5,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501,
'colsample_bytree': 1.0
}
with strategy.scope() :
model = tf.keras.Sequential([
efn.EfficientNetB7(
 input_shape=(IM_Z, IM_Z, 3),
weights='imagenet',
include_top=False 
),
L.GlobalAveragePooling2D() ,
L.Dense(train_labels.shape[1], activation='softmax')
 ])
 model.compile(
 optimizer='adam',
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
<choose_model_class>
scores_dict = {'f_classif': [2.0746468465171377, 2.0753843541953687, 2.062191535440333, 2.0654327826583034, 2.0643551320704936, 2.0617560048382675,
2.061565197738015, 2.0598878198917494, 2.0654865223333143, 2.0632788555735777, 2.058002635080971, 2.051075689018734,
2.0472543961304583, 2.052401474353084, 2.055924154798443, 2.0561794619762352, 2.0549680611994963, 2.057123777802326,
2.0591868861136904, 2.0577745274024553],
'mutual_info_classif': [2.0866763775014006, 2.0745431497064324, 2.0564324832516427, 2.060125564781158, 2.067334544167612, 2.0665943783246448,
2.063891669849029, 2.070194051004794, 2.0667490707700447, 2.0681653852378354, 2.0592743636982345, 2.061260741522344,
2.05680667824411, 2.0565047875243003, 2.058252567141659, 2.0554927194831922, 2.0562776429736873, 2.0618179277444084,
2.06364125584214, 2.0577745274024553],
'n_features': [98, 196, 294, 392, 490, 588, 685, 783, 881, 979, 1077, 1175, 1273, 1370, 1468, 1566, 1664, 1762, 1860, 1958]}
lrfn = build_lrfn()
lr_schedule = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=1)
 STEPS_PER_EPOCH = train_labels.shape[0] // BATCH_SIZE<train_model>
scores_dict = {'f_classif': [2.1495892622081354, 2.0778182269587147, 2.0716153738740006, 2.06152950679902, 2.0645162758752553, 2.0627705797004032, 2.0610992303725157,
2.057762113735462, 2.0618360883613627, 2.0603197111525984, 2.06081274633874, 2.0580767195278056, 2.0527646572747127, 2.0498353445032533,
2.052442594925, 2.0564456881902133, 2.0582284644115365, 2.0558612960548635, 2.0580900016350094, 2.058218782401599],
'mutual_info_classif': [2.1235703196243687, 2.084958198672301, 2.0596822478390955, 2.053305869981444, 2.063468853227225, 2.0674399950434323, 2.0658618511287874,
2.063003703200445, 2.0653174905858664, 2.0644340327023656, 2.0748993062333523, 2.0587602096358113, 2.0601495560836076, 2.0559629138548603,
2.0553852701221134, 2.058022171415446, 2.060755947658241, 2.057916705462307, 2.056245795262636, 2.0580691870837056],
'n_features': [10, 110, 210, 310, 410, 510, 610, 710, 810, 910, 1010, 1110, 1210, 1310, 1410, 1510, 1610, 1710, 1810, 1910]}
history = model.fit(
 train_dataset,
epochs=EPOCHS,
callbacks=[lr_schedule, ch_p],
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=valid_dataset
)<choose_model_class>
corr_matrix = X.corr().abs()
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
to_drop = [column for column in upper.columns if any(upper[column] > 0.99)]
X = X.drop(to_drop, axis=1)
 X_test = X_test.drop(to_drop, axis=1)
 result_dict_lgb_lgb = artgor_utils.train_model_regression(X, X_test, y, params=params, folds=folds, model_type='lgb', plot_feature_importance=True )
with strategy.scope() :
model1 = tf.keras.Sequential([
tf.keras.applications.DenseNet201(
 input_shape=(IM_Z, IM_Z, 3),
weights='imagenet',
include_top=False 
),
L.GlobalAveragePooling2D() ,
L.Dense(train_labels.shape[1], activation='softmax')
 ])
 model1.compile(
 optimizer='adam',
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
<choose_model_class>
submission['time_to_failure'] = result_dict_lgb['prediction']
submission.to_csv('submission_no_corr.csv' )
ch_p_den = ModelCheckpoint(filepath="model_den.h5", monitor='val_loss', save_weights_only=True,
verbose=1 )<train_model>
scores_dict = {'rfe_score': [2.103586938061856, 2.052535910798748, 2.053228199447811], 'n_features': [10, 110, 210]}
history1 = model1.fit(
 train_dataset,
epochs=EPOCHS,
callbacks=[lr_schedule, ch_p_den],
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=valid_dataset
)<save_to_csv>
%%time
rfr = RandomForestRegressor()
rfr = RandomForestRegressor(n_estimators=50, max_depth=5)
 result_dict_rfr = artgor_utils.train_model_regression(X, X_test, y, params=params, folds=folds, model_type='sklearn', model=rfr)
sub.loc[:, 'healthy':] = probs
sub.to_csv('submission.csv', index=False)
 sub.head()<load_from_csv>
%%time
linreg = linear_model.LinearRegression(normalize=False, copy_X=True, n_jobs=-1)
 result_dict_linreg = artgor_utils.train_model_regression(X, X_test, y, params=None, folds=folds, model_type='sklearn', model=linreg)
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 df_sample = pd.read_csv('.. /input/sample_submission.csv')
<define_variables>
%%time
ridge = linear_model.Ridge(normalize=True)
 parameter_grid = {'alpha': [0.1, 1.0, 10.0]}
grid_search = GridSearchCV(ridge, param_grid=parameter_grid, cv=folds, scoring='neg_mean_absolute_error', n_jobs=-1)
 grid_search.fit(X, y)
 print('Best score: {}'.format(grid_search.best_score_))
print('Best parameters: {}'.format(grid_search.best_params_))
ridge = linear_model.Ridge(**grid_search.best_params_, normalize=True)
 result_dict_ridge = artgor_utils.train_model_regression(X, X_test, y, params=params, folds=folds, model_type='sklearn', model=ridge)
print(len(df_train.columns))
new_cont_ord_cols = [c for c in df_train.columns if not c.endswith('bin')]
no_bin_cat_cols = [c for c in new_cont_ord_cols if not c.endswith('cat')][2:]<load_from_csv>
%%time
knn = neighbors.KNeighborsRegressor()
parameter_grid = {'n_neighbors': [50, 100]}
grid_search = GridSearchCV(knn, param_grid=parameter_grid, cv=folds, scoring='neg_mean_absolute_error', n_jobs=-1)
 grid_search.fit(X, y)
 print('Best score: {}'.format(grid_search.best_score_))
print('Best parameters: {}'.format(grid_search.best_params_))
knn = neighbors.KNeighborsRegressor(**grid_search.best_params_)
 result_dict_knn = artgor_utils.train_model_regression(X, X_test, y, params=params, folds=folds, model_type='sklearn', model=knn )
<data_type_conversions>
%%time
lasso = linear_model.Lasso(normalize=True)
 parameter_grid = {'alpha': [0.1, 1.0, 10.0]}
grid_search = GridSearchCV(lasso, param_grid=parameter_grid, cv=folds, scoring='neg_mean_absolute_error', n_jobs=-1)
 grid_search.fit(X, y)
 print('Best score: {}'.format(grid_search.best_score_))
print('Best parameters: {}'.format(grid_search.best_params_))
lasso = linear_model.Lasso(**grid_search.best_params_, normalize=True)
 result_dict_lasso = artgor_utils.train_model_regression(X, X_test, y, params=params, folds=folds, model_type='sklearn', model=lasso )
<drop_column>
%%time
etr = ExtraTreesRegressor()
etr = ExtraTreesRegressor(n_estimators=1000, max_depth=10)
 result_dict_etr = artgor_utils.train_model_regression(X, X_test, y, params=params, folds=folds, model_type='sklearn', model=etr )
target = df_train['target'] 
<count_unique_values>
%%time
adr = AdaBoostRegressor()
parameter_grid = {'n_estimators': [10, 50],
}
grid_search = GridSearchCV(adr, param_grid=parameter_grid, cv=folds, scoring='neg_mean_absolute_error', n_jobs=-1)
 grid_search.fit(X, y)
 print('Best score: {}'.format(grid_search.best_score_))
print('Best parameters: {}'.format(grid_search.best_params_))
adr = AdaBoostRegressor(**grid_search.best_params_)
 result_dict_adr = artgor_utils.train_model_regression(X, X_test, y, params=params, folds=folds, model_type='sklearn', model=adr )
print(df_train.dtypes.unique())
 print(df_train.dtypes.nunique())
 print(df_test.dtypes.unique())
 print(df_test.dtypes.nunique() )<count_missing_values>
params = {'num_leaves': 128,
'min_child_samples': 79,
'objective': 'gamma',
'max_depth': -1,
'learning_rate': 0.01,
"boosting_type": "gbdt",
"subsample_freq": 5,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501,
'colsample_bytree': 0.2
}
result_dict_lgb = artgor_utils.train_model_regression(X=X, X_test=X_test, y=y, params=params, folds=folds, model_type='lgb',
eval_metric='mae', plot_feature_importance=True )
print(df_train.isnull().values.any())
 print(df_test.isnull().values.any() )<feature_engineering>
xgb_params = {'eta': 0.03,
'max_depth': 9,
'subsample': 0.85,
'colsample_bytree': 0.3,
'objective': 'reg:linear',
'eval_metric': 'mae',
'silent': True,
'nthread': -1}
result_dict_xgb = artgor_utils.train_model_regression(X=X, X_test=X_test, y=y, params=xgb_params, folds=folds, model_type='xgb' )
df_train[(df_train == -1)] = np.nan
df_test[(df_test == -1)] = np.nan
print('done' )<count_missing_values>
submission['time_to_failure'] =(result_dict_lgb['prediction'] + result_dict_etr['prediction'] + result_dict_xgb['prediction'])/ 3
print(submission.head())
 submission.to_csv('blending.csv' )
print(df_train.isnull().values.any())
 print(df_test.isnull().values.any() )<data_type_conversions>
train_stack = np.vstack([result_dict_rfr['oof'], result_dict_ridge['oof'], result_dict_knn['oof'], result_dict_lasso['oof'], result_dict_etr['oof'],
result_dict_adr['oof'], result_dict_lgb['oof'], result_dict_xgb['oof'], result_dict_etr['oof']] ).transpose()
train_stack = pd.DataFrame(train_stack, columns=['rfr', 'ridge', 'knn', 'lasso', 'etr', 'adr', 'lgb', 'xgb', 'etr'])
 test_stack = np.vstack([result_dict_rfr['prediction'], result_dict_ridge['prediction'], result_dict_knn['prediction'], result_dict_lasso['prediction'], result_dict_etr['prediction'],
result_dict_adr['prediction'], result_dict_lgb['prediction'], result_dict_xgb['prediction'], result_dict_etr['prediction']] ).transpose()
test_stack = pd.DataFrame(test_stack, columns=['rfr', 'ridge', 'knn', 'lasso', 'etr', 'adr', 'lgb', 'xgb', 'etr'] )
cols_missing_val_train = df_train.columns[df_train.isnull().any() ].tolist()
print(cols_missing_val_train)
 print('
')
 cols_missing_val_test = df_test.columns[df_test.isnull().any() ].tolist()
print(cols_missing_val_test )<categorify>
params = {'num_leaves': 8,
'objective': 'regression',
'max_depth': 2,
'learning_rate': 0.01,
"boosting": "gbdt",
"bagging_seed": 11,
"metric": 'rmse',
"verbosity": -1}
result_dict_lgb_stack = artgor_utils.train_model_regression(X=train_stack, X_test=test_stack, y=y, params=params, folds=folds, model_type='lgb',
eval_metric='mae', plot_feature_importance=False,
columns=(list(train_stack.columns)) )
<data_type_conversions><EOS> 
submission['time_to_failure'] = result_dict_lgb_stack['prediction']
print(submission.head())
 submission.to_csv('stacking.csv' )