{"cells":[{"metadata":{},"cell_type":"markdown","source":"[Rapids](https://rapids.ai) is an open-source GPU accelerated Data Sceince and Machien Learnign library, developed adn mainatained by [Nvidia](https://www.nvidia.com). It is designed to be compatible with many existing CPU tools, such as Pandas, scikit-learn, numpy, etc. It enables **massive** acceleration of many data-science and machine learning tasks, oftentimes by a factor fo 100X, or even more. \n\nRapids is still undergoing developemnt, and as of right now it's not availabel in the Kaggle Docker environment. If you are interested in installing and riunning Rapids locally on your own machine, then you shoudl [refer to the followong instructions](https://rapids.ai/start.html).\n\nThe first successful install of a Rapids library on kaggle was done by [Chris Deotte](https://www.kaggle.com/cdeotte) in the follwiong [Digit Recognizer kernel](https://www.kaggle.com/cdeotte/rapids-gpu-knn-mnist-0-97). An improved install version that uses a Kaggle Dataset for install can be found [here](https://www.kaggle.com/cdeotte/rapids-data-augmentation-mnist-0-985).  In this kerenl we'll follow that approach."},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"%%time\n# INSTALL RAPIDS OFFLINE (FROM KAGGLE DATASET). TAKES 1 MINUTE :-)\nimport sys\n!cp ../input/rapids/rapids.0.11.0 /opt/conda/envs/rapids.tar.gz\n!cd /opt/conda/envs/ && tar -xzvf rapids.tar.gz\nsys.path = [\"/opt/conda/envs/rapids/lib\"] + [\"/opt/conda/envs/rapids/lib/python3.6\"] + [\"/opt/conda/envs/rapids/lib/python3.6/site-packages\"] + sys.path\n!cp /opt/conda/envs/rapids/lib/libxgboost.so /opt/conda/lib/","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom sklearn.metrics import accuracy_score, roc_auc_score\nimport cudf, cuml\nimport cupy as cp\nfrom cuml.linear_model import LogisticRegression\nimport numpy as np\n#from cuml.metrics import accuracy_score\n\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\nimport gc\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"In this kernels we'll use Ridge regression as our model. Ridge regression is actually a very good choice for **classification** problem when the evaluation metric is AUC - it often performs nearly as well, or even better, than Logistic Reression. "},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"code","source":"from cuml.linear_model import Ridge\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = cudf.read_csv('../input/multi-cat-encodings/X_train_te.csv')\ntest = cudf.read_csv('../input/multi-cat-encodings/X_test_te.csv')\nsample_submission = cudf.read_csv('../input/cat-in-the-dat-ii/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_oof = cp.zeros((train.shape[0],))\ntest_preds = 0\ntrain_oof.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def auc_cp(y_true,y_pred):\n    y_true = y_true.astype('float32')\n    ids = np.argsort(-y_pred) # we want descedning order\n    y_true = y_true[ids.values]\n    y_pred = y_pred[ids.values]\n    zero = 1 - y_true\n    acc_one = cp.cumsum(y_true)\n    acc_zero = cp.cumsum(zero)\n    sum_one = cp.sum(y_true)\n    sum_zero = cp.sum(zero)\n    tpr = acc_one/sum_one\n    fpr = acc_zero/sum_zero\n    return calculate_area(fpr,tpr)\n\ndef calculate_area(fpr,tpr):\n    return cp.sum((fpr[1:]-fpr[:-1])*(tpr[1:]+tpr[:-1]))/2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features = test.columns\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nn_splits = 5\nkf = KFold(n_splits=n_splits, random_state=137)\nscores = []\n\nfor jj, (train_index, val_index) in enumerate(kf.split(train)):\n    print(\"Fitting fold\", jj+1)\n    train_features = train.loc[train['fold_column'] != jj][features]\n    train_target = train.loc[train['fold_column'] != jj]['target'].values.astype(float)\n    \n    val_features = train.loc[train['fold_column'] == jj][features]\n    val_target = train.loc[train['fold_column'] == jj]['target'].values.astype(float)\n    \n    model = Ridge(alpha = 5)\n    model.fit(train_features, train_target)\n    val_pred = model.predict(val_features)\n    train_oof[val_index] = val_pred\n    val_target = cp.asarray(val_target)\n    score = auc_cp(val_target, val_pred)\n    print(\"Fold AUC:\", score)\n    scores.append(cp.asnumpy(score))\n    test_preds += model.predict(test).values/n_splits\n    del train_features, train_target, val_features, val_target\n    gc.collect()\n    \nprint(\"Mean AUC:\", np.mean(scores))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission['target'] = test_preds\nsample_submission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cp.save('test_preds', test_preds)\ncp.save('train_oof', train_oof)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}