Kaggle competition: LANL Earthquake Prediction
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<predict_on_test>
warnings.filterwarnings("ignore")
 warnings.simplefilter(action='ignore', category=FutureWarning)
ENet_pred = np.expm1(ENet.predict(X_test.values))<save_to_csv>
train_X_0 = pd.read_csv(".. /input/lanl-master-s-features-creating-0/train_X_features_865.csv")
 train_X_1 = pd.read_csv(".. /input/lanl-master-s-features-creating-1/train_X_features_865.csv")
 y_0 = pd.read_csv(".. /input/lanl-master-s-features-creating-0/train_y.csv", index_col=False, header=None)
 y_1 = pd.read_csv(".. /input/lanl-master-s-features-creating-1/train_y.csv", index_col=False, header=None )
sub = pd.DataFrame()
sub['Id'] = test['Id']
sub['SalePrice'] = ENet_pred
sub.to_csv('submission.csv',index=False )<set_options>
train_X = pd.concat([train_X_0, train_X_1], axis=0)
 train_X = train_X.reset_index(drop=True)
 print(train_X.shape)
 train_X.head()
%matplotlib inline<load_from_csv>
y = pd.concat([y_0, y_1], axis=0)
 y = y.reset_index(drop=True)
 y[0].shape
def load_housing_data(data_name):
csv_path = os.path.join('.. /input/house-prices-advanced-regression-techniques', data_name)
 return pd.read_csv(csv_path )<load_from_csv>
test_X = pd.read_csv(".. /input/lanl-master-s-features-creating-0/test_X_features_10.csv")
train, test = load_housing_data("train.csv"), load_housing_data("test.csv" )<count_values>
scaler = StandardScaler()
train_columns = train_X.columns
train_X[train_columns] = scaler.fit_transform(train_X[train_columns])
 test_X[train_columns] = scaler.transform(test_X[train_columns] )
train['YrSold'].value_counts()<data_type_conversions>
train_columns = train_X.columns
n_fold = 5
def to_str(feature, data=train):
data[feature] = data[feature].astype(str )<data_type_conversions>
%%time
folds = KFold(n_splits=n_fold, shuffle=True, random_state=42)
 oof = np.zeros(len(train_X))
train_score = []
fold_idxs = []
predictions = np.zeros(len(test_X))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_X,train_y.values)) :
strLog = "fold {}".format(fold_)
 print(strLog)
 fold_idxs.append(val_idx)
 X_tr, X_val = train_X[train_columns].iloc[trn_idx], train_X[train_columns].iloc[val_idx]
y_tr, y_val = train_y.iloc[trn_idx], train_y.iloc[val_idx]
model = CatBoostRegressor(n_estimators=30000, verbose=-1, objective="MAE", loss_function="MAE", boosting_type="Ordered", task_type="GPU")
 model.fit(X_tr,
y_tr,
eval_set=[(X_val, y_val)],
verbose=2500,
early_stopping_rounds=600)
 oof[val_idx] = model.predict(X_val)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = train_columns
fold_importance_df["importance"] = model.feature_importances_[:len(train_columns)]
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += model.predict(test_X[train_columns])/ folds.n_splits
train_score.append(model.best_score_['learn']["MAE"])
 cv_score = mean_absolute_error(train_y, oof)
 print(f"After {n_fold} test_CV = {cv_score:.3f} | train_CV = {np.mean(train_score):.3f} | {cv_score-np.mean(train_score):.3f}", end=" ")
 today = str(datetime.date.today())
 submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 submission["time_to_failure"] = predictions
submission.to_csv(f'CatBoost_MAE_{today}_test_{cv_score:.3f}_train_{np.mean(train_score):.3f}.csv', index=False)
 submission.head()
to_str('YrSold')
 to_str('MoSold')
 to_str('MSSubClass')
 to_str('YrSold',test)
 to_str('MoSold',test)
 to_str('MSSubClass',test )<count_values>
%%time
folds = KFold(n_splits=n_fold, shuffle=True, random_state=42)
 oof = np.zeros(len(train_X))
train_score = []
fold_idxs = []
predictions = np.zeros(len(test_X))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_X,train_y.values)) :
strLog = "fold {}".format(fold_)
 print(strLog)
 fold_idxs.append(val_idx)
 X_tr, X_val = train_X[train_columns].iloc[trn_idx], train_X[train_columns].iloc[val_idx]
y_tr, y_val = train_y.iloc[trn_idx], train_y.iloc[val_idx]
model = CatBoostRegressor(n_estimators=30000, verbose=-1, objective="RMSE", loss_function="RMSE", boosting_type="Ordered", task_type="GPU")
 model.fit(X_tr,
y_tr,
eval_set=[(X_val, y_val)],
verbose=2500,
early_stopping_rounds=600)
 oof[val_idx] = model.predict(X_val)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = train_columns
fold_importance_df["importance"] = model.feature_importances_[:len(train_columns)]
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += model.predict(test_X[train_columns])/ folds.n_splits
train_score.append(model.best_score_['learn']["RMSE"])
 cv_score = mean_absolute_error(train_y, oof)
 print(f"After {n_fold} test_CV = {cv_score:.3f} | train_CV = {np.mean(train_score):.3f} | {cv_score-np.mean(train_score):.3f}", end=" ")
 today = str(datetime.date.today())
 submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 submission["time_to_failure"] = predictions
submission.to_csv(f'CatBoost_RMSE_{today}_test_{cv_score:.3f}_train_{np.mean(train_score):.3f}.csv', index=False)
 submission.head()