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

熱線電話:13121318867

登錄
首頁精彩閱讀基于隨機(jī)梯度下降的矩陣分解推薦算法
基于隨機(jī)梯度下降的矩陣分解推薦算法
2018-03-24
收藏

基于隨機(jī)梯度下降的矩陣分解推薦算法

SVD是矩陣分解常用的方法,其原理為:矩陣M可以寫成矩陣A、B與C相乘得到,而B可以與A或者C合并,就變成了兩個(gè)元素M1與M2的矩陣相乘可以得到M。
矩陣分解推薦的思想就是基于此,將每個(gè)user和item的內(nèi)在feature構(gòu)成的矩陣分別表示為M1與M2,則內(nèi)在feature的乘積得到M;因此我們可以利用已有數(shù)據(jù)(user對(duì)item的打分)通過隨機(jī)梯度下降的方法計(jì)算出現(xiàn)有user和item最可能的feature對(duì)應(yīng)到的M1與M2(相當(dāng)于得到每個(gè)user和每個(gè)item的內(nèi)在屬性),這樣就可以得到通過feature之間的內(nèi)積得到user沒有打過分的item的分?jǐn)?shù)。
本文所采用的數(shù)據(jù)是movielens中的數(shù)據(jù),且自行切割成了train和test,但是由于數(shù)據(jù)量較大,沒有用到全部數(shù)據(jù)。

代碼如下:

[python] view plain copy

    # -*- coding: utf-8 -*-  
    """
    Created on Mon Oct  9 19:33:00 2017
     
    @author: wjw
    """  
    import pandas as pd  
    import numpy as np  
    import os  
      
    def difference(left,right,on): #求兩個(gè)dataframe的差集  
        df = pd.merge(left,right,how='left',on=on) #參數(shù)on指的是用于連接的列索引名稱  
        left_columns = left.columns  
        col_y = df.columns[-1] # 得到最后一列  
        df = df[df[col_y].isnull()]#得到boolean的list  
        df = df.iloc[:,0:left_columns.size]#得到的數(shù)據(jù)里面還有其他同列名的column  
        df.columns = left_columns # 重新定義columns  
        return df  
          
    def readfile(filepath): #讀取文件,同時(shí)得到訓(xùn)練集和測(cè)試集  
          
        pwd = os.getcwd()#返回當(dāng)前工程的工作目錄  
        os.chdir(os.path.dirname(filepath))  
        #os.path.dirname()獲得filepath文件的目錄;chdir()切換到filepath目錄下  
        initialData =  pd.read_csv(os.path.basename(filepath))  
        #basename()獲取指定目錄的相對(duì)路徑  
        os.chdir(pwd)#回到先前工作目錄下  
        predData = initialData.iloc[:,0:3] #將最后一列數(shù)據(jù)去掉  
        newIndexData = predData.drop_duplicates()  
        trainData = newIndexData.sample(axis=0,frac = 0.1) #90%的數(shù)據(jù)作為訓(xùn)練集  
        testData = difference(newIndexData,trainData,['userId','movieId']).sample(axis=0,frac=0.1)  
        return trainData,testData  
      
    def getmodel(train):  
        slowRate = 0.99  
        preRmse = 10000000.0  
        max_iter = 100  
        features = 3  
        lamda = 0.2  
        gama = 0.01 #隨機(jī)梯度下降中加入,防止更新過度  
        user = pd.DataFrame(train.userId.drop_duplicates(),columns=['userId']).reset_index(drop=True) #把在原來dataFrame中的索引重新設(shè)置,drop=True并拋棄  
      
        movie = pd.DataFrame(train.movieId.drop_duplicates(),columns=['movieId']).reset_index(drop=True)  
        userNum = user.count().loc['userId'] #671  
        movieNum = movie.count().loc['movieId']   
        userFeatures = np.random.rand(userNum,features) #構(gòu)造user和movie的特征向量集合  
        movieFeatures = np.random.rand(movieNum,features)  
        #假設(shè)每個(gè)user和每個(gè)movie有3個(gè)feature  
        userFeaturesFrame =user.join(pd.DataFrame(userFeatures,columns = ['f1','f2','f3']))  
        movieFeaturesFrame =movie.join(pd.DataFrame(movieFeatures,columns= ['f1','f2','f3']))  
        userFeaturesFrame = userFeaturesFrame.set_index('userId')  
        movieFeaturesFrame = movieFeaturesFrame.set_index('movieId') #重新設(shè)置index  
        
        for i in range(max_iter):   
            rmse = 0  
            n = 0  
            for index,row in user.iterrows():  
                uId = row.userId  
                userFeature = userFeaturesFrame.loc[uId] #得到userFeatureFrame中對(duì)應(yīng)uId的feature  
      
                u_m = train[train['userId'] == uId] #找到在train中userId點(diǎn)評(píng)過的movieId的data  
                for index,row in u_m.iterrows():   
                    u_mId = int(row.movieId)  
                    realRating = row.rating  
                    movieFeature = movieFeaturesFrame.loc[u_mId]   
      
                    eui = realRating-np.dot(userFeature,movieFeature)  
                    rmse += pow(eui,2)  
                    n += 1  
                    userFeaturesFrame.loc[uId] += gama * (eui*movieFeature-lamda*userFeature)   
                    movieFeaturesFrame.loc[u_mId] += gama*(eui*userFeature-lamda*movieFeature)  
            nowRmse = np.sqrt(rmse*1.0/n)  
            print('step:%f,rmse:%f'%((i+1),nowRmse))  
            if nowRmse<preRmse:  
                preRmse = nowRmse  
            elif nowRmse<0.5:  
                break  
            elif nowRmse-preRmse<=0.001:  
                break  
            gama*=slowRate  
        return userFeaturesFrame,movieFeaturesFrame  
       
    def evaluate(userFeaturesFrame,movieFeaturesFrame,test):  
        test['predictRating']='NAN'  # 新增一列  
      
        for index,row in test.iterrows():   
             
            print(index)  
            userId = row.userId  
            movieId = row.movieId  
            if userId not in userFeaturesFrame.index or movieId not in movieFeaturesFrame.index:  
                continue  
            userFeature = userFeaturesFrame.loc[userId]  
            movieFeature = movieFeaturesFrame.loc[movieId]  
            test.loc[index,'predictRating'] = np.dot(userFeature,movieFeature) #不定位到不能修改值  
              
        return test   
          
    if __name__ == "__main__":  
        filepath = r"E:\學(xué)習(xí)\研究生\推薦系統(tǒng)\ml-latest-small\ratings.csv"  
        train,test = readfile(filepath)  
        userFeaturesFrame,movieFeaturesFrame = getmodel(train)  
        result = evaluate(userFeaturesFrame,movieFeaturesFrame,test)  

在test中得到的結(jié)果為:

NAN則是訓(xùn)練集中沒有的數(shù)據(jù)


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

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }