Introduction to Machine Learning

1. ความหมายของ Machine Learning

Machine Learning (ML) หรือ การเรียนรู้ของเครื่อง คือสาขาหนึ่งของปัญญาประดิษฐ์ (Artificial Intelligence) ที่เน้นการพัฒนาอัลกอริทึมและโมเดลทางสถิติที่ช่วยให้คอมพิวเตอร์สามารถ "เรียนรู้" จากข้อมูลและปรับปรุงประสิทธิภาพในการทำงานโดยอัตโนมัติ โดยไม่ต้องถูกโปรแกรมอย่างชัดเจนสำหรับทุกสถานการณ์

1.1 คำนิยาม

ตามคำนิยามของ Tom Mitchell (1997):

"A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E."

แปลความว่า: โปรแกรมคอมพิวเตอร์จะถือว่า "เรียนรู้" ได้ เมื่อประสิทธิภาพในการทำงาน (P) สำหรับงาน (T) นั้นๆ ดีขึ้นตามประสบการณ์ (E) ที่ได้รับ

1.2 ตัวอย่างการประยุกต์ใช้

1.3 ความแตกต่างระหว่าง Traditional Programming และ Machine Learning

%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#cc241d','primaryTextColor':'#ebdbb2','primaryBorderColor':'#d65d0e','lineColor':'#98971a','secondaryColor':'#458588','tertiaryColor':'#ebdbb2','background':'#282828','mainBkg':'#3c3836','secondBkg':'#504945','textColor':'#ebdbb2','fontSize':'16px'}}}%%
flowchart TB
    subgraph Traditional["Traditional Programming"]
        direction TB
        A1[Input Data] --> B1[Rules/Logic]
        B1 --> C1[Output]
    end
    
    subgraph ML["Machine Learning"]
        direction TB
        A2[Input Data] --> B2[Learning Algorithm]
        D2[Output/Labels] --> B2
        B2 --> C2[Model/Rules]
    end
    
    style Traditional fill:#3c3836,stroke:#d65d0e,stroke-width:2px
    style ML fill:#3c3836,stroke:#98971a,stroke-width:2px

Traditional Programming:

Machine Learning:


2. ประเภทของ Machine Learning

Machine Learning แบ่งออกเป็น 4 ประเภทหลักตามลักษณะการเรียนรู้และข้อมูลที่ใช้

%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#cc241d','primaryTextColor':'#ebdbb2','primaryBorderColor':'#d65d0e','lineColor':'#98971a','secondaryColor':'#458588','tertiaryColor':'#ebdbb2','background':'#282828','mainBkg':'#3c3836','secondBkg':'#504945','textColor':'#ebdbb2','fontSize':'16px'}}}%%
mindmap
  root((Machine Learning))
    Supervised Learning
      Classification
      Regression
    Unsupervised Learning
      Clustering
      Dimensionality Reduction
    Semi-Supervised Learning
      Combination Approach
    Reinforcement Learning
      Agent-Environment
      Reward-Based

2.1 Supervised Learning (การเรียนรู้แบบมีผู้สอน)

คำนิยาม: เป็นการเรียนรู้จากข้อมูลที่มี Labels หรือคำตอบที่ถูกต้องกำกับไว้แล้ว โดยโมเดลจะเรียนรู้ความสัมพันธ์ระหว่าง Input (X) และ Output (y)

สมการพื้นฐาน:

y = f ( X ) + ε

โดยที่:

แบ่งเป็น 2 ประเภทย่อย คือ Classification (การจำแนกประเภท) และ Regression (การถดถอย)

2.1.1 Classification (การจำแนกประเภท)

เป็นการทำนายค่าที่เป็นกลุ่มหรือประเภทที่แยกจากกันชัดเจน (Discrete/Categorical)

ตัวอย่าง:

อัลกอริทึมที่นิยม:

2.1.2 Regression (การถดถอย)

เป็นการทำนายค่าที่เป็นตัวเลขต่อเนื่อง (Continuous)

ตัวอย่าง:

อัลกอริทึมที่นิยม:

2.2 Unsupervised Learning (การเรียนรู้แบบไม่มีผู้สอน)

คำนิยาม: เป็นการเรียนรู้จากข้อมูลที่ไม่มี Labels กำกับ โมเดลจะพยายามค้นหารูปแบบ (Patterns) หรือโครงสร้าง (Structure) ที่ซ่อนอยู่ในข้อมูลเองโดยอัตโนมัติ

วัตถุประสงค์หลัก:

ประเภทย่อยที่สำคัญ คือ Clustering (การจัดกลุ่ม) และ Dimensionality Reduction (การลดมิติข้อมูล)

