3

哪种方法最适合计算不相关数据集的 Matthews 相关系数 (MCC) 值?

4

2 回答 2

6

我不确定这里的“最佳方法”是什么意思,但考虑到混淆矩阵,计算应该很简单。在 Python 中:

import math

# tp is true positives, fn is false negatives, etc
mcc = (tp*tn - fp*fn) / math.sqrt( (tp + fp)*(tp + fn)*(tn + fp)*(tn + fn) )
于 2013-12-12T16:55:12.533 回答
1

以前的答案是正确的,但是在您的公式中,您可能还需要考虑分母中四个总和中的任何一个为零的情况;在这种情况下,分母可以任意设置为 1。

为了完整起见,我在下面添加了 R 代码(原始代码可以在这里找到)

mcc <- function (actual, predicted)
{
  # handles zero denominator and verflow error on large-ish products in denominator.
  #
  # actual = vector of true outcomes, 1 = Positive, 0 = Negative
  # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative
  # function returns MCC

  TP <- sum(actual == 1 & predicted == 1)
  TN <- sum(actual == 0 & predicted == 0)
  FP <- sum(actual == 0 & predicted == 1)
  FN <- sum(actual == 1 & predicted == 0)

  sum1 <- TP+FP; sum2 <-TP+FN ; sum3 <-TN+FP ; sum4 <- TN+FN;
  denom <- as.double(sum1)*sum2*sum3*sum4 # as.double to avoid overflow error on large products

  if (any(sum1==0, sum2==0, sum3==0, sum4==0)) {
    denom <- 1
  }

  mcc <- ((TP*TN)-(FP*FN)) / sqrt(denom)
  return(mcc)
}
于 2016-10-22T17:49:13.543 回答