Majority vote implementation. Mục tiêu: thực hành được trên sklearn hiện đại, có thể chạy trong môi trường Python 3.10+.
Mục tiêu
- Nắm khái niệm cốt lõi trong mục sách tương ứng.
- Chạy được ví dụ code minh họa và biến thể tham số.
- Liên hệ với bài trước/sau trong cùng chương theo MANIFEST.
Lý thuyết
Tóm tắt lý thuyết
Nội dung chi tiết bám sát mục Kungfutech đã ghi trong bookRef. Khi triển khai production, luôn bọc tiền xử lý + model trong Pipeline và dùng cross-validation để ước lượng hiệu năng.
Tham chiếu chính thức
Ưu tiên đọc scikit-learn User Guide cho API cụ thể; mỗi estimator có trang doc riêng liên kết từ hướng dẫn.
Code Python
from sklearn.datasets import load_wine
from sklearn.ensemble import VotingClassifier
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
X, y = load_wine(return_X_y=True)
clf1 = make_pipeline(StandardScaler(), LogisticRegression(max_iter=2000))
clf2 = make_pipeline(StandardScaler(), SVC(kernel="rbf", probability=True))
ensemble = VotingClassifier(estimators=[("lr", clf1), ("svc", clf2)], voting="soft")
print("Soft voting CV:", cross_val_score(ensemble, X, y, cv=5).mean().round(3))
Lỗi thường gặp
- Data leakage: fit vectorizer/scaler trên toàn bộ dữ liệu trước khi split.
- Random state không cố định: kết quả khó reproduce.
Bài tập
- Chạy lại ví dụ với
random_statekhác và so sánh metric. - Thêm một bước trong
Pipeline(ví dụ scaling nếu phù hợp) và đo lại.
Tóm tắt
Cài đặt Majority Vote và VotingClassifier sklearn — hoàn thành mục Kungfutech — Ch.7 — Majority vote implementation trong lộ trình batch 3.
Tiếp tục lộ trình tại khóa Machine Learning cơ bản — bài tiếp theo: Đánh giá và tune ensemble — CV và probability.