2.2.1 Clustering (การจัดกลุ่ม)

เป็นการแบ่งข้อมูลออกเป็นกลุ่มๆ โดยที่ข้อมูลในกลุ่มเดียวกันมีความคล้ายคลึงกันสูง

ตัวอย่างการใช้งาน:

อัลกอริทึมที่นิยม:

2.2.2 Dimensionality Reduction (การลดมิติข้อมูล)

เป็นการลดจำนวน Features ของข้อมูลโดยพยายามรักษาข้อมูลสำคัญไว้ให้มากที่สุด

ประโยชน์:

อัลกอริทึมที่นิยม:

2.3 Semi-Supervised Learning (การเรียนรู้แบบกึ่งมีผู้สอน)

คำนิยาม: เป็นการเรียนรู้ที่ใช้ข้อมูลทั้งที่มี Labels และไม่มี Labels ร่วมกัน โดยทั่วไปข้อมูลที่มี Labels จะมีจำนวนน้อย ในขณะที่ข้อมูลที่ไม่มี Labels มีจำนวนมาก

เหตุผลที่ใช้:

สถานการณ์ที่เหมาะสม:

เทคนิคที่นิยม:

2.4 Reinforcement Learning (การเรียนรู้เชิงเสริมแรง)

คำนิยาม: เป็นการเรียนรู้ผ่านการทดลองและผิดพลาด (Trial and Error) โดย Agent (ตัวกระทำ) จะเรียนรู้จากการโต้ตอบกับ Environment (สภาพแวดล้อม) และได้รับ Reward (รางวัล) หรือ Penalty (โทษ) จากการกระทำ

องค์ประกอบหลัก:

%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#cc241d','primaryTextColor':'#ebdbb2','primaryBorderColor':'#d65d0e','lineColor':'#98971a','secondaryColor':'#458588','tertiaryColor':'#ebdbb2','background':'#282828','mainBkg':'#3c3836','secondBkg':'#504945','textColor':'#ebdbb2','fontSize':'16px'}}}%%
graph LR
    A[Agent] -->|Action| B[Environment]
    B -->|State| A
    B -->|Reward| A
    
    style A fill:#3c3836,stroke:#458588,stroke-width:3px
    style B fill:#3c3836,stroke:#98971a,stroke-width:3px
  1. Agent - ตัวกระทำที่เรียนรู้และตัดสินใจ
  2. Environment - สภาพแวดล้อมที่ Agent โต้ตอบด้วย
  3. State (s) - สถานะปัจจุบันของสภาพแวดล้อม
  4. Action (a) - การกระทำที่ Agent สามารถทำได้
  5. Reward (r) - ค่าตอบแทนที่ได้รับจากการกระทำ
  6. Policy (π) - กลยุทธ์ในการเลือกกระทำในแต่ละสถานะ

ตัวอย่างการใช้งาน:

อัลกอริทึมที่นิยม:


3. Pipeline ของ ML Project

การพัฒนาโปรเจคท์ Machine Learning มีขั้นตอนหลัก 6 ขั้นตอน ดังแผนภาพ:

%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#cc241d','primaryTextColor':'#ebdbb2','primaryBorderColor':'#d65d0e','lineColor':'#98971a','secondaryColor':'#458588','tertiaryColor':'#ebdbb2','background':'#282828','mainBkg':'#3c3836','secondBkg':'#504945','textColor':'#ebdbb2','fontSize':'16px'}}}%%
flowchart TD
    A[1. Problem Definition] --> B[2. Data Collection]
    B --> C[3. Data Preprocessing]
    C --> D[4. Feature Engineering]
    D --> E[5. Model Training]
    E --> F[6. Model Evaluation]
    F --> G{Good enough?}
    G -->|No| H[Iterate]
    H --> C
    H --> D
    H --> E
    G -->|Yes| I[Deployment]
    
    style A fill:#3c3836,stroke:#cc241d,stroke-width:3px
    style B fill:#3c3836,stroke:#d65d0e,stroke-width:3px
    style C fill:#3c3836,stroke:#d79921,stroke-width:3px
    style D fill:#3c3836,stroke:#98971a,stroke-width:3px
    style E fill:#3c3836,stroke:#458588,stroke-width:3px
    style F fill:#3c3836,stroke:#b16286,stroke-width:3px
    style I fill:#3c3836,stroke:#689d6a,stroke-width:3px

3.1 Problem Definition (การกำหนดปัญหา)

วัตถุประสงค์: กำหนดปัญหาและเป้าหมายของโปรเจคท์ให้ชัดเจน

คำถามสำคัญ:

