博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ProbS CF matlab源代码(二分系统)(原创作品,转载注明出处,谢谢!)
阅读量:5727 次
发布时间:2019-06-18

本文共 5586 字,大约阅读时间需要 18 分钟。

%ProbS

clear all;

%% 数据读入与预处理

data = load('E:\network_papers\u1.base');

test = load('E:\network_papers\u1.test');

R = preprocess(data.train);

T = preprocess(test.test);

[M,N] = size(R);
[m,n] = size(T);

w = resource_allocate(R,du,di);

for u = 1:M

    index_i_n(u).id = find( R(u,:) == 0 );
end
%% 对每个用户u,对其所有uncollected items预测评分

PR = zeros(M,N);

for u = 1:M
    index_y = find( R(u,:) ~= 0 );
    vec = R(u,index_y);
    for k = 1:length(index_i_n(u).id)
        PR( u, index_i_n(u).id(k) ) = w( index_i_n(u).id(k), index_y ) * vec';
    end
end

value = evaluate('precision',R,PR,T,index_i_n);

hit=hitrate(PR,T,20);
save  predi_matrix PR;

------------------------------------------------------------------------------------------------

%Preprocess

function R = preprocess (A)

[m,n] = size(A);
M = max( A(:,1) );
N = max( A(:,2) );
B(M,N) = 0;
for i = 1:m
        B( A(i,1), A(i,2) ) = A(i,3);
end
B( B < 3 ) = 0;
B( B >= 3 ) = 1;
R = B;

-------------------------------------------------------------------------------------------------------------

%evalate

% evaluate function for multiplied rate for recommendation system

% opt:选择的评价标准,PR:经过预评分的训练集,T:测试集,index_n:所有用户没有评价的物品的索引
function value = evaluate(opt,R,PR,T,index_i_n)
[m,n] = size(T);
[M,N] = size(R);
%% 选择评价方法
switch (opt)

    %% 均方根差

    case {'RMSE'}
        RMSE = zeros(1,m);
        for u = 1:m
            index_tmp = index_i_n(u).id;
            index_tmp( index_tmp > n ) = [];
            len = length(index_tmp);
            vec = PR(u,index_tmp) - T(u,index_tmp);
            RMSE(u) = sqrt( sum( vec .* vec ) / len );
            if ~(mod(u,10))
                fprintf('%d\n',u);
            end
        end
        value = sum(RMSE) / length(RMSE);
        fprintf('The RMSE is:\n%d',value);

       

       
      %%  Pearson积矩相关系数,衡量预测评分和真实评分的线性相关程度
       % pcc在-1到1之间,越靠近1或者-1,线性相关性越好,0表示没有相关性
    case {'pcc'}
        pcc = zeros(1,m);
        for u = 1:m
            index_tmp = index_i_n(u).id;
            index_tmp( index_tmp > n ) = [];
            len = length(index_tmp);
           
            predict = PR(u,index_tmp);
            real = T(u,index_tmp);
            mean_predict = sum(predict) / len;
            mean_real = sum(real) / length(real);
           
            vec1 = predict - mean_predict;
            vec2 = real - mean_real;
            sum1 = vec1 * vec1';
            sum2 = vec2 * vec2';
            if ( sum1 ~= 0 ) && ( sum2 ~= 0 )
                pcc(u) = vec1 * vec2' / sqrt( sum1 * sum2 );
            end
            if ~(mod(u,10))
                fprintf('%d\n',u);
            end
        end
        value = sum(pcc) / m;
        fprintf('The PCC is:\n%d',value);

     

       
        %% 命中率hitting rate 只适用于二值标准,如“喜欢”、“不喜欢”
    case {'hitrate'}
        [SR,index_sr] = sort(PR,2,'descend');
        rato(m,n) = 0;
        for u = 1:m
            sumu = sum(T(u,:));
            rec = 1;
            while rec <= n
                tmp1 = index_sr(u,1:rec);
                tmp1( tmp1 > n ) = [];
                tmp2 = T(u,tmp1);
                if (sumu ~= 0)
                    rato(u,rec) = sum(tmp2) / sumu;
                end
                    rec = rec + 1;
            end
            if ~(mod(u,10))
                fprintf('%d\n',u);
            end 
        end
        value = sum(rato) / m;
     
        x = 1:length(value);
        plot(x,value,'--r');
        hold on;
        xlabel('length of recommendation list');
        ylabel('hitting rate');
       
        %% 平均排序分
    case {'rankscore'}
        [SR,index_sr] = sort(PR,2,'descend');
        %rato = zeros( 1, m );
        for u = 1:m
            len1 = length( index_i_n(u).id );
            index_i_t = find( T(u,:) == 1 );
            len2 = length( index_i_t );
            index_tmp = zeros( 1, len2 );
            if len2 ~= 0
                for k = 1:len2
                    tmp = index_i_t(k);
                    index_tmp(k) = find( index_sr(u,:) == tmp );
                end
                rato(u) = sum( index_tmp / len1 ) / len2;
            end
        end
        value = sum(rato) / length(rato);
        fprintf('The average rank score is:\n%d\n',value);
       
       %% 准确度及准确度提高比例
    case {'precision'}
        L = 10;
        [SR,index_sr] = sort(PR,2,'descend');
        list = index_sr(:,1:L);
        p = zeros(1,m);
        for u = 1:m
            index_i_t = find( T(u,:) == 1 );
            vec = intersect( index_i_t, list(u,:) );
            p(u) = numel(vec) / L;
        end
        value = sum(p) / m;
        ep = value * M * N / sum( sum(T) );
        fprintf('The precision is:\n%d\n',value);
        fprintf('The precision enhancement is:\n%d\n',ep);
       
        %% recall & recall enhancement
    case {'recall'}
        L = 20;
        [SR,index_sr] = sort(PR,2,'descend');
        list = index_sr(:,1:L);
        for u = 1:m
            index_i_t = find( T(u,:) == 1 );
            vec = ismember( index_i_t, list(u,:) );
            if sum( T(u,:) ) ~= 0
                recall(u) = sum(vec) / sum( T(u,:) );
            end
        end
        value = sum(recall) / length(recall);
        er = value * M / L;
        fprintf('The recall is:\n%d\n',value);
        fprintf('The recall enhancement is:\n%d\n',er);
        %% personalization
    case {'personalization'}
        L = 20;
        [SR,index_sr] = sort(PR,2,'descend');
        list = index_sr(:,1:L);
        flag = 1;
        h = zeros(m,m);
        for u = 1:m
            for k = flag:m
                tmp = intersect( list(u,:), list(k,:) );
                h(u,k) = 1 - length( tmp ) / L;
                h(k,u) = h(u,k);
            end
            flag = flag + 1;
        end
        value = sum( sum(h) ) / ( m^2 - m );
        fprintf('The personalization is:\n%d\n',value);
    case {'novelty'}
        degree_i = sum( R,1 );
        L = 20;
        [SR,index_sr] = sort(PR,2,'descend');
        list = index_sr(:,1:L);
        I = zeros(1,m);
        for u = 1:m
            vec1 = degree_i( 1, list(u,:) );
            vec2 = M ./ vec1;
            mult = 1;
            for k = 1:length(vec2)
                mult = mult * vec2(k);
            end
            I(u) = log2(mult) / L;
        end
        value = sum(I) / m;
        fprintf('The novelty is:\n%d\n',value);         
        
       
