Skip to content

Commit

Permalink
Merge pull request #20 from mthorrell/f1_score
Browse files Browse the repository at this point in the history
added precision, recall, f1_score
  • Loading branch information
mfrasco authored Jun 9, 2018
2 parents 426aa55 + ac43891 commit 07bcee0
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(auc)
export(bias)
export(ce)
export(f1)
export(fbeta_score)
export(ll)
export(logLoss)
export(mae)
Expand All @@ -20,7 +21,9 @@ export(mdae)
export(mse)
export(msle)
export(percent_bias)
export(precision)
export(rae)
export(recall)
export(rmse)
export(rmsle)
export(rrse)
Expand Down
62 changes: 62 additions & 0 deletions R/binary_classification.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,65 @@ ll <- function(actual, predicted) {
logLoss <- function(actual, predicted) {
return(mean(ll(actual, predicted)))
}



#' Precision
#'
#' \code{precision} computes proportion of observations predicted to be in the
#' positive class (i.e. the element in \code{predicted} equals 1)
#' that actually belong to the positive class (i.e. the element
#' in \code{actual} equals 1)
#'
#' @inheritParams params_binary
#' @export
#' @seealso \code{\link{recall}} \code{\link{fbeta_score}}
#' @examples
#' actual <- c(1, 1, 1, 0, 0, 0)
#' predicted <- c(1, 1, 1, 1, 1, 1)
#' precision(actual, predicted)
precision <- function(actual, predicted) {
return(mean(actual[predicted == 1]))
}

#' Recall
#'
#' \code{recall} computes proportion of observations in the positive class
#' (i.e. the element in \code{actual} equals 1) that are predicted
#' to be in the positive class (i.e. the element in \code{predicted}
#' equals 1)
#'
#' @inheritParams params_binary
#' @export
#' @seealso \code{\link{precision}} \code{\link{fbeta_score}}
#' @examples
#' actual <- c(1, 1, 1, 0, 0, 0)
#' predicted <- c(1, 0, 1, 1, 1, 1)
#' recall(actual, predicted)
recall <- function(actual, predicted) {
return(mean(predicted[actual == 1]))
}

#' F-beta Score
#'
#' \code{fbeta_score} computes a weighted harmonic mean of Precision and Recall.
#' The \code{beta} parameter controls the weighting.
#'
#' @inheritParams params_binary
#' @param beta A non-negative real number controlling how close the F-beta score is to
#' either Precision or Recall. When \code{beta} is at the default of 1,
#' the F-beta Score is exactly an equally weighted harmonic mean.
#' The F-beta score will weight toward Precision when \code{beta} is less
#' than one. The F-beta score will weight toward Recall when \code{beta} is
#' greater than one.
#' @export
#' @seealso \code{\link{precision}} \code{\link{recall}}
#' @examples
#' actual <- c(1, 1, 1, 0, 0, 0)
#' predicted <- c(1, 0, 1, 1, 1, 1)
#' recall(actual, predicted)
fbeta_score <- function(actual, predicted, beta = 1) {
prec <- precision(actual, predicted)
rec <- recall(actual, predicted)
return((1 + beta^2) * prec * rec / ((beta^2 * prec) + rec))
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ All functions in the **Metrics** package take at least two arguments: `actual` a
| binary classification | Area Under ROC Curve | auc | ![equation](https://latex.codecogs.com/gif.latex?%5Cdpi%7B150%7D%20%5Cint_0%5E1%20%5B1%20-%20G_1%28G%5E%7B-1%7D_0%281%20-%20v%29%29%5D%20dv). `help(auc)` for details. |
| binary classification | Log Loss | ll | ![equation](https://latex.codecogs.com/gif.latex?%5Cdpi%7B150%7D%20x_i%20*%20%5Cln%28y_i%29%20&plus;%20%281%20-%20x_i%29%20*%20%5Cln%281%20-%20y_i%29) |
| binary classification | Mean Log Loss | logloss | ![equation](https://latex.codecogs.com/gif.latex?%5Cdpi%7B150%7D%20%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi%3D1%7D%5En%20x_i%20*%20%5Cln%28y_i%29%20&plus;%20%281%20-%20x_i%29%20*%20%5Cln%281%20-%20y_i%29) |
| binary classification | Precision | precision | ![equation](https://latex.codecogs.com/gif.latex?%5Cdpi%7B150%7D%20%5Cfrac%7B1%7D%7B%5Csum%20I%28y_i%20%3D%201%29%7D%5Csum_%7Bi%3D1%7D%20%5E%7Bn%7D%20I%28y_i%20%3D%201%29x_i) |
| binary classification | Recall | recall | ![equation](https://latex.codecogs.com/gif.latex?%5Cdpi%7B150%7D%20%5Cfrac%7B1%7D%7B%5Csum%20I%28x_i%20%3D%201%29%7D%5Csum_%7Bi%3D1%7D%20%5E%7Bn%7D%20I%28x_i%20%3D%201%29y_i) |
| binary classification | F-beta Score | fbeta_score | ![equation](https://latex.codecogs.com/gif.latex?%5Cdpi%7B150%7D%20%281%20&plus;%20%5Cbeta%5E2%29%20%5Cfrac%7B%5Ctext%7Bprecision%7D%20*%20%5Ctext%7Brecall%7D%7D%7B%20%28%5Cbeta%5E2%20*%20%5Ctext%7Bprecision%7D%29%20&plus;%20%5Ctext%7Brecall%7D%7D) |
35 changes: 35 additions & 0 deletions man/fbeta_score.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/precision.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/recall.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/testthat/test-binary_classification.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,22 @@ test_that('mean los loss is calculated correctly', {
expect_equal(logLoss(c(1,1,1,0,0,0),c(.5,.1,.01,.9,.75,.001)), 1.881797068998267)
})

test_that('precision is calculated correctly', {
expect_equal(precision(c(1,1,0,0),c(1,1,0,0)), 1)
expect_equal(precision(c(0,0,1,1),c(1,1,0,0)), 0)
expect_equal(precision(c(1,1,0,0),c(1,1,1,1)), 1/2)
})

test_that('recall is calculated correctly', {
expect_equal(recall(c(1,1,0,0),c(1,1,0,0)), 1)
expect_equal(recall(c(0,0,1,1),c(1,1,0,0)), 0)
expect_equal(recall(c(1,1,1,1),c(1,0,0,1)), 1/2)
})

test_that('f-beta score is calculated correctly',{
expect_equal(fbeta_score(c(1,1,0,0),c(1,1,0,0)), 1)
expect_equal(fbeta_score(c(0,0,1,1),c(1,1,1,0)), 2/5)
expect_equal(fbeta_score(c(1,1,1,1),c(1,0,0,1)), 2/3)
expect_equal(fbeta_score(c(1,1,0,0),c(1,1,1,1),beta=0), 1/2)
})

0 comments on commit 07bcee0

Please sign in to comment.