ตัวอย่าง:

3.2 Data Collection (การรวบรวมข้อมูล)

วัตถุประสงค์: รวบรวมข้อมูลที่มีคุณภาพและเพียงพอสำหรับการเทรนโมเดล

แหล่งข้อมูล:

ข้อควรพิจารณา:

3.3 Data Preprocessing (การเตรียมข้อมูล)

วัตถุประสงค์: ทำความสะอาดและเตรียมข้อมูลให้พร้อมสำหรับการวิเคราะห์

ขั้นตอนสำคัญ:

  1. Handling Missing Values (จัดการค่าที่หายไป)

  2. Handling Outliers (จัดการค่าผิดปกติ)

  3. Data Transformation (การแปลงข้อมูล)

  4. Encoding Categorical Variables (แปลงตัวแปรเชิงหมวดหมู่)

  5. Feature Scaling (ปรับสเกลของ Features)

3.4 Feature Engineering (การสร้าง Features)

วัตถุประสงค์: สร้าง Features ใหม่ที่มีประโยชน์และเลือก Features ที่เหมาะสม

เทคนิคการสร้าง Features:

Feature Selection (การเลือก Features):

ประโยชน์:

3.5 Model Training (การเทรนโมเดล)

วัตถุประสงค์: เลือกอัลกอริทึมที่เหมาะสมและเทรนโมเดล

ขั้นตอน:

  1. แบ่งข้อมูล (Data Splitting)

  2. เลือกอัลกอริทึม

  3. เทรนโมเดล

Hyperparameter Tuning:

Cross-Validation:

3.6 Model Evaluation (การประเมินโมเดล)

วัตถุประสงค์: ประเมินประสิทธิภาพโมเดลด้วย Test Set และตรวจสอบปัญหาต่างๆ

การประเมินผล:

ตรวจสอบปัญหา:

การปรับปรุง:


4. การประเมินผลโมเดล

การประเมินผลโมเดล Machine Learning ต้องใช้ Metrics ที่เหมาะสมกับประเภทของปัญหา

4.1 Metrics สำหรับ Classification

4.1.1 Confusion Matrix

Confusion Matrix เป็นตารางแสดงผลการทำนายเทียบกับค่าจริง

%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#cc241d','primaryTextColor':'#ebdbb2','primaryBorderColor':'#d65d0e','lineColor':'#98971a','secondaryColor':'#458588','tertiaryColor':'#ebdbb2','background':'#282828','mainBkg':'#3c3836','secondBkg':'#504945','textColor':'#ebdbb2','fontSize':'14px'}}}%%
graph TD
    A[Confusion Matrix] --> B[True Positive TP]
    A --> C[True Negative TN]
    A --> D[False Positive FP]
    A --> E[False Negative FN]
    
    B --> F[ทำนาย Positive ถูกต้อง]
    C --> G[ทำนาย Negative ถูกต้อง]
    D --> H[ทำนาย Positive ผิด
Type I Error] E --> I[ทำนาย Negative ผิด
Type II Error] style A fill:#3c3836,stroke:#cc241d,stroke-width:3px style B fill:#3c3836,stroke:#689d6a,stroke-width:2px style C fill:#3c3836,stroke:#689d6a,stroke-width:2px style D fill:#3c3836,stroke:#d65d0e,stroke-width:2px style E fill:#3c3836,stroke:#d65d0e,stroke-width:2px

องค์ประกอบ:

4.1.2 Accuracy (ความแม่นยำ)

Accuracy = TP + TN TP + TN + FP + FN

ความหมาย: สัดส่วนของการทำนายที่ถูกต้องทั้งหมด

เมื่อไหร่ควรใช้:

ข้อจำกัด:

4.1.3 Precision (ความแม่นยำของการทำนาย Positive)

Precision = TP TP + FP

ความหมาย: จากที่ทำนายว่า Positive มีกี่เปอร์เซ็นต์ที่ถูกต้อง

เมื่อไหร่ควรใช้:

4.1.4 Recall / Sensitivity (ความครบถ้วนของการจับ Positive)

Recall = TP TP + FN

ความหมาย: จากที่เป็น Positive จริง โมเดลจับได้กี่เปอร์เซ็นต์

เมื่อไหร่ควรใช้:

4.1.5 F1-Score (ค่าสมดุลระหว่าง Precision และ Recall)

F1 = 2 × Precision × Recall Precision + Recall

ความหมาย: Harmonic Mean ของ Precision และ Recall

เมื่อไหร่ควรใช้:

4.1.6 ROC Curve และ AUC

ROC (Receiver Operating Characteristic) Curve:

AUC (Area Under the Curve):

