Xác thực chéo

Xác thực Mô hình trong Python

Kasey Jones

Data Scientist

Xác thực chéo

Tạo tập train, validation và test bằng cách chia dữ liệu thành 3 phần. Ví dụ: 60% train, 20% validation, 20% test.

Xác thực Mô hình trong Python

Xác thực chéo

Xác thực chéo yêu cầu tách dữ liệu thành nhiều lần train/validation, không chỉ một lần 80:20. Ví dụ có thể dùng 5 lần tách 80:20.

Xác thực Mô hình trong Python

n_splits: số lần chia cross-validation

shuffle: boolean cho biết có xáo trộn trước khi chia

random_state: hạt giống ngẫu nhiên

from sklearn.model_selection import KFold

X = np.array(range(40))
y = np.array([0] * 20 + [1] * 20)

kf = KFold(n_splits=5)

splits = kf.split(X)
Xác thực Mô hình trong Python
kf = KFold(n_splits=5)
splits = kf.split(X)

for train_index, test_index in splits: print(len(train_index), len(test_index))
32 8 32 8 32 8 32 8 32 8
# In ra một bộ chỉ mục:
print(train_index, test_index)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 ...]
[32 33 34 35 36 37 38 39]
Xác thực Mô hình trong Python
rfr = RandomForestRegressor(n_estimators=25, random_state=1111)

errors = [] for train_index, val_index in splits: X_train, y_train = X[train_index], y[train_index] X_val, y_val = X[val_index], y[val_index] rfr.fit(X_train, y_train) predictions = rfr.predict(X_val) errors.append(<some_accuracy_metric>)
print(np.mean(errors))
4.25
Xác thực Mô hình trong Python

Thời gian luyện tập

Xác thực Mô hình trong Python

Preparing Video For Download...