不平衡的数据集使用scikit-learn进行负例数量优势​​​​​​​

2024-05-16 12:59:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个不平衡的二进制数据集,大多数是1个标签(6比1)。在

我用一个LinearSVC模型运行GridSearchCV,使用class\u weight='balanced'优化'C'参数。因为1个多数,我想我需要一个像'metrics.average_precision_得分'有一个区别:它将根据0标签而不是1来计算分数

  1. 我这样做对吗?在
  2. 我有办法吗?在

Tags: 数据模型参数二进制标签分数classprecision
1条回答
网友
1楼 · 发布于 2024-05-16 12:59:33

我最终在Scikit评分函数文档中找到了答案。在

可以根据负面标签计算得分,方法是将其重新定义为“正面标签”(仅用于评分)。例如:

from sklearn.model_selection import GridSearchCV
from sklearn.metrics import precision_score, make scorer
# here the scoring function is created. make_scorer passes the pos_label=0
# argument to sklearn.metrics.precision_score() to create the desired function. 
neg_precision = make_scorer(precision_score, pos_label=0)
# some random C parameters for completion
params = {'C': [0.01, 0.03, 0.1, 0.3, 1, 3, 10]}
clf = GridSearchCV(LinearSVC(class_weight='balanced'), cv=10,param_grid=params, scoring=neg_precision)
clf.fit(X, y)

我个人决定使用scoring='f1'u macro'。这将计算正标签的f1分数和负标签的f1分数的非加权平均值。这产生了我追求的结果。在

相关问题 更多 >