หลักสูตร 1145100 เน้นการใช้ภาษา Python และโมดูลที่เกี่ยวข้องเพื่อพัฒนาโครงงาน
Scikit-Learn & Statsmodels
TensorFlow & PyTorch
PyCaret & Agent AI
MCP & ADK
# --- Statsmodels: Statistical Inference ---
import statsmodels.api as sm
# ต้องเพิ่ม constant (intercept) ด้วยตัวเอง
X_stat = sm.add_constant(X)
model_stat = sm.Logit(y, X_stat).fit()
# แสดงผลการทดสอบทางสถิติอย่างละเอียด
print(model_stat.summary())
# --- Scikit-Learn: Prediction Pipeline ---
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
model = make_pipeline(StandardScaler(), LogisticRegression())
model.fit(X, y)
เน้นการใช้งานในระดับ Production และมีความสามารถในระบบนิเวศขนาดใหญ่ (TFX, TF Lite)
เป็นที่นิยมในงานวิจัยด้วยระบบ Dynamic Computation Graph ที่ยืดหยุ่นและเขียนโค้ดได้แบบ Pythonic
การเลือกใช้ขึ้นอยู่กับความต้องการของโครงงาน (Research vs Production)
PyCaret ช่วยให้การพัฒนา Workflow รวดเร็วขึ้นโดยการทำ Low-code automation
# PyCaret 3 บรรทัดเพื่อหาโมเดลที่ดีที่สุด
from pycaret.classification import *
s = setup(data, target = 'label', session_id = 123)
best = compare_models()
ในสัปดาห์นี้เราจะเปลี่ยนจากการเขียนสคริปต์แยกส่วน เป็นการสร้าง **Workflow** ที่สมบูรณ์
Preprocessing: ใช้ Scikit-Learn จัดการ Data Pipeline
Prototyping: ใช้ PyCaret ทดสอบหลายโมเดลพร้อมกันอย่างรวดเร็ว
Evaluation: ใช้ Agent AI ประเมินผลลัพธ์อัตโนมัติ
# Example: Standard Pipeline
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
workflow = Pipeline([
('imputer', SimpleImputer(strategy='mean')),
('scaler', StandardScaler()),
('model', RandomForestClassifier())
])
PyCaret ทำหน้าที่เป็นเครื่องมือ Low-code เพื่อเร่งกระบวนการทดลอง
# ค้นหาโมเดลที่ดีที่สุดเพื่อส่งต่อให้ Agent AI
best_model = compare_models()
evaluate_model(best_model)
การใช้ Agent Development Kit (ADK) สร้าง Agent เพื่อประเมินโมเดล
Confusion Matrix, F1-Score, และ Metrics อื่นๆ
วิเคราะห์หา Bias หรือ Overfitting และสรุปผล
*เป็นการนำเทคโนโลยี AI มาเพิ่มประสิทธิภาพในกระบวนการทำงานด้านข้อมูล
ใช้ MCP เพื่อสร้าง Context Layers สำหรับโมเดล AI
"Workflow = Scikit-Learn + PyCaret + MCP + Agent AI"
ก่อนที่ Agent จะประเมินผลได้ เราต้องกำหนดตัวชี้วัดที่เป็นมาตรฐานจากโมดูลวิทยาการข้อมูล
Agent จะใช้ข้อมูลเหล่านี้ในการตัดสินใจว่าโมดูลใดมีประสิทธิภาพสูงสุด
ใช้ **ADK** ในการสร้าง Command-based Data Agent
# ADK Conceptual Structure
from adk import Agent
evaluator = Agent(role="Evaluator")
@evaluator.command("analyze")
def analyze_metrics(data):
if data.accuracy < 0.8:
return "Model needs tuning"
return "Model passed"
Agent ต้องการ **Context Layers** เพื่อการประเมินที่แม่นยำ
ใช้ MCP จัดเก็บข้อมูลโครงการ เช่น วัตถุประสงค์ของโมเดล และลักษณะของชุดข้อมูล
สร้าง Context Prompt เพื่อให้ Agent รู้ว่า "Accuracy 70%" อาจจะดีเพียงพอแล้วสำหรับข้อมูลที่มีความซับซ้อนสูง
Scikit-Learn / PyCaret
MCP Metadata
Agent AI (ADK)
"เทคโนโลยี AI ช่วยเพิ่มประสิทธิภาพในกระบวนการทำงานด้านข้อมูลอย่างเป็นระบบ"
ภารกิจ: พัฒนา Workflow และระบบประเมินอัตโนมัติ
สัปดาห์หน้า: Docker และการทำซ้ำงาน (Reproducibility)
ในคาบปฏิบัติการ 2 ชั่วโมง เราจะใช้ **AI as a Co-Developer**
เป้าหมาย: สร้างกระบวนการทำงานที่ทำซ้ำได้ (Reproducible Workflow) โดยใช้ Scikit-Learn
# 1. โหลดข้อมูลโดยใช้ Pandas
import pandas as pd
from sklearn.model_selection import train_test_split
housing = pd.read_csv("housing.csv")
# 2. แบ่งชุดข้อมูล Train (80%) และ Test (20%)
train_set, test_set = train_test_split(housing, test_size=0.2, random_state=42)
# 3. แยก Features และ Labels
X_train = train_set.drop("median_house_value", axis=1)
y_train = train_set["median_house_value"].copy()
เราจะสร้าง Numerical Pipeline เพื่อจัดการข้อมูลตัวเลข:
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
num_pipeline = Pipeline([
('imputer', SimpleImputer(strategy="median")), # เติมค่าว่างด้วยค่ากลาง [5]
('std_scaler', StandardScaler()), # ปรับสเกลข้อมูล [6]
])
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.ensemble import RandomForestRegressor
num_attribs = list(X_train.select_dtypes(include=['float64', 'int64']))
cat_attribs = ["ocean_proximity"]
# รวม Preprocessing สำหรับทุกประเภทข้อมูล [7]
full_pipeline = ColumnTransformer([
("num", num_pipeline, num_attribs),
("cat", OneHotEncoder(), cat_attribs),
])
# เชื่อมต่อ Preprocessing เข้ากับ Model (Estimator)
final_pipeline = Pipeline([
("prep", full_pipeline),
("algo", RandomForestRegressor())
])
final_pipeline.fit(X_train, y_train) # สั่งรันทั้ง Pipeline ในคำสั่งเดียว!
from sklearn.metrics import mean_squared_error
import numpy as np
predictions = final_pipeline.predict(X_train)
mse = mean_squared_error(y_train, predictions)
rmse = np.sqrt(mse)
print(f"RMSE: {rmse}") # วัดระยะห่างระหว่างผลทำนายกับความจริง
หากรันโค้ดแล้วเกิด ValueError หรือ KeyError
OneHotEncoder ถึงเปลี่ยนข้อมูลเป็น Sparse Matrix เป้าหมาย: ใช้ PyCaret เพื่อเปรียบเทียบโมเดลอย่างรวดเร็ว และใช้ Agent AI วิเคราะห์ผลลัพธ์
คำสั่ง setup() จะจัดการ Preprocessing (Imputation, Scaling, Encoding) ให้อัตโนมัติ
# 1. ติดตั้งและโหลด Library
from pycaret.classification import *
# 2. เริ่มต้นระบบ (สมมติใช้ชุดข้อมูล Diabetes [4])
s = setup(data=df, target='Outcome', session_id=123, train_size=0.7)
# PyCaret จะแสดงตารางสรุปการจัดการข้อมูล เช่น ข้อมูลว่างหรือประเภทตัวแปร
# รันและเปรียบเทียบโมเดลทั้งหมด
best_model = compare_models()
# ตารางผลลัพธ์จะปรากฏขึ้นพร้อมค่า
- Accuracy
- AUC (Area Under Curve) [7]
- Recall / Precision [8]
- F1-Score [8]
มองหาโมเดลที่มีค่า F1-Score และ AUC สูงพร้อมๆ กัน เพื่อความสมดุลในการทำนาย
*Tip: สังเกตว่า Gradient Boosting มักทำคะแนนได้ดีในข้อมูล Tabular
# ดู Confusion Matrix
plot_model(best_model, plot='confusion_matrix')
# ดู Feature Importance
plot_model(best_model, plot='feature')
# ปรับแต่งค่า Hyperparameters ให้ดีขึ้น
tuned_model = tune_model(best_model)
ใช้รูปภาพเหล่านี้เป็น Context เพื่อส่งให้ Agent AI ในขั้นตอนถัดไป
ส่งค่า Metrics จาก PyCaret เข้าไปยัง AI Agent (ChatGPT/Copilot) พร้อมใช้ Prompt ดังนี้
ผลลัพธ์: นักศึกษาต้องบันทึกคำวิจารณ์ของ Agent AI ลงในรายงาน Lab
สิ่งที่เรียนในสัปดาห์นี้จะเป็นฐานสำคัญสำหรับ
การสร้าง Command-based Data Agent เพื่อ Query ข้อมูลและจัดการโครงงานอัตโนมัติโดยใช้ ADK Documentation
การบูรณาการ MCP + Agent AI เพื่อนำเสนอผลงานปลายภาค
Next Week: Docker & Reproducibility (การฝัง MCP Metadata ใน Build Image)