我正在使用 hypopt 包中的 Gridsearch 函数来使用指定的验证集进行超参数搜索。分类的默认指标似乎是准确性(不太确定)。这里我想使用 F1 分数作为度量标准。我不知道应该在哪里指定指标。我查看了文档,但有点困惑。
有熟悉 hypopt 包的人知道我该怎么做吗?非常感谢。
from hypopt import GridSearch
log_reg_params = {"penalty": ['l1'], 'C': [0.001, 0.01]}
opt = GridSearch(model=LogisticRegression())
opt.fit(X_train, y_train, log_reg_params, X_val, y_val)
请您参考如下方法:
hypopt 包的默认指标是 score() 函数,适用于您使用的任何模型,因此在您的情况下它是 LogisticRegression()。 score() 默认为准确性。
如果您通过 pip install hypopt --upgrade 将 hypopt 包升级到版本 1.0.8,您可以在 scoring 参数中指定您选择的任何指标 GridSearch.fit(),例如fit(scoring='f1')。这是一个基于您使用 F1 指标的代码的简单工作示例:
from hypopt import GridSearch
param_grid = {"penalty": ['l1'], 'C': [0.001, 0.01]}
opt = GridSearch(model=LogisticRegression(), param_grid = param_grid)
# This will use f1 score as the scoring metric that you optimize.
opt.fit(X_train, y_train, X_val, y_val, scoring='f1')
hypopt 支持 sklearn 支持的大多数评分函数。
- 对于分类,
hypopt支持这些指标(作为字符串):'accuracy'、'brier_score_loss'、'average_precision'、'f1'、'f1_micro'、'f1_macro'、'f1_weighted'、 'neg_log_loss'、'precision'、'recall' 或 'roc_auc'。 - 对于回归,
hypopt支持:“explained_variance”、“neg_mean_absolute_error”、“neg_mean_squared_error”、“neg_mean_squared_log_error”、“neg_median_absolute_error”、“r2”。
您还可以创建自己的指标 your_custom_score_func(y_true, y_pred),方法是将其包装到如下对象中:
from sklearn.metrics import make_scorer
scorer = make_scorer(your_custom_score_func)
opt.fit(X_train, y_train, X_val, y_val, scoring=scorer)
您可以在此处的 hypopt.GridSearch.fit() 文档字符串中了解更多信息:
您可以在此处了解有关创建自己的自定义评分指标的更多信息:
