Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Jeux de données reliés aux films.
5"""
6import os
7import pickle
8from io import StringIO
9import pandas
10from pyquickhelper.filehelper import unzip_files, get_url_content_timeout
13def load_movielens_dataset(name='small', cache=None, fLOG=None):
14 """
15 Retourne un jeu de données extrait de la page
16 `movielens <https://grouplens.org/datasets/movielens/>`_.
17 Notebooks associés à ce jeu de données :
19 .. runpython::
20 :rst:
22 from papierstat.datasets.documentation import list_notebooks_rst_links
23 links = list_notebooks_rst_links('lectures', 'movielens')
24 links = [' * %s' % s for s in links]
25 print('\\n'.join(links))
27 @param name nom du jeu de données à télécharger
28 @param cache cache les files avec :epkg:`pickle`
29 @param fLOG logging function
30 @return dictionnaires de dataframes
32 *cache* est un fichier, si celui-ci est présent, il recherché
33 avec le module :epkg:`pickle`.
34 """
35 if cache is not None and os.path.exists(cache):
36 with open(cache, 'rb') as f:
37 return pickle.load(f)
38 if name == 'small':
39 url = 'http://files.grouplens.org/datasets/movielens/ml-latest-small.zip'
40 else:
41 raise ValueError( # pragma: no cover
42 "Value '{0}' is not implemented.".format(name))
43 if fLOG:
44 fLOG("[load_movielens_dataset] download '{0}'".format(url))
45 res = get_url_content_timeout(url, encoding=None, fLOG=fLOG)
46 if fLOG:
47 fLOG("[load_movielens_dataset] unzip {0} bytes".format(len(res)))
48 found = unzip_files(res, fLOG=fLOG)
49 if fLOG:
50 fLOG("[load_movielens_dataset] found {0} files".format(len(found)))
51 dfiles = {}
52 for name_, text in found:
53 if name_.endswith('.csv'):
54 df = pandas.read_csv(StringIO(text.decode('utf-8')), sep=',')
55 key = os.path.splitext(os.path.split(name_)[-1])[0]
56 dfiles[key] = df
57 if cache is not None:
58 with open(cache, 'wb') as f:
59 pickle.dump(dfiles, f)
60 return dfiles