开始使用免费开始使用

使用 SGDClassifier

在本章最后一个编码练习中,您将使用 SGDClassifier() 对正则化强度和损失函数(逻辑回归 vs. 线性 SVM)进行超参数搜索。

本练习是课程的一部分

Python 中的线性分类器

查看课程

练习说明

  • 实例化一个 SGDClassifier,设置 random_state=0
  • 在正则化强度以及 hingelog_loss 两种损失函数上进行搜索。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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))
编辑并运行代码