{ "metadata": { "name": "", "signature": "sha256:35a89cfe91118de85f09c6e61886d93be0314b8bec857ea57630b1d2817e0844" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Objective Functions: A Simple Example with Matrix Factorisation.\n", "\n", "## Data Science School, Nyeri, Kenya\n", "\n", "### 15th June 2015 Neil Lawrence\n", "\n", "In [the last session](./jupyter and probability intro.ipynb) we saw how we could load in a data set to pandas and use it for some simple data processing. We computed variaous probabilities on the data and I encouraged you to think about what sort of probabilities you need for prediction. This week we are going to take a slightly different tack. \n", "\n", "Broadly speaking there are two dominating approaches to machine learning problems. We started to consider the first approach last week: constructing models based on defining the relationship between variables using probabilities. This week we will consider the second approach: which involves defining an *objective function* and optimizing it. \n", "\n", "What do we mean by an objective function? An objective function could be an *error function* a *cost function* or a *benefit* function. In evolutionary computing they are called *fitness* functions. But the idea is always the same. We write down a mathematical equation which is then optimized to do the learning. The equation should be a function of the *data* and our model *parameters*. We have a choice when optimizing, either minimize or maximize. To avoid confusion, in the optimization field, we always choose to minimize the function. If we have function that we would like to maximize, we simply choose to minimize the negative of that function. \n", "\n", "So for this lab session, we are going to ignore probabilities, but don't worry, they will return! \n", "\n", "This week we are going to try and build a simple movie recommender system using an objective function. To do this, the first thing I'd like you to do is to install some software we've written for sharing information across google documents.\n", "\n", "## Open Data Science Software\n", "\n", "We are developing a suite of software tools for 'Open Data Science'. Open data science is an approach to sharing code, models and data that should make it easier for companies, health professionals and scientists to gain access to data science techniques. For some background on open data science you can read [this blog post](http://inverseprobability.com/2014/07/01/open-data-science/). The software is called `pods`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pods" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "/Users/neil/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pytz/__init__.py:29: UserWarning: Module pods was already imported from /Users/neil/sods/ods/pods/__init__.pyc, but /Users/neil/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages is being added to sys.path\n", " from pkg_resources import resource_stream\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Download the Movie Body Counts Data\n", "\n", "Now that we have the Open Data Science Software installed we can download the movie body counts data. When you run the commands below for the first time on a computer you will be asked to accept the download and acknowledge the source of the data. When you use a data set that someone has prepared you should always reference the data source to acknowledge the work that's been placed in. Here there is not only the work of the [researchers at NJIT](http://www.theswarmlab.com/r-vs-python-round-2-22/), but also the work of the [IMDb website](http://www.imdb.com) and [www.MovieBodyCounts.com](http://www.MovieBodyCounts.com). " ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = pods.datasets.movie_body_count()\n", "movies = d['Y']" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment Question 1\n", "\n", "Data ethics. If you find data available on the internet, can you simply use it without consequence? If you are given data by a fellow researcher can you publish that data on line? \n", "\n", "*10 marks*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Question 1 Answer\n", "\n", "Write your answer to the question in this box." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Recommender Systems\n", "\n", "A recommender system aims to make suggestions for items (films, books, other commercial products) given what it knows about users' tastes. The recommendation engine needs to represent the *taste* of all the users and the *characteristics* of each object. \n", "\n", "A common way for organizing objects is to place related objects spatially close together. For example in a library we try and put books that are on related topics near to each other on the shelves. One system for doing this is known as [Dewey Decimal Classification](http://en.wikipedia.org/wiki/Dewey_Decimal_Classification). In the Dewey Decimal Classification system (which dates from 1876) each subject is given a number (in fact it's a decimal number). For example, the field of Natural Sciences and Mathematics is given numbers which start with 500. Subjects based on Computer Science are given numbers which start 004 and works on the 'mathematical principles' of Computer science are given the series 004.0151 (which we might store as 4.0151 on a Computer). Whilst it's a classification system, the books in the library are typically laid out in the same order as the numbers, so we might expect that neighbouring numbers represent books that are related in subject. That seems to be exactly what we want when also representing films. Could we somehow represent each film's subject according to a number? In a similar way we could then imagine representing users with a list of numbers that represent things that each user is interested in.\n", "\n", "Actually a one dimensional representation of a subject can be very awkward. To see this, let's have a look at the Dewey Decimal Classification numbers for the 900s, which is listed as 'History and Geography'. We will focus on subjects in the 940s which can be found in this list from [Nova Southeastern University](http://www.nova.edu/library/help/misc/lc_dewey/dewey900.html#40). Whilst the ordering for places is somewhat sensible, it is also rather arbitrary. In the 940s we have Europe listed from 940-949, Asia listed from 950-959 and Africa listed from 960-969. Whilst it's true that Asia borders Europe, Africa is also very close, and the history of the Roman Empire spreads into [Carthage](http://en.wikipedia.org/wiki/Carthage) and later on Egypt. This image from Wikipedia shows a map of the Cathaginian Empire which fell after fighting with Rome. \n", "\n", "\n", "\n", "We now need to make a decision about whether Roman Histories are European or African, ideally we'd like them to be somewhere between the two, but we can't place them there in the Dewey Decimal system because between Europe and Africa is Asia, which has less to do with the Roman Empire than either Europe or Africa. Of course the fact that we've used a map provides a clue as to what to do next. Libraries are actually laid out on floors, so what if we were to use the spatial lay out to organise the sujbects of the books in two dimensions. Books on Geography could be laid out according to where in the world they are referring to. \n", "\n", "Such complexities are very hard to encapsulate in one number, but inspired by the map examples we can start considering how we might lay out films in two dimensions. Similarly, we can consider laying out a map of people's interests. If the two maps correspond to one another, the map of people could reflect where they might want to live in 'subject space'. We can think of representing people's tastes as where they might best like to sit in the library to access easily the books they are most interested in.\n", "\n", "\n", "## Inner Products for Representing Similarity\n", "\n", "Ideas like the above are good for gaining intuitions about what we might want, but the one of the skills of data science is representing those ideas mathematically. Mathematical abstraction of a problem is one of the key ways in which we've been able to progress as a society. Understanding planetary motions, as well as those of the smallest molecule (to quote Laplace's [Philosophical Essay on Probabilities](http://books.google.co.uk/books?id=1YQPAAAAQAAJ&printsec=frontcover&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=false)) needed to be done mathematically. The right mathematical model in machine learning can be slightly more elusive, because constructing it is a two stage process. \n", "\n", "1. We have to determine the right intuition for the system we want to represent. Notions such as 'subject' and 'interest' are not mathematically well defined, and even when we create a new interpretation of what they might mean, each interpretation may have its own weaknesses. \n", "\n", "2. Once we have our interpretation we can attempt to mathematically formalize it. In our library interpretation, that's what we need to do next. \n", "\n", "### The Library on an Infinite Plane\n", "\n", "Let's imagine a library which stores all the items we are interested in, not just books, but films and shopping items too. Such a library is likely to be very large, so we'll create it on an infinite two dimensional plane. This means we can use all the real numbers to represent the location of each item on the plane. For a two dimensional plane, we need to store the locations in a vector of numbers: we can decide that the $j$th item's location in the library is given by \n", "$$\n", "\\mathbf{v}_j = \\begin{bmatrix} v_{j,1} \\\\ v_{j,2}\\end{bmatrix},\n", "$$\n", "where $v_{j,1}$ represents the $j$th item's location in the East-West direction (or the $x$-axis) and $v_{j,2}$ represents the $j$th item's location in the North-South direction (or the $y$-axis). Now we need to specify the location where each user sits so that all the items that interest them are nearby: we can also represent the $i$th user's location with a vector \n", "$$\n", "\\mathbf{u}_i = \\begin{bmatrix} u_{i,1} \\\\ u_{i,2}\\end{bmatrix}.\n", "$$\n", "Finally, we need some way of recording a given user's affinity for a given item. This affinity might be the rating that the user gives the film. We can use $y_{i,j}$ to represent user $i$'s affinity for item $j$. \n", "\n", "For our film example we might imagine wanting to order films in a few ways. We could imagine organising films in the North-South direction as to how romantic they are. We could place the more romantic films further North and the less romantic films further South. For the East-West direction we could imagine ordering them according to how historic they are: we can imagine placing science fiction films to the East and historical drama to the West. In this case, fans of historical romances would be based in the North-West location, whilst fans of Science Fiction Action films might be located in the South-East (if we assume that 'Action' is the opposite of 'Romance', which is not necessarily the case). How do we lay out all these films? Have we got the right axes? In machine learning the answer is to 'let the data speak'. Use the data to try and obtain such a lay out. To do this we first need to obtain the data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Obtaining the Data\n", "\n", "We are using a functionality of the Open Data Science software library to obtain the data. This functionality involves some prewritten code which distributes to each of you a google spreadsheet where you can rate movies that you've seen. For completeness the code follows. Try and read and understand the code, but don't run it! It has already been run centrally by me. \n", "\n", "``` python\n", "\n", "import pods\n", "import pandas as pd\n", "import numpy as np\n", "user_data = pd.DataFrame(index=movies.index, columns=['title', 'year', 'rating', 'prediction'])\n", "user_data['title']=movies.Film\n", "user_data['year']=movies.Year\n", "\n", "accumulator=pods.lab.distributor(spreadsheet_title='COM4509/6509 Movie Ratings:', user_sep='\\t')\n", "# function to apply to data before giving it to user \n", "# Select 50 movies at random and order them according to year.\n", "max_movies = 50\n", "function = lambda x: x.loc[np.random.permutation(x.index)[:max_movies]].sort(columns='year')\n", "accumulator.write(data_frame=user_data, comment='Film Ratings', \n", " function=function)\n", "accumulator.write_comment('Rate Movie Here (score 1-5)', row=1, column=4)\n", "accumulator.share(share_type='writer', send_notifications=True)\n", "\n", "\n", "\n", "```\n", "\n", "In your Google docs account you should be able to find a spreadsheet called 'COM4509/6509 Movie Ratings: Your Name'. In the spreadsheet You should find a number of movies listed according to year. In the column titled 'rating' please place your rating using a score of 1-5 for *any* of the movies you've seen from the list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once you have placed your ratings we can download the data from your spreadsheets to a central file where the ratings of the whole class can be stored. We will build an algorithm on these ratings and use them to make predictions for the rest of the class. Firstly, here's the code for reading the ratings from each of the spreadsheets.\n", "\n", "``` python\n", "import numpy as np\n", "import pandas as pd\n", "import os\n", "\n", "accumulator = pods.lab.distributor(user_sep='\\t')\n", "data = accumulator.read(usecols=['rating'], dtype={'index':int, 'rating':np.float64}, header=2)\n", "\n", "for user in data:\n", " if data[user].rating.count()>0: # only add user if they rated something\n", " # add a new field to movies with that user's ratings.\n", " movies[user] = data[user]['rating']\n", "\n", "# Store the csv on disk where it will be shared through dropbox.\n", "movies.to_csv(os.path.join(pods.lab.class_dir,'movies.csv'), index_label='index')\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we will convert our data structure into a form that is appropriate for processing. We will convert the `movies` object into a data base which contains the movie, the user and the score using the following command." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pandas as pd\n", "import os\n", "pods.util.download_url('https://www.dropbox.com/s/s6gqvp9b383b59y/movies.csv?dl=0&raw=1', store_directory = 'class_movie', save_name='movies.csv')\n", "movies = pd.read_csv(os.path.join(pods.datasets.data_path, 'class_movie', 'movies.csv')).set_index('index')\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment Question 2\n", "The movies data is now in a data frame which contains one column for each user rating the movie. Have a look at the data, what do the entries with 'NaN' in mean?\n", "\n", "*5 marks*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Answer Question 2\n", "\n", "Write your answer to the question in this box." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Processing the Data\n", "\n", "We will now prepare the data set for processing. To do this we are going to conver the data into a new format using the `melt` command." ] }, { "cell_type": "code", "collapsed": false, "input": [ "user_names = list(set(movies.columns)-set(movies.columns[:9]))\n", "Y = pd.melt(movies.reset_index(), id_vars=['Film', 'index'], \n", " var_name='user', value_name='rating', \n", " value_vars=user_names)\n", "Y = Y.dropna(axis=0)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment Question 3\n", "\n", "What is a pivot table? What does the `pandas` command `pd.melt` do? \n", "\n", "*10 marks*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Question 3 Answer\n", "\n", "Write your answer to the question in this box." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Measuring Similarity\n", "\n", "We now need a measure for determining the similarity between the item and the user: how close the user is sitting to the item in the rooom if you like. We are going to use the inner product between the vector representing the item and the vector representing the user. \n", "\n", "An inner product (or [dot product](http://en.wikipedia.org/wiki/Dot_product)) between two vectors $\\mathbf{a}$ and $\\mathbf{b}$ is written as $\\mathbf{a}\\cdot\\mathbf{b}$. Or in vector notation we sometimes write it as $\\mathbf{a}^\\top\\mathbf{b}$. An inner product is simply the sume of the products of each element of the vector,\n", "$$\n", "\\mathbf{a}^\\top\\mathbf{b} = \\sum_{i} a_i b_i\n", "$$\n", "The inner product can be seen as a measure of similarity. The inner product gives us the cosine of the angle between the two vectors multiplied by their length. The smaller the angle between two vectors the larger the inner product. \n", "$$\n", "\\mathbf{a}^\\top\\mathbf{b} = |\\mathbf{a}||\\mathbf{b}| \\cos(\\theta)\n", "$$\n", "where $\\theta$ is the angle between two vectors and $|\\mathbf{a}|$ and $|\\mathbf{b}|$ are the respective lengths of the two vectors.\n", "\n", "Since we want each user to be sitting near each item, then we want the inner product to be large for any two items which are rated highly by that user. We can do this by trying to force the inner product $\\mathbf{u}_i^\\top\\mathbf{v}_j$ to be similar to the rating given by the user, $y_{i,j}$. To ensure this we will use a least squares objective function for all user ratings." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Objective Function\n", "\n", "The error function (or objective function, or cost function) we will choose is known as 'sum of squares', we will aim to minimize the sum of squared squared error between the inner product of $\\mathbf{u}_i$ and $\\mathbf{v}_i$ and the observed score for the user/item pairing, given by $y_{i, j}$. \n", "\n", "The total objective function can be written as\n", "$$\n", "E(\\mathbf{U}, \\mathbf{V}) = \\sum_{i,j} s_{i,j} (y_{i,j} - \\mathbf{u}_i^\\top \\mathbf{v}_j)^2\n", "$$\n", "where $s_{i,j}$ is an *indicator* variable that is 1 if user $i$ has rated item $j$ and is zero otherwise. Here $\\mathbf{U}$ is the matrix made up of all the vectors $\\mathbf{u}$,\n", "$$\n", "\\mathbf{U} = \\begin{bmatrix} \\mathbf{u}_1 \\dots \\mathbf{u}_n\\end{bmatrix}^\\top\n", "$$\n", "where we note that $i$th *row* of $\\mathbf{U}$ contains the vector associated with the $i$th user and $n$ is the total number of users. This form of matrix is known as a *design matrix*. Similarly, we define the matrix\n", "$$\n", "\\mathbf{V} = \\begin{bmatrix} \\mathbf{v}_1 \\dots \\mathbf{v}_m\\end{bmatrix}^\\top\n", "$$\n", "where again the $j$th row of $\\mathbf{V}$ contains the vector associated with the $j$th item and $m$ is the total number o items in the data set.\n", "\n", "## Objective Optimization\n", "\n", "The idea is to mimimize this objective. A standard, simple, technique for minimizing an objective is *gradient descent* or *steepest descent*. In gradient descent we simply choose to update each parameter in the model by subtracting a multiple of the objective function's gradient with respect to the parameters. So for a parameter $u_{i,j}$ from the matrix $\\mathbf{U}$ we would have an update as follows:\n", "$$\n", "u_{i,j} \\leftarrow u_{i,j} - \\eta \\frac{\\text{d} E(\\mathbf{U}, \\mathbf{V})}{\\text{d}u_{i,j}} \n", "$$\n", "where $\\eta$ (which is pronounced *eta* in English) is a Greek letter representing the *learning rate*. \n", "\n", "We can compute the gradient of the objective function with respect to $u_{k,\\ell}$ as\n", "$$\n", "\\frac{\\text{d}E(\\mathbf{U}, \\mathbf{V})}{\\text{d}u_{k,\\ell}} = 2\\sum_{j} s_{k,j}v_{j,\\ell}(y_{k,\\ell} - \\mathbf{u}_k^\\top\\mathbf{v}_{j}). \n", "$$\n", "Similarly each parameter $v_{i,j}$ needs to be updated according to its gradient. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment Question 4\n", "\n", "What is the gradient of the objective function with respect to $v_{k, \\ell}$? Write your answer in the box below, and explain which differentiation techniques you used to get there. You will be expected to justify your answer in class by oral questioning. Create a function for computing this gradient that is used in the algorithm below.\n", "\n", "*20 marks*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Question 4 Answer\n", "\n", "Write your answer to the question in this box." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Question 4 Code Answer" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Steepest Descent Algorithm\n", "\n", "In the steepest descent algorithm we aim to minimize the objective function by subtacting the gradient of the objective function from the parameters. To start with though, we need initial values for the matrix $\\mathbf{U}$ and the matrix $\\mathbf{V}$. Let's create them as `pandas` data frames and initialise them randomly with small values." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "q = 2 # the dimension of our map of the 'library'\n", "learn_rate = 0.01\n", "U = pd.DataFrame(np.random.normal(size=(len(user_names), q))*0.001, index=user_names)\n", "V = pd.DataFrame(np.random.normal(size=(len(movies.index), q))*0.001, index=movies.index)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also will subtract the mean from the rating before we try and predict them predictions. Have a think about why this might be a good idea (hint, what will the gradients be if we don't subtract the mean)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "Y['rating'] -= Y['rating'].mean()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have the initial values set, we can start the optimization. First we define a function for the gradient of the objective and the objective function itself." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def objective_gradient(Y, U, V):\n", " gU = pd.DataFrame(np.zeros((U.shape)), index=U.index)\n", " gV = pd.DataFrame(np.zeros((V.shape)), index=V.index)\n", " obj = 0.\n", " for ind, series in Y.iterrows():\n", " film = series['index']\n", " user = series['user']\n", " rating = series['rating']\n", " prediction = np.dot(U.loc[user], V.loc[film]) # vTu\n", " diff = prediction - rating # vTu - y\n", " obj += diff*diff\n", " gU.loc[user] += 2*diff*V.loc[film]\n", " gV.loc[film] += 2*diff*U.loc[user]\n", " return obj, gU, gV" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can write our simple optimisation route. This allows us to observe the objective function as the optimization proceeds." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "iterations = 100\n", "for i in range(iterations):\n", " obj, gU, gV = objective_gradient(Y, U, V)\n", " print \"Iteration\", i, \" Objective function: \", obj\n", " U -= learn_rate*gU\n", " V -= learn_rate*gV\n", "\n", " " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment Question 5\n", "\n", "What happens as you increase the number of iterations? What happens if you increase the learning rate?\n", "\n", "*10 marks*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Question 5 Answer\n", "\n", "Write your answer to the question in this box." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "import pylab as plt\n", "fig, ax = plt.subplots(figsize=(8,8))\n", "ax.plot(U[0], U[1], 'rx')\n", "for index in U.index:\n", " ax.text(U[0][index], U[1][index], index)\n", "fig, ax = plt.subplots(figsize=(8,8))\n", "ax.plot(V[0], V[1], 'rx')\n", "for index in V.index:\n", " # display the movie if it was rated.\n", " if np.sum(Y['index']==index)>0:\n", " ax.text(V[0][index], V[1][index], movies['Film'][index])\n", " " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stochastic Gradient Descent or Robbins Monroe Algorithm\n", "\n", "Stochastic gradient descent involves updating separating each gradient update according to each separate observation, rather than summing over them all. It is an approximate optimization method, but it has proven convergence under certain conditions and can be much faster in practice. It is used widely by internet companies for doing machine learning in practice. For example, Facebook's ad ranking algorithm uses stochastic gradient descent. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment Question 6\n", "\n", "Create a stochastic gradient descent version of the algorithm. Monitor the objective function after every 1000 updates to ensure that it is decreasing. When you have finished, plot the movie map and the user map in two dimensions. Label the plots with the name of the movie or user.\n", "\n", "*30 marks*" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Question 6 Code Answer" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Making Predictions\n", "\n", "Predictions can be made from the model of the appropriate rating for a given user, $i$, for a given film, $j$, by simply taking the inner product between their vectors $\\mathbf{u}_i$ and $\\mathbf{v}_j$. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Is Our Map Enough? Are Our Data Enough?\n", "\n", "Is two dimensions really enough to capture the complexity of humans and their artforms? Perhaps we need even more dimensions to capture that complexity. Extending our books analogy further, consider how we should place books that have a historical timeframe as well as some geographical location. Do we really want books from the 2nd World War to sit alongside books from the Roman Empire? Books on the American invasion of Sicily in 1943 are perhaps less related to books about Carthage than those that study the Jewish Revolt from 66-70 (in the Roman Province of Judaea). So books that relate to subjects which are closer in time should be stored together. However, a student of rebellion against empire may also be interested in the relationship between the Jewish Revolt of 66-70 and the Indian Rebellion of 1857, nearly 1800 years later. Whilst the technologies are different, the psychology of the people is shared: a rebellious nation angainst their imperial masters, triggered by misrule with a religious and cultural background. To capture such complexities we would need further dimensions in our latent representation. But are further dimensions justified by the amount of data we have? Can we really understand the facets of a film that only has at most three or four ratings?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Going Further\n", "\n", "If you want to take this model further then you'll need more data. One possible source of data is the [`movielens` data set](http://grouplens.org/datasets/movielens/). They have data sets containing up to ten million movie ratings. The few ratings we were able to collect in the class are not enough to capture the rich structure underlying these films. Imagine if we assume that the ratings are uniformly distributed between 1 and 5. If you know something about information theory then you could use that to work out the maximum number of *bits* of information we could gain per rating. \n", "\n", "Now we'll download the movielens 100k data and see if we can extract information about these movies." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pods\n", "d = pods.datasets.movielens100k()\n", "Y=d['Y']" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment Question 7\n", "\n", "Use stochastic gradient descent to make a movie map for the movielens data. Plot the map of the movies when you are finished.\n", "\n", "*15 marks*" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Code for question 7 here." ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }