k-Nearest Neighbors
DISTANCE · TOP-K · VOTEOne-hot และ Normalize แถวข้อมูล คำนวณ Euclidean Distance จัดอันดับเพื่อนบ้านห้ารายการ แล้วรวมคะแนนเสียงด้วย GROUP BY
Select Run pure SQL.
PURE SQL MACHINE LEARNING · NO PYTHON
Machine Learning เริ่มจาก Representation, Distance, Probability, Optimization และ Aggregation ซึ่ง SQL มี Building Block เหล่านี้อยู่แล้ว Lab นี้เปิดกล่องดำด้วยการสร้าง ML หกตระกูลจาก Relational Operation โดยตรง
SIX LIVE FAMILIES
กด Run SQL เพื่อรัน Model Pipeline แต่ละแบบบน Server ระบบส่งกลับผลและ SQL ที่ใช้ ขณะที่ Source Row อยู่ภายใน Engine
One-hot และ Normalize แถวข้อมูล คำนวณ Euclidean Distance จัดอันดับเพื่อนบ้านห้ารายการ แล้วรวมคะแนนเสียงด้วย GROUP BY
Select Run pure SQL.
นับ Class Prior และ Conditional Likelihood ด้วย Aggregate ใช้ Laplace Smoothing และรวม Log Probability เพื่อป้องกัน Underflow
Select Run pure SQL.
สร้าง Model จาก Aggregate Statistics ด้วย regr_slope, regr_intercept และ regr_r2 แล้วทำนายใน Statement เดียว
Select Run pure SQL.
ใช้ CROSS JOIN วัดระยะทุก Point–Centroid, Window Function เลือก Cluster และ GROUP BY คำนวณ Centroid รอบถัดไป
Select Run pure SQL.
Self-join รายการใน Basket เดียวกัน แล้วนับความถี่คู่สินค้าเพื่อคำนวณ Support, Confidence และ Lift
Select Run pure SQL.
ให้ SQL เรียนค่าเฉลี่ยและส่วนเบี่ยงเบนมาตรฐานจากข้อมูล แล้ว Score และจัดอันดับ Observation ที่ผิดปกติ
Select Run pure SQL.
EMBEDDED EXCEL WORKSHEET
Workbook ปี 2022 สอน Dataset เดียวกันผ่านมุม Probability และ Distance แท็บด้านล่างรักษาลำดับการเรียนรู้เดิม ดาวน์โหลด Excel เพื่อเปิดดูและแก้สูตรได้
| No. | Outlook | Temperature | Humidity | Windy | Play |
|---|---|---|---|---|---|
| 1 | sunny | hot | high | false | no |
| 2 | sunny | hot | high | true | no |
| 3 | overcast | hot | high | false | yes |
| 4 | rainy | mild | high | false | yes |
| 5 | rainy | cool | normal | false | yes |
| 6 | rainy | cool | normal | true | no |
| 7 | overcast | cool | normal | true | yes |
| 8 | sunny | mild | high | false | no |
| 9 | sunny | mild | normal | false | yes |
| 10 | rainy | mild | normal | false | yes |
| 11 | sunny | mild | normal | true | yes |
| 12 | overcast | mild | high | true | yes |
| 13 | overcast | hot | normal | false | yes |
| 14 | rainy | mild | high | true | no |
| Class | Prior | P(rainy|class) | P(cool|class) | P(high|class) | P(windy=true|class) | Original product |
|---|---|---|---|---|---|---|
| yes | 9/14 | 3/9 | 2/9 | 3/9 | 3/9 | 0.00823 |
| no | 5/14 | 2/5 | 1/5 | 4/5 | 3/5 | 0.03840 |
การคูณที่มองเห็นช่วยให้ตาม Conditional Evidence ได้ง่าย
เพิ่ม Class Prior, Laplace Smoothing และ Log Score เพื่อรองรับค่าที่ไม่เคยพบและ Underflow
| Neighbor | Distance | Class |
|---|---|---|
| 1 | 0.0361 | no |
| 8 | 0.1562 | no |
| 9 | 0.1838 | yes |
| 2 | 1.0026 | no |
| 11 | 1.0108 | yes |
Vote: no = 3, yes = 2 → prediction = no
HOW SQL BECOMES ML
Pure SQL ไม่ได้หมายความว่า Algorithm ทุกชนิดจะเขียนได้สวยหรือ Scale เท่ากัน แต่หมายถึง Transformation ตั้งแต่ Training Row ไปถึง Decision ถูกแสดงและรันภายใน Data Engine
RELATED LESSONS
ต่อด้วย Declarative ML เพื่อเห็น Ecosystem, MADlib เพื่อใช้ In-database Algorithm ที่มีวุฒิภาวะ และ DuckDB × ClickHouse เพื่อเปรียบเทียบ Execution Boundary
NEXT PURE SQL LAB
ต่อด้วยระบบแนะนำเจ็ดวิธีผ่าน SQL ล้วน ตั้งแต่ Popularity, Graph Co-occurrence, Collaborative Filtering ไปถึง Content, Time-decay และ Hybrid Ranking