Unsupervised Learning

Unsupervised Learning ใน Python

Benjamin Wilson

Director of Research at lateral.io

Unsupervised learning

  • Unsupervised learning ค้นหารูปแบบในข้อมูล
  • เช่น การจัดกลุ่ม ลูกค้าตามประวัติการซื้อ
  • บีบอัดข้อมูลโดยใช้รูปแบบการซื้อ (การลดมิติข้อมูล)
Unsupervised Learning ใน Python

Supervised vs. Unsupervised learning

  • Supervised learning ค้นหารูปแบบเพื่องานพยากรณ์
  • เช่น จำแนกเนื้องอกว่าไม่ร้ายแรงหรือเป็นมะเร็ง (ป้ายกำกับ)
  • Unsupervised learning ค้นหารูปแบบในข้อมูล
  • ... แต่ ไม่มี งานพยากรณ์เฉพาะเจาะจง
Unsupervised Learning ใน Python

ชุดข้อมูล Iris

  • ข้อมูลการวัดของต้นไอริสหลายต้น
  • ไอริส 3 สายพันธุ์:
    • setosa
    • versicolor
    • virginica
  • ความยาวกลีบดอก ความกว้างกลีบดอก ความยาวกลีบเลี้ยง ความกว้างกลีบเลี้ยง (ฟีเจอร์ ของชุดข้อมูล)

ไอริส

1 https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html
Unsupervised Learning ใน Python

Array, ฟีเจอร์ และตัวอย่าง

  • NumPy array แบบ 2 มิติ
  • คอลัมน์คือค่าที่วัดได้ (ฟีเจอร์)
  • แถวแทนต้นไอริส (ตัวอย่าง)
Unsupervised Learning ใน Python

ข้อมูล Iris มี 4 มิติ

  • ตัวอย่างไอริสคือจุดในปริภูมิ 4 มิติ
  • มิติ = จำนวนฟีเจอร์
  • มิติสูงเกินไปจนไม่สามารถแสดงภาพได้!
  • ... แต่ unsupervised learning ช่วยให้เข้าใจข้อมูลได้
Unsupervised Learning ใน Python

k-means clustering

  • ค้นหากลุ่มของตัวอย่าง
  • ต้องระบุจำนวนกลุ่ม
  • ใช้งานผ่าน sklearn ("scikit-learn")
Unsupervised Learning ใน Python
print(samples)
[[ 5.   3.3  1.4  0.2]
 [ 5.   3.5  1.3  0.3]
 ...
 [ 7.2  3.2  6.   1.8]]
from sklearn.cluster import KMeans

model = KMeans(n_clusters=3)
model.fit(samples)
KMeans(n_clusters=3)
labels = model.predict(samples)

print(labels)
[0 0 1 1 0 1 2 1 0 1 ...]
Unsupervised Learning ใน Python

ป้ายกำกับกลุ่มสำหรับตัวอย่างใหม่

  • ตัวอย่างใหม่สามารถกำหนดให้กับกลุ่มที่มีอยู่ได้
  • k-means จดจำค่าเฉลี่ยของแต่ละกลุ่ม ("centroid")
  • ค้นหา centroid ที่ใกล้ที่สุดสำหรับตัวอย่างใหม่แต่ละตัว
Unsupervised Learning ใน Python

ป้ายกำกับกลุ่มสำหรับตัวอย่างใหม่

print(new_samples)
[[ 5.7  4.4  1.5  0.4]
 [ 6.5  3.   5.5  1.8]
 [ 5.8  2.7  5.1  1.9]]
new_labels = model.predict(new_samples)

print(new_labels)
[0 2 1]
Unsupervised Learning ใน Python

Scatter plot

  • Scatter plot ของความยาวกลีบเลี้ยงกับความยาวกลีบดอก
  • แต่ละจุดแทนตัวอย่างไอริสหนึ่งต้น
  • ระบายสีจุดตามป้ายกำกับกลุ่ม
  • PyPlot (matplotlib.pyplot)

Scatter plot

Unsupervised Learning ใน Python

Scatter plot

import matplotlib.pyplot as plt

xs = samples[:,0] ys = samples[:,2]
plt.scatter(xs, ys, c=labels)
plt.show()
Unsupervised Learning ใน Python

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

Unsupervised Learning ใน Python

Preparing Video For Download...