%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);

ProbS CF matlab源代码(二分系统)(原创作品,转载注明出处,谢谢!)的更多相关文章

  1. 从零开始安装hue(原创-转载注明出处)

    hue安装需要从github上面下载源码,进行编译安装.github上面给出的安装教程很简单 然而实际上在安装的过程中遇到了无数个坑,下面开始真正意义上的从零开始安装hue. 安装环境: centOS ...

  2. Java使用JNA方式调用DLL(动态链接库)(原创,装载请注明出处)

    Java使用JNA调用DLL 1.准备 1.JDK环境 2.Eclipse 3.JNA包 下载JNA包: (1).JNA的Github:https://github.com/java-native-a ...

  3. paper 69:Haar-like矩形遍历检测窗口演示Matlab源代码[转载]

    Haar-like矩形遍历检测窗口演示Matlab源代码 clc; clear; close all; % Haar-like特征矩形计算 board = 24 % 检测窗口宽度 num = 24 % ...

  4. 基于小波变换的数字图像处理(MATLAB源代码)

    基于小波变换的数字图像处理(MATLAB源代码) clear all; close all; clc;M=256;%原图像长度N=64; %水印长度[filename1,pathname]=uiget ...

  5. [原创作品] 对获取多层json值的封装

    今天篇头不废话了,交流加群:164858883 在我们接收后端返回的json数据的时候,在数据缺失的时候,如果直接接收会导致致命错误的发生.可能有些同学会说通常都会有,不用判断直接获取也行.之前我也是 ...

  6. [原创作品] web项目构建(一)

    今天开始,将推出web项目构建教程,与<javascript精髓整理篇>一并更新.敬请关注. 这篇作为这一系列开头,主要讲述web项目的构建技术大全.在众多人看来,web前端开发无非就是写 ...

  7. [原创作品] RequireJs入门进阶教程

    最近我发现RSS采集数据是个很好玩的东西,就是可以直接把别人的数据放在自己的网站上.如果网友们在其他地方发现这篇文章,还是来博客园看吧(http://zhutty.cnblogs.com).这样代码比 ...

  8. [原创作品]web网页中的锚点

    因为近来在从事web前端开发的工作,所以写的文章也都是关于web这一块.以后将分享算法和web高级编程的内容,很多公司的web前端不够重视,以为是很low-level,给的待遇也很一般,其实,这都是很 ...

  9. [原创作品]手把手教你怎么写jQuery插件

    这次随笔,向大家介绍如何编写jQuery插件.啰嗦一下,很希望各位IT界的‘攻城狮’们能和大家一起分享,一起成长.点击左边我头像下边的“加入qq群”,一起分享,一起交流,当然,可以一起吹水.哈,不废话 ...

随机推荐

  1. [转] C#中的Dictionary的使用

    txw1958 的 原文 说明    必须包含名空间System.Collection.Generic     Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)     键 ...

  2. Yii 显示错误信息(Fatal Error,Warning)在页面上

    Yii由于设计上对于一些php奇怪问题的顾虑,并没有像cake,kohana一样把php错误信息打印在页面上. 遇到错误时,只是显示白页,这让没有经验的programmer会一头雾水. 实际上通常vh ...

  3. pdm 中怎么修改表的Name值时使Code值不变

    修改方法:PowerDesign中的选项菜单里修改,在[Tool]-->[General Options]->[Dialog]->[Operating modes]->[Nam ...

  4. Delphi 提示在Delphi的IDE中,按Ctrl+Shift+G键可以为一个接口生成一个新的GUID。

    对于Object Pascal语言来说,最近一段时间最有意义的改进就是从Delphi3开始支持接口(interface),接口定义了能够与一个对象进行交互操作的一组过程和函数.对一个接口进行定义包含两 ...

  5. [转]linux的du和df命令

    转自:http://blog.csdn.net/kmesg/article/details/6570800 今天也有同学问我Linux下查看目录大小的命令,现在也将前阵子学习到du/df两个命令总结一 ...

  6. 多态.xml

    pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6; ...

  7. stringstream实例

    stringstream的具体作用稍后来总结,这里分享一个实例,从txt文档中读取数据,并对进行处理. #include <iostream> #include <sstream&g ...

  8. Application Cache

    轉發處:http://www.cnblogs.com/blackbird/archive/2012/06/12/2546751.html HTML5提供了一系列的特性来支持离线应用: applicat ...

  9. BITED数学建模七日谈之七:临近比赛时的准备工作

    经过前面六天的文章分享,相信大家对数学模型的相关准备.学习都有了更新的认识,希望大家能从中有所收获,以便更高效地准备比赛和学习数学模型,本文是数学建模经验谈的最后一天:临近比赛的准备工作,希望在临近比 ...

  10. Spring Batch的事务– Part 3: 略过和重试

    原文:https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-3-skip-and-retry/ This i ...