บทความใดที่คล้ายกับ 'Cristiano Ronaldo'?
ในวิดีโอ คุณได้เรียนรู้วิธีใช้ฟีเจอร์ NMF และ cosine similarity เพื่อค้นหาบทความที่คล้ายกัน
ลองนำสิ่งที่เรียนมาประยุกต์ใช้กับโมเดล NMF สำหรับบทความ Wikipedia ยอดนิยม โดยค้นหาบทความที่คล้ายกับบทความเกี่ยวกับนักฟุตบอล Cristiano Ronaldo มากที่สุด ฟีเจอร์ NMF ที่ได้จากขั้นตอนก่อนหน้าอยู่ในตัวแปร nmf_features และ titles คือลิสต์ของชื่อบทความทั้งหมด
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Unsupervised Learning ใน Python
คำแนะนำการฝึกหัด
- Import
normalizeจากsklearn.preprocessing - นำฟังก์ชัน
normalize()ไปใช้กับnmf_featuresแล้วเก็บผลลัพธ์ไว้ในตัวแปรnorm_features - สร้าง DataFrame ชื่อ
dfจากnorm_featuresโดยใช้titlesเป็น index - ใช้
.loc[]ของdfเพื่อเลือกแถวของ'Cristiano Ronaldo'แล้วกำหนดผลลัพธ์ให้กับตัวแปรarticle - ใช้เมธอด
.dot()ของdfกับarticleเพื่อคำนวณ cosine similarity ของทุกแถวกับarticle - แสดงผลด้วยเมธอด
.nlargest()ของsimilaritiesเพื่อดูบทความที่คล้ายกันมากที่สุด ส่วนนี้เตรียมไว้ให้แล้ว กด ส่งคำตอบ เพื่อดูผลลัพธ์ได้เลย!
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Perform the necessary imports
import pandas as pd
from ____ import ____
# Normalize the NMF features: norm_features
norm_features = ____
# Create a DataFrame: df
df = ____
# Select the row corresponding to 'Cristiano Ronaldo': article
article = df.loc[____]
# Compute the dot products: similarities
similarities = ____
# Display those with the largest cosine similarity
print(similarities.nlargest())