Bạn đang xem thử công khai
Đăng nhập để lưu tiến độ, làm bài tập, thảo luận và nhận phản hồi.
Bài này nối lại tất cả những gì đã học thành một quy trình duy nhất.
| # | Bước | Câu hỏi cần trả lời |
|---|---|---|
| 1 | Xác định bài toán | Dự đoán cái gì? Regression hay classification? |
| 2 | Thu thập dữ liệu | Đủ chưa? Có đại diện cho thực tế không? |
| 3 | Khám phá (EDA) | Phân phối ra sao? Thiếu bao nhiêu? Có ngoại lai không? |
| 4 | Chia train/test | Có cần stratify không? |
| 5 | Tiền xử lý | Điền thiếu, chuẩn hoá, mã hoá — fit chỉ trên train |
| 6 | Baseline | Mô hình đơn giản làm mốc so sánh |
| 7 | Huấn luyện & tinh chỉnh | Cross-validation trên tập train |
| 8 | Đánh giá cuối | Chạy tập test một lần |
| 9 | Diễn giải & triển khai | Mô hình sai kiểu gì? Sai đó có chấp nhận được không? |
import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix
data = pd.read_csv("data.csv")
target = "Outcome"
x, y = data.drop(target, axis=1), data[target]
x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.2, random_state=1009, stratify=y)
pipe = Pipeline([
("imputer", SimpleImputer(strategy="median")),
("scaler", StandardScaler()),
("model", RandomForestClassifier(random_state=100)),
])
grid = {"model__max_depth": [5, 10, None], "model__n_estimators": [100, 300]}
search = GridSearchCV(pipe, grid, cv=5, scoring="f1", n_jobs=-1)
search.fit(x_train, y_train)
y_pred = search.best_estimator_.predict(x_test) # test chạy MỘT lần
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
Có được classification_report chưa phải là xong. Hãy hỏi tiếp:
Dừng lại ở con số accuracy. Mô hình được dùng bởi người thật, để ra quyết định thật. Hiểu nó sai ở đâu quan trọng không kém việc nó đúng bao nhiêu phần trăm.