-
-
Save shurain/fa3cbb61cb316faa8587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"from pandas.io.parsers import read_csv\n", | |
"%matplotlib inline\n", | |
"\n", | |
"from matplotlib import pyplot as plt\n", | |
"import matplotlib as mpl\n", | |
"\n", | |
"import scipy\n", | |
"\n", | |
"import xgboost as xgb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"bst = xgb.Booster({'nthread':4})\n", | |
"bst.load_model(\"0001.model\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"dtest = xgb.DMatrix(\"test.buffer\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"test_sid = np.load(\"test.sid.npy\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"test_iid = np.load(\"test.iid.npy\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"y_pred_xgb_prob = bst.predict(dtest)\n", | |
"\n", | |
"y_pred_xgb = np.ones_like(y_pred_xgb_prob)\n", | |
"y_pred_xgb[:] = y_pred_xgb_prob\n", | |
"\n", | |
"threshold = 0.047\n", | |
"\n", | |
"y_pred_xgb[y_pred_xgb >= threshold] = 1\n", | |
"y_pred_xgb[y_pred_xgb < threshold] = 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2724529 0.330174261563\n" | |
] | |
} | |
], | |
"source": [ | |
"print np.count_nonzero(y_pred_xgb), 1.0 * np.count_nonzero(y_pred_xgb) / len(test_sid)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"df = pd.DataFrame(y_pred_xgb, index=test_sid).reset_index()\n", | |
"df.columns = ['sid', 'pred_xgb']\n", | |
"df['iid'] = test_iid" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>sid</th>\n", | |
" <th>pred_xgb</th>\n", | |
" <th>iid</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>5</td>\n", | |
" <td>1</td>\n", | |
" <td>214530776</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>5</td>\n", | |
" <td>1</td>\n", | |
" <td>214530776</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>5</td>\n", | |
" <td>1</td>\n", | |
" <td>214530776</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>10</td>\n", | |
" <td>0</td>\n", | |
" <td>214820942</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>10</td>\n", | |
" <td>0</td>\n", | |
" <td>214826810</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" sid pred_xgb iid\n", | |
"0 5 1 214530776\n", | |
"1 5 1 214530776\n", | |
"2 5 1 214530776\n", | |
"3 10 0 214820942\n", | |
"4 10 0 214826810" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"guess_df = df[df['pred_xgb'] == 1].groupby('sid')['iid'].apply(lambda group: ','.join(str(k) for k in set(group.values)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"guess_df.reset_index().to_csv(\"xgb_0.1.guess\", sep=\";\", index=False, header=False)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment