Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

python - Pass estimator to custom score function via sklearn.metrics.make_scorer

I'd like to make a custom scoring function involving classification probabilities as follows:

def custom_score(y_true, y_pred_proba):
    error = ...
    return error

my_scorer = make_scorer(custom_score, needs_proba=True)

gs = GridSearchCV(estimator=KNeighborsClassifier(),
                  param_grid=[{'n_neighbors': [6]}],
                  cv=5,
                  scoring=my_scorer)

Is there any way to pass the estimator, as fit by GridSearch with the given data and parameters, to my custom scoring function? Then I could interpret the probabilities using estimator.classes_

For example:

def custom_score(y_true, y_pred_proba, clf):
    class_labels = clf.classes_
    error = ...
    return error
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is an alternative way to make a scorer mentioned in the documentation. Using this method I can do the following:

def my_scorer(clf, X, y_true):
    class_labels = clf.classes_
    y_pred_proba = clf.predict_proba(X)
    error = ...
    return error

gs = GridSearchCV(estimator=KNeighborsClassifier(),
                  param_grid=[{'n_neighbors': [6]}],
                  cv=5,
                  scoring=my_scorer)

This avoids the use of sklearn.metrics.make_scorer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...