99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀簡單易學的機器學習算法——協(xié)同過濾推薦算法(2)
簡單易學的機器學習算法——協(xié)同過濾推薦算法(2)
2017-03-25
收藏

簡單易學的機器學習算法——協(xié)同過濾推薦算法(2)

一、基于協(xié)同過濾推薦系統(tǒng)
    協(xié)同過濾(Collaborative Filtering)的推薦系統(tǒng)的原理是通過將用戶和其他用戶的數(shù)據(jù)進行比對來實現(xiàn)推薦的。比對的具體方法就是通過計算兩個用戶數(shù)據(jù)之間的相似性,通過相似性的計算來說明兩個用戶數(shù)據(jù)之間的相似程度。相似度函數(shù)的設計必須滿足度量空間的三點要求,即非負性,對稱性和三角不等性。常用的相似度的計算方法有:歐式距離法、皮爾遜相關系數(shù)法和夾角余弦相似度法。具體的可以參見上一篇文章“協(xié)同過濾推薦算法(1) ”。
二、面臨的問題
    在基本的協(xié)同過濾推薦系統(tǒng)中(主要指上面所提到的基本模型中),我們是在整個空間上計算相似度,進而實現(xiàn)推薦的。但是現(xiàn)實中的數(shù)據(jù)往往并不是那么規(guī)整,普遍的現(xiàn)象就是在用戶數(shù)據(jù)中出現(xiàn)很多未評分項,如下面所示的數(shù)據(jù):

對于這樣的稀疏矩陣,我們利用基本的協(xié)同過濾推薦算法的效率必將很低。對于這樣的稀疏矩陣,我們可以利用SVD對其進行降維,將這樣的稀疏矩陣映射到另一個具體的主題空間,SVD降維的原理可以參見博文“SVD奇異值分解”。
三、利用SVD構造主題空間
    我們對上面所示的這樣一個矩陣進行SVD分解,分解的結果為:
1、U矩陣

(U矩陣,矩陣U主要反應的是用戶信息)
2、對角陣S

(S矩陣,矩陣S主要反映的是11個奇異值)
3、VT矩陣

(VT矩陣,矩陣VT主要反映的是物品信息)
4、選取奇異值并映射主題空間
   奇異值分解公式為:,現(xiàn)在我們要將原始數(shù)據(jù)映射到反映物品的相互關系中。選取前5個奇異值,奇異值的選取符合能量的規(guī)則,選擇出來的奇異值的能量要能反映90%的原始信息。這樣新的主題空間的計算方式為:數(shù)據(jù)分析師培訓
即可得新的主題空間:
四、實驗的仿真
    我們在這樣的數(shù)據(jù)集上做推薦計算。其中user為2號用戶。


(相似度的計算)

(推薦結果)
MATLAB代碼
主程序
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
%% 主函數(shù)  
 
% 導入數(shù)據(jù)  
%data = [4,4,0,2,2;4,0,0,3,3;4,0,0,1,1;1,1,1,2,0;2,2,2,0,0;1,1,1,0,0;5,5,5,0,0];  
data = [2,0,0,4,4,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0,5;0,0,0,0,0,0,0,1,0,4,0;3,3,4,0,3,0,0,2,2,0,0;5,5,5,0,0,0,0,0,0,0,0;  
    0,0,0,0,0,0,5,0,0,5,0;4,0,4,0,0,0,0,0,0,0,5;0,0,0,0,0,4,0,0,0,0,4;0,0,0,0,0,0,5,0,0,5,0;0,0,0,3,0,0,0,0,4,5,0;  
    1,1,2,1,1,2,1,0,4,5,0];  
 
% reccomendation  
%[sortScore, sortIndex] = recommend(data, 3, 'cosSim');  
[sortScore, sortIndex] = recommend(data, 2, 'cosSim');  
 
len = size(sortScore);  
 
finalRec = [sortIndex, sortScore];  
disp(finalRec);  

SVD空間映射的函數(shù)
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
function [ score ] = SVDEvaluate( data, user, simMeas, item )  
    [m,n] = size(data);  
    simTotal = 0;  
    ratSimTotal = 0;  
      
    % 奇異值分解  
    [U S V] = svd(data);  
    % 求使得保留90%能量的奇異值  
    sizeN = 0;%記錄維數(shù)  
    [m_1,n_1] = size(S);  
    a = 0;%求總能量  
    for i = 1:m_1  
        a = a + S(i,i)*S(i,i);  
    end  
    b = a*0.9;%能量的90%  
    c = 0;  
    for i = 1:n_1  
        c = c + S(i,i)*S(i,i);  
        if c >= b  
            sizeN = i;  
            break;  
        end  
    end  
      
    %物品降維后的空間  
    itemTransformed = data' * U(:,1:sizeN) * S(1:sizeN,1:sizeN)^(-1);  
      
    for j = 1:n  
        userRating = data(user, j);%此用戶評價的商品  
          
        if userRating == 0 || j == item%只是找到已評分的商品  
            continue;  
        end  
          
        vectorA = itemTransformed(item,:);  
        vectorB = itemTransformed(j,:);  
        switch simMeas  
           case {'cosSim'}  
               similarity = cosSim(vectorA,vectorB);  
           case {'ecludSim'}  
               similarity = ecludSim(vectorA,vectorB);  
           case {'pearsSim'}  
               similarity = pearsSim(vectorA,vectorB);  
        end  
          
        disp(['the ', num2str(item), ' and ', num2str(j), ' similarity is ', num2str(similarity)]);  
        simTotal = simTotal + similarity;  
        ratSimTotal = ratSimTotal + similarity * userRating;  
    end  
    if simTotal == 0  
        score = 0;  
    else  
        score = ratSimTotal./simTotal;  
    end  
end  

推薦的函數(shù)
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
function [ sortScore, sortIndex ] = recommend( data, user, simMeas )  
    % 獲取data的大小  
    [m, n] = size(data);%m為用戶,n為商品  
    if user > m  
        disp('The user is not in the dataBase');  
    end  
      
    % 尋找用戶user未評分的商品  
    unratedItem = zeros(1,n);  
    numOfUnrated = 0;  
    for j = 1:n  
        if data(user, j) == 0  
            unratedItem(1,j) = 1;%0表示已經(jīng)評分,1表示未評分  
            numOfUnrated = numOfUnrated + 1;  
        end  
    end  
      
    if numOfUnrated == 0  
        disp('the user has rated all items');  
    end  
      
    % 對未評分項打分,已達到推薦的作用  
    itemScore = zeros(numOfUnrated,2);  
    r = 0;  
    for j = 1:n  
        if unratedItem(1,j) == 1%找到未評分項  
            r = r + 1;  
            %score = evaluate(data, user, simMeas, j);  
            score = SVDEvaluate(data, user, simMeas, j);  
            itemScore(r,1) = j;  
            itemScore(r,2) = score;  
        end  
    end  
    %排序,按照分數(shù)的高低進行推薦  
    [sortScore, sortIndex_1] = sort(itemScore(:,2),'descend');  
    [numOfIndex,x] = size(sortIndex_1(:,1));  
    sortIndex = zeros(numOfIndex,1);  
    for m = 1:numOfIndex  
        sortIndex(m,:) = itemScore(sortIndex_1(m,:),1);  
    end  
end 

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師考試動態(tài)
數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }