jamescalam
commited on
Commit
·
57f8196
1
Parent(s):
9233340
Create new file
Browse files- movielens-recent-ratings.py +110 -0
movielens-recent-ratings.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import pandas as pd
|
3 |
+
import re
|
4 |
+
import json
|
5 |
+
|
6 |
+
_CITATION = """\
|
7 |
+
@InProceedings{huggingface:dataset,
|
8 |
+
title = {MovieLens Ratings},
|
9 |
+
author={Ismail Ashraq, James Briggs},
|
10 |
+
year={2022}
|
11 |
+
}
|
12 |
+
"""
|
13 |
+
|
14 |
+
_DESCRIPTION = """\
|
15 |
+
This is a dataset that streams user ratings from the MovieLens 25M dataset from the MovieLens servers.
|
16 |
+
"""
|
17 |
+
_HOMEPAGE = "https://grouplens.org/datasets/movielens/"
|
18 |
+
|
19 |
+
_LICENSE = ""
|
20 |
+
|
21 |
+
_URL = "https://files.grouplens.org/datasets/movielens/ml-25m.zip"
|
22 |
+
|
23 |
+
class MovieLens(datasets.GeneratorBasedBuilder):
|
24 |
+
"""The MovieLens 25M dataset for ratings"""
|
25 |
+
|
26 |
+
def _info(self):
|
27 |
+
return datasets.DatasetInfo(
|
28 |
+
description=_DESCRIPTION,
|
29 |
+
features=datasets.Features(
|
30 |
+
{
|
31 |
+
"imdb_id": datasets.Value("string"),
|
32 |
+
"movie_id": datasets.Value("int32"),
|
33 |
+
"user_id": datasets.Value("int32"),
|
34 |
+
"rating": datasets.Value("float32"),
|
35 |
+
"title": datasets.Value("string"),
|
36 |
+
"year": datasets.Value("int32"),
|
37 |
+
}
|
38 |
+
),
|
39 |
+
supervised_keys=None,
|
40 |
+
homepage="https://grouplens.org/datasets/movielens/",
|
41 |
+
citation=_CITATION,
|
42 |
+
)
|
43 |
+
|
44 |
+
def _split_generators(self, dl_manager):
|
45 |
+
new_url = dl_manager.download_and_extract(_URL)
|
46 |
+
# PREPROCESS
|
47 |
+
# load all files
|
48 |
+
movie_ids = pd.read_csv(new_url+"/ml-25m/links.csv")
|
49 |
+
movie_meta = pd.read_csv(new_url+"/ml-25m/movies.csv")
|
50 |
+
movie_ratings = pd.read_csv(new_url+"/ml-25m/ratings.csv")
|
51 |
+
# merge to create movies dataframe
|
52 |
+
movies = movie_meta.merge(movie_ids, on="movieId")
|
53 |
+
# keep only subset of recent movies
|
54 |
+
recent_movies = movies[movies["imdbId"].astype(int) >= 2000000].fillna("None")
|
55 |
+
# mask movie ratings for movies that exist in movies
|
56 |
+
mask = movie_ratings['movieId'].isin(recent_movies["movieId"])
|
57 |
+
filtered_movie_ratings = movie_ratings[mask]
|
58 |
+
# merge with movies
|
59 |
+
df = filtered_movie_ratings.merge(
|
60 |
+
recent_movies, on="movieId"
|
61 |
+
).astype(
|
62 |
+
{"movieId": int, "userId": int, "rating": float}
|
63 |
+
)
|
64 |
+
# remove user and movies which occurs only once in the dataset
|
65 |
+
df = df.groupby("movieId").filter(lambda x: len(x) > 2)
|
66 |
+
df = df.groupby("userId").filter(lambda x: len(x) > 2)
|
67 |
+
# convert unique movie IDs to sequential index values
|
68 |
+
unique_movieids = sorted(df["movieId"].unique())
|
69 |
+
mapping = {unique_movieids[i]: i for i in range(len(unique_movieids))}
|
70 |
+
df["movie_id"] = df["movieId"].map(lambda x: mapping[x])
|
71 |
+
# get unique user sequential index values
|
72 |
+
unique_userids = sorted(df["userId"].unique())
|
73 |
+
mapping = {unique_userids[i]: i for i in range(len(unique_userids))}
|
74 |
+
df["user_id"] = df["userId"].map(lambda x: mapping[x])
|
75 |
+
# add "tt" prefix to align with IMDB URL IDs
|
76 |
+
df["imdb_id"] = df["imdbId"].apply(lambda x: "tt" + str(x))
|
77 |
+
# extract year from title where possible
|
78 |
+
year_list = []
|
79 |
+
find_year = re.compile(r"(?<=\()\d{4}(?=\))")
|
80 |
+
for title in df['title']:
|
81 |
+
match = find_year.search(title)
|
82 |
+
if match:
|
83 |
+
year_list.append(int(match[0]))
|
84 |
+
else:
|
85 |
+
year_list.append(None)
|
86 |
+
# add year to df
|
87 |
+
df['year'] = year_list
|
88 |
+
# drop rows where no year is found
|
89 |
+
df = df[~df["year"].isna()]
|
90 |
+
# we also don't need all columns
|
91 |
+
df = df[
|
92 |
+
["imdb_id", "movie_id", "user_id", "rating", "title", "year"]
|
93 |
+
]
|
94 |
+
# save
|
95 |
+
df.to_json(new_url+"/ratings.jsonl", orient="records", lines=True)
|
96 |
+
|
97 |
+
return [
|
98 |
+
datasets.SplitGenerator(
|
99 |
+
name=datasets.Split.TRAIN,
|
100 |
+
gen_kwargs={"filepath": new_url+"/ratings.jsonl"}
|
101 |
+
),
|
102 |
+
]
|
103 |
+
|
104 |
+
def _generate_examples(self, filepath):
|
105 |
+
"""This function returns the examples in the raw (text) form."""
|
106 |
+
with open(filepath, "r") as f:
|
107 |
+
id_ = 0
|
108 |
+
for line in f:
|
109 |
+
yield id_, json.loads(line)
|
110 |
+
id_ += 1
|