{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Stream predictor using popularity after 5 days and playlist information"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Requirements\n",
    "\n",
    "Using conda, the requirements are:\n",
    "* pandas\n",
    "* numpy\n",
    "* scikit-learn\n",
    "* tpot\n",
    "* xgboost\n",
    "* ipykernel (to run this jupyter notebook)\n",
    "\n",
    "There is also a `requirements.txt` file in the examples directory that has exact package versions that should work with pip."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Documentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Note: This is a symbolic link from: datascience/modelling/model_application/\n",
    "from stream_predictor import streams\n",
    "\n",
    "\n",
    "import warnings \n",
    "warnings.simplefilter(\"ignore\", UserWarning) # Upgraded scikit-learn but using previous version of pickled file\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[0;31mSignature:\u001b[0m \u001b[0mstreams\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpop_5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mplaylists\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
       "\u001b[0;31mDocstring:\u001b[0m\n",
       "Apply streams model based on popularity after 5 days and playlist that the track exists on, \n",
       "including the playlist position. \n",
       "\n",
       "NOTE: there is a hardcoded path included here!\n",
       "\n",
       "Parameters\n",
       "----------\n",
       "pop_5: int\n",
       "    Popularity after 5 days\n",
       "playlists: dict( playlistId -> position on playlist )\n",
       "    Playlists and their position\n",
       "\n",
       "Returns\n",
       "-------\n",
       "Predicted total streams after 5 days INCLUDING the first five days\n",
       "\u001b[0;31mFile:\u001b[0m      ~/workspace/whitelist/thundr/datascience/examples/stream_predictor/stream_predictor.py\n",
       "\u001b[0;31mType:\u001b[0m      function\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "?streams"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "143129.84320998058"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pop_5 = 45\n",
    "playlists = {}\n",
    "streams(pop_5, playlists)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "164365.71248976173"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pop_5 = 45\n",
    "playlists = {'37i9dQZEVXbLRQDuF5jeBp' : 8 }\n",
    "streams(pop_5, playlists)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "There are unknown playlists provided: ['doesnt_exist']",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-e361f53c877b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mpop_5\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m45\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mplaylists\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'doesnt_exist'\u001b[0m \u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mstreams\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpop_5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mplaylists\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/workspace/whitelist/thundr/datascience/examples/stream_predictor/stream_predictor.py\u001b[0m in \u001b[0;36mstreams\u001b[0;34m(pop_5, playlists)\u001b[0m\n\u001b[1;32m     41\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     42\u001b[0m     \u001b[0;32mif\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0munknown_playlists\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 43\u001b[0;31m         \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'There are unknown playlists provided: {unknown_playlists}'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     44\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     45\u001b[0m     \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfeature_columns\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: There are unknown playlists provided: ['doesnt_exist']"
     ]
    }
   ],
   "source": [
    "pop_5 = 45\n",
    "playlists = {'doesnt_exist' : 1 }\n",
    "streams(pop_5, playlists)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "164146.11022053924"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# This yields a smaller outcome than the previous with only the first playlist\n",
    "pop_5 = 45\n",
    "playlists = {'37i9dQZEVXbLRQDuF5jeBp' : 1, '0JyJ4Nt68jBV0sJUErDlmx':1}\n",
    "streams(pop_5, playlists)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:whitelist]",
   "language": "python",
   "name": "conda-env-whitelist-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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
