Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<compute_test_metric>
warnings.filterwarnings("ignore")
 PandasDataFrame = TypeVar('pandas.core.frame.DataFrame')
IcebergModel.evaluate(x=X_band,y=target )<predict_on_test>
train_X = pd.read_csv('.. /input/mlip-data/train_features.csv')
 test_X = pd.read_csv('.. /input/mlip-data/test_features.csv')
 train_y = pd.read_csv('.. /input/mlip-data/y.csv' )
pred_label = IcebergModel.predict(x=X_band)
 pred_label[pred_label>0.5]=1
pred_label[pred_label<=0.5]=0
print(classification_report(target,pred_label))<data_type_conversions>
submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv', index_col='seg_id')
 submission.shape
X_band_test_1=np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in test_df["band_1"]])
 X_band_test_2=np.array([np.array(band ).astype(np.float32 ).reshape(75, 75)for band in test_df["band_2"]])
 X_test = np.zeros([8424,75,75,3])
 for t in range(8424):
X_test[t,:,:,0] = X_band_test_1[t]
X_test[t,:,:,1] = X_band_test_2[t]
X_test[t,:,:,2] =(X_band_test_1[t]+X_band_test_2[t])/2<save_to_csv>
mse_scoring = make_scorer(mean_squared_error, greater_is_better=False)
 mae_scoring = make_scorer(mean_absolute_error, greater_is_better=False)
 k_fold = KFold(n_splits=7, shuffle=True, random_state=13 )
pred = IcebergModel.predict(x=X_test)
 sub_df = pd.DataFrame()
sub_df['id'] = test_df['id']
sub_df['is_iceberg'] = pred
sub_df.to_csv('output.csv',index=False )<load_from_csv>
def run_lgb(
 X_tr: PandasDataFrame,
X_val: PandasDataFrame,
y_tr: PandasDataFrame,
y_val: PandasDataFrame,
test_data: PandasDataFrame,
params: Dict 
):

 early_stop = 200
num_rounds = 10000
verbose_eval=1000
d_train = lgb.Dataset(X_tr, label=y_tr)
 d_valid = lgb.Dataset(X_val, label=y_val)
 watchlist = [d_train, d_valid]
model = lgb.train(
 params,
train_set=d_train,
num_boost_round=num_rounds,
valid_sets=watchlist,
verbose_eval=verbose_eval,
early_stopping_rounds=early_stop 
)
 val_pred = model.predict(X_val, num_iteration=model.best_iteration)
 prediction = model.predict(test_data, num_iteration=model.best_iteration)
 return val_pred, prediction, model.feature_importance(importance_type="split")
 def run_cv_model(
 train_data,
train_target,
test_data,
model_fn, params,
scoring,
label='model',
feature_imp=False,
n_folds = 5 
):
oof_val = np.zeros(len(train_data))
predictions = np.zeros(len(test_data))
oof_predict = np.zeros(( n_folds, test_data.shape[0]))
scores = []
feature_importance_df = pd.DataFrame()
folds = KFold(n_splits=n_folds, shuffle=True, random_state=42)
 train_columns = train_data.columns.values
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_data, train_target.values)) :
strLog = "fold {} for {}".format(fold_, label)
 print(strLog)
 X_tr, X_val = train_data.iloc[trn_idx], train_data.iloc[val_idx]
y_tr, y_val = train_target.iloc[trn_idx], train_target.iloc[val_idx]
val_pred, prediction, feature_importances = model_fn(
 X_tr, X_val,
y_tr, y_val,
test_data,
params 
)
 score = scoring(y_val, val_pred)
 scores.append(score)
 oof_val[val_idx] = val_pred
