精确,回忆和f测量在R。

时间:2021-04-25 20:39:31

I haven't used R in a while, so maybe I'm just not used to it yet, but.. I have a table in R with two colums, the first one has predicted values (a value can be either 0 or 1), the second one has the actual values (also 0 or 1). I need to find recall, precision and f-measures, but cannot find a good function for it in R. (I also read about ROCR, but all I could do was creating some plots, but I really don't need plots, I need the numbers).

我已经有一段时间没有使用R了,所以也许我还不习惯,但是。我有一个表在R两科勒姆,第一个预测值(值可以是0或1),第二个有实际值(0或1)。我需要找到回忆,精度和f-measures,但不能找到一个好的函数在R .(我也读到ROCR,但我唯一能做的就是创建一些情节,但是我真的不需要情节,我需要这些数字)。

Is there any good functions for finding precision, recall and f-measure in R? Are there any different ways to do it?

在R中有什么好的函数来寻找精度,回忆和f测量吗?有什么不同的方法吗?

2 个解决方案

#1


19  

First I create a data set as

首先,我创建一个数据集。

> predict <- sample(c(0, 1), 20, replace=T)
> true <- sample(c(0, 1), 20, replace=T)

I suppose those 1's in the predicted values are the retrieved. The total number of retrieved is

我想在预测值中这1是被检索的。检索的总数是。

> retrieved <- sum(predict)

Precision which is the fraction of retrieved instances that are relevant, is

精确是检索到的实例中相关的部分。

> precision <- sum(predict & true) / retrieved

Recall which is the fraction of relevant instances that are retrieved, is

回想一下,这是检索到的相关实例的一小部分,是。

> recall <- sum(predict & true) / sum(true)

F-measure is 2 * precision * recall / (precision + recall) is

F-measure是2 * precision * recall / (precision + recall)。

> Fmeasure <- 2 * precision * recall / (precision + recall)

#2


8  

Just packaging Patrick's great answer neatly into a function ...

把帕特里克的伟大答案巧妙地包装成一个功能……

measurePrecisionRecall <- function(predict, actual_labels){
  precision <- sum(predict & actual_labels) / sum(predict)
  recall <- sum(predict & actual_labels) / sum(actual_labels)
  fmeasure <- 2 * precision * recall / (precision + recall)

  cat('precision:  ')
  cat(precision * 100)
  cat('%')
  cat('\n')

  cat('recall:     ')
  cat(recall * 100)
  cat('%')
  cat('\n')

  cat('f-measure:  ')
  cat(fmeasure * 100)
  cat('%')
  cat('\n')
}

#1


19  

First I create a data set as

首先,我创建一个数据集。

> predict <- sample(c(0, 1), 20, replace=T)
> true <- sample(c(0, 1), 20, replace=T)

I suppose those 1's in the predicted values are the retrieved. The total number of retrieved is

我想在预测值中这1是被检索的。检索的总数是。

> retrieved <- sum(predict)

Precision which is the fraction of retrieved instances that are relevant, is

精确是检索到的实例中相关的部分。

> precision <- sum(predict & true) / retrieved

Recall which is the fraction of relevant instances that are retrieved, is

回想一下,这是检索到的相关实例的一小部分,是。

> recall <- sum(predict & true) / sum(true)

F-measure is 2 * precision * recall / (precision + recall) is

F-measure是2 * precision * recall / (precision + recall)。

> Fmeasure <- 2 * precision * recall / (precision + recall)

#2


8  

Just packaging Patrick's great answer neatly into a function ...

把帕特里克的伟大答案巧妙地包装成一个功能……

measurePrecisionRecall <- function(predict, actual_labels){
  precision <- sum(predict & actual_labels) / sum(predict)
  recall <- sum(predict & actual_labels) / sum(actual_labels)
  fmeasure <- 2 * precision * recall / (precision + recall)

  cat('precision:  ')
  cat(precision * 100)
  cat('%')
  cat('\n')

  cat('recall:     ')
  cat(recall * 100)
  cat('%')
  cat('\n')

  cat('f-measure:  ')
  cat(fmeasure * 100)
  cat('%')
  cat('\n')
}