-
Notifications
You must be signed in to change notification settings - Fork 0
/
accuracy.m
27 lines (25 loc) · 875 Bytes
/
accuracy.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function [score, match] = accuracy(true_labels, cluster_labels)
%ACCURACY Compute clustering accuracy using the true and cluster labels and
% return the value in 'score'.
%
% Input : true_labels : N-by-1 vector containing true labels
% cluster_labels : N-by-1 vector containing cluster labels
%
% Output : score : clustering accuracy
%
% Author : Wen-Yen Chen (wychen@alumni.cs.ucsb.edu)
% Chih-Jen Lin (cjlin@csie.ntu.edu.tw)
% Compute the confusion matrix 'cmat', where
% col index is for true label (CAT),
% row index is for cluster label (CLS).
n = length(true_labels);
cat = spconvert([(1:n)' true_labels ones(n,1)]);
cls = spconvert([(1:n)' cluster_labels ones(n,1)]);
cls = cls';
cmat = full(cls * cat);
%
% Calculate accuracy
%
[match, cost] = hungarian(-cmat);
score = 100*(-cost/n);
match=match';