if feature_imp == True:
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = train_columns
fold_importance_df["importance"] = feature_importances[:len(train_columns)]
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 oof_predict[fold_] = prediction
predictions += prediction/folds.n_splits
print('CV score: {0:.4f}, std: {1:.4f}.
 '.format(np.mean(score), np.std(score)))
 print('CV mean score: {0:.4f}, std: {1:.4f}.
 '.format(np.mean(scores), np.std(scores)))
 return oof_val, oof_predict, predictions, feature_importance_df, scores
sub_path = ".. /input/statoil-iceberg-submissions"
all_files = os.listdir(sub_path)
 outs = [pd.read_csv(os.path.join(sub_path, f), index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "is_iceberg_" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub.head() 
<feature_engineering>
def LGB_bayesian(**lgb_bayes_params):
lgb_bayes_params['num_leaves'] = int(lgb_bayes_params['num_leaves'])
 lgb_bayes_params['max_depth'] = int(lgb_bayes_params['max_depth'])
 lgb_bayes_params['min_child_samples'] = int(lgb_bayes_params['min_child_samples'])
 lgb_bayes_params['max_bin'] = int(lgb_bayes_params['max_bin'])
 lgb_bayes_params['subsample_freq'] = int(lgb_bayes_params['subsample_freq'])
 lgb_bayes_params['min_child_weight'] = int(lgb_bayes_params['min_child_weight'])
 lgb_bayes_params['subsample_for_bin'] = int(lgb_bayes_params['subsample_for_bin'])
 lgb_bayes_params['n_estimators'] = int(lgb_bayes_params['n_estimators'])
 fix_lgb_params = {
'objective':'regression',
"boosting": "gbdt",
"metric": 'mae',
}
lgb_params = {**fix_lgb_params, **lgb_bayes_params}
oof_val, oof_predict, predictions, feature_importance_df, scores = run_cv_model(
 train_data=train_X,
train_target=train_y,
test_data=test_X,
model_fn=run_lgb,
params=lgb_params,
scoring=mean_absolute_error,
label="lgb",
feature_imp=True,
n_folds=8 
)
 return -np.mean(scores )
concat_sub['is_iceberg_max'] = concat_sub.iloc[:, 1:6].max(axis=1)
 concat_sub['is_iceberg_min'] = concat_sub.iloc[:, 1:6].min(axis=1)
 concat_sub['is_iceberg_mean'] = concat_sub.iloc[:, 1:6].mean(axis=1)
 concat_sub['is_iceberg_median'] = concat_sub.iloc[:, 1:6].median(axis=1 )<define_variables>
search_spaces = {
'learning_rate':(0.01,.5),
'num_leaves':(2, 350),
'max_depth':(50, 500),
'min_child_samples':(20, 150),
'max_bin':(30000, 100000),
'subsample':(0.01, 1.0),
'subsample_freq':(2, 20),
'colsample_bytree':(0.01, 1.0),
'min_child_weight':(0, 10),
'subsample_for_bin':(100000, 800000),
'reg_lambda':(1e-9, 1000),
'reg_alpha':(1e-9, 1.0),
'scale_pos_weight':(1e-6, 1000),
'n_estimators':(1000, 10000),
'min_gain_to_split':(0.0, 1.0),
}
lgb_bo = BayesianOptimization(LGB_bayesian,search_spaces, random_state=7)
cutoff_lo = 0.7
cutoff_hi = 0.3<save_to_csv>
lgb_bayes_params = lgb_bo.max['params']
lgb_bayes_params['num_leaves'] = int(lgb_bayes_params['num_leaves'])
 lgb_bayes_params['max_depth'] = int(lgb_bayes_params['max_depth'])
 lgb_bayes_params['min_child_samples'] = int(lgb_bayes_params['min_child_samples'])
 lgb_bayes_params['max_bin'] = int(lgb_bayes_params['max_bin'])
 lgb_bayes_params['subsample_freq'] = int(lgb_bayes_params['subsample_freq'])
 lgb_bayes_params['min_child_weight'] = int(lgb_bayes_params['min_child_weight'])
 lgb_bayes_params['subsample_for_bin'] = int(lgb_bayes_params['subsample_for_bin'])
 lgb_bayes_params['n_estimators'] = int(lgb_bayes_params['n_estimators'] )
concat_sub['is_iceberg'] = concat_sub['is_iceberg_mean']
concat_sub[['id', 'is_iceberg']].to_csv('stack_mean.csv',
index=False, float_format='%.6f' )<save_to_csv>
lgb_bayes_params = {'colsample_bytree': 0.07731235427729943,
'learning_rate': 0.021390697326123965,
'max_bin': 52576,
'max_depth': 339,
'min_child_samples': 119,
'min_child_weight': 6,
'min_gain_to_split': 0.4223894968003159,
'n_estimators': 1560,
'num_leaves': 25,
'reg_alpha': 0.7079338662764831,
'reg_lambda': 368.73133802650744,
'scale_pos_weight': 363.3220921999365,
'subsample': 0.9910157826344941,
'subsample_for_bin': 693174,
'subsample_freq': 19}
concat_sub['is_iceberg'] = concat_sub['is_iceberg_median']
concat_sub[['id', 'is_iceberg']].to_csv('stack_median.csv',
index=False, float_format='%.6f' )<save_to_csv>
class FeatureSelector() :

 def __init__(self, data, labels=None):
self.data = data
self.labels = labels
if labels is None:
print('No labels provided.Feature importance based methods are not available.')
 self.base_features = list(data.columns)
 self.one_hot_features = None
self.record_missing = None
self.record_single_unique = None
self.record_collinear = None
self.record_zero_importance = None
self.record_low_importance = None
self.missing_stats = None
self.unique_stats = None
self.corr_matrix = None
self.feature_importances = None
self.ops = {}
self.one_hot_correlated = False
def identify_missing(self, missing_threshold):

 self.missing_threshold = missing_threshold
missing_series = self.data.isnull().sum() / self.data.shape[0]
self.missing_stats = pd.DataFrame(missing_series ).rename(columns = {'index': 'feature', 0: 'missing_fraction'})
 self.missing_stats = self.missing_stats.sort_values('missing_fraction', ascending = False)
 record_missing = pd.DataFrame(missing_series[missing_series > missing_threshold] ).reset_index().rename(columns =
{'index': 'feature',
0: 'missing_fraction'})
 to_drop = list(record_missing['feature'])
 self.record_missing = record_missing
self.ops['missing'] = to_drop
print('%d features with greater than %0.2f missing values.
 ' %(len(self.ops['missing']), self.missing_threshold))
def identify_single_unique(self):

 unique_counts = self.data.nunique()
self.unique_stats = pd.DataFrame(unique_counts ).rename(columns = {'index': 'feature', 0: 'nunique'})
 self.unique_stats = self.unique_stats.sort_values('nunique', ascending = True)
 record_single_unique = pd.DataFrame(unique_counts[unique_counts == 1] ).reset_index().rename(columns = {'index': 'feature',
0: 'nunique'})
 to_drop = list(record_single_unique['feature'])
 self.record_single_unique = record_single_unique
self.ops['single_unique'] = to_drop
print('%d features with a single unique value.
 ' % len(self.ops['single_unique']))
def identify_collinear(self, correlation_threshold, one_hot=False):

 self.correlation_threshold = correlation_threshold
self.one_hot_correlated = one_hot
if one_hot:
features = pd.get_dummies(self.data)
 self.one_hot_features = [column for column in features.columns if column not in self.base_features]
self.data_all = pd.concat([features[self.one_hot_features], self.data], axis = 1)
 corr_matrix = pd.get_dummies(features ).corr()
else:
corr_matrix = self.data.corr()
self.corr_matrix = corr_matrix
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].abs() > correlation_threshold)]
record_collinear = pd.DataFrame(columns = ['drop_feature', 'corr_feature', 'corr_value'])
 for column in to_drop:
corr_features = list(upper.index[upper[column].abs() > correlation_threshold])
 corr_values = list(upper[column][upper[column].abs() > correlation_threshold])
 drop_features = [column for _ in range(len(corr_features)) ]
temp_df = pd.DataFrame.from_dict({'drop_feature': drop_features,
'corr_feature': corr_features,
'corr_value': corr_values})
 record_collinear = record_collinear.append(temp_df, ignore_index = True)
 self.record_collinear = record_collinear
self.ops['collinear'] = to_drop
print('%d features with a correlation magnitude greater than %0.2f.
 ' %(len(self.ops['collinear']), self.correlation_threshold))
def identify_zero_importance(self, feature_importances):

 if self.labels is None:
raise ValueError("No training labels provided.")
 features = pd.get_dummies(self.data)
 self.one_hot_features = [column for column in features.columns if column not in self.base_features]
self.data_all = pd.concat([features[self.one_hot_features], self.data], axis = 1)
 feature_names = list(features.columns)
 features = np.array(features)
 labels = np.array(self.labels ).reshape(( -1,))
feature_importances = feature_importances.sort_values('importance', ascending = False ).reset_index(drop = True)
 feature_importances['normalized_importance'] = feature_importances['importance'] / feature_importances['importance'].sum()
feature_importances['cumulative_importance'] = np.cumsum(feature_importances['normalized_importance'])
 record_zero_importance = feature_importances[feature_importances['importance'] == 0.0]
to_drop = list(record_zero_importance['feature'])
 self.feature_importances = feature_importances
self.record_zero_importance = record_zero_importance
self.ops['zero_importance'] = to_drop
print('
%d features with zero importance after one-hot encoding.
 ' % len(self.ops['zero_importance']))
def identify_low_importance(self, cumulative_importance):

 self.cumulative_importance = cumulative_importance
if self.feature_importances is None:
raise NotImplementedError()
self.feature_importances = self.feature_importances.sort_values('cumulative_importance')
 record_low_importance = self.feature_importances[self.feature_importances['cumulative_importance'] > cumulative_importance]
to_drop = list(record_low_importance['feature'])
 self.record_low_importance = record_low_importance
self.ops['low_importance'] = to_drop
print('%d features required for cumulative importance of %0.2f after one hot encoding.' %(len(self.feature_importances)-
len(self.record_low_importance), self.cumulative_importance))
print('%d features do not contribute to cumulative importance of %0.2f.
 ' %(len(self.ops['low_importance']),
self.cumulative_importance))
def identify_all(self, selection_params):

 for param in ['missing_threshold', 'correlation_threshold', 'eval_metric', 'task', 'cumulative_importance']:
if param not in selection_params.keys() :
raise ValueError('%s is a required parameter for this method.' % param)
 self.identify_missing(selection_params['missing_threshold'])
 self.identify_single_unique()
self.identify_collinear(selection_params['correlation_threshold'])
 self.identify_zero_importance(task = selection_params['task'], eval_metric = selection_params['eval_metric'])
 self.identify_low_importance(selection_params['cumulative_importance'])
 self.all_identified = set(list(chain(*list(self.ops.values()))))
self.n_identified = len(self.all_identified)
 print('%d total features out of %d identified for removal after one-hot encoding.
 ' %(self.n_identified,
self.data_all.shape[1]))
def check_removal(self, keep_one_hot=True):

 self.all_identified = set(list(chain(*list(self.ops.values()))))
print('Total of %d features identified for removal' % len(self.all_identified))
if not keep_one_hot:
if self.one_hot_features is None:
print('Data has not been one-hot encoded')
 else:
one_hot_to_remove = [x for x in self.one_hot_features if x not in self.all_identified]
print('%d additional one-hot features can be removed' % len(one_hot_to_remove))
return list(self.all_identified)
 def remove(self, methods, keep_one_hot = True):

 features_to_drop = []
if methods == 'all':
data = self.data_all
print('{} methods have been run
'.format(list(self.ops.keys())))
 features_to_drop = set(list(chain(*list(self.ops.values()))))
else:
if 'zero_importance' in methods or 'low_importance' in methods or self.one_hot_correlated:
data = self.data_all
else:
data = self.data
for method in methods:
if method not in self.ops.keys() :
raise NotImplementedError('%s method has not been run' % method)
 else:
features_to_drop.append(self.ops[method])
 features_to_drop = set(list(chain(*features_to_drop)))
 features_to_drop = list(features_to_drop)
 if not keep_one_hot:
if self.one_hot_features is None:
print('Data has not been one-hot encoded')
 else:
features_to_drop = list(set(features_to_drop)| set(self.one_hot_features))
data = data.drop(columns = features_to_drop)
 self.removed_features = features_to_drop
if not keep_one_hot:
	print('Removed %d features including one-hot features.' % len(features_to_drop))
else:
	print('Removed %d features.' % len(features_to_drop))
return data
def plot_missing(self):

 if self.record_missing is None:
raise NotImplementedError("Missing values have not been calculated.Run `identify_missing`")
 self.reset_plot()
plt.style.use('seaborn-white')
 plt.figure(figsize =(7, 5))
plt.hist(self.missing_stats['missing_fraction'], bins = np.linspace(0, 1, 11), edgecolor = 'k', color = 'red', linewidth = 1.5)
 plt.xticks(np.linspace(0, 1, 11)) ;
plt.xlabel('Missing Fraction', size = 14); plt.ylabel('Count of Features', size = 14);
plt.title("Fraction of Missing Values Histogram", size = 16);
def plot_unique(self):

 if self.record_single_unique is None:
raise NotImplementedError('Unique values have not been calculated.Run `identify_single_unique`')
 self.reset_plot()
self.unique_stats.plot.hist(edgecolor = 'k', figsize =(7, 5))
plt.ylabel('Frequency', size = 14); plt.xlabel('Unique Values', size = 14);
plt.title('Number of Unique Values Histogram', size = 16);
def plot_collinear(self, plot_all = False):

 if self.record_collinear is None:
raise NotImplementedError('Collinear features have not been idenfitied.Run `identify_collinear`.')
 if plot_all:
	corr_matrix_plot = self.corr_matrix
	title = 'All Correlations'
else:
	
	
	 corr_matrix_plot = self.corr_matrix.loc[list(set(self.record_collinear['corr_feature'])) ,
	 list(set(self.record_collinear['drop_feature'])) ]
	 title = "Correlations Above Threshold"
f, ax = plt.subplots(figsize=(10, 8))
cmap = sns.diverging_palette(220, 10, as_cmap=True)
 sns.heatmap(corr_matrix_plot, cmap=cmap, center=0,
linewidths=.25, cbar_kws={"shrink": 0.6})
 ax.set_yticks([x + 0.5 for x in list(range(corr_matrix_plot.shape[0])) ])
 ax.set_yticklabels(list(corr_matrix_plot.index), size = int(160 / corr_matrix_plot.shape[0])) ;
ax.set_xticks([x + 0.5 for x in list(range(corr_matrix_plot.shape[1])) ])
 ax.set_xticklabels(list(corr_matrix_plot.columns), size = int(160 / corr_matrix_plot.shape[1])) ;
plt.title(title, size = 14)
 def plot_feature_importances(self, plot_n = 15, threshold = None):

 if self.record_zero_importance is None:
raise NotImplementedError('Feature importances have not been determined.Run `idenfity_zero_importance`')
 if plot_n > self.feature_importances.shape[0]:
plot_n = self.feature_importances.shape[0] - 1
self.reset_plot()
plt.figure(figsize =(10, 6))
ax = plt.subplot()
ax.barh(list(reversed(list(self.feature_importances.index[:plot_n]))),
self.feature_importances['normalized_importance'][:plot_n],
align = 'center', edgecolor = 'k')
 ax.set_yticks(list(reversed(list(self.feature_importances.index[:plot_n]))))
ax.set_yticklabels(self.feature_importances['feature'][:plot_n], size = 12)
 plt.xlabel('Normalized Importance', size = 16); plt.title('Feature Importances', size = 18)
 plt.show()
plt.figure(figsize =(6, 4))
plt.plot(list(range(1, len(self.feature_importances)+ 1)) , self.feature_importances['cumulative_importance'], 'r-')
 plt.xlabel('Number of Features', size = 14); plt.ylabel('Cumulative Importance', size = 14);
plt.title('Cumulative Feature Importance', size = 16);
if threshold:
importance_index = np.min(np.where(self.feature_importances['cumulative_importance'] > threshold))
plt.vlines(x = importance_index + 1, ymin = 0, ymax = 1, linestyles='--', colors = 'blue')
 plt.show() ;
print('%d features required for %0.2f of cumulative importance' %(importance_index + 1, threshold))
def reset_plot(self):
plt.rcParams = plt.rcParamsDefault
concat_sub['is_iceberg'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
0, concat_sub['is_iceberg_median']))
concat_sub[['id', 'is_iceberg']].to_csv('stack_pushout_median.csv',
index=False, float_format='%.6f' )<feature_engineering>
fix_lgb_params = {
'objective':'regression',
"boosting": "gbdt",
"metric": 'mae',
}
lgb_params = {**fix_lgb_params, **lgb_bayes_params}
oof_val, oof_predict, predictions, feature_importance_df, scores = run_cv_model(
 train_data=train_X,
train_target=train_y,
test_data=test_X,
model_fn=run_lgb,
params=lgb_params,
scoring=mean_absolute_error,
label="lgb",
feature_imp=True,
n_folds=8
)
concat_sub['is_iceberg'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1),
concat_sub['is_iceberg_max'],
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
concat_sub['is_iceberg_min'],
concat_sub['is_iceberg_mean']))
concat_sub[['id', 'is_iceberg']].to_csv('stack_minmax_mean.csv',
index=False, float_format='%.6f' )<feature_engineering>
submission.to_csv('submission.csv',index=True)
 fs = FeatureSelector(data = train_X, labels = train_y)
 feature_importance = feature_importance_df[["feature", "importance"]].groupby("feature" ).mean()
feature_importance['feature'] = feature_importance.index
concat_sub['is_iceberg'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1),
concat_sub['is_iceberg_max'],
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
concat_sub['is_iceberg_min'],
concat_sub['is_iceberg_median']))
concat_sub['is_iceberg'] = np.clip(concat_sub['is_iceberg'].values, 0.001, 0.999)
 concat_sub[['id', 'is_iceberg']].to_csv('stack_minmax_median.csv',
index=False, float_format='%.6f' )<load_from_csv>
one_hot_features = fs.one_hot_features
base_features = fs.base_features
print('There are %d original features' % len(base_features))
print('There are %d one-hot features' % len(one_hot_features))
sub_base = pd.read_csv('.. /input/statoil-iceberg-submissions/sub_200_ens_densenet.csv' )<feature_engineering>
all_to_remove = fs.check_removal()
concat_sub['is_iceberg_base'] = sub_base['is_iceberg']
concat_sub['is_iceberg'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1),
concat_sub['is_iceberg_max'],
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
concat_sub['is_iceberg_min'],
concat_sub['is_iceberg_base']))
concat_sub['is_iceberg'] = np.clip(concat_sub['is_iceberg'].values, 0.001, 0.999)
 concat_sub[['id', 'is_iceberg']].to_csv('stack_minmax_bestbase.csv',
index=False, float_format='%.6f' )<define_variables>
train_removed_all = fs.remove(methods = 'all', keep_one_hot=False )
%%writefile predict_only_when_confirmation.py
USE_BACK = 10
CHECK_WINS_RATE_THRES = 0.9
CHECK_PAST_PERIOD = 100
PRED_USE_STEP_THRES = 200
PRED_USE_SCORE_THRES = 0.9
my_actions = []
op_actions = []
pr_actions = []
cont_wins = 0
solutions = []
def predict(my_actions, op_actions):
size = len(my_actions)
 d = dict()
