Giới thiệu về bộ dữ liệu MovieLens

Xây dựng Recommendation Engine với PySpark

Jamen Long

Data Scientist at Nike

Bộ dữ liệu MovieLens

F. Maxwell Harper và Joseph A. Konstan. 2015
The MovieLens Datasets: History and Context.
ACM Transitions on Interactive Intelligent Systems (TiiS) 5, 4, Bài báo 19 (Tháng 12/2015), 19 trang. DOI=http://dx.doi.org/10.1145/2827872

Xây dựng Recommendation Engine với PySpark

Thống kê tóm tắt MovieLens

F. Maxwell Harper và Joseph A. Konstan. 2015
The MovieLens Datasets: History and Context.
ACM Transitions on Interactive Intelligent Systems (TiiS) 5, 4, Bài báo 19 (Tháng 12/2015), 19 trang. DOI=http://dx.doi.org/10.1145/2827872
 
Đánh giá: 20.000.000+
Người dùng: 138.493
Phim: 27.278

Xây dựng Recommendation Engine với PySpark

Khám phá dữ liệu

df.show()
df.columns()
Xây dựng Recommendation Engine với PySpark

Độ thưa của MovieLens

công thức độ thưa

Xây dựng Recommendation Engine với PySpark

Độ thưa: tử số

# Number of ratings in matrix
numerator = ratings.count()
Xây dựng Recommendation Engine với PySpark

Độ thưa: người dùng và phim

# Distinct users and movies
users = ratings.select("userId").distinct().count()
movies = ratings.select("movieId").distinct().count()
Xây dựng Recommendation Engine với PySpark

Độ thưa: mẫu số

# Number of ratings in matrix
numerator = ratings.count()

# Distinct users and movies
users = ratings.select("userId").distinct().count()
movies = ratings.select("movieId").distinct().count()

# Number of ratings matrix could contain if no empty cells denominator = users * movies
Xây dựng Recommendation Engine với PySpark

Độ thưa

# Number of ratings in matrix
numerator = ratings.count()

# Distinct users and movies
users = ratings.select("userId").distinct().count()
movies = ratings.select("movieId").distinct().count()

# Number of ratings matrix could contain if no empty cells
denominator = users * movies

#Calculating sparsity
sparsity = 1 - (numerator*1.0 / denominator)
print ("Sparsity: "), sparsity
Sparsity: .998
Xây dựng Recommendation Engine với PySpark

Phương thức .distinct()

ratings.select("userId").distinct().count()
671
Xây dựng Recommendation Engine với PySpark

Phương thức GroupBy

# Group by userId
ratings.groupBy("userId")
Xây dựng Recommendation Engine với PySpark

Phương thức GroupBy

# Num of song plays by userId
ratings.groupBy("userId").count().show()
+------+-----+
|userId|count|
+------+-----+
|   148|   76|
|   243|   12|
|    31|  232|
|   137|   16|
|   251|   19|
|    85|  752|
|    65|  737|
|   255|    9|
|    53|  190|
|   133|  302|
|   296|   74|
|    78|  301|
|   108|  136|
|   155|    3|
|   193|  174|
|   101|    1|
+------+-----+
Xây dựng Recommendation Engine với PySpark

GroupBy: giá trị nhỏ nhất

from pyspark.sql.functions import min, max, avg

# Min num of song plays by userId
msd.groupBy("userId").count()
              .select(min("count")).show()
+----------+
|min(count)|
+----------+
|         1|
+----------+
Xây dựng Recommendation Engine với PySpark

GroupBy: giá trị lớn nhất

# Max num of song plays by userId
ratings.groupBy("userId").count()
              .select(max("count")).show()
+----------+
|max(count)|
+----------+
|      1162|
+----------+
Xây dựng Recommendation Engine với PySpark

GroupBy: giá trị trung bình

# Avg num of song plays by userId
ratings.groupBy("userId").count()
              .select(avg("count")).show()
+----------+
|avg(count)|
+----------+
| 233.34579|
+----------+
Xây dựng Recommendation Engine với PySpark

Phương thức Filter

# Removes users with less than 20 ratings
ratings.groupBy("userId").count().filter(col("count") >= 20).show()
+------+-----+
|userId|count|
+------+-----+
|   148|   76|
|    31|  232|
|    85|  752|
|    65|  737|
|    53|  190|
|   133|  302|
|   296|   74|
|    78|  301|
|   108|  136|
|   193|  174|
+------+-----+
Xây dựng Recommendation Engine với PySpark

Cùng luyện tập nào!

Xây dựng Recommendation Engine với PySpark

Preparing Video For Download...