Kaggle competition: Categorical Feature Encoding Challenge II
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge-ii<define_variables>
%%time
!cp.. /input/rapids/rapids.0.11.0 /opt/conda/envs/rapids.tar.gz
!cd /opt/conda/envs/ && tar -xzvf rapids.tar.gz
sys.path = ["/opt/conda/envs/rapids/lib"] + ["/opt/conda/envs/rapids/lib/python3.6"] + ["/opt/conda/envs/rapids/lib/python3.6/site-packages"] + sys.path
!cp /opt/conda/envs/rapids/lib/libxgboost.so /opt/conda/lib/
y_target = np.log10(np.array(y_target)+ 1 )<split>
from cuml.linear_model import Ridge
X_train, X_test, y_train, y_test = train_test_split(sparse_matrix, y_target, test_size = 0.1 )<train_model>
train = cudf.read_csv('.. /input/multi-cat-encodings/X_train_te.csv')
 test = cudf.read_csv('.. /input/multi-cat-encodings/X_test_te.csv')
 sample_submission = cudf.read_csv('.. /input/cat-in-the-dat-ii/sample_submission.csv' )
start = time.time()
reg_ridge = Ridge(solver='sag', alpha=5)
 reg_ridge.fit(X_train, y_train)
 y_pred = reg_ridge.predict(X_test)
 print(rmsle(10 ** y_test - 1, 10 ** y_pred - 1))
print_time(start )<import_modules>
train_oof = cp.zeros(( train.shape[0],))
test_preds = 0
train_oof.shape
import lightgbm as lgb<compute_test_metric>
def auc_cp(y_true,y_pred):
y_true = y_true.astype('float32')
 ids = np.argsort(-y_pred)
 y_true = y_true[ids.values]
y_pred = y_pred[ids.values]
zero = 1 - y_true
acc_one = cp.cumsum(y_true)
 acc_zero = cp.cumsum(zero)
 sum_one = cp.sum(y_true)
 sum_zero = cp.sum(zero)
 tpr = acc_one/sum_one
fpr = acc_zero/sum_zero
return calculate_area(fpr,tpr)
 def calculate_area(fpr,tpr):
return cp.sum(( fpr[1:]-fpr[:-1])*(tpr[1:]+tpr[:-1])) /2
def rmsle_lgb(y_true, y_pred):
assert len(y_true)== len(y_pred)
 return np.sqrt(np.mean(np.power(np.log1p(y_true + 1)- np.log1p(y_pred + 1), 2)) )<train_model>
%%time
n_splits = 5
kf = KFold(n_splits=n_splits, random_state=137)
 scores = []
for jj,(train_index, val_index)in enumerate(kf.split(train)) :
print("Fitting fold", jj+1)
 train_features = train.loc[train['fold_column'] != jj][features]
train_target = train.loc[train['fold_column'] != jj]['target'].values.astype(float)
 val_features = train.loc[train['fold_column'] == jj][features]
val_target = train.loc[train['fold_column'] == jj]['target'].values.astype(float)
 model = Ridge(alpha = 5)
 model.fit(train_features, train_target)
 val_pred = model.predict(val_features)
 train_oof[val_index] = val_pred
val_target = cp.asarray(val_target)
 score = auc_cp(val_target, val_pred)
 print("Fold AUC:", score)
 scores.append(cp.asnumpy(score))
test_preds += model.predict(test ).values/n_splits
del train_features, train_target, val_features, val_target
gc.collect()
print("Mean AUC:", np.mean(scores))
print("Cell didn't crash...")
 start = time.time()
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'verbose': 1,
'num_leaves': 31,
'n_estimators': 1000,
'learning_rate': 0.5,
'max_depth': 10
}
reg_lgbm = lgb.LGBMRegressor(**params, n_jobs=4)
 print('check')
 reg_lgbm.fit(X_train, y_train)
 y_pred = reg_lgbm.predict(X_test)
 print(rmsle(10 ** y_test - 1, 10 ** y_pred - 1))
print_time(start )<define_variables>
sample_submission['target'] = test_preds
sample_submission.to_csv('submission.csv', index=False )
def stack_predictions(X_train, y_train, X_test, submit, K, *models):
train_preds = pd.DataFrame(index=np.array(range(X_train.shape[0])))
 test_preds = pd.DataFrame(index=np.array(range(X_test.shape[0])))
 submit_preds = pd.DataFrame(index=np.array(range(submit.shape[0])))
 folds = KFold(n_splits=K, shuffle=True)
 fold_n = 0
train_folds = np.zeros(len(train_preds))
for train_index, test_index in folds.split(X_train):
train_folds[test_index] = fold_n
fold_n += 1
fold_n = 0
test_folds = np.zeros(len(test_preds))
for train_index, test_index in folds.split(X_test):
test_folds[test_index] = fold_n
fold_n += 1
fold_n = 0
submit_folds = np.zeros(len(submit_preds))
for train_index, test_index in folds.split(submit):
submit_folds[test_index] = fold_n
fold_n += 1
for m, model in enumerate(models):
print('Selecting model %d.' %(m+1))
col = 'pred_col_' + str(m)
 train_preds[col] = np.nan
test_preds[col] = np.nan
submit_preds[col] = np.nan
for fold in range(K):
print('Processing a fold...')
 current_model = clone(model)
 current_model.fit(X_train[np.where(train_folds!=fold)], y_train[np.where(train_folds!=fold)])
 train_preds[col].iloc[np.where(train_folds==fold)] = current_model.predict(
 X_train[np.where(train_folds==fold)])
 test_preds[col].iloc[np.where(test_folds==fold)] = current_model.predict(
 X_test[np.where(test_folds==fold)])
 submit_preds[col].iloc[np.where(submit_folds==fold)] = current_model.predict(
 submit[np.where(submit_folds==fold)])
 return train_preds, test_preds, submit_preds<choose_model_class>
cp.save('test_preds', test_preds)
 cp.save('train_oof', train_oof )