end 
    
-------------------------------------------------------------------------------------------------           

%CF

%% 数据预处理

clear all;

%data = load('E:\network_papers\datasets\Jester\jeste_train');
%test = load('E:\network_papers\datasets\Jester\jester_test');
data = load('E:\network_papers\u1.base');
test = load('E:\network_papers\u1.test');

R = preprocess(data);

T = preprocess(test);
%{
R=data.train;
R(R<3)=0;
R(R>=3)=1;
T=test.test;
T(T<3)=0;
T(T>=3)=1;
du = sum(R,2);
di = sum(R,1);
ex=find(du==0);
R(ex,:)=[];
T(ex,:)=[];
du(ex,:)=[];
%}

[M,N] = size(R);

[m,n] = size(T);
for u = 1:M
    index_i_n(u).id = find( R(u,:) == 0 );
end
%% 计算出每个用户与其他用户之间的相似度

sim = get_Sim_u(R);

%% 预测评分

PR = zeros(M,N);

for u = 1:M
    index_n = find(  R(u,:) == 0 );
    for k = 1:length( index_n )
        PR( u, index_n(k) ) = predict_Rate( u, index_n(k), sim, R );
    end
end
 value = evaluate('precision',R,PR,T,index_i_n);
 hit=hitrate(PR,T,20);

 

转载于:https://www.cnblogs.com/90zeng/p/4121645.html

你可能感兴趣的文章
python中,如何将字符串转换为数字(将数字转换为整型),字符串的10转换为整型的10,10.5转换为10...
查看>>
1061. Dating (20)
查看>>
第二次实验的感悟
查看>>
【机器学习】一些基本概念及符号系统
查看>>
页面留白问题
查看>>
因为时间少
查看>>
leetcode 【 Best Time to Buy and Sell Stock II 】python 实现
查看>>
推荐15款创建漂亮幻灯片的 jQuery 插件
查看>>
【算法】CRF
查看>>
windows 8 微软拼音输入法
查看>>
Windows UI风格的设计(7)
查看>>
3. 指针的赋值
查看>>
linux小常识
查看>>
SQL中使用WITH AS提高性能 使用公用表表达式(CTE)简化嵌套SQL
查看>>
聊聊TaskExecutor的spring托管
查看>>
oracle 强行杀掉一个用户连接
查看>>
Git提交本地库代码到远程服务器的操作
查看>>
挨踢部落故事汇(13):扬长避短入行Oracle开发
查看>>
灾难拯救——让软件项目重回轨道
查看>>
ssh链接git服务器,解决push pull要求输入密码问题
查看>>