平均通勤时间ACT( train, test )
function [ thisauc ] = ACT( train, test )
%% 计算ACT指标并返回AUC值
D = sparse(eye(size(train,1)));
% 生成稀疏的单位矩阵
D(logical(D)) = sum(train,2);
% 生成度矩阵(对角线元素为同下标节点的度)
pinvL = sparse(pinv( full(D - train) )); clear D;【pinvL代表Lxy】
% 拉普拉斯矩阵的伪逆【pinv伪逆】
Lxx = diag(pinvL);
% 取对角线元素【下标相同的元素,为构建 Lxx、Lyy】
Lxx = repmat(Lxx, [1,size(train,1)]);
% 将对角线元素向量扩展为n×n阶矩阵【每行看成Lxx,每列看成Lyy】
sim = 1./(Lxx + Lxx' - 2*pinvL);
% 求相似度矩阵
sim(isnan(sim)) = 0; sim(isinf(sim)) = 0;
thisauc = CalcAUC(train,test,sim, 10000);
% 评测,计算该指标对应的AUC
end
余弦相似性CosPlus( train, test )
sim = pinvL ./ (Lxx*Lxx').^0.5; 【区别之处,其实只要有了Lxy,后续都好说了】
其余同ACT
重启的随机游走RWR( train, test, lambda )
function [ thisauc ] = RWR( train, test, lambda )
%% 计算RWR指标并返回AUC值
deg = repmat(sum(train,2),[1,size(train,2)]);
train = train ./ deg; clear deg;
% 求转移矩阵
I = sparse(eye(size(train,1)));
% 生成单位矩阵
sim = (1 - lambda) * inv(I- lambda * train') * I;
sim = sim+sim';
% 相似度矩阵计算完成
train = spones(train);
% 将邻接矩阵还原,因为无孤立点,所以不会有节点的度为0
thisauc = CalcAUC(train,test,sim, 10000);
% 评测,计算该指标对应的AUC
end
SimRank( train, test, lambda)
function [ thisauc ] = SimRank( train, test, lambda)
%% 计算SimRank指标并返回AUC值
deg = sum(train,1);
% 求节点的入度【按列求和】,构成行向量,供调用
lastsim = sparse(size(train,1), size(train,2));
% 存储前一步的迭代结果,初始化为全0矩阵
sim = sparse(eye(size(train,1)));
% 存储当前步的迭代结果,初始化为单位矩阵
while(sum(sum(abs(sim-lastsim)))>0.0000001)
% 迭代至稳态的判定条件【多次迭代之后sim与上一次之差不足0.000001才可退出while】
lastsim = sim; sim = sparse(size(train,1), size(train,2));
for nodex = 1:size(train,1)-1
%对每一对节点的值进行更新
if deg(nodex) == 0
continue;
end
for nodey = nodex+1:size(train,1)
%-----将点x的邻居和点y的邻居所组成的所有节点对的前一步迭代结果相加
if deg(nodey) == 0
continue;
end
sim(nodex,nodey) = lambda * sum(sum(lastsim(train(:,nodex)==1,train(:,nodey)==1))) / (deg(nodex)*deg(nodey));
end
end
sim = sim+sim'+ sparse(eye(size(train,1)));
end
thisauc = CalcAUC(train,test,sim, 10000);
% 评测,计算该指标对应的AUC
end
局部随机游走指标LRW( train, test, steps, lambda )
function [ thisauc ] = LRW( train, test, steps, lambda )
%% 计算LRW指标并返回AUC值
deg = repmat(sum(train,2),[1,size(train,2)]);
train = train ./ deg; clear deg;
% 求转移矩阵
I = sparse(eye(size(train,1)));
% 生成单位矩阵
sim = I;
stepi = 0;
while(stepi < steps)
% 随机游走的迭代
sim = (1-lambda)*I + lambda * train' * sim;
stepi = stepi + 1;
end
sim = sim+sim';
% 相似度矩阵计算完成
train = spones(train);
% 将邻接矩阵还原,因为无孤立点,所以不会有节点的度为0
thisauc = CalcAUC(train,test,sim, 10000);
% 评测,计算该指标对应的AUC
end
function [ thisauc ] = SRW( train, test, steps, lambda )
%% 计算SRW指标并返回AUC值
deg = repmat(sum(train,2),[1,size(train,2)]);
train = train ./ deg; clear deg;
% 求转移矩阵
I = sparse(eye(size(train,1)));
% 生成单位矩阵
tempsim = I;
% 用来暂存每步的迭代结果
stepi = 0; sim = sparse(size(train,1),size(train,2));
% 随机游走的迭代 sim用来存储每步迭代的分值之和
while(stepi < steps)
tempsim = (1-lambda)*I + lambda * train' * tempsim;
stepi = stepi + 1;
sim = sim + tempsim;
end
sim = sim+sim';
% 相似度矩阵计算完成
train = spones(train);
%将邻接矩阵还原,因为无孤立点,所以不会有节点的度为0
thisauc = CalcAUC(train,test,sim, 10000);
% 评测,计算该指标对应的AUC
end