-
Notifications
You must be signed in to change notification settings - Fork 0
/
kappaindex.m
58 lines (52 loc) · 1.3 KB
/
kappaindex.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function [kappa] = kappaindex(X,G,N)
% USAGE kappa=kappaindex(X,G,N)
%
% Computing Kappa Index for Clustering Agreement by Hossein Mobahi
%
% X is a vector of length M (number of data samples).
% Each entry of X is associated with the cluster index for that sample
% G is a vector in the same format of X belonging to ground truth indecies
% or a secondary clustering
% N is the number of clusters
%
% This measure only works when X and G have the same number of clusters
% In addition, the set of indecies must be integers from one through
% the number of clusters.
% However, they might be equivalent through a permutation of indecies
%
% Example:
% >> X=[1 1 2 2 3 3]; G=[2 2 1 1 3 3]; kappaindex(X,G,3)
% ans = 1
% >> X=[1 1 2 2 4 4 3]; G=[4 2 2 2 1 1 3]; kappaindex(X,G,4)
% ans = 0.8056
%
%
% Check all permutations of cluster indecies
% And pick the one with maximal agreement
P=perms(1:N);
maxn=-inf;
for i=1:size(P,1)
n=0;
for j=1:length(X)
if G(j)==P(i,X(j))
n=n+1;
end
end
if n>maxn
maxn=n;
maxi=i;
end
end
% Build matrix
A=zeros(N,N);
for j=1:length(X)
x=P(maxi,X(j));
g=G(j);
A(x,g)=A(x,g)+1;
end
% Compute the kappa index
t=0;
for i=1:N
t=t+sum(A(i,:))*sum(A(:,i));
end
kappa=(length(X)*trace(A)-t)/(length(X)*length(X)-t);