{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: ml_metrics in /root/anaconda3/lib/python3.5/site-packages\n",
      "Requirement already satisfied: numpy in /root/anaconda3/lib/python3.5/site-packages (from ml_metrics)\n",
      "Requirement already satisfied: pandas in /root/anaconda3/lib/python3.5/site-packages (from ml_metrics)\n",
      "Requirement already satisfied: python-dateutil>=2 in /root/anaconda3/lib/python3.5/site-packages (from pandas->ml_metrics)\n",
      "Requirement already satisfied: pytz>=2011k in /root/anaconda3/lib/python3.5/site-packages (from pandas->ml_metrics)\n",
      "Requirement already satisfied: six>=1.5 in /root/anaconda3/lib/python3.5/site-packages (from python-dateutil>=2->pandas->ml_metrics)\n"
     ]
    }
   ],
   "source": [
    "!pip install ml_metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ml_metrics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第5位平均精度均值（Mean Average Precision @ 5，MAP@5）\n",
    "$$ap@5 = \\sum_{k=1}^{5} \\frac{p(k)}{min(m,5)}$$\n",
    "示例：假设对于ID为“userid”的用户，算法推荐了itemid1 itemid2 itemid3 itemid4 itemid5等5个资讯，而用户实际点击了itemid1 itemid3 itemid6等3个资讯，则$$map@5 = \\frac{\\frac{1}{1}+\\frac{2}{3}}{3} = 0.56$$\n",
    "\n",
    "全部N个用户的平均精度均值是每个用户的精度均值的平均值\n",
    "$$map@5 = \\sum_{i=1}^{N} \\frac{ap@5}{N}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5555555555555555"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# apk@5\n",
    "ml_metrics.apk(actual=[1,3,6], predicted=[1,2,3,4,5,6], k=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "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"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "12px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false,
   "widenNotebook": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
