Sử dụng SGDClassifier
Trong bài tập lập trình cuối cùng này, bạn sẽ thực hiện tìm kiếm siêu tham số cho độ mạnh regularization và lựa chọn hàm mất mát (logistic regression vs. linear SVM) bằng SGDClassifier().
Bài tập này là một phần của khóa học
Bộ phân loại tuyến tính với Python
Hướng dẫn bài tập
- Khởi tạo một đối tượng
SGDClassifiervớirandom_state=0. - Tìm kiếm trên độ mạnh regularization và hai loại mất mát
hingevs.log_loss.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# We set random_state=0 for reproducibility
linear_classifier = ____(random_state=0)
# Instantiate the GridSearchCV object and run the search
parameters = {'alpha':[0.00001, 0.0001, 0.001, 0.01, 0.1, 1],
'loss':[____]}
searcher = GridSearchCV(linear_classifier, parameters, cv=10)
searcher.fit(X_train, y_train)
# Report the best parameters and the corresponding score
print("Best CV params", searcher.best_params_)
print("Best CV accuracy", searcher.best_score_)
print("Test accuracy of best grid search hypers:", searcher.score(X_test, y_test))