【问题标题】:TypeError: f1_score() takes at least 2 arguments (1 given)TypeError: f1_score() 至少需要 2 个参数(1 个给定)
【发布时间】:2017-12-29 03:49:00
【问题描述】:

我正在尝试运行 SGDClassifier,但出现此错误:

TypeError: f1_score() takes at least 2 arguments (1 given)

这是我的代码:

pipeline = Pipeline([
 ('clf', SGDClassifier())
])

parameters = {
    'clf__loss': ('log', 'hinge'),
    'clf__penalty': ['l1', 'l2', 'elasticnet'],
    'clf__alpha': [0.001, 0.0001, 0.00001, 0.000001]
}

score_func = make_scorer(metrics.f1_score(average='weighted'))

grid_search = GridSearchCV(pipeline, parameters, n_jobs=3,
verbose=1, scoring=score_func)



grid_search.fit(X, Y)

我该如何解决?

【问题讨论】:

标签: python scikit-learn


【解决方案1】:

你需要定义评分函数如下:

from sklearn.metrics import f1_score


f1_scorer = make_scorer(f1_score,average='weighted')

grid_search =GridSearchCV(pipeline, parameters, n_jobs=3,verbose=1, scoring=f1_scorer)

这很好用,可以做你想做的事。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我相信你需要像这样通过它:

    make_scorer(metrics.f1_score, average='weighted')
    

    您应该单独传递函数名称和任何关联的关键字参数。

    Documentation参考。

    【讨论】:

    • @ arpitsolanki你应该通过函数名而不调用它 i>。使用他们的代码,OP实际上是调用它(和错误地)提高错误。 span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2017-05-10
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多