Xây dựng Recommendation Engine với PySpark
Jamen Long
Data Scientist at Nike
Đánh giá tường minh

Đánh giá tường minh

Đánh giá ngầm định

Đánh giá tường minh

Đánh giá ngầm định

Thierry Bertin-Mahieux, Daniel P.W. Ellis, Brian Whitman, và Paul Lamere. The Million Song Dataset. Trong Kỷ yếu Hội nghị lần thứ 12 của International Society for Music Information Retrieval (SIMIR 20122), 2011.
ratings.show()
+------+------+---------+
|userId|songId|num_plays|
+------+------+---------+
| 10| 22| 5|
| 38| 99| 1|
| 38| 77| 3|
| 42| 99| 1|
+------+------+---------+
users = ratings.select("userId").distinct()
users.show()
+------+
|userId|
+------+
| 10|
| 38|
| 42|
+------+
songs = ratings.select("songId").distinct()
songs.show()
+------+
|songId|
+------+
| 22|
| 77|
| 99|
+------+
cross_join = users.crossJoin(songs)
cross_join.show()
+------+------+
|userId|songId|
+------+------+
| 10| 22|
| 10| 77|
| 10| 99|
| 38| 22|
| 38| 77|
| 38| 99|
| 42| 22|
| 42| 77|
| 42| 99|
+------+------+
cross_join = users.crossJoin(songs)
.join(ratings, ["userId", "songId"], "left")
cross_join.show()
+------+------+---------+
|userId|songId|num_plays|
+------+------+---------+
| 10| 22| 5|
| 10| 77| null|
| 10| 99| null|
| 38| 22| null|
| 38| 77| 3|
| 38| 99| 1|
| 42| 22| null|
| 42| 77| null|
| 42| 99| 1|
+------+------+---------+
cross_join = users.crossJoin(songs)
.join(ratings, ["userId", "songId"], "left").fillna(0)
cross_join.show()
+------+------+---------+
|userId|songId|num_plays|
+------+------+---------+
| 10| 22| 5|
| 10| 77| 0|
| 10| 99| 0|
| 38| 22| 0|
| 38| 77| 3|
| 38| 99| 1|
| 42| 22| 0|
| 42| 77| 0|
| 42| 99| 1|
+------+------+---------+
def add_zeros(df):
# Lấy danh sách user khác nhau
users = df.select("userId").distinct()
# Lấy danh sách bài hát khác nhau
songs = df.select("songId").distinct()
# Nối users và songs, điền trống bằng 0
cross_join = users.crossJoin(items) \
.join(df, ["userId", "songId"], "left").fillna(0)
return cross_join
Xây dựng Recommendation Engine với PySpark