NMF กับบทความ Wikipedia
ในวิดีโอ คุณได้เห็นการนำ NMF ไปใช้กับ array ความถี่ของคำแบบง่าย ถึงตาคุณบ้างแล้ว — คราวนี้จะนำ NMF ไปใช้กับ tf-idf word-frequency array ของบทความ Wikipedia ซึ่งอยู่ในรูป csr matrix ชื่อ articles ให้ fit โมเดลและ transform บทความดังกล่าว แล้วในแบบฝึกหัดถัดไปจะได้สำรวจผลลัพธ์ที่ได้
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Unsupervised Learning ใน Python
คำแนะนำการฝึกหัด
- Import
NMFจากsklearn.decomposition - สร้าง instance ของ
NMFชื่อmodelโดยกำหนดให้มี6components - Fit โมเดลกับข้อมูลจำนวนคำ
articles - ใช้ method
.transform()ของmodelเพื่อ transformarticlesแล้วเก็บผลลัพธ์ไว้ในตัวแปรnmf_features - แสดงผล
nmf_featuresเพื่อดูภาพรวมเบื้องต้น (.round(2)จะปัดค่าให้เหลือทศนิยม 2 ตำแหน่ง)
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import NMF
____
# Create an NMF instance: model
model = ____
# Fit the model to articles
____
# Transform the articles: nmf_features
nmf_features = ____
# Print the NMF features
print(nmf_features.round(2))