เมื่อไหร่ควรใช้:

4.2 Metrics สำหรับ Regression

4.2.1 Mean Absolute Error (MAE)

MAE = 1 n i = 1 n | y i y ^ i |

ความหมาย: ค่าเฉลี่ยของความผิดพลาดสัมบูรณ์

4.2.2 Mean Squared Error (MSE)

MSE = 1 n i = 1 n ( y i y ^ i ) 2

ความหมาย: ค่าเฉลี่ยของความผิดพลาดกำลังสอง

4.2.3 Root Mean Squared Error (RMSE)

RMSE = 1 n i = 1 n ( y i y ^ i ) 2

ความหมาย: รากที่สองของ MSE

4.2.4 R² Score (Coefficient of Determination)

R 2 = 1 SS res SS tot

โดยที่:

ความหมาย: สัดส่วนของ Variance ใน Target ที่โมเดลอธิบายได้


5. สรุป

Machine Learning เป็นเครื่องมือที่ทรงพลังในการแก้ปัญหาที่ซับซ้อนโดยการเรียนรู้รูปแบบจากข้อมูล

5.1 สรุปสั้น

1. ความหมายของ Machine Learning

2. ประเภทของ Machine Learning

%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#cc241d','primaryTextColor':'#ebdbb2','primaryBorderColor':'#d65d0e','lineColor':'#98971a','secondaryColor':'#458588','tertiaryColor':'#ebdbb2','background':'#282828','mainBkg':'#3c3836','secondBkg':'#504945','textColor':'#ebdbb2','fontSize':'14px'}}}%%
mindmap
  root((ML Types))
    Supervised
      มีข้อมูล Labels
      Classification
      Regression
    Unsupervised
      ไม่มี Labels
      Clustering
      Dimensionality Reduction
    Semi-Supervised
      Labels บางส่วน
      Self-Training
      Co-Training
    Reinforcement
      เรียนรู้จาก Reward
      Agent-Environment
      Q-Learning

3. Pipeline ของ ML Project

การพัฒนาโปรเจคท์ ML มีขั้นตอนหลัก:

  1. Problem Definition - กำหนดปัญหาและเป้าหมายให้ชัดเจน
  2. Data Collection - รวบรวมข้อมูลที่มีคุณภาพและเพียงพอ
  3. Data Preprocessing - ทำความสะอาดและเตรียมข้อมูล
  4. Feature Engineering - สร้างและเลือก Features ที่เหมาะสม
  5. Model Training - เลือกและเทรนอัลกอริทึมที่เหมาะสม
  6. Model Evaluation - ประเมินผลและปรับปรุงโมเดล

4. การประเมินผล

สำหรับ Classification:

สำหรับ Regression:

5.2 ข้อควรระวัง

Overfitting:

Underfitting:

Data Quality:

5.3 แนวทางการพัฒนาต่อ

เมื่อเข้าใจพื้นฐานแล้ว สามารถศึกษาต่อในหัวข้อ:

  1. Deep Learning - Neural Networks ขนาดใหญ่สำหรับข้อมูลที่ซับซ้อน
  2. Ensemble Methods - รวมหลายโมเดลเพื่อประสิทธิภาพที่ดีขึ้น
  3. Advanced Feature Engineering - เทคนิคการสร้าง Features ขั้นสูง
  4. Model Interpretability - การเข้าใจว่าโมเดลตัดสินใจอย่างไร
  5. Production ML - การนำโมเดลไปใช้งานจริง (MLOps)

5.4 ข้อคิดสำคัญ

"Machine Learning is not magic. It's statistics and optimization algorithms that learn patterns from data."

ความสำเร็จของโปรเจคท์ ML ไม่ได้ขึ้นอยู่กับอัลกอริทึมที่ซับซ้อนเพียงอย่างเดียว แต่ขึ้นอยู่กับ:

5.5 เอกสารอ้างอิง

สำหรับการศึกษาเพิ่มเติม:

Books:

Online Courses:

Documentation:


จุดเริ่มต้นของการเรียนรู้ Machine Learning

การเรียนรู้ Machine Learning เป็นการเดินทางที่ยาวนาน แต่เต็มไปด้วยความน่าสนใจ เริ่มต้นด้วยการทำความเข้าใจพื้นฐาน ฝึกฝนด้วยโปรเจคท์จริง และพัฒนาทักษะอย่างต่อเนื่อง ความรู้ที่ได้จากเนื้อหานี้จะเป็นรากฐานที่มั่นคงสำหรับการก้าวไปสู่หัวข้อที่ซับซ้อนยิ่งขึ้นในอนาคต

Happy Learning! 🚀