Non-negative matrix factorization (NMF)

Unsupervised Learning ใน Python

Benjamin Wilson

Director of Research at lateral.io

Non-negative matrix factorization

  • NMF = "non-negative matrix factorization"
  • เทคนิคการลดมิติข้อมูล
  • โมเดล NMF แปลผลได้ง่าย (ต่างจาก PCA)
  • แปลผลง่าย หมายความว่าอธิบายได้ง่ายด้วย!
  • อย่างไรก็ตาม ฟีเจอร์ทุกตัวต้องมีค่าไม่ติดลบ (>= 0)
Unsupervised Learning ใน Python

ส่วนประกอบที่แปลผลได้

  • NMF แสดงเอกสารในรูปของการรวมกันของหัวข้อ (หรือ "ธีม")

 

กล่องข้อความ: DataCamp is the first and foremost leader in Data Science Education offering skill-based training, pioneering technical innovation... ประมาณเท่ากับ 0.6 คูณกล่องข้อความที่มีคำว่า program, r, python, function และ method บวก 0.5 คูณกล่องข้อความที่มีคำว่า data, analysis, cluster, statistics, mean บวก 0.7 คูณกล่องข้อความที่มีคำว่า teaching, learn, lesson, lessons และ course

Unsupervised Learning ใน Python

ส่วนประกอบที่แปลผลได้

  • NMF แสดงรูปภาพในรูปของการรวมกันของรูปแบบต่าง ๆ

 

กล่องที่มี 3 แถบประมาณเท่ากับ 0.98 คูณกล่องที่มีแถบหนึ่ง บวก 0.91 คูณกล่องที่มีแถบต่างออกไป บวก 0.95 คูณกล่องที่มีแถบต่างออกไป

Unsupervised Learning ใน Python

การใช้งาน NMF ใน scikit-learn

  • ใช้รูปแบบ fit() / transform()
  • ต้องระบุจำนวน components เช่น NMF(n_components=2)
  • ใช้งานได้กับ NumPy array และ csr_matrix
Unsupervised Learning ใน Python

ตัวอย่างอาร์เรย์ความถี่คำ

  • อาร์เรย์ความถี่คำ มี 4 คำ และหลายเอกสาร
  • วัดการปรากฏของคำในแต่ละเอกสารด้วย "tf-idf"
    • "tf" = ความถี่ของคำในเอกสาร
    • "idf" ลดอิทธิพลของคำที่ปรากฏบ่อย

อาร์เรย์ความถี่คำ

Unsupervised Learning ใน Python

ตัวอย่างการใช้งาน NMF

  • samples คืออาร์เรย์ความถี่คำ
from sklearn.decomposition import NMF

model = NMF(n_components=2)
model.fit(samples)
NMF(n_components=2)
nmf_features = model.transform(samples)
Unsupervised Learning ใน Python

NMF components

  • NMF มี components
  • ... เช่นเดียวกับที่ PCA มี principal components
  • มิติของ components เท่ากับมิติของ samples
  • ค่าทุกตัวไม่ติดลบ
print(model.components_)
[[ 0.01  0.    2.13  0.54]
 [ 0.99  1.47  0.    0.5 ]]
Unsupervised Learning ใน Python

NMF features

  • ค่าฟีเจอร์ของ NMF ไม่ติดลบ
  • สามารถนำไปสร้าง samples ขึ้นใหม่ได้
  • ... โดยรวมค่าฟีเจอร์กับ components
print(nmf_features)
[[ 0.    0.2 ]
 [ 0.19  0.  ]
  ...
 [ 0.15  0.12]]
Unsupervised Learning ใน Python

การสร้าง sample ขึ้นใหม่

print(samples[i,:])
[ 0.12  0.18  0.32  0.14]
print(nmf_features[i,:])
[ 0.15  0.12]

NMF components ที่คูณด้วยค่าฟีเจอร์แล้วนำมารวมกัน

Unsupervised Learning ใน Python

การสร้าง sample ขึ้นใหม่

  • คูณ components ด้วยค่าฟีเจอร์แล้วนำมารวมกัน
  • สามารถแสดงในรูปของผลคูณเมทริกซ์ได้เช่นกัน
  • นี่คือ "Matrix Factorization" ใน "NMF"
Unsupervised Learning ใน Python

NMF ใช้ได้กับข้อมูลที่ไม่ติดลบเท่านั้น

  • ความถี่คำในแต่ละเอกสาร
  • รูปภาพที่เข้ารหัสเป็นอาร์เรย์
  • สเปกโตรแกรมเสียง
  • ประวัติการซื้อสินค้าบนอีคอมเมิร์ซ
  • ... และอื่น ๆ อีกมาก!
Unsupervised Learning ใน Python

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

Unsupervised Learning ใน Python

Preparing Video For Download...