Skip to content

Instantly share code, notes, and snippets.

@aksimhal
Created October 12, 2016 20:36
Show Gist options
  • Select an option

  • Save aksimhal/b12e8fb73ec26a5f5e7a6a9083f9357b to your computer and use it in GitHub Desktop.

Select an option

Save aksimhal/b12e8fb73ec26a5f5e7a6a9083f9357b to your computer and use it in GitHub Desktop.
Example usage of ARI
% Adjusted Rand Index Example
mu0 = [0, 0];
sigma0 = [1, 0; 0, 1];
h0 = mvnrnd(mu0, sigma0, 50);
mu1 = [2, 3];
sigma1 = [2, 0; 0, 2 ];
h1 = mvnrnd(mu1,sigma1, 50);
data = [h0; h1];
labels = ones(length(data), 1);
labels(51:end) = 2;
[idx, C] = kmeans(data, 2);
adjrand_index = ARI(idx, labels)
function adjrand=ARI(u,v)
%function adjrand=ARI(u,v)
%
% Computes the adjusted Rand index to assess the quality of a clustering.
% Perfectly random clustering returns the minimum score of 0, perfect
% clustering returns the maximum score of 1.
%
%INPUTS
% u = the labeling as predicted by a clustering algorithm
% v = the true labeling
%
%OUTPUTS
% adjrand = the adjusted Rand index
%
%
%Author: Tijl De Bie, february 2003.
n=length(u);
ku=max(u);
kv=max(v);
m=zeros(ku,kv);
for i=1:n
m(u(i),v(i))=m(u(i),v(i))+1;
end
mu=sum(m,2);
mv=sum(m,1);
a=0;
for i=1:ku
for j=1:kv
if m(i,j)>1
a=a+nchoosek(m(i,j),2);
end
end
end
b1=0;
b2=0;
for i=1:ku
if mu(i)>1
b1=b1+nchoosek(mu(i),2);
end
end
for i=1:kv
if mv(i)>1
b2=b2+nchoosek(mv(i),2);
end
end
c=nchoosek(n,2);
adjrand=(a-b1*b2/c)/(0.5*(b1+b2)-b1*b2/c);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment