Вступ до набору даних MovieLens

Створення рушіїв рекомендацій у PySpark

Jamen Long

Data Scientist at Nike

Набір даних MovieLens

F. Maxwell Harper і Joseph A. Konstan. 2015
The MovieLens Datasets: History and Context.
ACM Transitions on Interactive Intelligent Systems (TiiS) 5, 4, стаття 19 (грудень 2015), 19 сторінок. DOI=http://dx.doi.org/10.1145/2827872

Створення рушіїв рекомендацій у PySpark

Зведена статистика MovieLens

F. Maxwell Harper і Joseph A. Konstan. 2015
The MovieLens Datasets: History and Context.
ACM Transitions on Interactive Intelligent Systems (TiiS) 5, 4, стаття 19 (грудень 2015), 19 сторінок. DOI=http://dx.doi.org/10.1145/2827872
 
Оцінки: 20 000 000+
Користувачі: 138 493
Фільми: 27 278

Створення рушіїв рекомендацій у PySpark

Дослідіть дані

df.show()
df.columns()
Створення рушіїв рекомендацій у PySpark

Розрідженість MovieLens

формула розрідженості

Створення рушіїв рекомендацій у PySpark

Розрідженість: чисельник

# Number of ratings in matrix
numerator = ratings.count()
Створення рушіїв рекомендацій у PySpark

Розрідженість: користувачі й фільми

# Distinct users and movies
users = ratings.select("userId").distinct().count()
movies = ratings.select("movieId").distinct().count()
Створення рушіїв рекомендацій у 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
Створення рушіїв рекомендацій у 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
Створення рушіїв рекомендацій у PySpark

Метод .distinct()

ratings.select("userId").distinct().count()
671
Створення рушіїв рекомендацій у PySpark

Метод GroupBy

# Group by userId
ratings.groupBy("userId")
Створення рушіїв рекомендацій у 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|
+------+-----+
Створення рушіїв рекомендацій у 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|
+----------+
Створення рушіїв рекомендацій у PySpark

GroupBy: максимум

# Max num of song plays by userId
ratings.groupBy("userId").count()
              .select(max("count")).show()
+----------+
|max(count)|
+----------+
|      1162|
+----------+
Створення рушіїв рекомендацій у PySpark

GroupBy: середнє

# Avg num of song plays by userId
ratings.groupBy("userId").count()
              .select(avg("count")).show()
+----------+
|avg(count)|
+----------+
| 233.34579|
+----------+
Створення рушіїв рекомендацій у 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|
+------+-----+
Створення рушіїв рекомендацій у PySpark

Давайте потренуємось!

Створення рушіїв рекомендацій у PySpark

Preparing Video For Download...