mahout的推薦引擎Taste的學習筆記(一)

2021-09-01 15:23:26 字數 2485 閱讀 5179

mahout中的乙個模組taste實現了推薦引擎的功能,到網上查了一下資料,都沒有任何taste原始碼分析,只有自己看一看**了,能記的就記錄下來,以後用到的時候就方便了。

推薦引擎的原理是協同過濾 (collaborative filtering, 簡稱 cf),下邊就用這個縮寫了。

1、基於使用者的cf

基於使用者的 cf 的基本思想相當簡單,基於使用者對物品的偏好找到相鄰鄰居使用者,然後將鄰居使用者喜歡的推薦給當前使用者。計算上,就是將乙個使用者對所有物品的偏好作為乙個向量 來計算使用者之間的相似度,找到 k 鄰居後,根據鄰居的相似度權重以及他們對物品的偏好,**當前使用者沒有偏好的未涉及物品,計算得到乙個排序的物品列表作為推薦。

(一)基於 mahout 實現 user cf

datamodel model = new filedatamodel(new file("preferences.dat")); 

usersimilarity similarity = new pearsoncorrelationsimilarity(model);

userneighborhood neighborhood = new nearestnuserneighborhood(100, similarity, model);

recommender recommender = new genericuserbasedrecommender(model,

neighborhood, similarity);

//這是介面recommender的功能函式

@override

public listrecommend(long userid, int howmany, idrescorer rescorer) throws tasteexception '", userid);

//得到userid的所有howmany個相鄰的user

long theneighborhood = neighborhood.getuserneighborhood(userid);

if (theneighborhood.length == 0)

//先得到userid的所有鄰居使用者評價過的item列表,

//然後在這個所有鄰居評價過的item中去除掉userid評價過的item,剩下的item為allitemmids

fastidset allitemids = getallotheritems(theneighborhood, userid);

//在給定的使用者userid和該使用者的鄰居使用者情況下,輸入某乙個item(物品),這些鄰居對這個物品的評分的

//平均值,就是userid使用者對該item的評分的估測值

topitems.estimatorestimator = new estimator(userid, theneighborhood);

//將上邊allitemmids中的item逐個用estimator進行估測評分,然後選出howmany得分最高的返回

listtopitems = topitems

.gettopitems(howmany, allitemids.iterator(), rescorer, estimator);

log.debug("recommendations are: {}", topitems);

return topitems;

}

從上邊的**實現,可以看到基於使用者的cf分為以下幾步:

1、給定乙個userid的使用者,獲取該使用者的鄰居使用者

long theneighborhood = neighborhood.getuserneighborhood(userid);
2、得到了鄰居使用者,我們從資料來源中得到這些鄰居使用者所評價過的所有item的列表,然後再從這個列表中把userid使用者評價過的item去除掉,剩下的就是userid沒評價過但他的鄰居評價過的item

fastidset allitemids = getallotheritems(theneighborhood, userid);
3、建立乙個評估器estimator,它的作用是:在給定的使用者userid和該使用者的鄰居使用者情況下,輸入某乙個item(物品),這些鄰居對這個物品的評分的平均值,就是userid使用者對該item的評分的估測值

topitems.estimatorestimator = new estimator(userid, theneighborhood);
4、將所有userid沒評價過但他的鄰居評價過的item使用評估器進行評估,選出在評估中得分最高的howmany個item,並將這些item返回給recommender作為推薦結果。

listtopitems = topitems

.gettopitems(howmany, allitemids.iterator(), rescorer, estimator);

下篇文章,分析這些步驟的細節部分

Mahout分布式推薦引擎介紹

一直以為在 mahout 的在分布式上做了很多東西,很高深。最近一段時間由於工作中要實現一些分布式演算法,所以硬著頭皮看了一下它的原始碼。當時我匆忙的看過 kmeans 的實現,這次我的工作是在搜尋引擎日誌記錄中找相似 query 我是按照 query 以及它對應的點選商品來進行相似 query 匹...

Mapreduce 推薦引擎

購買過該商品的顧客還購買過哪些商品 給乙個商品,推薦購買過這個商品的使用者經常購買的五件產品 輸出是鍵值對,鍵是商品,值是5個商品的列表 map1 key userid value userid購買過的產品 map userid,item reduce1 reduce userid,item i1,...

推薦引擎分類介紹

搜尋引擎是當前快速查詢目標資訊的最好途徑。在使用者對自己需求很明確時,用搜尋引擎可以方便地通過關鍵字快速找到自己需要的資訊。但搜尋引擎並不能完全滿足使用者對資訊發現的需求,因為在很多情況下,使用者其實並不明確自己的需要,或者他們的需求很難用簡單的關鍵字來表述,又或者他們需要更加符合他們個人口味和喜好...