recommenderlab套件介紹
Reference: Michael Hahsler(2011). "recommenderlab: A Framework for Developing and Testing Recommendation Algorithms."
推薦系統基本觀念—Collaborative Filtering(協同式過濾)
1. User-based Collaborative Filtering(UBCF)
Memory-based CF
- 採用「全部」或巨量的使用者資料去進行推薦→佔記憶體
- 推薦系統通常是在線上進行運算,因此上述特性會不利於演算速度
原理
- 假設:擁有相似偏好的人會對項目有相似的評分標準。
- 從與 相似的 個( )鄰居( )的評分紀錄中計算出(e.g.平均) 的缺失項目( )評分,進而取前幾高 做推薦。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 如何計算相似度?
- Pearson Correlation Coefficient
- 即統計學常用之相關係數:r
- 公式:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Cosine Similarity
- 計算各點之間的Cosine角度,角度愈小欲相似。
- 公式:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 如何找出鄰居(Neighborhood)?
- KNN(k-nearest-neighbors)演算法:以新點為中心距離K個最近的點中,哪種類別最多即為該類別。
- 給定一個Similarity Threshold值來選出相似的人
- 計算 的評分( ):
- 簡易版公式:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 加權版公式:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 鑑於在 人之中,仍有與 距離的差異,因此需增加權重(Weights)
- 為 和 ( within neighbors) 的相似度
- 移除個人評分上的偏誤(為了讓每個user的評分權重一樣):
- Normalization(常態化):Z-score
- Center the row:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
2. Item-based Collaborative Filtering(IBCF)
Model-based CF
- 採用部分的(Reduced Similarity Matrix by )使用者資料去學習一個Compact Model
- 因為模型小( x ),較有效率
- 應用上可以完全地達成預先運算(fully precomputed)
原理
- 假設:喜歡i品項的人也會喜歡與i品項有相似的特徵的項目。
- 將 有的評分( )與Similarity Matrix前 高( )的評分矩陣拿去做加權總和(Weighted Sum),算出 其他缺失項目的評分,並取前幾高的 做推薦。
- Similarity Matrix:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 計算評分的公式:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 移除個人評分偏誤的方法同UBCF
Other model-based approaches
I. Slope One algorithm (Lemire and Maclachlan 2005)
II. Latent Factors approach using matrix decomposition (Koren, Bell, and Volinsky 2009)
3. Using 0-1 Data
真實情境
- 我們拿到的資料有很大的機率都是Binary的資料(有/無購買商品),不一定都有Ratings
- 我們不知道消費者沒有購買商品,是因為哪種情況?
推薦演算法的因應作法
網站流量資料中,我們只知道該頁面是否被預覽的資訊(Binary Data)
- 1:使用者喜歡此項目(非缺失項目)
- 0:使用者不喜歡此項目/使用者不知道此項目存在(缺失項目)
因此在0-1 Case中,
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 缺點:非缺失項目全部都是1,將無法計算Pearson correlation或Cosine similarity
- 解方:使用Jaccard index
- 另一種Similarity的衡量方式(只專注在1的情況)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
where X and Y are the sets of the items with a 1 in user profiles and respectively.
基於關聯規則(Association Rules)的0-1 Data
- 與IBCF相似(使用條件機率similarity)
- 可以預先運算
- 關聯規則的重要評估指標:
應用於推薦系統上
- 找出所有匹配 的規則
- 推薦N個獨一(unique)、擁有最高 的 規則
4. 評估方法
1. 評估預測評分值(Ratings)
-
MAE:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
RMSE:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
2. 評估Top-N的推薦
actual \ predicted |
negative |
positive |
negative |
a (TN) |
b (FP) |
positive |
c (FN) |
d (TP) |
- Accuracy:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- MAE:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Precision:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Recall:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- E-measure:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
controls the trade-off between precision and recall.
- F-measure:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
recommenderlab
的功能
1. 建立資料
- 資料轉換
as(data, "realRatingMatrix")
:模型使用
- 查看資料
getRatingMatrix(data)
:取得矩陣內容
as(data, "list")
:推薦的items可以用list查看
as(data, "data.frame")
:用data frame的形式查看
2. 移除評分偏誤
normalize(data)
:Center the row
- 會使極端評分(只評0分和5分)、中性評分(都評2~3分)的人變得沒有差別
normalize(data, method="Z-score")
:Z-score
- 若想保留不同user的評分級距資訊,使用此法較合理(平均數為0,標準差為1)
denormalize(data)
:還原常態化
3. 視覺化評分資料分佈
image(data, main="Title")

4. 製作0-1 Data
binarize(data, minRating)
- 設定 ≥ minRating 以上的評分為1,其餘為0
5. 模型製作與預測
查看所有推薦演算法
recommenderRegistry$get_entries(dataType = "realRatingMatrix")
建立一般模型
Recommender(data, method)
建立驗證模型
evaluationScheme(data, method="split", train, given, goodRating)
- method:
- split:切割train/test資料
- cross:交叉驗證
- given:數字(要放入幾個items訓練模型)
- 每個user有多個items的評分,given決定模型在看了n個評分後,就去猜unknown items的評分。
- 由於訓練時會保留了一些known data,因此可以計算error。
- 若填入負數,表示扣除該數字,放入全部的資料(all-but-n)。
- goodRating:一種threshold,決定多少以上算是好的評分。
evaluate(scheme, method="POPULAR", type = "topNList", n=c(1,3,5,10,15,20))
- 可以取代predict的功能,直接幫你跑完結果。
- 一次做top-1, top-3, …到top-20的模型。
預測模型
predict(model, newData, type)
- model:做完Recommender(data, method)之後的變數。
- type:評分方式
- topN, ratings, … 依據不同model方法而不同。
- 可以用
names(getModel(model))
查看。
- type選擇使用topN時,可直接省略type,並設定n:直接選擇predict n個結果。
bestN(predict, n)
:選取TopN中最高的n個。
評估模型
calcPredictionAccuracy(predict, unknowData)
:計算RMSE/MSE/MAE
getConfusionMatrix(evaluate)[[1]]
:計算TP/FP/FN/TN/precision/recall/TPR/FPR
範例Demo
Jester線上笑話推薦系統
- 5000份匿名評分資料
- 1999年4月到2003年5月
1. 讀取資料
2. 敘述性統計
rowCounts()
:user總共評價幾筆資料
rowMeans()
:user的平均評分
我們發現資料有很大的評分偏誤:

多數人評價了70筆或全部的笑話:

平均評分的分數接近左偏的常態分佈:

3. 移除評分偏誤

4. 比較模型與表現評估
- ROC曲線(TPR vs FPR)
- 對於ROC曲線,我們希望找到TPR高且FPR低的點(左上角),代表猜對的多、猜錯的少
- 但這兩者剛好是一個trade-off,不能兩全其美
- 因此可依據domain knowledge去決定應選擇何種組合

上圖的線為不同的推薦演算法;數字代表推薦TopN個商品。
隨著推薦個數(TopN)愈多,TPR和FPR皆會上升(猜得愈多,猜中/猜錯的次數都會變大)。
結論:UBCF表現最佳且遠高於Based line的Random演算法(亂猜,機率0.5)。


透過互動式App感受推薦系統的樣貌→ ShinyApp by Michael Hahsler.