แนะนำชุดข้อมูล MovieLens

การสร้าง Recommendation Engines ด้วย PySpark

Jamen Long

Data Scientist at Nike

ชุดข้อมูล MovieLens

F. Maxwell Harper and Joseph A. Konstan. 2015
The MovieLens Datasets: History and Context.
ACM Transitions on Interactive Intelligent Systems (TiiS) 5, 4, Article 19 (December 2015), 19 Pages. DOI=http://dx.doi.org/10.1145/2827872

การสร้าง Recommendation Engines ด้วย PySpark

สถิติสรุปของ MovieLens

F. Maxwell Harper and Joseph A. Konstan. 2015
The MovieLens Datasets: History and Context.
ACM Transitions on Interactive Intelligent Systems (TiiS) 5, 4, Article 19 (December 2015), 19 Pages. DOI=http://dx.doi.org/10.1145/2827872
 
การให้คะแนน: 20,000,000+
ผู้ใช้: 138,493
ภาพยนตร์: 27,278

การสร้าง Recommendation Engines ด้วย PySpark

สำรวจข้อมูล

df.show()
df.columns()
การสร้าง Recommendation Engines ด้วย PySpark

ความเบาบางของข้อมูลใน MovieLens

สูตรความเบาบางของข้อมูล

การสร้าง Recommendation Engines ด้วย PySpark

ความเบาบาง: ตัวเศษ

# Number of ratings in matrix
numerator = ratings.count()
การสร้าง Recommendation Engines ด้วย PySpark

ความเบาบาง: ผู้ใช้และภาพยนตร์

# Distinct users and movies
users = ratings.select("userId").distinct().count()
movies = ratings.select("movieId").distinct().count()
การสร้าง Recommendation Engines ด้วย PySpark

ความเบาบาง: ตัวส่วน

# 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
การสร้าง Recommendation Engines ด้วย PySpark

ความเบาบาง

# 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
การสร้าง Recommendation Engines ด้วย PySpark

เมธอด .distinct()

ratings.select("userId").distinct().count()
671
การสร้าง Recommendation Engines ด้วย PySpark

เมธอด GroupBy

# Group by userId
ratings.groupBy("userId")
การสร้าง Recommendation Engines ด้วย PySpark

เมธอด 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|
+------+-----+
การสร้าง Recommendation Engines ด้วย PySpark

เมธอด GroupBy: ค่าต่ำสุด

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|
+----------+
การสร้าง Recommendation Engines ด้วย PySpark

เมธอด GroupBy: ค่าสูงสุด

# Max num of song plays by userId
ratings.groupBy("userId").count()
              .select(max("count")).show()
+----------+
|max(count)|
+----------+
|      1162|
+----------+
การสร้าง Recommendation Engines ด้วย PySpark

เมธอด GroupBy: ค่าเฉลี่ย

# Avg num of song plays by userId
ratings.groupBy("userId").count()
              .select(avg("count")).show()
+----------+
|avg(count)|
+----------+
| 233.34579|
+----------+
การสร้าง Recommendation Engines ด้วย PySpark

เมธอด 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|
+------+-----+
การสร้าง Recommendation Engines ด้วย PySpark

มาฝึกกันเถอะ!

การสร้าง Recommendation Engines ด้วย PySpark

Preparing Video For Download...