我是Matlab的新手,我正在尝试实现规范化的互信息,但是我收到了一个错误。 我正在使用MATHWorks提供的规范化互信息函数:归一化互信息 Mo Chen的1.0版(734字节) 完全矢量化的实现NMI。 NMI通常用于评估聚类结果。
这是我正在使用的代码:
function nmi1
addpath('C:\Users\user\Desktop\Normalization')
FeaturesFile = dlmread('test.txt');
[x,y]=size(FeaturesFile);
z = nmi(x,y);
end
这是函数nmi
function z = nmi(x, y)
% Compute normalized mutual information I(x,y)/sqrt(H(x)*H(y)) of two discrete variables x and y.
% Input:
% x, y: two integer vector of the same length
% Ouput:
% z: normalized mutual information z=I(x,y)/sqrt(H(x)*H(y))
% Written by Mo Chen (sth4nth@gmail.com).
assert(numel(x) == numel(y));
n = numel(x);
x = reshape(x,1,n);
y = reshape(y,1,n);
l = min(min(x),min(y));
x = x-l+1;
y = y-l+1;
k = max(max(x),max(y));
idx = 1:n;
Mx = sparse(idx,x,1,n,k,n);
My = sparse(idx,y,1,n,k,n);
Pxy = nonzeros(Mx'*My/n); %joint distribution of x and y
Hxy = -dot(Pxy,log2(Pxy));
% hacking, to elimative the 0log0 issue
Px = nonzeros(mean(Mx,1));
Py = nonzeros(mean(My,1));
% entropy of Py and Px
Hx = -dot(Px,log2(Px));
Hy = -dot(Py,log2(Py));
% mutual information
MI = Hx + Hy - Hxy;
% normalized mutual information
z = sqrt((MI/Hx)*(MI/Hy));
z = max(0,z);
你知道我做错了吗?
先谢谢