for u in range(USE_BACK):
d[f"OP_{u}"] = op_actions[u: size -(USE_BACK - u)]
d[f"MY_{u}"] = my_actions[u: size -(USE_BACK - u)]
X_train = pd.DataFrame(d)
 y_train = op_actions[USE_BACK: size]
y_train = pd.DataFrame(y_train, columns=["y"])
 n = dict()
for u in range(USE_BACK):
n[f"OP_{u}"] = [op_actions[size -(USE_BACK - u)]]
n[f"MY_{u}"] = [my_actions[size -(USE_BACK - u)]]
X_test = pd.DataFrame(n)
 classifier = lgb.LGBMClassifier(
 random_state=0,
n_estimators=20, 
)
 classifier.fit(X_train, y_train)
 return classifier.predict_proba(X_test ).tolist() [0], int(classifier.predict(X_test)[0])
 def randomize() :
return int(random.randint(0, 2))
def predict_only_when_confirmation(observation, configuration):
global my_actions
global op_actions
global pr_actions
global cont_wins
if observation.step != 0:
op_actions.append(observation.lastOpponentAction)
 if observation.step > USE_BACK:
pred_proba, pred = predict(my_actions, op_actions)
 pr_actions.append(pred)
 if observation.step > PRED_USE_STEP_THRES:
