diff --git a/src/Bayes approx learning.ipynb b/src/Bayes approx learning.ipynb new file mode 100644 index 0000000..fdb0108 --- /dev/null +++ b/src/Bayes approx learning.ipynb @@ -0,0 +1,1728 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Third attempt at learning" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import plotly\n", + "import plotly.graph_objs as go\n", + "plotly.offline.init_notebook_mode()\n", + "import os\n", + "import random\n", + "import math\n", + "from tqdm import tqdm\n", + "from sklearn.metrics import mean_squared_error\n", + "from sklearn.decomposition import PCA\n", + "from scipy.stats import spearmanr\n", + "import pymc3 as pm\n", + "import theano\n", + "import datetime\n", + "theano.config.compute_test_value = 'raise'\n", + "%matplotlib inline\n", + "from multiprocessing import Pool, cpu_count" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "SELECTED_DATA_DIR = \"../selected-data/\"\n", + "MOVIES_FILE = \"best_movie_ratings_features_engineered.csv\"\n", + "USERS_FILE = \"users_ratings.csv\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Read raw data" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ratingvotesActionAdventureAnimationBiographyComedyCrimeDramaFamily...2007200820092010201120122013201420152016
title
Ying xiong (2002)0.7915166210000000...0000000000
\n", + "

1 rows × 52 columns

