MovieLens is an online movie rating and recommendation system created by the GroupLens lab in the CS department of the University of Minnesota. What's useful for us is that they post lots of data online that we can use. We'll look at what they call their "latest" "small" dataset, which is enough to give us a sense of what's there. Note specifically the license terms (roughly: mention source, problems are your own). We got the idea from Wes McKinney's pandas book.
There are two aspects of this that take some work. The first is reading individual files from a zip posted on the internet. The easy way is to do this is manually -- download the file, unzip it, read it from your hard drive -- but we prefer automated to easy. The second aspect is merging information from different files.
This IPython notebook was created by Dave Backus and Brian LeBlanc in Python 3.5 for the NYU Stern course Data Bootcamp.
In [2]:
import pandas as pd # data package
import requests, io # internet and input tools
import zipfile as zf # zip file tools
import sys # system module, used to get Python version
import datetime as dt # date tools, used to note current date
%matplotlib inline
print('\nPython version: ', sys.version)
print('Pandas version: ', pd.__version__)
print('Requests version: ', requests.__version__)
print("Today's date:", dt.date.today())
The data comes as a zip file that contains several csv's. We get the details from the README inside. (It's written in Markdown, so it's easier to read if we use a browser to format it. Or we could cut and paste into a Markdown cell in an IPython notebook, which we do at the bottom of this notebook.)
The file descriptions are:
ratings.csv
: each line is an individual film rating with the rater and movie id's and the rating. Order: userId, movieId, rating, timestamp
. tags.csv
: each line is a tag on a specific film. Order: userId, movieId, tag, timestamp
. movies.csv
: each line is a movie name, it's id, and its genre. Order: movieId, title, genres
. Multiple genres are separated by "pipes" |
. links.csv
: each line contains the movie id and corresponding id's at IMBd and TMDb. The easy way to input this data is to download the zip file onto our computer, unzip it, and read the individual csv files using read.csv()
. But anyone can do it the easy way. We're looking for an automated way, so that if we do this again, possibly with updated data, the whole process is in our code.
Automated data entry involves these steps:
read_csv()
and read_excel()
functions, but here we need to do it for ourselves. The package authors add: Recreational use of other HTTP libraries may result in dangerous side-effects, including: security vulnerabilities, verbose code, reinventing the wheel, constantly reading documentation, depression, headaches, or even death."
io.Bytes
reconstructs it as a file, here a zip file. read_csv
as usual. We found this Stack Overflow exchange helpful.
Digression. This is probably more than you want to know, but it's a reminder of what goes on behind the scenes when we apply read_csv
to a url. Here we grab whatever is at the url. Then we get its contents, convert it to bytes, identify it as a zip file, and read its components using read_csv
. It's a lot easier when this happens automatically, but a reminder what's involved if we ever have to look into the details.
In [13]:
# get "response" from url
url = 'http://files.grouplens.org/datasets/movielens/ml-latest-small.zip'
r = requests.get(url)
print('Response type:', type(r))
print('Response .content:', type(r.content))
print('Respnse headers:\n', r.headers, sep='')
In [14]:
# convert bytes to zip file
mlz = zf.ZipFile(io.BytesIO(r.content))
print('Type of zipfile object:', type(mlz))
In [15]:
# what's in the zip file?
mlz.namelist()
Out[15]:
In [16]:
# extract and read as csv's
movies = pd.read_csv(mlz.open(mlz.namelist()[2]))
ratings = pd.read_csv(mlz.open(mlz.namelist()[3]))
tags = pd.read_csv(mlz.open(mlz.namelist()[5]))
In [7]:
# what do we have?
for df in [movies, ratings, tags]:
print('\nType:', type(df))
print('Dimensions:', df.shape)
print('Variables:', list(df))
print('Head:', df.head())
In [18]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
This dataset (ml-latest-small) describes 5-star rating and free-text tagging activity from MovieLens, a movie recommendation service. It contains 105339 ratings and 6138 tag applications across 10329 movies. These data were created by 668 users between April 03, 1996 and January 09, 2016. This dataset was generated on January 11, 2016.
Users were selected at random for inclusion. All selected users had rated at least 20 movies. No demographic information is included. Each user is represented by an id, and no other information is provided.
The data are contained in four files, links.csv
, movies.csv
, ratings.csv
and tags.csv
. More details about the contents and use of all these files follows.
This is a development dataset. As such, it may change over time and is not an appropriate dataset for shared research results. See available benchmark datasets if that is your intent.
This and other GroupLens data sets are publicly available for download at http://grouplens.org/datasets/.
Neither the University of Minnesota nor any of the researchers involved can guarantee the correctness of the data, its suitability for any particular purpose, or the validity of results based on the use of the data set. The data set may be used for any research purposes under the following conditions:
In no event shall the University of Minnesota, its affiliates or employees be liable to you for any damages arising out of the use or inability to use these programs (including but not limited to loss of data or data being rendered inaccurate).
If you have any further questions or comments, please email grouplens-info@cs.umn.edu
GroupLens is a research group in the Department of Computer Science and Engineering at the University of Minnesota. Since its inception in 1992, GroupLens's research projects have explored a variety of fields including:
GroupLens Research operates a movie recommender based on collaborative filtering, MovieLens, which is the source of these data. We encourage you to visit http://movielens.org to try it out! If you have exciting ideas for experimental work to conduct on MovieLens, send us an email at grouplens-info@cs.umn.edu - we are always interested in working with external collaborators.
The dataset files are written as comma-separated values files with a single header row. Columns that contain commas (,
) are escaped using double-quotes ("
). These files are encoded as UTF-8. If accented characters in movie titles or tag values (e.g. Misérables, Les (1995)) display incorrectly, make sure that any program reading the data, such as a text editor, terminal, or script, is configured for UTF-8.
MovieLens users were selected at random for inclusion. Their ids have been anonymized. User ids are consistent between ratings.csv
and tags.csv
(i.e., the same id refers to the same user across the two files).
Only movies with at least one rating or tag are included in the dataset. These movie ids are consistent with those used on the MovieLens web site (e.g., id 1
corresponds to the URL https://movielens.org/movies/1). Movie ids are consistent between ratings.csv
, tags.csv
, movies.csv
, and links.csv
(i.e., the same id refers to the same movie across these four data files).
All ratings are contained in the file ratings.csv
. Each line of this file after the header row represents one rating of one movie by one user, and has the following format:
userId,movieId,rating,timestamp
The lines within this file are ordered first by userId, then, within user, by movieId.
Ratings are made on a 5-star scale, with half-star increments (0.5 stars - 5.0 stars).
Timestamps represent seconds since midnight Coordinated Universal Time (UTC) of January 1, 1970.
All tags are contained in the file tags.csv
. Each line of this file after the header row represents one tag applied to one movie by one user, and has the following format:
userId,movieId,tag,timestamp
The lines within this file are ordered first by userId, then, within user, by movieId.
Tags are user-generated metadata about movies. Each tag is typically a single word or short phrase. The meaning, value, and purpose of a particular tag is determined by each user.
Timestamps represent seconds since midnight Coordinated Universal Time (UTC) of January 1, 1970.
Movie information is contained in the file movies.csv
. Each line of this file after the header row represents one movie, and has the following format:
movieId,title,genres
Movie titles are entered manually or imported from https://www.themoviedb.org/, and include the year of release in parentheses. Errors and inconsistencies may exist in these titles.
Genres are a pipe-separated list, and are selected from the following:
Identifiers that can be used to link to other sources of movie data are contained in the file links.csv
. Each line of this file after the header row represents one movie, and has the following format:
movieId,imdbId,tmdbId
movieId is an identifier for movies used by https://movielens.org. E.g., the movie Toy Story has the link https://movielens.org/movies/1.
imdbId is an identifier for movies used by http://www.imdb.com. E.g., the movie Toy Story has the link http://www.imdb.com/title/tt0114709/.
tmdbId is an identifier for movies used by https://www.themoviedb.org. E.g., the movie Toy Story has the link https://www.themoviedb.org/movie/862.
Use of the resources listed above is subject to the terms of each provider.
Prior versions of the MovieLens dataset included either pre-computed cross-folds or scripts to perform this computation. We no longer bundle either of these features with the dataset, since most modern toolkits provide this as a built-in feature. If you wish to learn about standard approaches to cross-fold computation in the context of recommender systems evaluation, see LensKit for tools, documentation, and open-source code examples.
In [ ]: