【问题标题】:Getting good accuracy but very low precision for classification task on an unbalanced dataset在不平衡数据集上的分类任务获得良好的准确性但非常低的精度
【发布时间】:2017-02-13 12:45:48
【问题描述】:

我有一个不平衡的数据集,其中正类大约有 10,000 个条目,负类大约有 8,00,000 个条目。我正在尝试一个简单的 scikit 的 LogisticRegression 模型作为基线模型,class_weight='balanced' (希望不平衡的问题应该得到解决?)。

但是,我的准确度得分为 0.83,但准确度得分为 0.03。可能是什么问题?不平衡部分需要单独处理吗?

这是我当前的代码:

>>> train = []
>>> target = []
>>> len(posList)
... 10214
>>> len(negList)
... 831134
>>> for entry in posList:
...     train.append(entry)
...     target.append(1)
...
>>> for entry in negList:
...     train.append(entry)
...     target.append(-1)
...
>>> train = np.array(train)
>>> target = np.array(target)
>>> 
>>> X_train, X_test, y_train, y_test = train_test_split(train, target, test_size=0.3, random_state=42)
>>> 
>>> model = LogisticRegression(class_weight='balanced')
>>> model.fit(X_train, y_train)
LogisticRegression(C=1.0, class_weight='balanced', dual=False,
          fit_intercept=True, intercept_scaling=1, max_iter=100,
          multi_class='ovr', n_jobs=1, penalty='l2', random_state=None,
          solver='liblinear', tol=0.0001, verbose=0, warm_start=False)
>>> 
>>> predicted = model.predict(X_test)
>>> 
>>> metrics.accuracy_score(y_test, predicted)
0.835596671213
>>> 
>>> metrics.precision_score(y_test, predicted, average='weighted')
/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py:976: DeprecationWarning: From version 0.18, binary input will not be handled specially when using averaged precision/recall/F-score. Please use average='binary' to report only the positive class performance.
  'positive class performance.', DeprecationWarning)
0.033512518766

【问题讨论】:

  • I am getting an accuracy score of 0.83, but a precision score of 0.03. What could be the issue - 它可以帮助您了解当您随机预测 10000 个正数而其余的负数时,检查您获得的准确度/精度分数。

标签: python python-2.7 scikit-learn


【解决方案1】:

我想我明白发生了什么。

考虑一个虚拟分类器,它将为您的数据集的每个样本返回多数类。对于像您这样的不平衡集合,这似乎很公平(我们称您的正类 class 1 和负类 class 0)。分类器的准确度为:831134/(831134+10214.0) = 0.987859958067292。是的,准确率为 99%,但它不能很好地表示您的分类器。相反,我们最好看出它的精度。因此,由于您的数据确实不平衡(比率1:80),逻辑回归表现不佳,但对于虚拟分类器,它具有很高的准确度。

精确度定义为真阳性真阳性假阳性之和的比率。换句话说,它是真正属于类1的元素所有检测到属于类1的元素中的比例

LinearRegression分类器的准确性是acc = 0.835596671213。因此,它们是虚拟分类器和逻辑回归之间的准确度差异:diff = 0.987859958067292 - 0.835596671213 = 0.15226328685429202。所以 15% 的数据被虚拟分类器错误分类,这几乎对应于n_misclass = 0.15*(831134+10214.0)=126202.2 个样本。因此,Logistic 回归将 126202 个样本分类为来自 1 类,而它们只是 10214

Logistic 回归的精确度 可能类似于:prec = 10214/126202.0 = 0.081

在您的情况下,它在准确性方面的表现似乎不如我们上面看到的那么好。但这大致是为了让您了解可能发生的事情。

【讨论】:

  • 是的,我明白这一点,但在 scikit 的文档scikit-learn.org/stable/modules/generated/… 中,他们提到对于 class_weight='balanced' 它处理不平衡部分。 ““平衡”模式使用 y 的值自动调整与输入数据中的类频率成反比的权重,如 n_samples / (n_classes * np.bincount(y))。“。不应该解决上述问题? span>
猜你喜欢
  • 2023-02-10
  • 2021-04-22
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 2020-04-07
  • 2020-10-09
相关资源
最近更新 更多