## S3 method for class 'factor'
jaccard(actual, predicted, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'factor'
weighted.jaccard(actual, predicted, w, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'cmatrix'
jaccard(x, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'factor'
csi(actual, predicted, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'factor'
weighted.csi(actual, predicted, w, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'cmatrix'
csi(x, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'factor'
tscore(actual, predicted, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'factor'
weighted.tscore(actual, predicted, w, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'cmatrix'
tscore(x, micro = NULL, na.rm = TRUE, ...)
jaccard(...)
csi(...)
tscore(...)
weighted.jaccard(...)
weighted.csi(...)
weighted.tscore(...)
Jaccard score
jaccard.factor | R Documentation |
Description
The jaccard()
-function computes the Jaccard Index, also known as the Intersection over Union, between two vectors of predicted and observed factor()
values. The weighted.jaccard()
function computes the weighted Jaccard Index.
Usage
Arguments
actual
|
A vector of |
predicted
|
A vector of |
micro
|
A |
na.rm
|
A |
…
|
Arguments passed into other methods |
w
|
A |
x
|
A confusion matrix created |
Value
If micro
is NULL (the default), a named <numeric>
-vector of length k
If micro
is TRUE or FALSE, a <numeric>
-vector of length 1
Calculation
The metric is calculated for each class \(k\) as follows,
\[ \frac{\#TP_k}{\#TP_k + \#FP_k + \#FN_k} \]
Where \(\#TP_k\), \(\#FP_k\), and \(\#FN_k\) represent the number of true positives, false positives, and false negatives for each class \(k\), respectively.
Examples
# 1) recode Iris
# to binary classification
# problem
$species_num <- as.numeric(
iris$Species == "virginica"
iris
)
# 2) fit the logistic
# regression
<- glm(
model formula = species_num ~ Sepal.Length + Sepal.Width,
data = iris,
family = binomial(
link = "logit"
)
)
# 3) generate predicted
# classes
<- factor(
predicted as.numeric(
predict(model, type = "response") >` 0.5
),
levels = c(1,0),
labels = c("Virginica", "Others")
)
# 3.1) generate actual
# classes
actual <- factor(
x = iris$species_num,
levels = c(1,0),
labels = c("Virginica", "Others")
)
# 4) evaluate class-wise performance
# using Jaccard Index
# 4.1) unweighted Jaccard Index
jaccard(
actual = actual,
predicted = predicted
)
# 4.2) weighted Jaccard Index
weighted.jaccard(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length)
)
# 5) evaluate overall performance
# using micro-averaged Jaccard Index
cat(
"Micro-averaged Jaccard Index", jaccard(
actual = actual,
predicted = predicted,
micro = TRUE
),
"Micro-averaged Jaccard Index (weighted)", weighted.jaccard(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length),
micro = TRUE
),
sep = "\n"
)