40
Chapter 8 Clustering : K-means 01/06/57 1 Clustering : K-mean โดย ผศ.วิภาวรรณ บัวทอง

Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

  • Upload
    doduong

  • View
    251

  • Download
    7

Embed Size (px)

Citation preview

Page 1: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Chapter 8Clustering : K-means

01/06/571 Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง

Page 2: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

การจดกลมในการท าเหมองขอมล

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง2

Cluster : เปนกลมหรอแหลงเกบสะสม (collection) ของวตถตางๆ สามารถน ามาจดกลมกนตามความเหมอน (Similarity) สามารถน ามาจดกลมกนตามความแตกตาง (Dissimilarity or

Distance)

Cluster Analysis เปนกระบวนการจดวตถตางๆ ใหอยกลมทเหมาะสม ซงมคณสมบต

ทวตถทอยในกลมเดยวกนจะคลายกน แตมความแตกตางจากวตถในกลมอน

Page 3: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

การจดกลมในการท าเหมองขอมล (ตอ)

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง3

Clustering การจดกลมจะแตกตางจากการแบงประเภทขอมล (Classification) โดยจะแบงกลมขอมลจากความคลาย โดยไมมการก าหนดคลาสประเภทขอมลไวกอนหรอไมทราบจ านวนกลมลวงหนา เปนการเรยนรแบบไมมผสอน (unsupervised classification)

Page 4: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

ชนดของตวแปรทใชในเทคนค K-Means Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง4

ตองเปนตวแปรเชงปรมาณ (Quantitative) - สเกลอนตรภาค (Interval Scale)

- สเกลอตราสวน (Ratio Scale)

Page 5: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

What is Good Clustering?

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง5

จดกลมโดยพยายามใหระยะหางของสงทอยในกลมเดยวกนอยใกลกนใหมากทสด (Minimize Intra-Cluster Distances) และระยะหางทอยตางกลมมความหางแตกตางกนมาก ทสด (Maximize Inter-Cluster Distances)

Page 6: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Notion of a Cluster can be Ambiguous

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง6

Page 7: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Clustering Algorithms

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง7

K-means clustering **

Hierarchical clustering

Page 8: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

K-means Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง8

K-means หรอเรยกอกอยางหนงวา การวเคราะหกลมแบบไมเปนขนตอน (Nonhierarchical Cluster Analysis) หรอ การแบงสวน (Partioning)

เปนอลกอรทมเทคนคการเรยนรโดยไมมผสอนทงายทสด เพราะเปนการแกปญหาการจดกลมทรจกกนทวไป โดยอลกอรทม K-Means จะตดแบง (Partition) วตถออกเปน K กลม

แทนคาแตละกลมดวยคาเฉลยของกลม ซงใชเปนจดศนยกลาง (centroid) ของกลมในการวดระยะหางของขอมลในกลมเดยวกน

Page 9: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

ประเภทของ Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง9

Partitional Clustering คอการแบงกลมอยางชดเจนโดยไมมกลมใดซอนทบกนอย

Page 10: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Hierarchical clustering ความสมพนธแบบล าดบชน

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง10

Page 11: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Partitioning Algorithms: Basic Concept

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง11

Partitioning method: Partitioning a database D of n objects into a set of k clusters, such that the sum of squared distances is minimized (where ci is the centroid or medoid of cluster Ci )

Page 12: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

K-means Clustering Algorithm

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง12

Method1) ก าหนดหรอสมคาเรมตน จานวน k คา(กลม) และก าหนดจด

ศนยกลางเรมตน k จด เรยกวา cluster centers หรอ(centroid) 2) น าวตถทงหมดจดเขากลม โดยท าการหาคาระยะหางระหวางขอมล

กบจดศนยกลาง หากขอมลไหนใกลคาจดศนยกลางตวไหนทสดอยกลมนน

3) หาคาเฉลย (Mean) แตละกลม ใหเปนคาจดศนยกลางใหม 4) ท าซ าขอ 2) จนกระทงคาเฉลยหรอจดศนยกลางในแตละกลมจะไม

เปลยนแปลง

Page 13: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

An Example of K-Means Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง13

Page 14: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

มาตรวดความเหมอน

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง14

