Kaggle competition: Categorical Feature Encoding Challenge II
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge-ii<load_from_csv>
%%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/
start = time.time()
print("Reading test data...")
 test = pd.read_csv('.. /input/test.csv')
 end = time.time()
print("It takes {0:.2f} seconds to read 'test.csv'.".format(end - start))<drop_column>
from cuml.linear_model import Ridge
target = train.pop('target')
 train_ids = train.pop('ID_code')
 test_ids = test.pop('ID_code' )<define_variables>
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' )
len_train = len(train )<concatenate>
train_oof = cp.zeros(( train.shape[0],))
test_preds = 0
train_oof.shape
merged = pd.concat([train, test] )<set_options>
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
del test, train
gc.collect()<data_type_conversions>
%%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))
for col in merged.columns:
merged[col] =(( merged[col] - merged[col].mean())
 / merged[col].std() ).astype('float32')
 merged[col+'^2'] = merged[col] * merged[col]
merged[col+'^3'] = merged[col] * merged[col] * merged[col]
merged[col+'^4'] = merged[col] * merged[col] * merged[col] * merged[col]
merged[col+'_cp'] = rankdata(merged[col] ).astype('float32')
 merged[col+'_cnp'] = norm.cdf(merged[col] ).astype('float32' )<define_variables>
sample_submission['target'] = test_preds
sample_submission.to_csv('submission.csv', index=False )
<data_type_conversions><EOS> 
cp.save('test_preds', test_preds)
 cp.save('train_oof', train_oof )