開始使用免費開始

使用 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))
編輯並執行程式碼