เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

รูปแบบที่ถูกต้องและผู้ใช้ที่ไม่ซ้ำกัน

ลองดู dataframe R จะเห็นว่าข้อมูลอยู่ในรูปแบบ "wide" ทั่วไป โดยแต่ละคอลัมน์แทนหนังคนละเรื่อง นอกจากนี้ชื่อ User และชื่อหนังยังไม่อยู่ในรูปแบบจำนวนเต็ม ให้ทำตามขั้นตอนเพื่อเตรียมข้อมูลนี้ให้พร้อมสำหรับ ALS อย่างถูกต้อง

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

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

ดูคอร์ส

คำแนะนำการฝึกหัด

  • นำเข้าแพ็กเกจ monotonically_increasing_id จาก pyspark.sql.functions และดู dataframe R โดยใช้เมธอด .show()
  • ใช้ฟังก์ชัน to_long() เพื่อแปลง dataframe R ให้อยู่ในรูปแบบ "long" แล้วตั้งชื่อ dataframe ใหม่ว่า ratings
  • สร้าง dataframe ชื่อ users ที่เก็บผู้ใช้ที่ไม่ซ้ำกันทั้งหมดจาก dataframe โดยใช้ .distinct() และแบ่ง dataframe ออกเป็น 1 partition โดยใช้เมธอด .coalesce(1)
  • ใช้เมธอด monotonically_increasing_id() ภายใน withColumn() เพื่อสร้างคอลัมน์ใหม่ใน dataframe ของผู้ใช้ที่เก็บเลขจำนวนเต็มไม่ซ้ำกันสำหรับผู้ใช้แต่ละคน ตั้งชื่อคอลัมน์นี้ว่า userId และอย่าลืมเรียกเมธอด .persist() บน dataframe สุดท้ายเพื่อให้ค่า ID จำนวนเต็มใหม่คงอยู่

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Import monotonically_increasing_id and show R
from pyspark.sql.functions import ____
R.show()

# Use the to_long() function to convert the dataframe to the "long" format.
ratings = to_long(____)
ratings.show()

# Get unique users and repartition to 1 partition
users = ratings.select("____").____().____()

# Create a new column of unique integers called "userId" in the users dataframe.
users = users.withColumn("____", monotonically_increasing_id()).____()
users.show()
แก้ไขและรันโค้ด