Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix confusmat error described in issue #35 #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/source/perfeval.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ Classification Performance

Compute error rate of predictions given by ``pred`` w.r.t. the ground truths given in ``gt``.

.. function:: confusmat(k, gt, pred)
.. function:: confusmat(gt, pred)

Compute the confusion matrix of the predictions given by ``pred`` w.r.t. the ground truths given in ``gt``.
Here, ``k`` is the number of classes.

It returns an integer matrix ``R`` of size ``(k, k)``, such that ``R(i, j) == countnz((gt .== i) & (pred .== j))``.
It returns an integer matrix ``R`` of size ``(k, k)`` where k is the number of classes in ``gt``,
such that ``R(i, j) == countnz((gt .== i) & (pred .== j))``.

**Examples:**

Expand All @@ -29,7 +29,7 @@ Classification Performance

julia> pred = [1, 1, 2, 2, 2, 3, 3, 3];

julia> C = confusmat(3, gt, pred) # compute confusion matrix
julia> C = confusmat(gt, pred) # compute confusion matrix
3x3 Array{Int64,2}:
2 1 0
0 2 1
Expand Down
12 changes: 8 additions & 4 deletions src/perfeval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ correctrate(gt::IntegerVector, r::IntegerVector) = counteq(gt, r) / length(gt)
errorrate(gt::IntegerVector, r::IntegerVector) = countne(gt, r) / length(gt)

## confusion matrix

function confusmat(k::Integer, gts::IntegerVector, preds::IntegerVector)
function confusmat(gts::IntegerVector, preds::IntegerVector)
n = length(gts)
length(preds) == n || throw(DimensionMismatch("Inconsistent lengths."))

gtslbl = sort(unique(gts))
k = length(gtslbl)

lookup = Dict(reverse.(enumerate(gtslbl)|> collect))
R = zeros(Int, k, k)
for i = 1:n
@inbounds g = gts[i]
@inbounds p = preds[i]
@inbounds g = lookup[gts[i]]
@inbounds p = lookup[preds[i]]
R[g, p] += 1
end
return R
Expand Down
8 changes: 4 additions & 4 deletions test/perfeval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import StatsBase: harmmean

## correctrate & errorrate

a = [1, 1, 1, 2, 2, 2, 3, 3]
a = [0, 1, 1, 2, 2, 2, 3, 3]
b = [1, 1, 2, 2, 2, 3, 3, 3]

@test correctrate(a, b) == 0.75
@test errorrate(a, b) == 0.25
@test correctrate(a, b) == 0.625
@test errorrate(a, b) == 0.375

## confusmat

@test confusmat(3, a, b) == [2 1 0; 0 2 1; 0 0 2]
@test confusmat(a, b) == [0 1 0 0; 0 1 1 0; 0 0 2 1; 0 0 0 2]

## counthits & hitrates

Expand Down