การแปลงฟีเจอร์เพื่อการจัดกลุ่มที่ดีขึ้น

Unsupervised Learning ใน Python

Benjamin Wilson

Director of Research at lateral.io

ชุดข้อมูลไวน์แคว้น Piedmont

  • ตัวอย่าง 178 รายการจากไวน์แดง 3 พันธุ์: Barolo, Grignolino และ Barbera

  • ฟีเจอร์วัดองค์ประกอบทางเคมี เช่น ปริมาณแอลกอฮอล์

  • คุณสมบัติทางสายตา เช่น "ความเข้มของสี"

1 Source: https://archive.ics.uci.edu/ml/datasets/Wine
Unsupervised Learning ใน Python

การจัดกลุ่มไวน์

from sklearn.cluster import KMeans
model = KMeans(n_clusters=3)
labels = model.fit_predict(samples)
Unsupervised Learning ใน Python

กลุ่มเทียบกับพันธุ์ไวน์

df = pd.DataFrame({'labels': labels, 
                       'varieties': varieties})
ct = pd.crosstab(df['labels'], df['varieties'])

print(ct)
varieties  Barbera  Barolo  Grignolino
labels                                
0               29      13          20
1                0      46           1
2               19       0          50
Unsupervised Learning ใน Python

ความแปรปรวนของฟีเจอร์

  • ฟีเจอร์ของไวน์มีความแปรปรวนต่างกันมาก!

  • ความแปรปรวนของฟีเจอร์วัดการกระจายของค่า

feature     variance
alcohol         0.65
malic_acid      1.24
...
od280           0.50
proline     99166.71

กราฟกระจายของตัวแปร od280 เทียบกับตัวแปร malic_acid

Unsupervised Learning ใน Python

ความแปรปรวนของฟีเจอร์

  • ฟีเจอร์ของไวน์มีความแปรปรวนต่างกันมาก!

  • ความแปรปรวนของฟีเจอร์วัดการกระจายของค่า

feature     variance
alcohol         0.65
malic_acid      1.24
...
od280           0.50
proline     99166.71

กราฟกระจายของตัวแปร od280 เทียบกับหมายเลขการสังเกต

Unsupervised Learning ใน Python

StandardScaler

  • ใน KMeans: ความแปรปรวนของฟีเจอร์ = อิทธิพลของฟีเจอร์

  • StandardScaler แปลงแต่ละฟีเจอร์ให้มีค่าเฉลี่ย 0 และความแปรปรวน 1

  • เรียกว่าฟีเจอร์ที่ผ่านการ "standardize" แล้ว

กราฟกระจายของ od280 และ proline หลัง standardize

Unsupervised Learning ใน Python

sklearn StandardScaler

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
scaler.fit(samples) StandardScaler(copy=True, with_mean=True, with_std=True)
samples_scaled = scaler.transform(samples)
Unsupervised Learning ใน Python

เมธอดที่คล้ายกัน

  • StandardScaler และ KMeans มีเมธอดที่คล้ายกัน

  • ใช้ fit() / transform() กับ StandardScaler

  • ใช้ fit() / predict() กับ KMeans

Unsupervised Learning ใน Python

StandardScaler แล้วจึง KMeans

  • ต้องทำสองขั้นตอน: StandardScaler แล้วจึง KMeans

  • ใช้ pipeline ของ sklearn เพื่อรวมหลายขั้นตอนเข้าด้วยกัน

  • ข้อมูลไหลจากขั้นตอนหนึ่งไปยังขั้นตอนถัดไป

Unsupervised Learning ใน Python

Pipeline รวมหลายขั้นตอนเข้าด้วยกัน

from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
scaler = StandardScaler()
kmeans = KMeans(n_clusters=3)

from sklearn.pipeline import make_pipeline
pipeline = make_pipeline(scaler, kmeans)
pipeline.fit(samples)
Pipeline(steps=...)
labels = pipeline.predict(samples)
Unsupervised Learning ใน Python

การ standardize ฟีเจอร์ช่วยให้การจัดกลุ่มดีขึ้น

เมื่อ standardize ฟีเจอร์แล้ว:

varieties  Barbera  Barolo  Grignolino
labels                                
0                0      59           3
1               48       0           3
2                0       0          65

ไม่ standardize ฟีเจอร์ให้ผลที่แย่มาก:

varieties  Barbera  Barolo  Grignolino
labels                                
0               29      13          20
1                0      46           1
2               19       0          50
Unsupervised Learning ใน Python

ขั้นตอน preprocessing ของ sklearn

  • StandardScaler เป็นขั้นตอน "preprocessing"

  • MaxAbsScaler และ Normalizer เป็นตัวอย่างอื่น

Unsupervised Learning ใน Python

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

Unsupervised Learning ใน Python

Preparing Video For Download...