Page 15: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

การจดกลมโดยใชหลกเกณฑตางๆ

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง15

Page 16: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง16

Page 17: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง17

สมคาเรมตน จานวน k คา เรยกวา cluster centers (centroid) สมมต k =3 แสดงวา c1, c2 และ c3 เปน centroid ทเราสมขนมา

c1(2, 10), c2(5, 8) and c3(1, 2).

Page 18: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง18

ขนตอนท 1 หาความหางกนระหวางขอมล 2 ขอมล คอ หาความหางจากขอมล A =(x1, y1) และ centroid =(x2, y2) โดยใชสตร Euclidean ดงน

หรอ

Page 19: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง19

ขนตอนท 2 หาระยะหางระหวางขอมล กบจดศนยกลาง (ตวอยางบางชดขอมล)

Page 20: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

รอบท 1 ไดการจดกลมขอมลดงตอไปน

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง20

Page 21: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

น ามาสรางกลมใหม

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง21

Page 22: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง22

ขนตอนท 3 หาคาเฉลยแตละกลม ใหเปน คาจดศนยกลางใหม

ส าหรบ Cluster 1 มจดเดยวคอ A1(2, 10) แสดงวา C1(2,10) ยงคงเดม ส าหรบ Cluster 2 ม 5 จดอยกลมเดยวกน เพราะฉะนนหา C2 ใหม

( (8+5+7+6+4)/5, (4+8+5+4+9)/5 ) = C2(6, 6) ส าหรบ Cluster 3 ม 2 จดอยกลมเดยวกน

( (2+1)/2, (5+2)/2 ) = C3(1.5, 3.5)

Page 23: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง23

รอบท 2

Page 24: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง24

รอบท 2

Page 25: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง25

Page 26: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง26

รอบท 3

Page 27: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

รอบท 3

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง27

Page 28: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

รอบท 4

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง28

Page 29: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

รอบท 4

Example: K-Mean Clustering

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง29

Page 30: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

ขอดและขอดอยของเทคนค K-means

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง30

ขอด

1. เมอจ านวนขอมลมจ านวนมาก และมจ านวนกลมนอย การหาคาเฉลยแบบ K-means อาจจะค านวณไดเรวกวา การจดกลมแบบอน ๆ (Hierarchical)

2. ขนตอนการหาคาเฉลยแบบ K-means อาจจะไดสมาชก ภายในกลมหนาแนนกวาการจดกลมแบบ Hierarchical โดยเฉพาะถากลมเปนวงกลม

Page 31: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

ขอดและขอดอยของเทคนค K-means

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง31

ขอดอย

1. การหาคา K ทเหมาะสมคาดเดาไดยาก

2. ท างานไดไมดถากลมขอมลไมเปนรปวงกลม 3. มขอจากดในเรองของขนาด ความหนาแนน และรปราง

Page 32: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Limitations of K-means: Differing Sizes

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง32

Page 33: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Limitations of K-means: Differing Density

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง33

Page 34: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Limitations of K-means: Non-globular Shapes

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง34

Page 35: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Overcoming K-means Limitations

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง35

One solution is to use many clusters.

Find parts of clusters, but need to put together.

Page 36: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Overcoming K-means Limitations

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง36

Page 37: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Overcoming K-means Limitations

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง37

Page 38: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

What Is the Problem of the K-Means Method?

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง38

The k-means algorithm is sensitive to outliers ! Since an object with an extremely large value may substantially distort the

distribution of the data

K-Medoids: Instead of taking the mean value of the object in a cluster as a reference point, medoids can be used, which is the most centrally located object in a cluster

Page 39: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง39

Page 40: Chapter 8 Clustering : K-means - Wipawan's Blog · ชนิดของตัวแปรที่ใช้ในเทคนิค K-Means Clustering 4 Clustering : K-mean โดย

Exercise

01/06/57Clustering : K-mean โดย ผศ.วภาวรรณ บวทอง40

จากขอมลขางลาง จงใช K-means ในการหา Clusters โดยให K=3 และใชขอมล สามเรคอรดแรกเปนคาน าหนก (weight or center) ของ Cluster เรมตน