การประเมินการจัดกลุ่ม

Unsupervised Learning ใน Python

Benjamin Wilson

Director of Research at lateral.io

การประเมินการจัดกลุ่ม

  • ตรวจสอบความสอดคล้องกับสายพันธุ์ iris ได้
  • ... แต่ถ้าไม่มีข้อมูลสายพันธุ์ให้เปรียบเทียบล่ะ?
  • วัดคุณภาพของการจัดกลุ่ม
  • ช่วยกำหนดจำนวนกลุ่มที่เหมาะสม
Unsupervised Learning ใน Python

Iris: กลุ่มเทียบกับสายพันธุ์

  • k-means พบ 3 กลุ่มในตัวอย่าง iris
  • กลุ่มเหล่านี้สอดคล้องกับสายพันธุ์หรือไม่?
species  setosa  versicolor  virginica
labels
0             0           2         36
1            50           0          0
2             0          48         14
Unsupervised Learning ใน Python

Cross tabulation ด้วย pandas

  • กลุ่มเทียบกับสายพันธุ์คือ "cross-tabulation"
  • ใช้ไลบรารี pandas
  • กำหนดสายพันธุ์ของแต่ละตัวอย่างเป็น list species
print(species)
['setosa', 'setosa', 'versicolor', 'virginica', ... ]
Unsupervised Learning ใน Python

จัดเรียง labels และสายพันธุ์

import pandas as pd
df = pd.DataFrame({'labels': labels, 'species': species})
print(df)
     labels     species
0         1      setosa
1         1      setosa
2         2  versicolor
3         2   virginica
4         1      setosa
...
Unsupervised Learning ใน Python

Crosstab ของ labels และสายพันธุ์

ct = pd.crosstab(df['labels'], df['species'])
print(ct)
species  setosa  versicolor  virginica
labels
0             0           2         36
1            50           0          0
2             0          48         14

ถ้าไม่มีข้อมูลสายพันธุ์ จะประเมินการจัดกลุ่มได้อย่างไร?

Unsupervised Learning ใน Python

การวัดคุณภาพการจัดกลุ่ม

  • ใช้เพียงตัวอย่างและ label ของกลุ่ม

  • การจัดกลุ่มที่ดีมีกลุ่มที่กระชับ

  • ตัวอย่างในแต่ละกลุ่มอยู่ใกล้กัน

Unsupervised Learning ใน Python

Inertia วัดคุณภาพการจัดกลุ่ม

  • วัดความกระจายของกลุ่ม (ยิ่งน้อยยิ่งดี)
  • คำนวณจากระยะห่างระหว่างแต่ละตัวอย่างกับ centroid ของกลุ่ม
  • หลังเรียก fit() เข้าถึงได้ผ่าน attribute inertia_
  • k-means พยายามลดค่า inertia เมื่อเลือกกลุ่ม
from sklearn.cluster import KMeans

model = KMeans(n_clusters=3)
model.fit(samples)
print(model.inertia_)
78.9408414261
Unsupervised Learning ใน Python

จำนวนกลุ่มที่เหมาะสม

  • การจัดกลุ่มชุดข้อมูล iris ด้วยจำนวนกลุ่มต่างกัน
  • จำนวนกลุ่มมากขึ้นทำให้ inertia ลดลง
  • จำนวนกลุ่มที่เหมาะสมที่สุดคือเท่าไร?

กราฟเส้นแสดงจำนวนกลุ่มเทียบกับ inertia

Unsupervised Learning ใน Python

จะเลือกจำนวนกลุ่มเท่าไรดี?

  • การจัดกลุ่มที่ดีมีกลุ่มที่กระชับ (inertia ต่ำ)
  • ... แต่ไม่ควรมีกลุ่มมากเกินไป!
  • เลือกจุด "elbow" ในกราฟ inertia
  • จุดที่ inertia เริ่มลดลงช้าลง
  • เช่น สำหรับชุดข้อมูล iris เลข 3 เป็นตัวเลือกที่ดี

กราฟเส้นแสดงจำนวนกลุ่มเทียบกับ inertia

Unsupervised Learning ใน Python

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

Unsupervised Learning ใน Python

Preparing Video For Download...