{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9be42a71-b3bd-3223-81a0-5163b0af2102"},"outputs":[],"source":"import numpy as np\nimport pandas as pd\nfrom xgboost import XGBClassifier\nfrom sklearn.metrics import matthews_corrcoef, roc_auc_score\nfrom sklearn.cross_validation import cross_val_score, StratifiedKFold\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"359ebd0e-50db-4123-cb38-b491a0654cc9"},"outputs":[],"source":"# I'm limited by RAM here and taking the first N rows is likely to be\n# a bad idea for the date data since it is ordered.\n# Sample the data in a roundabout way:\ndate_chunks = pd.read_csv(\"../input/train_date.csv\", index_col=0, chunksize=100000, dtype=np.float32)\nnum_chunks = pd.read_csv(\"../input/train_numeric.csv\", index_col=0,\n                         usecols=list(range(969)), chunksize=100000, dtype=np.float32)\nX = pd.concat([pd.concat([dchunk, nchunk], axis=1).sample(frac=0.05)\n               for dchunk, nchunk in zip(date_chunks, num_chunks)])\ny = pd.read_csv(\"../input/train_numeric.csv\", index_col=0, usecols=[0,969], dtype=np.float32).loc[X.index].values.ravel()\nX = X.values"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"98809097-28d8-c66a-4ff7-3382a3ba0c71"},"outputs":[],"source":"clf = XGBClassifier(base_score=0.005)\nclf.fit(X, y)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ff1ba968-928b-28bb-a409-ad8962eb176a"},"outputs":[],"source":"# threshold for a manageable number of features\nplt.hist(clf.feature_importances_[clf.feature_importances_>0])\nimportant_indices = np.where(clf.feature_importances_>0.005)[0]\nprint(important_indices)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"a5d22e50-a35c-f332-cae5-311da787e182"},"outputs":[],"source":"# load entire dataset for these features. \n# note where the feature indices are split so we can load the correct ones straight from read_csv\nn_date_features = 1156\nX = np.concatenate([\n    pd.read_csv(\"../input/train_date.csv\", index_col=0, dtype=np.float32,\n                usecols=np.concatenate([[0], important_indices[important_indices < n_date_features] + 1])).values,\n    pd.read_csv(\"../input/train_numeric.csv\", index_col=0, dtype=np.float32,\n                usecols=np.concatenate([[0], important_indices[important_indices >= n_date_features] + 1 - 1156])).values\n], axis=1)\ny = pd.read_csv(\"../input/train_numeric.csv\", index_col=0, dtype=np.float32, usecols=[0,969]).values.ravel()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"51bfe912-d179-f32d-ea6a-7bf24a45c5d7"},"outputs":[],"source":"\n\n\n\nclf = XGBClassifier(max_depth=5, base_score=0.005)\ncv = StratifiedKFold(y, n_folds=3)\npreds = np.ones(y.shape[0])\nfor i, (train, test) in enumerate(cv):\n    preds[test] = clf.fit(X[train], y[train]).predict_proba(X[test])[:,1]\n    print(\"fold {}, ROC AUC: {:.3f}\".format(i, roc_auc_score(y[test], preds[test])))\nprint(roc_auc_score(y, preds))\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1af9f830-a2da-9e95-9635-828e810ceb2f"},"outputs":[],"source":"\n\n\n\n# pick the best threshold out-of-fold\nthresholds = np.linspace(0.01, 0.99, 50)\nmcc = np.array([matthews_corrcoef(y, preds>thr) for thr in thresholds])\nplt.plot(thresholds, mcc)\nbest_threshold = thresholds[mcc.argmax()]\nprint(mcc.max())\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b686c914-7f69-5b0c-3ef9-29aaa11ebf9a"},"outputs":[],"source":"# load test data\nX = np.concatenate([\n    pd.read_csv(\"../input/test_date.csv\", index_col=0, dtype=np.float32,\n                usecols=np.concatenate([[0], important_indices[important_indices<1156]+1])).values,\n    pd.read_csv(\"../input/test_numeric.csv\", index_col=0, dtype=np.float32,\n                usecols=np.concatenate([[0], important_indices[important_indices>=1156] +1 - 1156])).values\n], axis=1)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"00b52c1c-9cf1-9478-ae46-5fa35ecc2db4"},"outputs":[],"source":"# generate predictions at the chosen threshold\npreds = (clf.predict_proba(X)[:,1] > best_threshold).astype(np.int8)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"18e3a92c-e5ea-c98c-fc90-fc164705761f"},"outputs":[],"source":"\n\n\n\n# and submit\nsub = pd.read_csv(\"../input/sample_submission.csv\", index_col=0)\nsub[\"Response\"] = preds\nsub.to_csv(\"submission.csv.gz\", compression=\"gzip\")\n"}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.5.2"}},"nbformat":4,"nbformat_minor":0}