CONF_A = max(pred_proba)> PRED_USE_SCORE_THRES
CONF_B = sum([op==l for op, l in zip(op_actions[-CHECK_PAST_PERIOD:], pr_actions[-CHECK_PAST_PERIOD-1:-1])])/CHECK_PAST_PERIOD > CHECK_WINS_RATE_THRES
if CONF_A or CONF_B:
my_action = pred
my_action =(my_action + 1)% 3
else:
my_action = randomize()
else:
my_action = randomize()
else:
my_action = randomize()
my_actions.append(my_action)
 return my_action<install_modules>
def LGB_bayesian_sec(**lgb_bayes_params):
lgb_bayes_params['num_leaves'] = int(lgb_bayes_params['num_leaves'])
 lgb_bayes_params['max_depth'] = int(lgb_bayes_params['max_depth'])
 lgb_bayes_params['min_child_samples'] = int(lgb_bayes_params['min_child_samples'])
 lgb_bayes_params['max_bin'] = int(lgb_bayes_params['max_bin'])
 lgb_bayes_params['subsample_freq'] = int(lgb_bayes_params['subsample_freq'])
 lgb_bayes_params['min_child_weight'] = int(lgb_bayes_params['min_child_weight'])
 lgb_bayes_params['subsample_for_bin'] = int(lgb_bayes_params['subsample_for_bin'])
 lgb_bayes_params['n_estimators'] = int(lgb_bayes_params['n_estimators'])
 fix_lgb_params = {
'objective':'regression',
"boosting": "gbdt",
"metric": 'mae',
}
lgb_params = {**fix_lgb_params, **lgb_bayes_params}
oof_val, oof_predict, predictions, feature_importance_df, scores = run_cv_model(
 train_data=train_df,
train_target=train_y,
test_data=test_df,
model_fn=run_lgb,
params=lgb_params,
scoring=mean_absolute_error,
label="lgb",
feature_imp=True,
n_folds=8 
)
 return -np.mean(scores )