\n", + "
" + ], + "text/plain": [ + " rating votes Action Adventure Animation Biography \\\n", + "title \n", + "Ying xiong (2002) 0.79 151662 1 0 0 0 \n", + "\n", + " Comedy Crime Drama Family ... 2007 2008 2009 2010 \\\n", + "title ... \n", + "Ying xiong (2002) 0 0 0 0 ... 0 0 0 0 \n", + "\n", + " 2011 2012 2013 2014 2015 2016 \n", + "title \n", + "Ying xiong (2002) 0 0 0 0 0 0 \n", + "\n", + "[1 rows x 52 columns]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies_raw = pd.read_csv(SELECTED_DATA_DIR + MOVIES_FILE, index_col=0)\n", + "movies_raw.rating = movies_raw.rating/10\n", + "movies_raw.sample()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
userratinglink
movie
I, Robot (2004)480319860.7tt0343818
\n", + "
" + ], + "text/plain": [ + " user rating link\n", + "movie \n", + "I, Robot (2004) 48031986 0.7 tt0343818" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "users = pd.read_csv(SELECTED_DATA_DIR + USERS_FILE, index_col=0)\n", + "users.rating = users.rating/10\n", + "users.sample()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reduce data dimension (PCA)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "WANTED_DIM = 20" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pca_df = movies_raw[list(movies_raw.columns[2:])]\n", + "pca = PCA(n_components=WANTED_DIM)\n", + "pca_df = pd.DataFrame(pca.fit_transform(pca_df))\n", + "pca_df.index = movies_raw.index" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(1000, 52)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies_raw.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Pourcentage of variance in dataset conserveted" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.81671990142463713" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pca.explained_variance_ratio_.sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "movies = pd.concat([movies_raw[list(movies_raw.columns[:2])], pd.DataFrame(pca_df)] ,axis=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Actions selection function" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def random_choice(user_features, movies, epoch, s):\n", + " \"\"\" random approach to the problem, always exploring\"\"\"\n", + " return movies.sample()" + ] + }, + { + "cell_type": "code", + "execution_count": 312, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "from numpy.linalg import inv, det\n", + "from scipy.special import digamma\n", + "from numpy import dot\n", + "\n", + "# a_N & b_N & a_0 & b_0 : scalar\n", + "# mu_rho_0 & mu_beta_0 & lambda_rho_N & lambda_beta_N \n", + "# & nu_rho_N & nu_beta_N : matrix\n", + "# rho & beta : vector\n", + "\n", + "# x_i c'est le vecteur de feature de la ieme recommandation (vector)\n", + "# t_i c'est le temps passé depuis la ieme recommandation (scalar) TODO check si bonne valeur\n", + "# r_i c'est le user feedback pour la ieme recommandation (scalar)\n", + "\n", + "def mdot(matrices):\n", + " res = matrices[0]\n", + " for m in matrices[1:]:\n", + " res = dot(res, m)\n", + " return res\n", + "\n", + "def exp_tau(a_N, b_N): # scalar\n", + " return np.scalar(a_N/b_N)\n", + "\n", + "def exp_rho(lambda_rho_N, nu_rho_N): # vector\n", + " return dot(nu_rho_N, inv(lambda_rho_N))\n", + " \n", + "# TODO : check if not wrong\n", + "def exp_t_rho(lambda_rho_N, nu_rho_N): # vector\n", + " return dot(inv(lambda_rho_N.T), nu_rho_N.T)\n", + "\n", + "def exp_rho_t_rho(lambda_rho_N, e_rho, e_t_rho): # matrix\n", + " return inv(lambda_rho_N) + dot(e_rho, e_t_rho)\n", + "\n", + "def exp_beta(lambda_beta_N, nu_beta_N): # vector\n", + " return dot(nu_beta_N, inv(lambda_beta_N))\n", + " \n", + "# TODO : check if not wrong\n", + "def exp_t_beta(lambda_beta_N, nu_beta_N): # vector\n", + " return dot(inv(lambda_beta_N.T), nu_beta_N.T)\n", + "\n", + "def exp_beta_t_beta(lambda_beta_N, e_beta, e_t_beta): # matrix\n", + " return inv(lambda_beta_N) + dot(e_beta, e_t_beta)\n", + "\n", + "def upd_a_N(p, K, N, a_0): # scalar\n", + " return 0.5*(p + K + N) + a_0\n", + "\n", + "def upd_b_N(b_0, r_D_0, r_E_0, e_beta_t_beta, e_rho_t_rho, e_rho, e_beta, mu_rho_0, mu_beta_0, history_data, N): # scalar\n", + " p1 = np.trace(dot(r_D_0, e_rho_t_rho)) + dot(dot((mu_rho_0 - 2*e_rho), r_D_0),mu_rho_0.T) # scalar\n", + " p2 = np.trace(r_E_0*e_beta_t_beta) + dot(dot((mu_beta_0 - 2*e_beta), r_E_0), mu_beta_0.T) # scalar\n", + " p3 = sum([ r_i**2 - 2*r_i*mdot([x_i, e_rho.T, t_i, e_beta.T]) + mdot([x_i, e_rho_t_rho, x_i.T, t_i, e_beta_t_beta, t_i.T]) for x_i, t_i, r_i in history_data]) # scalar\n", + " res = b_0 + 0.5*p1 + 0.5*p2 + 0.5*p3\n", + " return np.asscalar(res)\n", + "\n", + "def msum(matrices):\n", + " print(\"matrices\", matrices)\n", + " if len(matrices)==0:\n", + " return []\n", + " res = matrices[0]\n", + " for m in matrices[1:]:\n", + " res += m\n", + " print(\"res\",res)\n", + " return res\n", + "\n", + "def upd_lambda_rho_N(e_tau, r_D_0, history_data, e_beta_t_beta): # matrix\n", + " m = [ mdot([x_i.T, t_i, e_beta_t_beta, t_i.T, x_i]) for x_i, t_i, r_i in history_data ]\n", + " if len(m):\n", + " return e_tau*(r_D_0 + msum(m))\n", + " return e_tau*r_D_0 \n", + "\n", + "def upd_nu_rho_N(e_tau, r_D_0, mu_rho_0, e_beta, history_data): # vector\n", + " for x_i, t_i, r_i in history_data:\n", + " print(\"xit\",x_i.T)\n", + " print(\"ti\", t_i)\n", + " print(\"e_betat\", e_beta.T)\n", + " m = [ r_i*mdot([x_i.T, t_i, e_beta.T]) for x_i, t_i, r_i in history_data ]\n", + " if len(m):\n", + " return e_tau*(dot(mu_rho_0, r_D_0) + msum(m))\n", + " return e_tau*dot(mu_rho_0, r_D_0)\n", + "\n", + "def upd_lambda_beta_N(e_tau, r_E_0, history_data, e_rho_t_rho): # matrix\n", + " m = [ mdot([t_i.T, x_i, e_rho_t_rho, x_i.T, t_i]) for x_i, t_i, r_i in history_data ]\n", + " if len(m):\n", + " return e_tau*(r_E_0 + msum(m))\n", + " return e_tau*r_E_0\n", + "\n", + "def upd_nu_beta_N(e_tau, r_E_0, mu_beta_0, e_rho, history_data): # vector\n", + " m = [ r_i*mdot([t_i.T, x_i, e_rho.T]) for x_i, t_i, r_i in history_data ]\n", + " if len(m):\n", + " return e_tau*(dot(mu_beta_0, r_E_0) + msum(m))\n", + " return e_tau*dot(mu_beta_0, r_E_0)\n", + "\n", + "def variational_lower_bound(a_0, b_0, a_N, b_N, p, d_D_0, d_E_0, D_0, E_0, r_D_0, \n", + " r_E_0, lambda_rho_N, lambda_beta_N, mu_rho_0, mu_beta_0,\n", + " e_rho, e_beta, K, e_rho_t_rho, e_beta_t_beta, history_data): \n", + " # d_ : determinant\n", + " # l_ : ln (log)\n", + " # r_ : reverse\n", + " # e_ : expected\n", + " # di_ : digamma\n", + " di_a_N = digamma(a_N)\n", + " l_b_N = np.log(b_N)\n", + " r_lambda_rho_N = inv(lambda_rho_N)\n", + " r_lambda_beta_N = inv(lambda_beta_N)\n", + " p1 = a_0*np.log(b_0) + (a_0 - 1)*(di_a_N-l_b_N) -b_0*(a_N/b_N) # scalar\n", + " p2 = -0.5*p*np.log(2*np.pi) -0.5*np.log(d_D_0) +0.5*p*(di_a_N-l_b_N) # scalar\n", + " p31 = mu_rho_0-e_rho # vector\n", + " p3 = -(a_N/(2*b_N))*(np.trace(dot(D_0, r_lambda_rho_N)) + mdot([p31, r_D_0, p31.T])) # scalar\n", + " p4 = -0.5*K*np.log(2*np.pi) -0.5*np.log(d_E_0) + 0.5*K*(di_a_N-l_b_N) # scalar\n", + " p51 = mu_beta_0-e_beta # matrix\n", + " p5 = -(a_N/(2*b_N))*(np.trace(dot(E_0, r_lambda_beta_N)) + mdot([p51, r_E_0, p51.T])) # scalar\n", + " p6 = -0.5*np.log(2*np.pi) +0.5*(di_a_N-l_b_N) # scalar\n", + " p71 = sum([ r_i**2 + mdot([x_i, e_rho_t_rho, x_i.T, t_i, e_beta_t_beta, t_i.T]) for x_i, t_i, r_i in history_data ])\n", + " p72 = sum([ r_i * mdot([x_i, e_rho.T, t_i, e_beta.T]) for x_i, t_i, r_i in history_data ])\n", + " p7 = -(a_N/(2*b_N))*p71 + (a_N/b_N)*p72 # scalar\n", + " p8 = 0.5*K*(1+np.log(2*np.pi)) + 0.5*np.log(det(r_lambda_beta_N)) # scalar\n", + " p9 = 0.5*p*(1+np.log(2*np.pi)) + 0.5*np.log(det(r_lambda_rho_N)) # scalar\n", + " p10 = -(a_N-1)*di_a_N - l_b_N + a_N # scalar\n", + " return p1 + p2 + p3[0][0] + p4 + p5[0][0] + p6 + p7 + p8 + p9 + p10\n", + "\n", + "def variational_inference(history_data, N, p, K):\n", + " # Hyperparameters\n", + " a_0 = 2\n", + " b_0 = 2*10**(-8)\n", + " \n", + " D_0 = np.eye(p) * 10**(-2)\n", + " E_0 = np.eye(K) * 10**(-2)\n", + " r_D_0 = inv(D_0) # reverse D_0\n", + " r_E_0 = inv(E_0) # reverse E_0\n", + " d_D_0 = det(D_0) # determinant D_0\n", + " d_E_0 = det(E_0) # determinant E_0\n", + " \n", + " mu_rho_0 = np.zeros((1,p)) # TODO check dimensions\n", + " mu_beta_0 = np.zeros((1,K)) # TODO check dimensions\n", + " \n", + " lambda_rho_N = np.eye(p)\n", + " lambda_beta_N = np.eye(K)\n", + " nu_rho_N = np.zeros((1,p))\n", + " nu_beta_N = np.zeros((1,K))\n", + " a_N = upd_a_N(p, K, N, a_0)\n", + " b_N = 1\n", + "\n", + " old_l = -1\n", + " s = 0\n", + " while (1):\n", + " e_tau = exp_tau(a_N, b_N)\n", + " e_beta = exp_beta(lambda_beta_N, nu_beta_N)\n", + "\n", + " e_t_beta = exp_t_beta(lambda_beta_N, nu_beta_N)\n", + " e_beta_t_beta = exp_beta_t_beta(lambda_beta_N, e_beta, e_t_beta)\n", + " e_rho = exp_rho(lambda_rho_N, nu_rho_N)\n", + " e_t_rho = exp_t_rho(lambda_rho_N, nu_rho_N)\n", + " e_rho_t_rho = exp_rho_t_rho(lambda_rho_N, e_rho, e_t_rho)\n", + " \n", + " lambda_rho_N = upd_lambda_rho_N(e_tau, r_D_0, history_data, e_beta_t_beta)\n", + " nu_rho_N = upd_nu_rho_N(e_tau, r_D_0, mu_rho_0, e_beta, history_data)\n", + " lambda_beta_N = upd_lambda_beta_N(e_tau, r_E_0, history_data, e_rho_t_rho)\n", + " nu_beta_N = upd_nu_beta_N(e_tau, r_E_0, mu_beta_0, e_rho, history_data)\n", + " a_N = upd_a_N(p, K, N, a_0)\n", + " b_N = upd_b_N(b_0, r_D_0, r_E_0, e_beta_t_beta, e_rho_t_rho, e_rho, e_beta, mu_rho_0, mu_beta_0, history_data, N)\n", + "\n", + " l = variational_lower_bound(a_0, b_0, a_N, b_N, p, d_D_0, d_E_0, D_0, E_0, r_D_0, \n", + " r_E_0, lambda_rho_N, lambda_beta_N, mu_rho_0, mu_beta_0, \n", + " e_rho, e_beta, K, e_rho_t_rho, e_beta_t_beta, history_data)\n", + " if (l==old_l or s==1000): # check convergence\n", + " break\n", + " old_l = l\n", + " s += 1\n", + " \n", + " #print(l, s)\n", + " return lambda_rho_N, nu_rho_N, lambda_beta_N, nu_beta_N, a_N, b_N\n", + "\n", + "def upd_q_rho(rho, lambda_rho_N, nu_rho_N):\n", + " return -0.5*mdot([rho, lambda_rho_N, rho.T]) + dot(nu_rho_N.T, rho)\n", + "\n", + "def upd_q_beta(beta, lambda_beta_N, nu_beta_N):\n", + " return -0.5*mdot([beta, lambda_beta_N, beta.T]) + dot(nu_beta_N.T, beta)\n", + "\n", + "def upd_q_tau(tau, a_N, b_N):\n", + " return tau**(a_N-1) * np.exp(-b_N*tau)\n", + "\n", + "r_history = []\n", + "t_classes = [ [0, 2**(-3)] ]\n", + "for eps in range(-3, 11):\n", + " t_classes.append([2**eps, 2**(eps+1)])\n", + "t_classes.append([2**11, float(\"inf\")])\n", + " \n", + "def featurize_time(t):\n", + " global t_classes\n", + " if t==0:\n", + " t = 43200\n", + " for i, (b1, b2) in enumerate(t_classes):\n", + " if b1<=t\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrl_multiple_users\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0musers\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmovies\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mALGOS\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mN\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m500\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mN_USER\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mrl_multiple_users\u001b[0;34m(users, movies, algorithms, s, N, N_USER)\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0muser\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0musers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0musers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0muser\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mmovies_user\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmovies_sample\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mmovies_sample\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mreinforcement_learning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muser\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmovies_user\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0malgo\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mN\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0mres_algo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mresults_all\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres_algo\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36mreinforcement_learning\u001b[0;34m(user, moviestc, choicef, s, numberSimulation)\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mt\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumberSimulation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mnow\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mrecommandation\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mchoicef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muser_features\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmovies\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0mrecommandation_features\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_movie_features\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrecommandation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0muser_rating\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0muser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrecommandation\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\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\"rating\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36mbayes_approx\u001b[0;34m(user_features, movies, epoch, s)\u001b[0m\n\u001b[1;32m 202\u001b[0m \u001b[0mK\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt_classes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 203\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 204\u001b[0;31m \u001b[0mlambda_rho_N\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnu_rho_N\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlambda_beta_N\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnu_beta_N\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma_N\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb_N\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvariational_inference\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr_history\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mepoch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mK\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 205\u001b[0m \u001b[0mi_lambda_rho_N\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlambda_rho_N\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 206\u001b[0m \u001b[0mi_lambda_beta_N\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlambda_beta_N\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36mvariational_inference\u001b[0;34m(history_data, N, p, K)\u001b[0m\n\u001b[1;32m 151\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 152\u001b[0m \u001b[0mlambda_rho_N\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mupd_lambda_rho_N\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me_tau\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr_D_0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhistory_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0me_beta_t_beta\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 153\u001b[0;31m \u001b[0mnu_rho_N\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mupd_nu_rho_N\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me_tau\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr_D_0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmu_rho_0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0me_beta\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhistory_data\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 154\u001b[0m \u001b[0mlambda_beta_N\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mupd_lambda_beta_N\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me_tau\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr_E_0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhistory_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0me_rho_t_rho\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 155\u001b[0m \u001b[0mnu_beta_N\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mupd_nu_beta_N\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me_tau\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr_E_0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmu_beta_0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0me_rho\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhistory_data\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36mupd_nu_rho_N\u001b[0;34m(e_tau, r_D_0, mu_rho_0, e_beta, history_data)\u001b[0m\n\u001b[1;32m 74\u001b[0m \u001b[0mm\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m \u001b[0mr_i\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mmdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx_i\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mT\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt_i\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0me_beta\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mT\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx_i\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt_i\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr_i\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mhistory_data\u001b[0m \u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 75\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 76\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0me_tau\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmu_rho_0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr_D_0\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mmsum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 77\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0me_tau\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmu_rho_0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr_D_0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 78\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (1,20) (20,16) " + ] + } + ], + "source": [ + "res = rl_multiple_users(users, movies, ALGOS, N=500, N_USER=10, s=200)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "for metric, tgraph, xaxix in zip(METRICS,TITLE_GRAPH,X_AXIS):\n", + " data = []\n", + " for algo, algon in enumerate(ALGOS_NAME):\n", + " temp = np.average(np.array([i[metric] for i in res[algo]]), axis=0)[1:]\n", + " data.append(go.Scatter(\n", + " x = list([i for i in range(len(temp))]),\n", + " y = temp,\n", + " name=algon\n", + " ))\n", + " layout = dict(title = tgraph,\n", + " xaxis = dict(title = tgraph),\n", + " yaxis = dict(title = xaxix),\n", + " )\n", + " fig = dict(data=data, layout=layout)\n", + " plotly.offline.iplot(fig)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "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.4.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}