Python 使用scikit学习的具有负面示例多数的不平衡数据集

Python 使用scikit学习的具有负面示例多数的不平衡数据集,python,scikit-learn,Python,Scikit Learn,我有一个不平衡的二进制数据集,大多数是1个标签(6到1) 我使用LinearSVC模型运行GridSearchCV,使用class_weight='balanced'优化'C'参数。由于1占多数,我想我需要一个像“metrics.average\u precision\u score”这样的评分函数,但有一个区别:它将根据0而不是1来计算分数 我这样做对吗 我有办法吗 我最终在Scikit评分函数文档中找到了答案 通过将负面标签重新定义为“正面标签”(仅适用于评分),可以根据负面标签计算分数。例

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

我使用LinearSVC模型运行GridSearchCV,使用class_weight='balanced'优化'C'参数。由于1占多数,我想我需要一个像“metrics.average\u precision\u score”这样的评分函数,但有一个区别:它将根据0而不是1来计算分数

  • 我这样做对吗
  • 我有办法吗

  • 我最终在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_macro'。这将计算阳性标签f1分数和阴性标签f1分数的非加权平均值。这产生了我想要的结果