Kaggle competition: Statoil/C-CORE Iceberg Classifier Challenge
mean_dist = dists.mean(axis=1)
 max_dist = dists.max(axis=1)
 min_dist = dists.min(axis=1 )<concatenate>
sub_path = ".. /input/statoil-iceberg-submissions"
all_files = os.listdir(sub_path)
 all_files = all_files[1:3]
all_files.append('submission38.csv')
 all_files
X_ = np.hstack(( X, mean_dist.reshape(-1, 1), max_dist.reshape(-1, 1), min_dist.reshape(-1, 1)) )<train_model>
out1 = pd.read_csv(".. /input/statoil-iceberg-submissions/sub_200_ens_densenet.csv", index_col=0)
 out2 = pd.read_csv(".. /input/statoil-iceberg-submissions/sub_TF_keras.csv", index_col=0)
 out3 = pd.read_csv(".. /input/submission38-lb01448/submission38.csv", index_col=0)
 concat_sub = pd.concat([out1, out2, out3], 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()
X_train, X_valid, y_train, y_valid = train_test_split(X_, np.log1p(y), test_size=0.20, random_state=42)
 params = {'learning_rate': 0.02, 'max_depth': 13, 'boosting': 'gbdt', 'objective': 'regression', 'metric': 'rmse', 'is_training_metric': True, 'num_leaves': 12**2, 'feature_fraction': 0.9,
'bagging_fraction': 0.8, 'bagging_freq': 5, 'num_threads': 16}
model = lgb.train(params, lgb.Dataset(X_train, label=y_train), 2000, lgb.Dataset(X_valid, label=y_valid), verbose_eval=50, early_stopping_rounds=20)
 print('RMSLE on valid data: {0:.4}.'.format(rmsle(np.expm1(y_valid), np.expm1(model.predict(X_valid)))) )<compute_train_metric>
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 )
%%time
test_dists, _ = neigh.kneighbors(X_test, n_neighbors=3 )<concatenate>
cutoff_lo = 0.75
cutoff_hi = 0.25
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_ = np.hstack(( X_test, test_mean_dist.reshape(-1, 1), test_max_dist.reshape(-1, 1), test_min_dist.reshape(-1, 1)) )<train_model>
%%time
prediction = np.zeros(( test.shape[0], 1))
score = []
for train_i, test_i in kf.split(X_):
print('Fold')
 X_train = X_[train_i]
y_train = np.log1p(y)[train_i]
X_valid = X_[test_i]
y_valid = np.log1p(y)[test_i]
model = lgb.train(params, lgb.Dataset(X_train, label=y_train), 2000, lgb.Dataset(X_valid, label=y_valid), verbose_eval=1000, early_stopping_rounds=100)
 pred = model.predict(X_test_ ).reshape(-1, 1)
 prediction += np.expm1(pred)
 score.append(model.best_score['valid_0']['rmse'])
 print('Mean score: {:.6}.Std score: {:.6}'.format(np.mean(score), np.std(score)) )<save_to_csv>
sub_base = pd.read_csv('.. /input/submission38-lb01448/submission38.csv' )
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = prediction / 10
sub.to_csv('lgb.csv', index=False )<load_from_csv>
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('submission39.csv',
index=False, float_format='%.6f' )