欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年08月31日 09:55:21   作者:ge_nius  
這篇文章主要為大家詳細(xì)介紹了基于隨機(jī)梯度下降的矩陣分解推薦算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

SVD是矩陣分解常用的方法,其原理為:矩陣M可以寫成矩陣A、B與C相乘得到,而B可以與A或者C合并,就變成了兩個元素M1與M2的矩陣相乘可以得到M。

矩陣分解推薦的思想就是基于此,將每個user和item的內(nèi)在feature構(gòu)成的矩陣分別表示為M1與M2,則內(nèi)在feature的乘積得到M;因此我們可以利用已有數(shù)據(jù)(user對item的打分)通過隨機(jī)梯度下降的方法計算出現(xiàn)有user和item最可能的feature對應(yīng)到的M1與M2(相當(dāng)于得到每個user和每個item的內(nèi)在屬性),這樣就可以得到通過feature之間的內(nèi)積得到user沒有打過分的item的分?jǐn)?shù)。

本文所采用的數(shù)據(jù)是movielens中的數(shù)據(jù),且自行切割成了train和test,但是由于數(shù)據(jù)量較大,沒有用到全部數(shù)據(jù)。

代碼如下:

# -*- 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): #求兩個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): #讀取文件,同時得到訓(xùn)練集和測試集
 
 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()獲取指定目錄的相對路徑
 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è)每個user和每個movie有3個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中對應(yīng)uId的feature
 
   u_m = train[train['userId'] == uId] #找到在train中userId點(diǎn)評過的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ù)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python隨機(jī)數(shù)種子(random seed)的使用

    Python隨機(jī)數(shù)種子(random seed)的使用

    在科學(xué)技術(shù)和機(jī)器學(xué)習(xí)等其他算法相關(guān)任務(wù)中,我們經(jīng)常需要用到隨機(jī)數(shù),本文就詳細(xì)的介紹一下Python隨機(jī)數(shù)種子,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 詳解用Pytest+Allure生成漂亮的HTML圖形化測試報告

    詳解用Pytest+Allure生成漂亮的HTML圖形化測試報告

    這篇文章主要介紹了詳解用Pytest+Allure生成漂亮的HTML圖形化測試報告,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Django使用模板后無法找到靜態(tài)資源文件問題解決

    Django使用模板后無法找到靜態(tài)資源文件問題解決

    這篇文章主要介紹了Django使用模板后無法找到靜態(tài)資源文件問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Pytorch之保存讀取模型實(shí)例

    Pytorch之保存讀取模型實(shí)例

    今天小編就為大家分享一篇Pytorch之保存讀取模型實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python filter()及reduce()函數(shù)使用方法解析

    Python filter()及reduce()函數(shù)使用方法解析

    這篇文章主要介紹了Python filter()及reduce()函數(shù)使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • Python iter()函數(shù)用法實(shí)例分析

    Python iter()函數(shù)用法實(shí)例分析

    這篇文章主要介紹了Python iter()函數(shù)用法,結(jié)合實(shí)例形式詳細(xì)分析了Python iter()函數(shù)的功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2018-03-03
  • PyHacker編寫URL批量采集器

    PyHacker編寫URL批量采集器

    這篇文章主要為大家介紹了SpringBoot整合VUE?EasyExcel實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入導(dǎo)出,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python實(shí)現(xiàn)信號時域統(tǒng)計特征提取代碼

    python實(shí)現(xiàn)信號時域統(tǒng)計特征提取代碼

    今天小編就為大家分享一篇python實(shí)現(xiàn)信號時域統(tǒng)計特征提取代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • langchain Prompt大語言模型使用技巧詳解

    langchain Prompt大語言模型使用技巧詳解

    這篇文章主要為大家介紹了langchain Prompt大語言模型使用技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • python中的錯誤處理

    python中的錯誤處理

    異常是指程序中的例外,違例情況。異常機(jī)制是指程序出現(xiàn)錯誤后,程序的處理方法。當(dāng)出現(xiàn)錯誤后,程序的執(zhí)行流程發(fā)生改變,程序的控制權(quán)轉(zhuǎn)移到異常處理。
    2016-04-04

最新評論