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

时间:2013-05-02 07:20:41

标签: statistics

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

2 个答案:

答案 0 :(得分:6)

我不确定"最好的方法"这里,但给定confusion matrix计算应该是直截了当的。在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) )

答案 1 :(得分:1)

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

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

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)
}