Scikit-learn (sklearn) เป็นไลบรารี Machine Learning Open Source ใน Python ที่ได้รับความนิยมมากที่สุด สร้างบน NumPy, SciPy, Matplotlib ให้เครื่องมือครบครันสำหรับ Statistical Modeling
การติดตั้ง
pip install scikit-learn
หมวดหลักของอัลกอริทึมใน sklearn
1. Supervised Learning (การเรียนรู้แบบมีผู้ดูแล)
ข้อมูลมี Label (Target) ให้โมเดลเรียนรูปแบบ Input → Output ประเภทย่อย:
- Classification: ทำนายคลาส (Discrete) เช่น Logistic Regression, SVM, Decision Tree, Random Forest, KNN, Naive Bayes
- Regression: ทำนายค่าต่อเนื่อง เช่น Linear Regression, Ridge, Lasso, Decision Tree Regressor, Random Forest Regressor
ข้อจำกัด: ต้องมีมนุษย์ Label ข้อมูล ใช้เวลาและต้นทุนสูง
2. Unsupervised Learning (การเรียนรู้แบบไม่มีผู้ดูแล)
ข้อมูลไม่มี Label โมเดลหา Pattern เองเอง ประเภทย่อย:
- Clustering: จัดกลุ่มข้อมูลคล้ายกัน เช่น K-Means, DBSCAN, Hierarchical Clustering
- Dimensionality Reduction: ลดมิติฟีเจอร์ เช่น PCA, t-SNE, Truncated SVD
- Anomaly Detection: หาค่าผิดปกติ เช่น Isolation Forest, One-Class SVM
3. Model Evaluation & Validation
- Cross-Validation: แบ่งข้อมูล Train/Test หลายรอบ (K-Fold CV) เพื่อประเมินโมเดลให้แม่นยำ
- Metrics: Accuracy, Precision, Recall, F1, ROC-AUC, MSE, MAE, R²
- Hyperparameter Tuning: GridSearchCV, RandomizedSearchCV
4. Preprocessing & Feature Engineering
- Scaling: StandardScaler, MinMaxScaler, RobustScaler
- Encoding: OneHotEncoder, LabelEncoder, OrdinalEncoder
- Imputation: SimpleImputer (填补缺失值)
- Feature Selection: SelectKBest, RFE
5. Pipeline & Model Persistence
Pipeline: รวม Preprocessing + Model เป็นขั้นตอนเดียวjoblib.dump/load: บันทึก/โหลดโมเดล
Curse of Dimensionality (ลคنةมิติ)
เมื่อฟีเจอร์เยอะเกินกว่า ต้องการหน่วยความจำมาก โมเดลทำงานช้า และ Overfitting หรือมาก ดังนั้น Dimensionality Reduction (PCA, Feature Selection) จึงสำคัญ
เวิร์กโฟลว์มาตรฐาน
- Load Data
- EDA (Exploratory Data Analysis)
- Preprocessing (Clean, Scale, Encode)
- Train/Test Split
- Model Selection & Training
- Hyperparameter Tuning (CV)
- Evaluation on Test Set
- Deployment