1

私は、gridsearch によって返される最適な推定量を計算する必要があるプロジェクトを行っていました。

parameters = {'gamma':[0.1, 0.5, 1, 10, 100], 'C':[1, 5, 10, 100, 1000]}

# TODO: Initialize the classifier
svr = svm.SVC()

# TODO: Make an f1 scoring function using 'make_scorer' 
f1_scorer = make_scorer(score_func)

# TODO: Perform grid search on the classifier using the f1_scorer as the scoring method
grid_obj = grid_search.GridSearchCV(svr, parameters, scoring=f1_scorer)

# TODO: Fit the grid search object to the training data and find the optimal parameters
grid_obj = grid_obj.fit(X_train, y_train)
pred = grid_obj.predict(X_test)
def score_func():
    f1_score(y_test, pred, pos_label='yes')

# Get the estimator
clf = grid_obj.best_estimator_

gridsearch オブジェクトを作成した後に予測を行うため、f1_scorer 関数を作成する方法がわかりません。gridsearch がスコアリング方法として使用するため、obj を作成した後に f1_scorer を宣言できません。グリッドサーチ用のこのスコアリング関数を作成する方法を教えてください。

4

2 に答える 2

0

渡すスコアラー関数は、パラメータとして およびmake_scorerを取る必要があります。その情報を使用して、スコアを計算するために必要なものがすべて揃っています。次に、GridSearchCV が適合し、可能なパラメーターのセットごとにスコア関数を内部的に呼び出します。事前に y_pred を計算する必要はありません。y_truey_pred

次のようになります。

def score_func(y_true, y_pred):
    """Calculate f1 score given the predicted and expected labels"""
    return f1_score(y_true, y_pred, pos_label='yes')

f1_scorer = make_scorer(score_func)
GridSearchCV(svr, parameters, scoring=f1_scorer)
于 2016-06-09T10:13:42.077 に答える
0
clf = svm.SVC()

# TODO: Make an f1 scoring function using 'make_scorer' 
f1_scorer = make_scorer(f1_score,pos_label='yes')

# TODO: Perform grid search on the classifier using the f1_scorer as the scoring method
grid_obj = GridSearchCV(clf,parameters,scoring=f1_scorer)

# TODO: Fit the grid search object to the training data and find the optimal parameters
grid_obj = grid_obj.fit(X_train, y_train)

# Get the estimator
clf = grid_obj.best_estimator_
于 2016-06-14T12:42:00.823 に答える