始める無料で始める

SGDClassifier を使う

この最後のコーディング演習では、SGDClassifier() を使って、正則化強度と損失関数(ロジスティック回帰 vs. 線形 SVM)のハイパーパラメータサーチを行います。

この演習はコースの一部です

Python で学ぶ線形分類器

コースを見る

演習の手順

  • random_state=0 を指定して SGDClassifier のインスタンスを作成します。
  • 正則化強度と、hingelog_loss の2種類の損失関数を探索します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# 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))
コードを編集して実行