!pip install -q -U kaggle_environments<compute_test_metric>
lgb_bo = BayesianOptimization(LGB_bayesian_sec,search_spaces, random_state=7)
def simulate_score(player1, player2, steps):
return evaluate(
 "rps",
[player1, player2],
configuration={"episodeSteps": steps}
)<feature_engineering>
with warnings.catch_warnings() :
warnings.filterwarnings('ignore')
 lgb_bo.maximize(init_points=50, n_iter=50, acq='ucb' )
opponent_dict = dict()
opponent_dict["black"] = os.listdir(".. /input/rps-dojo/black_belt")
 opponent_dict["blue"] = os.listdir(".. /input/rps-dojo/blue_belt")
 opponent_dict["white"] = os.listdir(".. /input/rps-dojo/white_belt")
 results = []
for types, opponents in tqdm(opponent_dict.items()):
path = f".. /input/rps-dojo/{types}_belt"
for opponent in tqdm(opponents):
result = simulate_score("predict_only_when_confirmation.py", os.path.join(path, opponent), 1000)
 result = [opponent, result[0], types]
results.append(result)
 results_df = pd.DataFrame(results, columns=["opponent", "score", "belt"] )<import_modules>
lgb_bayes_params = lgb_bo.max['params']
lgb_bayes_params['num_leaves'] = int(lgb_bayes_params['num_leaves'])
 lgb_bayes_params['max_depth'] = int(lgb_bayes_params['max_depth'])
 lgb_bayes_params['min_child_samples'] = int(lgb_bayes_params['min_child_samples'])
 lgb_bayes_params['max_bin'] = int(lgb_bayes_params['max_bin'])
 lgb_bayes_params['subsample_freq'] = int(lgb_bayes_params['subsample_freq'])
 lgb_bayes_params['min_child_weight'] = int(lgb_bayes_params['min_child_weight'])
 lgb_bayes_params['subsample_for_bin'] = int(lgb_bayes_params['subsample_for_bin'])
 lgb_bayes_params['n_estimators'] = int(lgb_bayes_params['n_estimators'])
 lgb_params = {**fix_lgb_params, **lgb_bayes_params}
import os
import sys
import operator
import numpy as np
import pandas as pd
from scipy import sparse
import random
import xgboost as xgb
from sklearn import model_selection, preprocessing, ensemble
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import log_loss
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from collections import defaultdict, Counter<train_model>
oof_val, oof_predict, predictions, feature_importance_df, scores = run_cv_model(
 train_data=train_df,
train_target=train_y,
test_data=test_df,
model_fn=run_lgb,
params=lgb_params,
scoring=mean_absolute_error,
label="lgb",
feature_imp=True,
n_folds=8
)
<load_pretrained><EOS> 
submission.time_to_failure = predictions
submission.to_csv('clean_submission.csv',index=True )