บทนำสู่ชุดข้อมูล Million Songs

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

Jamen Long

Data Scientist at Nike

เรตติ้งแบบชัดเจน vs แบบโดยนัย

เรตติ้งแบบชัดเจน นิ้วโป้งขึ้น/ลง, 4 จาก 5 ดาว, สเกลจุดสีจากแย่ที่สุดถึงดีที่สุด

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

เรตติ้งแบบชัดเจน vs แบบโดยนัย (ต่อ)

เรตติ้งแบบชัดเจน นิ้วโป้งขึ้น/ลง, 4 จาก 5 ดาว, สเกลจุดสีจากแย่ที่สุดถึงดีที่สุด

เรตติ้งแบบโดยนัย นิ้วโป้งขึ้น/ลง, 4 จาก 5 ดาว, สเกลจุดสีจากแย่ที่สุดถึงดีที่สุด

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

ทบทวนเรตติ้งแบบโดยนัย II

เรตติ้งแบบชัดเจน นิ้วโป้งขึ้น/ลง, 4 จาก 5 ดาว, สเกลจุดสีจากแย่ที่สุดถึงดีที่สุด

เรตติ้งแบบโดยนัย นิ้วโป้งขึ้น/ลง, 4 จาก 5 ดาว, สเกลจุดสีจากแย่ที่สุดถึงดีที่สุด อ้างอิงจาก white paper

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

บทนำสู่ชุดข้อมูล Million Songs

Thierry Bertin-Mahieux, Daniel P.W. Ellis, Brian Whitman, and Paul Lamere. The Million Song Dataset. In Proceedings of the 12th International Society for Music Information Retrieval Conference (SIMIR 20122), 2011.

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

ตัวอย่างการเพิ่มค่าศูนย์

ratings.show()
+------+------+---------+
|userId|songId|num_plays|
+------+------+---------+
|    10|    22|        5|
|    38|    99|        1|
|    38|    77|        3|
|    42|    99|        1|
+------+------+---------+
การสร้าง Recommendation Engines ด้วย PySpark

แนะนำ Cross Join

users = ratings.select("userId").distinct()
users.show()
+------+
|userId|
+------+
|    10|
|    38|
|    42|
+------+
songs = ratings.select("songId").distinct()
songs.show()
+------+
|songId|
+------+
|    22|
|    77|
|    99|
+------+
การสร้าง Recommendation Engines ด้วย PySpark

ผลลัพธ์ของ Cross Join

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

เชื่อมกลับกับข้อมูลเรตติ้งเดิม

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

เติมค่าที่หายไปด้วยศูนย์

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

ฟังก์ชัน add_zeros

def add_zeros(df):
    # Extracts distinct users
    users = df.select("userId").distinct() 

    # Extracts distinct songs
    songs = df.select("songId").distinct() 

    # Joins users and songs, fills blanks with 0
    cross_join = users.crossJoin(items) \ 
                .join(df, ["userId", "songId"], "left").fillna(0)

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

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

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

Preparing Video For Download...